xref: /freebsd/usr.bin/time/time.c (revision a07101ab73161f8cbcd0f9a4942ec29bb0abb4eb)
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>
5903a22248SPoul-Henning Kamp #include <string.h>
60fabfd133SDag-Erling Smørgrav #include <unistd.h>
619a4902a9SMartin Cracauer #include <signal.h>
629b50d902SRodney W. Grimes 
63f0850246SJohn Polstra static int getstathz __P((void));
64ea257bd5SDavid E. O'Brien static void humantime __P((FILE *, long, long));
6580c486a4SPhilippe Charnier static void usage __P((void));
66f0850246SJohn Polstra 
6780c486a4SPhilippe Charnier int
689b50d902SRodney W. Grimes main(argc, argv)
699b50d902SRodney W. Grimes 	int argc;
709b50d902SRodney W. Grimes 	char **argv;
719b50d902SRodney W. Grimes {
729b50d902SRodney W. Grimes 	register int pid;
73ea257bd5SDavid E. O'Brien 	int aflag, ch, hflag, lflag, status, pflag;
749b50d902SRodney W. Grimes 	struct timeval before, after;
759b50d902SRodney W. Grimes 	struct rusage ru;
76fabfd133SDag-Erling Smørgrav 	FILE *out = stderr;
77fabfd133SDag-Erling Smørgrav 	char *ofn = NULL;
789a4902a9SMartin Cracauer 	int exitonsig = 0; /* Die with same signal as child */
799b50d902SRodney W. Grimes 
80ea257bd5SDavid E. O'Brien 	aflag = hflag = lflag = pflag = 0;
81ea257bd5SDavid E. O'Brien 	while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
829b50d902SRodney W. Grimes 		switch((char)ch) {
8303a22248SPoul-Henning Kamp 		case 'a':
84fabfd133SDag-Erling Smørgrav 			aflag = 1;
8503a22248SPoul-Henning Kamp 			break;
86ea257bd5SDavid E. O'Brien 		case 'h':
87ea257bd5SDavid E. O'Brien 			hflag = 1;
88ea257bd5SDavid E. O'Brien 			break;
899b50d902SRodney W. Grimes 		case 'l':
909b50d902SRodney W. Grimes 			lflag = 1;
919b50d902SRodney W. Grimes 			break;
929b81ca85SDag-Erling Smørgrav 		case 'o':
939b81ca85SDag-Erling Smørgrav 			ofn = optarg;
949b81ca85SDag-Erling Smørgrav 			break;
95844338c2SOllivier Robert 		case 'p':
96844338c2SOllivier Robert 			pflag = 1;
97844338c2SOllivier Robert 			break;
989b50d902SRodney W. Grimes 		case '?':
999b50d902SRodney W. Grimes 		default:
10080c486a4SPhilippe Charnier 			usage();
1019b50d902SRodney W. Grimes 		}
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes 	if (!(argc -= optind))
1049b50d902SRodney W. Grimes 		exit(0);
1059b50d902SRodney W. Grimes 	argv += optind;
1069b50d902SRodney W. Grimes 
107fabfd133SDag-Erling Smørgrav 	if (ofn) {
108fabfd133SDag-Erling Smørgrav 	        if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL)
1099b81ca85SDag-Erling Smørgrav 		        err(1, "%s", ofn);
1109b81ca85SDag-Erling Smørgrav 		setvbuf(out, (char *)NULL, _IONBF, (size_t)0);
111fabfd133SDag-Erling Smørgrav 	}
11203a22248SPoul-Henning Kamp 
1139b50d902SRodney W. Grimes 	gettimeofday(&before, (struct timezone *)NULL);
1141fd98d7dSDag-Erling Smørgrav 	switch(pid = fork()) {
1159b50d902SRodney W. Grimes 	case -1:			/* error */
11680c486a4SPhilippe Charnier 		err(1, "time");
1179b50d902SRodney W. Grimes 		/* NOTREACHED */
1189b50d902SRodney W. Grimes 	case 0:				/* child */
119844338c2SOllivier Robert 		errno = 0;
1209b50d902SRodney W. Grimes 		execvp(*argv, argv);
12180c486a4SPhilippe Charnier 		warn("%s", *argv);
122844338c2SOllivier Robert 		if (errno == ENOENT)
123844338c2SOllivier Robert 			_exit(127); /* POSIX: utility could not be found */
124844338c2SOllivier Robert 		else
125844338c2SOllivier Robert 			_exit(126); /* POSIX: utility could not be invoked */
1269b50d902SRodney W. Grimes 		/* NOTREACHED */
1279b50d902SRodney W. Grimes 	}
1289b50d902SRodney W. Grimes 	/* parent */
1299b50d902SRodney W. Grimes 	(void)signal(SIGINT, SIG_IGN);
1309b50d902SRodney W. Grimes 	(void)signal(SIGQUIT, SIG_IGN);
1319b50d902SRodney W. Grimes 	while (wait3(&status, 0, &ru) != pid);		/* XXX use waitpid */
1329b50d902SRodney W. Grimes 	gettimeofday(&after, (struct timezone *)NULL);
1339a4902a9SMartin Cracauer 	if ( ! WIFEXITED(status))
13480c486a4SPhilippe Charnier 		warnx("command terminated abnormally");
1359a4902a9SMartin Cracauer 	if (WIFSIGNALED(status))
1369a4902a9SMartin Cracauer 		exitonsig = WTERMSIG(status);
1379b50d902SRodney W. Grimes 	after.tv_sec -= before.tv_sec;
1389b50d902SRodney W. Grimes 	after.tv_usec -= before.tv_usec;
1399b50d902SRodney W. Grimes 	if (after.tv_usec < 0)
1409b50d902SRodney W. Grimes 		after.tv_sec--, after.tv_usec += 1000000;
141844338c2SOllivier Robert 	if (pflag) {
142844338c2SOllivier Robert 		/* POSIX wants output that must look like
143844338c2SOllivier Robert 		"real %f\nuser %f\nsys %f\n" and requires
144844338c2SOllivier Robert 		at least two digits after the radix. */
145844338c2SOllivier Robert 		fprintf(out, "real %ld.%02ld\n",
146844338c2SOllivier Robert 			after.tv_sec, after.tv_usec/10000);
147844338c2SOllivier Robert 		fprintf(out, "user %ld.%02ld\n",
148844338c2SOllivier Robert 			ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
149844338c2SOllivier Robert 		fprintf(out, "sys %ld.%02ld\n",
150844338c2SOllivier Robert 			ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
151ea257bd5SDavid E. O'Brien 	} else if (hflag) {
152ea257bd5SDavid E. O'Brien 		humantime(out, after.tv_sec, after.tv_usec/10000);
153a07101abSDavid E. O'Brien 		fprintf(out, " real\t");
154ea257bd5SDavid E. O'Brien 		humantime(out, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
155a07101abSDavid E. O'Brien 		fprintf(out, " user\t");
156ea257bd5SDavid E. O'Brien 		humantime(out, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
157ea257bd5SDavid E. O'Brien 		fprintf(out, " sys\n");
158844338c2SOllivier Robert 	} else {
159844338c2SOllivier Robert 		fprintf(out, "%9ld.%02ld real ",
160844338c2SOllivier Robert 			after.tv_sec, after.tv_usec/10000);
16103a22248SPoul-Henning Kamp 		fprintf(out, "%9ld.%02ld user ",
1629b50d902SRodney W. Grimes 			ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
16303a22248SPoul-Henning Kamp 		fprintf(out, "%9ld.%02ld sys\n",
1649b50d902SRodney W. Grimes 			ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
165844338c2SOllivier Robert 	}
1669b50d902SRodney W. Grimes 	if (lflag) {
167f0850246SJohn Polstra 		int hz = getstathz();
1689e5fd22bSPaul Traina 		u_long ticks;
1699b50d902SRodney W. Grimes 
1709b50d902SRodney W. Grimes 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
1719b50d902SRodney W. Grimes 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
1729e5fd22bSPaul Traina 
1739e5fd22bSPaul Traina 		/*
1749e5fd22bSPaul Traina 		 * If our round-off on the tick calculation still puts us at 0,
1759e5fd22bSPaul Traina 		 * then always assume at least one tick.
1769e5fd22bSPaul Traina 		 */
1779e5fd22bSPaul Traina 		if (ticks == 0)
1789e5fd22bSPaul Traina 			ticks = 1;
1799e5fd22bSPaul Traina 
18003a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1819b50d902SRodney W. Grimes 			ru.ru_maxrss, "maximum resident set size");
18203a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1839b50d902SRodney W. Grimes 			ru.ru_ixrss / ticks, "average shared memory size");
18403a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1859b50d902SRodney W. Grimes 			ru.ru_idrss / ticks, "average unshared data size");
18603a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1879b50d902SRodney W. Grimes 			ru.ru_isrss / ticks, "average unshared stack size");
18803a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1899b50d902SRodney W. Grimes 			ru.ru_minflt, "page reclaims");
19003a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1919b50d902SRodney W. Grimes 			ru.ru_majflt, "page faults");
19203a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1939b50d902SRodney W. Grimes 			ru.ru_nswap, "swaps");
19403a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1959b50d902SRodney W. Grimes 			ru.ru_inblock, "block input operations");
19603a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1979b50d902SRodney W. Grimes 			ru.ru_oublock, "block output operations");
19803a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1999b50d902SRodney W. Grimes 			ru.ru_msgsnd, "messages sent");
20003a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2019b50d902SRodney W. Grimes 			ru.ru_msgrcv, "messages received");
20203a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2039b50d902SRodney W. Grimes 			ru.ru_nsignals, "signals received");
20403a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2059b50d902SRodney W. Grimes 			ru.ru_nvcsw, "voluntary context switches");
20603a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
2079b50d902SRodney W. Grimes 			ru.ru_nivcsw, "involuntary context switches");
2089b50d902SRodney W. Grimes 	}
2099a4902a9SMartin Cracauer 	if (exitonsig) {
2109a4902a9SMartin Cracauer 		if (signal(exitonsig, SIG_DFL) < 0)
2119a4902a9SMartin Cracauer 			perror("signal");
2129a4902a9SMartin Cracauer 		else
2139a4902a9SMartin Cracauer 			kill(getpid(), exitonsig);
2149a4902a9SMartin Cracauer 	}
215bc2c47dfSJordan K. Hubbard 	exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
2169b50d902SRodney W. Grimes }
217f0850246SJohn Polstra 
21880c486a4SPhilippe Charnier static void
21980c486a4SPhilippe Charnier usage()
22080c486a4SPhilippe Charnier {
221ea257bd5SDavid E. O'Brien 	fprintf(stderr, "usage: time [-al] [-h|-p] [-o file] command\n");
2229b81ca85SDag-Erling Smørgrav 	exit(1);
22380c486a4SPhilippe Charnier }
22480c486a4SPhilippe Charnier 
225f0850246SJohn Polstra /*
226f0850246SJohn Polstra  * Return the frequency of the kernel's statistics clock.
227f0850246SJohn Polstra  */
228f0850246SJohn Polstra static int
229f0850246SJohn Polstra getstathz()
230f0850246SJohn Polstra {
231f0850246SJohn Polstra 	struct clockinfo clockrate;
232f0850246SJohn Polstra 	int mib[2];
233f0850246SJohn Polstra 	size_t size;
234f0850246SJohn Polstra 
235f0850246SJohn Polstra 	mib[0] = CTL_KERN;
236f0850246SJohn Polstra 	mib[1] = KERN_CLOCKRATE;
237f0850246SJohn Polstra 	size = sizeof clockrate;
238f0850246SJohn Polstra 	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
239f0850246SJohn Polstra 		err(1, "sysctl kern.clockrate");
240f0850246SJohn Polstra 	return clockrate.stathz;
241f0850246SJohn Polstra }
242ea257bd5SDavid E. O'Brien 
243ea257bd5SDavid E. O'Brien static void
244ea257bd5SDavid E. O'Brien humantime(out, sec, usec)
245ea257bd5SDavid E. O'Brien 	FILE *out;
246ea257bd5SDavid E. O'Brien 	long sec;
247ea257bd5SDavid E. O'Brien 	long usec;
248ea257bd5SDavid E. O'Brien {
249ea257bd5SDavid E. O'Brien 	long days, hrs, mins;
250ea257bd5SDavid E. O'Brien 
251ea257bd5SDavid E. O'Brien 	days = sec / (60L * 60 * 24);
252ea257bd5SDavid E. O'Brien 	sec %= (60L * 60 * 24);
253ea257bd5SDavid E. O'Brien 	hrs = sec / (60L * 60);
254ea257bd5SDavid E. O'Brien 	sec %= (60L * 60);
255ea257bd5SDavid E. O'Brien 	mins = sec / 60;
256ea257bd5SDavid E. O'Brien 	sec %= 60;
257ea257bd5SDavid E. O'Brien 
258ea257bd5SDavid E. O'Brien 	fprintf(out, "\t");
259ea257bd5SDavid E. O'Brien 	if (days)
260ea257bd5SDavid E. O'Brien 		fprintf(out, "%ldd", days);
261ea257bd5SDavid E. O'Brien 	if (hrs)
262ea257bd5SDavid E. O'Brien 		fprintf(out, "%ldh", hrs);
263ea257bd5SDavid E. O'Brien 	if (mins)
264ea257bd5SDavid E. O'Brien 		fprintf(out, "%ldm", mins);
265ea257bd5SDavid E. O'Brien 	fprintf(out, "%ld.%02lds", sec, usec);
266ea257bd5SDavid E. O'Brien }
267