xref: /freebsd/usr.bin/time/time.c (revision 3f6c6c912ffe7c9393155fa0f843fd1d3e64a394)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1987, 1988, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
3580c486a4SPhilippe Charnier static const char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1987, 1988, 1993\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
4180c486a4SPhilippe Charnier #if 0
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)time.c	8.1 (Berkeley) 6/6/93";
4380c486a4SPhilippe Charnier #endif
4480c486a4SPhilippe Charnier static const char rcsid[] =
45c3aac50fSPeter Wemm   "$FreeBSD$";
469b50d902SRodney W. Grimes #endif /* not lint */
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <sys/types.h>
499b50d902SRodney W. Grimes #include <sys/time.h>
509b50d902SRodney W. Grimes #include <sys/resource.h>
519b50d902SRodney W. Grimes #include <sys/signal.h>
52f0850246SJohn Polstra #include <sys/sysctl.h>
53bc2c47dfSJordan K. Hubbard #include <stdlib.h>
54bc2c47dfSJordan K. Hubbard #include <sys/wait.h>
55f0850246SJohn Polstra 
56f0850246SJohn Polstra #include <err.h>
579b50d902SRodney W. Grimes #include <stdio.h>
58844338c2SOllivier Robert #include <errno.h>
59e1fc0c16SAndrey A. Chernov #include <locale.h>
6003a22248SPoul-Henning Kamp #include <string.h>
61fabfd133SDag-Erling Smørgrav #include <unistd.h>
629a4902a9SMartin Cracauer #include <signal.h>
639b50d902SRodney W. Grimes 
643f330d7dSWarner Losh static int getstathz(void);
653f330d7dSWarner Losh static void humantime(FILE *, long, long);
663f330d7dSWarner Losh static void usage(void);
67f0850246SJohn Polstra 
68e1fc0c16SAndrey A. Chernov static char decimal_point;
69e1fc0c16SAndrey A. Chernov 
7080c486a4SPhilippe Charnier int
719b50d902SRodney W. Grimes main(argc, argv)
729b50d902SRodney W. Grimes 	int argc;
739b50d902SRodney W. Grimes 	char **argv;
749b50d902SRodney W. Grimes {
759b50d902SRodney W. Grimes 	register int pid;
76ea257bd5SDavid E. O'Brien 	int aflag, ch, hflag, lflag, status, pflag;
779b50d902SRodney W. Grimes 	struct timeval before, after;
789b50d902SRodney W. Grimes 	struct rusage ru;
79fabfd133SDag-Erling Smørgrav 	FILE *out = stderr;
80fabfd133SDag-Erling Smørgrav 	char *ofn = NULL;
819a4902a9SMartin Cracauer 	int exitonsig = 0; /* Die with same signal as child */
829b50d902SRodney W. Grimes 
83e1fc0c16SAndrey A. Chernov 	(void) setlocale(LC_NUMERIC, "");
84e1fc0c16SAndrey A. Chernov 	decimal_point = localeconv()->decimal_point[0];
85e1fc0c16SAndrey A. Chernov 
86ea257bd5SDavid E. O'Brien 	aflag = hflag = lflag = pflag = 0;
87ea257bd5SDavid E. O'Brien 	while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
889b50d902SRodney W. Grimes 		switch((char)ch) {
8903a22248SPoul-Henning Kamp 		case 'a':
90fabfd133SDag-Erling Smørgrav 			aflag = 1;
9103a22248SPoul-Henning Kamp 			break;
92ea257bd5SDavid E. O'Brien 		case 'h':
93ea257bd5SDavid E. O'Brien 			hflag = 1;
94ea257bd5SDavid E. O'Brien 			break;
959b50d902SRodney W. Grimes 		case 'l':
969b50d902SRodney W. Grimes 			lflag = 1;
979b50d902SRodney W. Grimes 			break;
989b81ca85SDag-Erling Smørgrav 		case 'o':
999b81ca85SDag-Erling Smørgrav 			ofn = optarg;
1009b81ca85SDag-Erling Smørgrav 			break;
101844338c2SOllivier Robert 		case 'p':
102844338c2SOllivier Robert 			pflag = 1;
103844338c2SOllivier Robert 			break;
1049b50d902SRodney W. Grimes 		case '?':
1059b50d902SRodney W. Grimes 		default:
10680c486a4SPhilippe Charnier 			usage();
1079b50d902SRodney W. Grimes 		}
1089b50d902SRodney W. Grimes 
1099b50d902SRodney W. Grimes 	if (!(argc -= optind))
1109b50d902SRodney W. Grimes 		exit(0);
1119b50d902SRodney W. Grimes 	argv += optind;
1129b50d902SRodney W. Grimes 
113fabfd133SDag-Erling Smørgrav 	if (ofn) {
114fabfd133SDag-Erling Smørgrav 	        if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL)
1159b81ca85SDag-Erling Smørgrav 		        err(1, "%s", ofn);
1169b81ca85SDag-Erling Smørgrav 		setvbuf(out, (char *)NULL, _IONBF, (size_t)0);
117fabfd133SDag-Erling Smørgrav 	}
11803a22248SPoul-Henning Kamp 
1199b50d902SRodney W. Grimes 	gettimeofday(&before, (struct timezone *)NULL);
1201fd98d7dSDag-Erling Smørgrav 	switch(pid = fork()) {
1219b50d902SRodney W. Grimes 	case -1:			/* error */
12280c486a4SPhilippe Charnier 		err(1, "time");
1239b50d902SRodney W. Grimes 		/* NOTREACHED */
1249b50d902SRodney W. Grimes 	case 0:				/* child */
1259b50d902SRodney W. Grimes 		execvp(*argv, argv);
126d6c762afSTim J. Robbins 		err(errno == ENOENT ? 127 : 126, "%s", *argv);
1279b50d902SRodney W. Grimes 		/* NOTREACHED */
1289b50d902SRodney W. Grimes 	}
1299b50d902SRodney W. Grimes 	/* parent */
1309b50d902SRodney W. Grimes 	(void)signal(SIGINT, SIG_IGN);
1319b50d902SRodney W. Grimes 	(void)signal(SIGQUIT, SIG_IGN);
1329b50d902SRodney W. Grimes 	while (wait3(&status, 0, &ru) != pid);		/* XXX use waitpid */
1339b50d902SRodney W. Grimes 	gettimeofday(&after, (struct timezone *)NULL);
1349a4902a9SMartin Cracauer 	if ( ! WIFEXITED(status))
13580c486a4SPhilippe Charnier 		warnx("command terminated abnormally");
1369a4902a9SMartin Cracauer 	if (WIFSIGNALED(status))
1379a4902a9SMartin Cracauer 		exitonsig = WTERMSIG(status);
1389b50d902SRodney W. Grimes 	after.tv_sec -= before.tv_sec;
1399b50d902SRodney W. Grimes 	after.tv_usec -= before.tv_usec;
1409b50d902SRodney W. Grimes 	if (after.tv_usec < 0)
1419b50d902SRodney W. Grimes 		after.tv_sec--, after.tv_usec += 1000000;
142844338c2SOllivier Robert 	if (pflag) {
143844338c2SOllivier Robert 		/* POSIX wants output that must look like
144844338c2SOllivier Robert 		"real %f\nuser %f\nsys %f\n" and requires
145844338c2SOllivier Robert 		at least two digits after the radix. */
146e1fc0c16SAndrey A. Chernov 		fprintf(out, "real %ld%c%02ld\n",
147e1fc0c16SAndrey A. Chernov 			after.tv_sec, decimal_point,
148e1fc0c16SAndrey A. Chernov 			after.tv_usec/10000);
149e1fc0c16SAndrey A. Chernov 		fprintf(out, "user %ld%c%02ld\n",
150e1fc0c16SAndrey A. Chernov 			ru.ru_utime.tv_sec, decimal_point,
151e1fc0c16SAndrey A. Chernov 			ru.ru_utime.tv_usec/10000);
152e1fc0c16SAndrey A. Chernov 		fprintf(out, "sys %ld%c%02ld\n",
153e1fc0c16SAndrey A. Chernov 			ru.ru_stime.tv_sec, decimal_point,
154e1fc0c16SAndrey A. Chernov 			ru.ru_stime.tv_usec/10000);
155ea257bd5SDavid E. O'Brien 	} else if (hflag) {
156ea257bd5SDavid E. O'Brien 		humantime(out, after.tv_sec, after.tv_usec/10000);
157a07101abSDavid E. O'Brien 		fprintf(out, " real\t");
158ea257bd5SDavid E. O'Brien 		humantime(out, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
159a07101abSDavid E. O'Brien 		fprintf(out, " user\t");
160ea257bd5SDavid E. O'Brien 		humantime(out, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
161ea257bd5SDavid E. O'Brien 		fprintf(out, " sys\n");
162844338c2SOllivier Robert 	} else {
163e1fc0c16SAndrey A. Chernov 		fprintf(out, "%9ld%c%02ld real ",
164e1fc0c16SAndrey A. Chernov 			after.tv_sec, decimal_point,
165e1fc0c16SAndrey A. Chernov 			after.tv_usec/10000);
166e1fc0c16SAndrey A. Chernov 		fprintf(out, "%9ld%c%02ld user ",
167e1fc0c16SAndrey A. Chernov 			ru.ru_utime.tv_sec, decimal_point,
168e1fc0c16SAndrey A. Chernov 			ru.ru_utime.tv_usec/10000);
169e1fc0c16SAndrey A. Chernov 		fprintf(out, "%9ld%c%02ld sys\n",
170e1fc0c16SAndrey A. Chernov 			ru.ru_stime.tv_sec, decimal_point,
171e1fc0c16SAndrey A. Chernov 			ru.ru_stime.tv_usec/10000);
172844338c2SOllivier Robert 	}
1739b50d902SRodney W. Grimes 	if (lflag) {
174f0850246SJohn Polstra 		int hz = getstathz();
1759e5fd22bSPaul Traina 		u_long ticks;
1769b50d902SRodney W. Grimes 
1779b50d902SRodney W. Grimes 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
1789b50d902SRodney W. Grimes 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
1799e5fd22bSPaul Traina 
1809e5fd22bSPaul Traina 		/*
1819e5fd22bSPaul Traina 		 * If our round-off on the tick calculation still puts us at 0,
1829e5fd22bSPaul Traina 		 * then always assume at least one tick.
1839e5fd22bSPaul Traina 		 */
1849e5fd22bSPaul Traina 		if (ticks == 0)
1859e5fd22bSPaul Traina 			ticks = 1;
1869e5fd22bSPaul Traina 
18703a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1889b50d902SRodney W. Grimes 			ru.ru_maxrss, "maximum resident set size");
18903a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1909b50d902SRodney W. Grimes 			ru.ru_ixrss / ticks, "average shared memory size");
19103a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1929b50d902SRodney W. Grimes 			ru.ru_idrss / ticks, "average unshared data size");
19303a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1949b50d902SRodney W. Grimes 			ru.ru_isrss / ticks, "average unshared stack size");
19503a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1969b50d902SRodney W. Grimes 			ru.ru_minflt, "page reclaims");
19703a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1989b50d902SRodney W. Grimes 			ru.ru_majflt, "page faults");
19903a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2009b50d902SRodney W. Grimes 			ru.ru_nswap, "swaps");
20103a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2029b50d902SRodney W. Grimes 			ru.ru_inblock, "block input operations");
20303a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2049b50d902SRodney W. Grimes 			ru.ru_oublock, "block output operations");
20503a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2069b50d902SRodney W. Grimes 			ru.ru_msgsnd, "messages sent");
20703a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2089b50d902SRodney W. Grimes 			ru.ru_msgrcv, "messages received");
20903a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2109b50d902SRodney W. Grimes 			ru.ru_nsignals, "signals received");
21103a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2129b50d902SRodney W. Grimes 			ru.ru_nvcsw, "voluntary context switches");
21303a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2149b50d902SRodney W. Grimes 			ru.ru_nivcsw, "involuntary context switches");
2159b50d902SRodney W. Grimes 	}
2169a4902a9SMartin Cracauer 	if (exitonsig) {
2170050672aSDavid Malone 		if (signal(exitonsig, SIG_DFL) == SIG_ERR)
2189a4902a9SMartin Cracauer 			perror("signal");
2199a4902a9SMartin Cracauer 		else
2209a4902a9SMartin Cracauer 			kill(getpid(), exitonsig);
2219a4902a9SMartin Cracauer 	}
222bc2c47dfSJordan K. Hubbard 	exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
2239b50d902SRodney W. Grimes }
224f0850246SJohn Polstra 
22580c486a4SPhilippe Charnier static void
22680c486a4SPhilippe Charnier usage()
22780c486a4SPhilippe Charnier {
2283f6c6c91STim J. Robbins 	fprintf(stderr,
2293f6c6c91STim J. Robbins 	    "usage: time [-al] [-h|-p] [-o file] utility [argument ...]\n");
2309b81ca85SDag-Erling Smørgrav 	exit(1);
23180c486a4SPhilippe Charnier }
23280c486a4SPhilippe Charnier 
233f0850246SJohn Polstra /*
234f0850246SJohn Polstra  * Return the frequency of the kernel's statistics clock.
235f0850246SJohn Polstra  */
236f0850246SJohn Polstra static int
237f0850246SJohn Polstra getstathz()
238f0850246SJohn Polstra {
239f0850246SJohn Polstra 	struct clockinfo clockrate;
240f0850246SJohn Polstra 	int mib[2];
241f0850246SJohn Polstra 	size_t size;
242f0850246SJohn Polstra 
243f0850246SJohn Polstra 	mib[0] = CTL_KERN;
244f0850246SJohn Polstra 	mib[1] = KERN_CLOCKRATE;
245f0850246SJohn Polstra 	size = sizeof clockrate;
246f0850246SJohn Polstra 	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
247f0850246SJohn Polstra 		err(1, "sysctl kern.clockrate");
248f0850246SJohn Polstra 	return clockrate.stathz;
249f0850246SJohn Polstra }
250ea257bd5SDavid E. O'Brien 
251ea257bd5SDavid E. O'Brien static void
252ea257bd5SDavid E. O'Brien humantime(out, sec, usec)
253ea257bd5SDavid E. O'Brien 	FILE *out;
254ea257bd5SDavid E. O'Brien 	long sec;
255ea257bd5SDavid E. O'Brien 	long usec;
256ea257bd5SDavid E. O'Brien {
257ea257bd5SDavid E. O'Brien 	long days, hrs, mins;
258ea257bd5SDavid E. O'Brien 
259ea257bd5SDavid E. O'Brien 	days = sec / (60L * 60 * 24);
260ea257bd5SDavid E. O'Brien 	sec %= (60L * 60 * 24);
261ea257bd5SDavid E. O'Brien 	hrs = sec / (60L * 60);
262ea257bd5SDavid E. O'Brien 	sec %= (60L * 60);
263ea257bd5SDavid E. O'Brien 	mins = sec / 60;
264ea257bd5SDavid E. O'Brien 	sec %= 60;
265ea257bd5SDavid E. O'Brien 
266ea257bd5SDavid E. O'Brien 	fprintf(out, "\t");
267ea257bd5SDavid E. O'Brien 	if (days)
268ea257bd5SDavid E. O'Brien 		fprintf(out, "%ldd", days);
269ea257bd5SDavid E. O'Brien 	if (hrs)
270ea257bd5SDavid E. O'Brien 		fprintf(out, "%ldh", hrs);
271ea257bd5SDavid E. O'Brien 	if (mins)
272ea257bd5SDavid E. O'Brien 		fprintf(out, "%ldm", mins);
273e1fc0c16SAndrey A. Chernov 	fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);
274ea257bd5SDavid E. O'Brien }
275