xref: /illumos-gate/usr/src/ucbcmd/rusage/rusage.c (revision 8c0b080c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
24*032624d5Sbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*032624d5Sbasabi  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * rusage
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <locale.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/time.h>
367c478bd9Sstevel@tonic-gate #include <sys/resource.h>
377c478bd9Sstevel@tonic-gate #include <sys/wait.h>
387c478bd9Sstevel@tonic-gate #include <signal.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate void
fprintt(s,tv)417c478bd9Sstevel@tonic-gate fprintt(s, tv)
427c478bd9Sstevel@tonic-gate 	char *s;
437c478bd9Sstevel@tonic-gate 	struct timeval *tv;
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 
46*032624d5Sbasabi 	(void) fprintf(stderr, gettext("%d.%02d %s "),
477c478bd9Sstevel@tonic-gate 		tv->tv_sec, tv->tv_usec/10000, s);
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate 
50*032624d5Sbasabi int
main(int argc,char ** argv)51*032624d5Sbasabi main(int argc, char **argv)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate 	union wait status;
54*032624d5Sbasabi 	int options = 0;
55*032624d5Sbasabi 	int p;
567c478bd9Sstevel@tonic-gate 	struct timeval before, after;
577c478bd9Sstevel@tonic-gate 	struct rusage ru;
587c478bd9Sstevel@tonic-gate 	struct timezone tz;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
63*032624d5Sbasabi #define	TEXT_DOMAIN "SYS_TEST"
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
667c478bd9Sstevel@tonic-gate 
67*032624d5Sbasabi 	if (argc <= 1)
687c478bd9Sstevel@tonic-gate 		exit(0);
697c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&before, &tz);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/* fork a child process to run the command */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	p = fork();
747c478bd9Sstevel@tonic-gate 	if (p < 0) {
757c478bd9Sstevel@tonic-gate 		perror("rusage");
767c478bd9Sstevel@tonic-gate 		exit(1);
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if (p == 0) {
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 		/* exec the command specified */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 		execvp(argv[1], &argv[1]);
847c478bd9Sstevel@tonic-gate 		perror(argv[1]);
857c478bd9Sstevel@tonic-gate 		exit(1);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/* parent code - wait for command to complete */
897c478bd9Sstevel@tonic-gate 
90*032624d5Sbasabi 	(void) signal(SIGINT, SIG_IGN);
91*032624d5Sbasabi 	(void) signal(SIGQUIT, SIG_IGN);
927c478bd9Sstevel@tonic-gate 	while (wait3(&status.w_status, options, &ru) != p)
937c478bd9Sstevel@tonic-gate 		;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/* get closing time of day */
967c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&after, &tz);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/* check for exit status of command */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if ((status.w_termsig) != 0)
101*032624d5Sbasabi 		(void) fprintf(stderr,
102*032624d5Sbasabi 		    gettext("Command terminated abnormally.\n"));
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	/* print an accounting summary line */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	after.tv_sec -= before.tv_sec;
1077c478bd9Sstevel@tonic-gate 	after.tv_usec -= before.tv_usec;
1087c478bd9Sstevel@tonic-gate 	if (after.tv_usec < 0) {
1097c478bd9Sstevel@tonic-gate 		after.tv_sec--;
1107c478bd9Sstevel@tonic-gate 		after.tv_usec += 1000000;
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	fprintt(gettext("real"), &after);
1137c478bd9Sstevel@tonic-gate 	fprintt(gettext("user"), &ru.ru_utime);
1147c478bd9Sstevel@tonic-gate 	fprintt(gettext("sys"), &ru.ru_stime);
1157c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%d pf %d pr %d sw"),
1167c478bd9Sstevel@tonic-gate 		ru.ru_majflt,
1177c478bd9Sstevel@tonic-gate 		ru.ru_minflt,
1187c478bd9Sstevel@tonic-gate 		ru.ru_nswap);
1197c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(" %d rb %d wb %d vcx %d icx"),
1207c478bd9Sstevel@tonic-gate 		ru.ru_inblock,
1217c478bd9Sstevel@tonic-gate 		ru.ru_oublock,
1227c478bd9Sstevel@tonic-gate 		ru.ru_nvcsw,
1237c478bd9Sstevel@tonic-gate 		ru.ru_nivcsw);
1247c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(" %d mx %d ix %d id %d is"),
1257c478bd9Sstevel@tonic-gate 		ru.ru_maxrss,
1267c478bd9Sstevel@tonic-gate 		ru.ru_ixrss,
1277c478bd9Sstevel@tonic-gate 		ru.ru_idrss,
1287c478bd9Sstevel@tonic-gate 		ru.ru_isrss);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
131*032624d5Sbasabi 	return ((int)status.w_retcode);
1327c478bd9Sstevel@tonic-gate }
133