xref: /freebsd/usr.bin/time/time.c (revision 09e8dea79366f1e5b3a73e8a271b26e4b6bf2e6a)
1 /*
2  * Copyright (c) 1987, 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1987, 1988, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)time.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/types.h>
49 #include <sys/time.h>
50 #include <sys/resource.h>
51 #include <sys/signal.h>
52 #include <sys/sysctl.h>
53 #include <stdlib.h>
54 #include <sys/wait.h>
55 
56 #include <err.h>
57 #include <stdio.h>
58 #include <errno.h>
59 #include <locale.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <signal.h>
63 
64 static int getstathz(void);
65 static void humantime(FILE *, long, long);
66 static void usage(void);
67 
68 static char decimal_point;
69 
70 int
71 main(argc, argv)
72 	int argc;
73 	char **argv;
74 {
75 	register int pid;
76 	int aflag, ch, hflag, lflag, status, pflag;
77 	struct timeval before, after;
78 	struct rusage ru;
79 	FILE *out = stderr;
80 	char *ofn = NULL;
81 	int exitonsig = 0; /* Die with same signal as child */
82 
83 	(void) setlocale(LC_NUMERIC, "");
84 	decimal_point = localeconv()->decimal_point[0];
85 
86 	aflag = hflag = lflag = pflag = 0;
87 	while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
88 		switch((char)ch) {
89 		case 'a':
90 			aflag = 1;
91 			break;
92 		case 'h':
93 			hflag = 1;
94 			break;
95 		case 'l':
96 			lflag = 1;
97 			break;
98 		case 'o':
99 			ofn = optarg;
100 			break;
101 		case 'p':
102 			pflag = 1;
103 			break;
104 		case '?':
105 		default:
106 			usage();
107 		}
108 
109 	if (!(argc -= optind))
110 		exit(0);
111 	argv += optind;
112 
113 	if (ofn) {
114 	        if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL)
115 		        err(1, "%s", ofn);
116 		setvbuf(out, (char *)NULL, _IONBF, (size_t)0);
117 	}
118 
119 	gettimeofday(&before, (struct timezone *)NULL);
120 	switch(pid = fork()) {
121 	case -1:			/* error */
122 		err(1, "time");
123 		/* NOTREACHED */
124 	case 0:				/* child */
125 		execvp(*argv, argv);
126 		err(errno == ENOENT ? 127 : 126, "%s", *argv);
127 		/* NOTREACHED */
128 	}
129 	/* parent */
130 	(void)signal(SIGINT, SIG_IGN);
131 	(void)signal(SIGQUIT, SIG_IGN);
132 	while (wait3(&status, 0, &ru) != pid);		/* XXX use waitpid */
133 	gettimeofday(&after, (struct timezone *)NULL);
134 	if ( ! WIFEXITED(status))
135 		warnx("command terminated abnormally");
136 	if (WIFSIGNALED(status))
137 		exitonsig = WTERMSIG(status);
138 	after.tv_sec -= before.tv_sec;
139 	after.tv_usec -= before.tv_usec;
140 	if (after.tv_usec < 0)
141 		after.tv_sec--, after.tv_usec += 1000000;
142 	if (pflag) {
143 		/* POSIX wants output that must look like
144 		"real %f\nuser %f\nsys %f\n" and requires
145 		at least two digits after the radix. */
146 		fprintf(out, "real %ld%c%02ld\n",
147 			after.tv_sec, decimal_point,
148 			after.tv_usec/10000);
149 		fprintf(out, "user %ld%c%02ld\n",
150 			ru.ru_utime.tv_sec, decimal_point,
151 			ru.ru_utime.tv_usec/10000);
152 		fprintf(out, "sys %ld%c%02ld\n",
153 			ru.ru_stime.tv_sec, decimal_point,
154 			ru.ru_stime.tv_usec/10000);
155 	} else if (hflag) {
156 		humantime(out, after.tv_sec, after.tv_usec/10000);
157 		fprintf(out, " real\t");
158 		humantime(out, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
159 		fprintf(out, " user\t");
160 		humantime(out, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
161 		fprintf(out, " sys\n");
162 	} else {
163 		fprintf(out, "%9ld%c%02ld real ",
164 			after.tv_sec, decimal_point,
165 			after.tv_usec/10000);
166 		fprintf(out, "%9ld%c%02ld user ",
167 			ru.ru_utime.tv_sec, decimal_point,
168 			ru.ru_utime.tv_usec/10000);
169 		fprintf(out, "%9ld%c%02ld sys\n",
170 			ru.ru_stime.tv_sec, decimal_point,
171 			ru.ru_stime.tv_usec/10000);
172 	}
173 	if (lflag) {
174 		int hz = getstathz();
175 		u_long ticks;
176 
177 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
178 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
179 
180 		/*
181 		 * If our round-off on the tick calculation still puts us at 0,
182 		 * then always assume at least one tick.
183 		 */
184 		if (ticks == 0)
185 			ticks = 1;
186 
187 		fprintf(out, "%10ld  %s\n",
188 			ru.ru_maxrss, "maximum resident set size");
189 		fprintf(out, "%10ld  %s\n",
190 			ru.ru_ixrss / ticks, "average shared memory size");
191 		fprintf(out, "%10ld  %s\n",
192 			ru.ru_idrss / ticks, "average unshared data size");
193 		fprintf(out, "%10ld  %s\n",
194 			ru.ru_isrss / ticks, "average unshared stack size");
195 		fprintf(out, "%10ld  %s\n",
196 			ru.ru_minflt, "page reclaims");
197 		fprintf(out, "%10ld  %s\n",
198 			ru.ru_majflt, "page faults");
199 		fprintf(out, "%10ld  %s\n",
200 			ru.ru_nswap, "swaps");
201 		fprintf(out, "%10ld  %s\n",
202 			ru.ru_inblock, "block input operations");
203 		fprintf(out, "%10ld  %s\n",
204 			ru.ru_oublock, "block output operations");
205 		fprintf(out, "%10ld  %s\n",
206 			ru.ru_msgsnd, "messages sent");
207 		fprintf(out, "%10ld  %s\n",
208 			ru.ru_msgrcv, "messages received");
209 		fprintf(out, "%10ld  %s\n",
210 			ru.ru_nsignals, "signals received");
211 		fprintf(out, "%10ld  %s\n",
212 			ru.ru_nvcsw, "voluntary context switches");
213 		fprintf(out, "%10ld  %s\n",
214 			ru.ru_nivcsw, "involuntary context switches");
215 	}
216 	if (exitonsig) {
217 		if (signal(exitonsig, SIG_DFL) == SIG_ERR)
218 			perror("signal");
219 		else
220 			kill(getpid(), exitonsig);
221 	}
222 	exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
223 }
224 
225 static void
226 usage()
227 {
228 	fprintf(stderr,
229 	    "usage: time [-al] [-h|-p] [-o file] utility [argument ...]\n");
230 	exit(1);
231 }
232 
233 /*
234  * Return the frequency of the kernel's statistics clock.
235  */
236 static int
237 getstathz()
238 {
239 	struct clockinfo clockrate;
240 	int mib[2];
241 	size_t size;
242 
243 	mib[0] = CTL_KERN;
244 	mib[1] = KERN_CLOCKRATE;
245 	size = sizeof clockrate;
246 	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
247 		err(1, "sysctl kern.clockrate");
248 	return clockrate.stathz;
249 }
250 
251 static void
252 humantime(out, sec, usec)
253 	FILE *out;
254 	long sec;
255 	long usec;
256 {
257 	long days, hrs, mins;
258 
259 	days = sec / (60L * 60 * 24);
260 	sec %= (60L * 60 * 24);
261 	hrs = sec / (60L * 60);
262 	sec %= (60L * 60);
263 	mins = sec / 60;
264 	sec %= 60;
265 
266 	fprintf(out, "\t");
267 	if (days)
268 		fprintf(out, "%ldd", days);
269 	if (hrs)
270 		fprintf(out, "%ldh", hrs);
271 	if (mins)
272 		fprintf(out, "%ldm", mins);
273 	fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);
274 }
275