xref: /freebsd/usr.bin/time/time.c (revision 6e824f3713011f7955a4f88fb16445e8e2cbe72c)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1987, 1988, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes #include <sys/types.h>
339b50d902SRodney W. Grimes #include <sys/resource.h>
349b50d902SRodney W. Grimes #include <sys/signal.h>
35f0850246SJohn Polstra #include <sys/sysctl.h>
3669df5b5aSYaroslav Tykhiy #include <sys/time.h>
37bc2c47dfSJordan K. Hubbard #include <sys/wait.h>
38f0850246SJohn Polstra 
39f0850246SJohn Polstra #include <err.h>
40844338c2SOllivier Robert #include <errno.h>
41e1fc0c16SAndrey A. Chernov #include <locale.h>
4269df5b5aSYaroslav Tykhiy #include <signal.h>
4369df5b5aSYaroslav Tykhiy #include <stdio.h>
4469df5b5aSYaroslav Tykhiy #include <stdlib.h>
4561d8eedeSDavid Malone #include <stdint.h>
46821df508SXin LI #include <string.h>
47cb938d95SAlan Somers #include <time.h>
48fabfd133SDag-Erling Smørgrav #include <unistd.h>
499b50d902SRodney W. Grimes 
503f330d7dSWarner Losh static int getstathz(void);
513f330d7dSWarner Losh static void humantime(FILE *, long, long);
52cb938d95SAlan Somers static void showtime(FILE *, struct timespec *, struct timespec *,
53b6d7073dSPawel Jakub Dawidek     struct rusage *);
54b6d7073dSPawel Jakub Dawidek static void siginfo(int);
55cccdaf50SAlfonso Gregory static void usage(void) __dead2;
56f0850246SJohn Polstra 
57*6e824f37SKyle Evans static volatile sig_atomic_t siginfo_recvd;
58e1fc0c16SAndrey A. Chernov static char decimal_point;
59cb938d95SAlan Somers static struct timespec before_ts;
60b6d7073dSPawel Jakub Dawidek static int hflag, pflag;
61e1fc0c16SAndrey A. Chernov 
6280c486a4SPhilippe Charnier int
main(int argc,char ** argv)63f4ac32deSDavid Malone main(int argc, char **argv)
649b50d902SRodney W. Grimes {
65b6d7073dSPawel Jakub Dawidek 	int aflag, ch, lflag, status;
6669df5b5aSYaroslav Tykhiy 	int exitonsig;
6769df5b5aSYaroslav Tykhiy 	pid_t pid;
6815309540SYaroslav Tykhiy 	struct rlimit rl;
699b50d902SRodney W. Grimes 	struct rusage ru;
70cb938d95SAlan Somers 	struct timespec after;
71fabfd133SDag-Erling Smørgrav 	char *ofn = NULL;
7269df5b5aSYaroslav Tykhiy 	FILE *out = stderr;
739b50d902SRodney W. Grimes 
74e1fc0c16SAndrey A. Chernov 	(void) setlocale(LC_NUMERIC, "");
75e1fc0c16SAndrey A. Chernov 	decimal_point = localeconv()->decimal_point[0];
76e1fc0c16SAndrey A. Chernov 
77ea257bd5SDavid E. O'Brien 	aflag = hflag = lflag = pflag = 0;
78ea257bd5SDavid E. O'Brien 	while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
799b50d902SRodney W. Grimes 		switch((char)ch) {
8003a22248SPoul-Henning Kamp 		case 'a':
81fabfd133SDag-Erling Smørgrav 			aflag = 1;
8203a22248SPoul-Henning Kamp 			break;
83ea257bd5SDavid E. O'Brien 		case 'h':
84ea257bd5SDavid E. O'Brien 			hflag = 1;
85ea257bd5SDavid E. O'Brien 			break;
869b50d902SRodney W. Grimes 		case 'l':
879b50d902SRodney W. Grimes 			lflag = 1;
889b50d902SRodney W. Grimes 			break;
899b81ca85SDag-Erling Smørgrav 		case 'o':
909b81ca85SDag-Erling Smørgrav 			ofn = optarg;
919b81ca85SDag-Erling Smørgrav 			break;
92844338c2SOllivier Robert 		case 'p':
93844338c2SOllivier Robert 			pflag = 1;
94844338c2SOllivier Robert 			break;
959b50d902SRodney W. Grimes 		case '?':
969b50d902SRodney W. Grimes 		default:
9780c486a4SPhilippe Charnier 			usage();
989b50d902SRodney W. Grimes 		}
999b50d902SRodney W. Grimes 
1009b50d902SRodney W. Grimes 	if (!(argc -= optind))
1019b50d902SRodney W. Grimes 		exit(0);
1029b50d902SRodney W. Grimes 	argv += optind;
1039b50d902SRodney W. Grimes 
104fabfd133SDag-Erling Smørgrav 	if (ofn) {
105689f0bd9SJilles Tjoelker 	        if ((out = fopen(ofn, aflag ? "ae" : "we")) == NULL)
1069b81ca85SDag-Erling Smørgrav 		        err(1, "%s", ofn);
1079b81ca85SDag-Erling Smørgrav 		setvbuf(out, (char *)NULL, _IONBF, (size_t)0);
108fabfd133SDag-Erling Smørgrav 	}
10903a22248SPoul-Henning Kamp 
1108a9ab042SAlan Somers 	if (clock_gettime(CLOCK_MONOTONIC, &before_ts))
111cb938d95SAlan Somers 		err(1, "clock_gettime");
1121fd98d7dSDag-Erling Smørgrav 	switch(pid = fork()) {
1139b50d902SRodney W. Grimes 	case -1:			/* error */
11480c486a4SPhilippe Charnier 		err(1, "time");
1159b50d902SRodney W. Grimes 		/* NOTREACHED */
1169b50d902SRodney W. Grimes 	case 0:				/* child */
1179b50d902SRodney W. Grimes 		execvp(*argv, argv);
118d6c762afSTim J. Robbins 		err(errno == ENOENT ? 127 : 126, "%s", *argv);
1199b50d902SRodney W. Grimes 		/* NOTREACHED */
1209b50d902SRodney W. Grimes 	}
1219b50d902SRodney W. Grimes 	/* parent */
1229b50d902SRodney W. Grimes 	(void)signal(SIGINT, SIG_IGN);
1239b50d902SRodney W. Grimes 	(void)signal(SIGQUIT, SIG_IGN);
124e48a49b3SJohn-Mark Gurney 	siginfo_recvd = 0;
125b6d7073dSPawel Jakub Dawidek 	(void)signal(SIGINFO, siginfo);
126e48a49b3SJohn-Mark Gurney 	(void)siginterrupt(SIGINFO, 1);
127e48a49b3SJohn-Mark Gurney 	while (wait4(pid, &status, 0, &ru) != pid) {
128e48a49b3SJohn-Mark Gurney 		if (siginfo_recvd) {
129e48a49b3SJohn-Mark Gurney 			siginfo_recvd = 0;
1308a9ab042SAlan Somers 			if (clock_gettime(CLOCK_MONOTONIC, &after))
131cb938d95SAlan Somers 				err(1, "clock_gettime");
132e48a49b3SJohn-Mark Gurney 			getrusage(RUSAGE_CHILDREN, &ru);
133cb938d95SAlan Somers 			showtime(stdout, &before_ts, &after, &ru);
134e48a49b3SJohn-Mark Gurney 		}
135e48a49b3SJohn-Mark Gurney 	}
1368a9ab042SAlan Somers 	if (clock_gettime(CLOCK_MONOTONIC, &after))
137cb938d95SAlan Somers 		err(1, "clock_gettime");
1389a4902a9SMartin Cracauer 	if ( ! WIFEXITED(status))
13980c486a4SPhilippe Charnier 		warnx("command terminated abnormally");
14069df5b5aSYaroslav Tykhiy 	exitonsig = WIFSIGNALED(status) ? WTERMSIG(status) : 0;
141cb938d95SAlan Somers 	showtime(out, &before_ts, &after, &ru);
1429b50d902SRodney W. Grimes 	if (lflag) {
143f0850246SJohn Polstra 		int hz = getstathz();
1449e5fd22bSPaul Traina 		u_long ticks;
1459b50d902SRodney W. Grimes 
1469b50d902SRodney W. Grimes 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
1479b50d902SRodney W. Grimes 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
1489e5fd22bSPaul Traina 
1499e5fd22bSPaul Traina 		/*
1509e5fd22bSPaul Traina 		 * If our round-off on the tick calculation still puts us at 0,
1519e5fd22bSPaul Traina 		 * then always assume at least one tick.
1529e5fd22bSPaul Traina 		 */
1539e5fd22bSPaul Traina 		if (ticks == 0)
1549e5fd22bSPaul Traina 			ticks = 1;
1559e5fd22bSPaul Traina 
15603a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1579b50d902SRodney W. Grimes 			ru.ru_maxrss, "maximum resident set size");
15803a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1599b50d902SRodney W. Grimes 			ru.ru_ixrss / ticks, "average shared memory size");
16003a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1619b50d902SRodney W. Grimes 			ru.ru_idrss / ticks, "average unshared data size");
16203a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1639b50d902SRodney W. Grimes 			ru.ru_isrss / ticks, "average unshared stack size");
16403a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1659b50d902SRodney W. Grimes 			ru.ru_minflt, "page reclaims");
16603a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1679b50d902SRodney W. Grimes 			ru.ru_majflt, "page faults");
16803a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1699b50d902SRodney W. Grimes 			ru.ru_nswap, "swaps");
17003a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1719b50d902SRodney W. Grimes 			ru.ru_inblock, "block input operations");
17203a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1739b50d902SRodney W. Grimes 			ru.ru_oublock, "block output operations");
17403a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1759b50d902SRodney W. Grimes 			ru.ru_msgsnd, "messages sent");
17603a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1779b50d902SRodney W. Grimes 			ru.ru_msgrcv, "messages received");
17803a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1799b50d902SRodney W. Grimes 			ru.ru_nsignals, "signals received");
18003a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1819b50d902SRodney W. Grimes 			ru.ru_nvcsw, "voluntary context switches");
18203a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1839b50d902SRodney W. Grimes 			ru.ru_nivcsw, "involuntary context switches");
1849b50d902SRodney W. Grimes 	}
18569df5b5aSYaroslav Tykhiy 	/*
18669df5b5aSYaroslav Tykhiy 	 * If the child has exited on a signal, exit on the same
18769df5b5aSYaroslav Tykhiy 	 * signal, too, in order to reproduce the child's exit status.
18869df5b5aSYaroslav Tykhiy 	 * However, avoid actually dumping core from the current process.
18969df5b5aSYaroslav Tykhiy 	 */
1909a4902a9SMartin Cracauer 	if (exitonsig) {
1910050672aSDavid Malone 		if (signal(exitonsig, SIG_DFL) == SIG_ERR)
19269df5b5aSYaroslav Tykhiy 			warn("signal");
19315309540SYaroslav Tykhiy 		else {
19415309540SYaroslav Tykhiy 			rl.rlim_max = rl.rlim_cur = 0;
19515309540SYaroslav Tykhiy 			if (setrlimit(RLIMIT_CORE, &rl) == -1)
19615309540SYaroslav Tykhiy 				warn("setrlimit");
1979a4902a9SMartin Cracauer 			kill(getpid(), exitonsig);
1989a4902a9SMartin Cracauer 		}
19915309540SYaroslav Tykhiy 	}
200bc2c47dfSJordan K. Hubbard 	exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
2019b50d902SRodney W. Grimes }
202f0850246SJohn Polstra 
20380c486a4SPhilippe Charnier static void
usage(void)204f4ac32deSDavid Malone usage(void)
20580c486a4SPhilippe Charnier {
2063f6c6c91STim J. Robbins 	fprintf(stderr,
2073f6c6c91STim J. Robbins 	    "usage: time [-al] [-h | -p] [-o file] utility [argument ...]\n");
2089b81ca85SDag-Erling Smørgrav 	exit(1);
20980c486a4SPhilippe Charnier }
21080c486a4SPhilippe Charnier 
211f0850246SJohn Polstra /*
212f0850246SJohn Polstra  * Return the frequency of the kernel's statistics clock.
213f0850246SJohn Polstra  */
214f0850246SJohn Polstra static int
getstathz(void)215f4ac32deSDavid Malone getstathz(void)
216f0850246SJohn Polstra {
217f0850246SJohn Polstra 	int mib[2];
218f0850246SJohn Polstra 	size_t size;
21969df5b5aSYaroslav Tykhiy 	struct clockinfo clockrate;
220f0850246SJohn Polstra 
221f0850246SJohn Polstra 	mib[0] = CTL_KERN;
222f0850246SJohn Polstra 	mib[1] = KERN_CLOCKRATE;
223f0850246SJohn Polstra 	size = sizeof clockrate;
224f0850246SJohn Polstra 	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
225f0850246SJohn Polstra 		err(1, "sysctl kern.clockrate");
226f0850246SJohn Polstra 	return clockrate.stathz;
227f0850246SJohn Polstra }
228ea257bd5SDavid E. O'Brien 
229ea257bd5SDavid E. O'Brien static void
humantime(FILE * out,long sec,long centisec)230cb938d95SAlan Somers humantime(FILE *out, long sec, long centisec)
231ea257bd5SDavid E. O'Brien {
232ea257bd5SDavid E. O'Brien 	long days, hrs, mins;
233ea257bd5SDavid E. O'Brien 
234ea257bd5SDavid E. O'Brien 	days = sec / (60L * 60 * 24);
235ea257bd5SDavid E. O'Brien 	sec %= (60L * 60 * 24);
236ea257bd5SDavid E. O'Brien 	hrs = sec / (60L * 60);
237ea257bd5SDavid E. O'Brien 	sec %= (60L * 60);
238ea257bd5SDavid E. O'Brien 	mins = sec / 60;
239ea257bd5SDavid E. O'Brien 	sec %= 60;
240ea257bd5SDavid E. O'Brien 
241ea257bd5SDavid E. O'Brien 	fprintf(out, "\t");
242ea257bd5SDavid E. O'Brien 	if (days)
243ea257bd5SDavid E. O'Brien 		fprintf(out, "%ldd", days);
244ea257bd5SDavid E. O'Brien 	if (hrs)
245ea257bd5SDavid E. O'Brien 		fprintf(out, "%ldh", hrs);
246ea257bd5SDavid E. O'Brien 	if (mins)
247ea257bd5SDavid E. O'Brien 		fprintf(out, "%ldm", mins);
248cb938d95SAlan Somers 	fprintf(out, "%ld%c%02lds", sec, decimal_point, centisec);
249ea257bd5SDavid E. O'Brien }
250b6d7073dSPawel Jakub Dawidek 
251b6d7073dSPawel Jakub Dawidek static void
showtime(FILE * out,struct timespec * before,struct timespec * after,struct rusage * ru)252cb938d95SAlan Somers showtime(FILE *out, struct timespec *before, struct timespec *after,
253b6d7073dSPawel Jakub Dawidek     struct rusage *ru)
254b6d7073dSPawel Jakub Dawidek {
255b6d7073dSPawel Jakub Dawidek 
256b6d7073dSPawel Jakub Dawidek 	after->tv_sec -= before->tv_sec;
257cb938d95SAlan Somers 	after->tv_nsec -= before->tv_nsec;
258cb938d95SAlan Somers 	if (after->tv_nsec < 0)
259cb938d95SAlan Somers 		after->tv_sec--, after->tv_nsec += 1000000000;
260b6d7073dSPawel Jakub Dawidek 
261b6d7073dSPawel Jakub Dawidek 	if (pflag) {
262b6d7073dSPawel Jakub Dawidek 		/* POSIX wants output that must look like
263b6d7073dSPawel Jakub Dawidek 		"real %f\nuser %f\nsys %f\n" and requires
264b6d7073dSPawel Jakub Dawidek 		at least two digits after the radix. */
26561d8eedeSDavid Malone 		fprintf(out, "real %jd%c%02ld\n",
26661d8eedeSDavid Malone 			(intmax_t)after->tv_sec, decimal_point,
267cb938d95SAlan Somers 			after->tv_nsec/10000000);
26861d8eedeSDavid Malone 		fprintf(out, "user %jd%c%02ld\n",
26961d8eedeSDavid Malone 			(intmax_t)ru->ru_utime.tv_sec, decimal_point,
270b6d7073dSPawel Jakub Dawidek 			ru->ru_utime.tv_usec/10000);
27161d8eedeSDavid Malone 		fprintf(out, "sys %jd%c%02ld\n",
27261d8eedeSDavid Malone 			(intmax_t)ru->ru_stime.tv_sec, decimal_point,
273b6d7073dSPawel Jakub Dawidek 			ru->ru_stime.tv_usec/10000);
274b6d7073dSPawel Jakub Dawidek 	} else if (hflag) {
275cb938d95SAlan Somers 		humantime(out, after->tv_sec, after->tv_nsec/10000000);
276b6d7073dSPawel Jakub Dawidek 		fprintf(out, " real\t");
277b6d7073dSPawel Jakub Dawidek 		humantime(out, ru->ru_utime.tv_sec, ru->ru_utime.tv_usec/10000);
278b6d7073dSPawel Jakub Dawidek 		fprintf(out, " user\t");
279b6d7073dSPawel Jakub Dawidek 		humantime(out, ru->ru_stime.tv_sec, ru->ru_stime.tv_usec/10000);
280b6d7073dSPawel Jakub Dawidek 		fprintf(out, " sys\n");
281b6d7073dSPawel Jakub Dawidek 	} else {
28261d8eedeSDavid Malone 		fprintf(out, "%9jd%c%02ld real ",
28361d8eedeSDavid Malone 			(intmax_t)after->tv_sec, decimal_point,
284cb938d95SAlan Somers 			after->tv_nsec/10000000);
28561d8eedeSDavid Malone 		fprintf(out, "%9jd%c%02ld user ",
28661d8eedeSDavid Malone 			(intmax_t)ru->ru_utime.tv_sec, decimal_point,
287b6d7073dSPawel Jakub Dawidek 			ru->ru_utime.tv_usec/10000);
28861d8eedeSDavid Malone 		fprintf(out, "%9jd%c%02ld sys\n",
28961d8eedeSDavid Malone 			(intmax_t)ru->ru_stime.tv_sec, decimal_point,
290b6d7073dSPawel Jakub Dawidek 			ru->ru_stime.tv_usec/10000);
291b6d7073dSPawel Jakub Dawidek 	}
292b6d7073dSPawel Jakub Dawidek }
293b6d7073dSPawel Jakub Dawidek 
294b6d7073dSPawel Jakub Dawidek static void
siginfo(int sig __unused)295b6d7073dSPawel Jakub Dawidek siginfo(int sig __unused)
296b6d7073dSPawel Jakub Dawidek {
297b6d7073dSPawel Jakub Dawidek 
298e48a49b3SJohn-Mark Gurney 	siginfo_recvd = 1;
299b6d7073dSPawel Jakub Dawidek }
300