xref: /illumos-gate/usr/src/ucbcmd/rusage/rusage.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
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 /*
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 int
51 main(int argc, char **argv)
52 {
53 	union wait status;
54 	int options = 0;
55 	int p;
56 	struct timeval before, after;
57 	struct rusage ru;
58 	struct timezone tz;
59 
60 	(void) setlocale(LC_ALL, "");
61 
62 #if !defined(TEXT_DOMAIN)
63 #define	TEXT_DOMAIN "SYS_TEST"
64 #endif
65 	(void) textdomain(TEXT_DOMAIN);
66 
67 	if (argc <= 1)
68 		exit(0);
69 	(void) gettimeofday(&before, &tz);
70 
71 	/* fork a child process to run the command */
72 
73 	p = fork();
74 	if (p < 0) {
75 		perror("rusage");
76 		exit(1);
77 	}
78 
79 	if (p == 0) {
80 
81 		/* exec the command specified */
82 
83 		execvp(argv[1], &argv[1]);
84 		perror(argv[1]);
85 		exit(1);
86 	}
87 
88 	/* parent code - wait for command to complete */
89 
90 	(void) signal(SIGINT, SIG_IGN);
91 	(void) signal(SIGQUIT, SIG_IGN);
92 	while (wait3(&status.w_status, options, &ru) != p)
93 		;
94 
95 	/* get closing time of day */
96 	(void) gettimeofday(&after, &tz);
97 
98 	/* check for exit status of command */
99 
100 	if ((status.w_termsig) != 0)
101 		(void) fprintf(stderr,
102 		    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 	return ((int)status.w_retcode);
132 }
133