xref: /illumos-gate/usr/src/ucbcmd/rusage/rusage.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 #ident	"%Z%%M%	%I%	%E% SMI"
23 
24 /*
25  * Copyright (c) 1988 by Sun Microsystems, Inc.
26  */
27 
28 /*
29  * rusage
30  */
31 
32 #include <locale.h>
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 #include <sys/wait.h>
38 #include <signal.h>
39 
40 void
41 fprintt(s, tv)
42 	char *s;
43 	struct timeval *tv;
44 {
45 
46 	(void) fprintf(stderr, gettext("%d.%02d %s "),
47 		tv->tv_sec, tv->tv_usec/10000, s);
48 }
49 
50 main(argc, argv)
51 	int argc;
52 	char **argv;
53 {
54 	union wait status;
55 	int options=0;
56 	register int p;
57 	struct timeval before, after;
58 	struct rusage ru;
59 	struct timezone tz;
60 
61 	(void) setlocale(LC_ALL, "");
62 
63 #if !defined(TEXT_DOMAIN)
64 #define TEXT_DOMAIN "SYS_TEST"
65 #endif
66 	(void) textdomain(TEXT_DOMAIN);
67 
68 	if (argc<=1)
69 		exit(0);
70 	(void) gettimeofday(&before, &tz);
71 
72 	/* fork a child process to run the command */
73 
74 	p = fork();
75 	if (p < 0) {
76 		perror("rusage");
77 		exit(1);
78 	}
79 
80 	if (p == 0) {
81 
82 		/* exec the command specified */
83 
84 		execvp(argv[1], &argv[1]);
85 		perror(argv[1]);
86 		exit(1);
87 	}
88 
89 	/* parent code - wait for command to complete */
90 
91 	(void)signal(SIGINT, SIG_IGN);
92 	(void)signal(SIGQUIT, SIG_IGN);
93 	while (wait3(&status.w_status, options, &ru) != p)
94 		;
95 
96 	/* get closing time of day */
97 	(void) gettimeofday(&after, &tz);
98 
99 	/* check for exit status of command */
100 
101 	if ((status.w_termsig) != 0)
102 		(void) fprintf(stderr, gettext("Command terminated abnormally.\n"));
103 
104 	/* print an accounting summary line */
105 
106 	after.tv_sec -= before.tv_sec;
107 	after.tv_usec -= before.tv_usec;
108 	if (after.tv_usec < 0) {
109 		after.tv_sec--;
110 		after.tv_usec += 1000000;
111 	}
112 	fprintt(gettext("real"), &after);
113 	fprintt(gettext("user"), &ru.ru_utime);
114 	fprintt(gettext("sys"), &ru.ru_stime);
115 	(void) fprintf(stderr, gettext("%d pf %d pr %d sw"),
116 		ru.ru_majflt,
117 		ru.ru_minflt,
118 		ru.ru_nswap);
119 	(void) fprintf(stderr, gettext(" %d rb %d wb %d vcx %d icx"),
120 		ru.ru_inblock,
121 		ru.ru_oublock,
122 		ru.ru_nvcsw,
123 		ru.ru_nivcsw);
124 	(void) fprintf(stderr, gettext(" %d mx %d ix %d id %d is"),
125 		ru.ru_maxrss,
126 		ru.ru_ixrss,
127 		ru.ru_idrss,
128 		ru.ru_isrss);
129 
130 	(void) fprintf(stderr, "\n");
131 	exit((int)status.w_retcode);
132 	/*NOTREACHED*/
133 }
134 
135