xref: /titanic_52/usr/src/ucbcmd/rusage/rusage.c (revision 3c112a2b34403220c06c3e2fcac403358cfba168)
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 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*
31  * rusage
32  */
33 
34 #include <locale.h>
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include <sys/wait.h>
40 #include <signal.h>
41 
42 void
43 fprintt(s, tv)
44 	char *s;
45 	struct timeval *tv;
46 {
47 
48 	(void) fprintf(stderr, gettext("%d.%02d %s "),
49 		tv->tv_sec, tv->tv_usec/10000, s);
50 }
51 
52 int
53 main(int argc, char **argv)
54 {
55 	union wait status;
56 	int options = 0;
57 	int p;
58 	struct timeval before, after;
59 	struct rusage ru;
60 	struct timezone tz;
61 
62 	(void) setlocale(LC_ALL, "");
63 
64 #if !defined(TEXT_DOMAIN)
65 #define	TEXT_DOMAIN "SYS_TEST"
66 #endif
67 	(void) textdomain(TEXT_DOMAIN);
68 
69 	if (argc <= 1)
70 		exit(0);
71 	(void) gettimeofday(&before, &tz);
72 
73 	/* fork a child process to run the command */
74 
75 	p = fork();
76 	if (p < 0) {
77 		perror("rusage");
78 		exit(1);
79 	}
80 
81 	if (p == 0) {
82 
83 		/* exec the command specified */
84 
85 		execvp(argv[1], &argv[1]);
86 		perror(argv[1]);
87 		exit(1);
88 	}
89 
90 	/* parent code - wait for command to complete */
91 
92 	(void) signal(SIGINT, SIG_IGN);
93 	(void) signal(SIGQUIT, SIG_IGN);
94 	while (wait3(&status.w_status, options, &ru) != p)
95 		;
96 
97 	/* get closing time of day */
98 	(void) gettimeofday(&after, &tz);
99 
100 	/* check for exit status of command */
101 
102 	if ((status.w_termsig) != 0)
103 		(void) fprintf(stderr,
104 		    gettext("Command terminated abnormally.\n"));
105 
106 	/* print an accounting summary line */
107 
108 	after.tv_sec -= before.tv_sec;
109 	after.tv_usec -= before.tv_usec;
110 	if (after.tv_usec < 0) {
111 		after.tv_sec--;
112 		after.tv_usec += 1000000;
113 	}
114 	fprintt(gettext("real"), &after);
115 	fprintt(gettext("user"), &ru.ru_utime);
116 	fprintt(gettext("sys"), &ru.ru_stime);
117 	(void) fprintf(stderr, gettext("%d pf %d pr %d sw"),
118 		ru.ru_majflt,
119 		ru.ru_minflt,
120 		ru.ru_nswap);
121 	(void) fprintf(stderr, gettext(" %d rb %d wb %d vcx %d icx"),
122 		ru.ru_inblock,
123 		ru.ru_oublock,
124 		ru.ru_nvcsw,
125 		ru.ru_nivcsw);
126 	(void) fprintf(stderr, gettext(" %d mx %d ix %d id %d is"),
127 		ru.ru_maxrss,
128 		ru.ru_ixrss,
129 		ru.ru_idrss,
130 		ru.ru_isrss);
131 
132 	(void) fprintf(stderr, "\n");
133 	return ((int)status.w_retcode);
134 }
135