xref: /freebsd/usr.bin/time/time.c (revision c3aac50f284c6cca5b4f2eb46aaa13812cb8b630)
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));
6480c486a4SPhilippe Charnier static void usage __P((void));
65f0850246SJohn Polstra 
6680c486a4SPhilippe Charnier int
679b50d902SRodney W. Grimes main(argc, argv)
689b50d902SRodney W. Grimes 	int argc;
699b50d902SRodney W. Grimes 	char **argv;
709b50d902SRodney W. Grimes {
7103a22248SPoul-Henning Kamp 	extern char *optarg;
7203a22248SPoul-Henning Kamp 	extern int optind;
7303a22248SPoul-Henning Kamp 
749b50d902SRodney W. Grimes 	register int pid;
75844338c2SOllivier Robert 	int aflag, ch, lflag, status, pflag;
769b50d902SRodney W. Grimes 	struct timeval before, after;
779b50d902SRodney W. Grimes 	struct rusage ru;
78fabfd133SDag-Erling Smørgrav 	FILE *out = stderr;
79fabfd133SDag-Erling Smørgrav 	char *ofn = NULL;
809a4902a9SMartin Cracauer 	int exitonsig = 0; /* Die with same signal as child */
819b50d902SRodney W. Grimes 
82844338c2SOllivier Robert 	aflag = lflag = pflag = 0;
83844338c2SOllivier Robert 	while ((ch = getopt(argc, argv, "alo:p")) != -1)
849b50d902SRodney W. Grimes 		switch((char)ch) {
8503a22248SPoul-Henning Kamp 		case 'a':
86fabfd133SDag-Erling Smørgrav 			aflag = 1;
8703a22248SPoul-Henning Kamp 			break;
889b50d902SRodney W. Grimes 		case 'l':
899b50d902SRodney W. Grimes 			lflag = 1;
909b50d902SRodney W. Grimes 			break;
919b81ca85SDag-Erling Smørgrav 		case 'o':
929b81ca85SDag-Erling Smørgrav 			ofn = optarg;
939b81ca85SDag-Erling Smørgrav 			break;
94844338c2SOllivier Robert 		case 'p':
95844338c2SOllivier Robert 			pflag = 1;
96844338c2SOllivier Robert 			break;
979b50d902SRodney W. Grimes 		case '?':
989b50d902SRodney W. Grimes 		default:
9980c486a4SPhilippe Charnier 			usage();
1009b50d902SRodney W. Grimes 		}
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes 	if (!(argc -= optind))
1039b50d902SRodney W. Grimes 		exit(0);
1049b50d902SRodney W. Grimes 	argv += optind;
1059b50d902SRodney W. Grimes 
106fabfd133SDag-Erling Smørgrav 	if (ofn) {
107fabfd133SDag-Erling Smørgrav 	        if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL)
1089b81ca85SDag-Erling Smørgrav 		        err(1, "%s", ofn);
1099b81ca85SDag-Erling Smørgrav 		setvbuf(out, (char *)NULL, _IONBF, (size_t)0);
110fabfd133SDag-Erling Smørgrav 	}
11103a22248SPoul-Henning Kamp 
1129b50d902SRodney W. Grimes 	gettimeofday(&before, (struct timezone *)NULL);
1131fd98d7dSDag-Erling Smørgrav 	switch(pid = fork()) {
1149b50d902SRodney W. Grimes 	case -1:			/* error */
11580c486a4SPhilippe Charnier 		err(1, "time");
1169b50d902SRodney W. Grimes 		/* NOTREACHED */
1179b50d902SRodney W. Grimes 	case 0:				/* child */
118844338c2SOllivier Robert 		errno = 0;
1199b50d902SRodney W. Grimes 		execvp(*argv, argv);
12080c486a4SPhilippe Charnier 		warn("%s", *argv);
121844338c2SOllivier Robert 		if (errno == ENOENT)
122844338c2SOllivier Robert 			_exit(127); /* POSIX: utility could not be found */
123844338c2SOllivier Robert 		else
124844338c2SOllivier Robert 			_exit(126); /* POSIX: utility could not be invoked */
1259b50d902SRodney W. Grimes 		/* NOTREACHED */
1269b50d902SRodney W. Grimes 	}
1279b50d902SRodney W. Grimes 	/* parent */
1289b50d902SRodney W. Grimes 	(void)signal(SIGINT, SIG_IGN);
1299b50d902SRodney W. Grimes 	(void)signal(SIGQUIT, SIG_IGN);
1309b50d902SRodney W. Grimes 	while (wait3(&status, 0, &ru) != pid);		/* XXX use waitpid */
1319b50d902SRodney W. Grimes 	gettimeofday(&after, (struct timezone *)NULL);
1329a4902a9SMartin Cracauer 	if ( ! WIFEXITED(status))
13380c486a4SPhilippe Charnier 		warnx("command terminated abnormally");
1349a4902a9SMartin Cracauer 	if (WIFSIGNALED(status))
1359a4902a9SMartin Cracauer 		exitonsig = WTERMSIG(status);
1369b50d902SRodney W. Grimes 	after.tv_sec -= before.tv_sec;
1379b50d902SRodney W. Grimes 	after.tv_usec -= before.tv_usec;
1389b50d902SRodney W. Grimes 	if (after.tv_usec < 0)
1399b50d902SRodney W. Grimes 		after.tv_sec--, after.tv_usec += 1000000;
140844338c2SOllivier Robert 	if (pflag) {
141844338c2SOllivier Robert 		/* POSIX wants output that must look like
142844338c2SOllivier Robert 		"real %f\nuser %f\nsys %f\n" and requires
143844338c2SOllivier Robert 		at least two digits after the radix. */
144844338c2SOllivier Robert 		fprintf(out, "real %ld.%02ld\n",
145844338c2SOllivier Robert 			after.tv_sec, after.tv_usec/10000);
146844338c2SOllivier Robert 		fprintf(out, "user %ld.%02ld\n",
147844338c2SOllivier Robert 			ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
148844338c2SOllivier Robert 		fprintf(out, "sys %ld.%02ld\n",
149844338c2SOllivier Robert 			ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
150844338c2SOllivier Robert 	} else {
151844338c2SOllivier Robert 		fprintf(out, "%9ld.%02ld real ",
152844338c2SOllivier Robert 			after.tv_sec, after.tv_usec/10000);
15303a22248SPoul-Henning Kamp 		fprintf(out, "%9ld.%02ld user ",
1549b50d902SRodney W. Grimes 			ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
15503a22248SPoul-Henning Kamp 		fprintf(out, "%9ld.%02ld sys\n",
1569b50d902SRodney W. Grimes 			ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
157844338c2SOllivier Robert 	}
1589b50d902SRodney W. Grimes 	if (lflag) {
159f0850246SJohn Polstra 		int hz = getstathz();
1609e5fd22bSPaul Traina 		u_long ticks;
1619b50d902SRodney W. Grimes 
1629b50d902SRodney W. Grimes 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
1639b50d902SRodney W. Grimes 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
1649e5fd22bSPaul Traina 
1659e5fd22bSPaul Traina 		/*
1669e5fd22bSPaul Traina 		 * If our round-off on the tick calculation still puts us at 0,
1679e5fd22bSPaul Traina 		 * then always assume at least one tick.
1689e5fd22bSPaul Traina 		 */
1699e5fd22bSPaul Traina 		if (ticks == 0)
1709e5fd22bSPaul Traina 			ticks = 1;
1719e5fd22bSPaul Traina 
17203a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1739b50d902SRodney W. Grimes 			ru.ru_maxrss, "maximum resident set size");
17403a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1759b50d902SRodney W. Grimes 			ru.ru_ixrss / ticks, "average shared memory size");
17603a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1779b50d902SRodney W. Grimes 			ru.ru_idrss / ticks, "average unshared data size");
17803a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1799b50d902SRodney W. Grimes 			ru.ru_isrss / ticks, "average unshared stack size");
18003a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1819b50d902SRodney W. Grimes 			ru.ru_minflt, "page reclaims");
18203a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1839b50d902SRodney W. Grimes 			ru.ru_majflt, "page faults");
18403a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1859b50d902SRodney W. Grimes 			ru.ru_nswap, "swaps");
18603a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1879b50d902SRodney W. Grimes 			ru.ru_inblock, "block input operations");
18803a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1899b50d902SRodney W. Grimes 			ru.ru_oublock, "block output operations");
19003a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1919b50d902SRodney W. Grimes 			ru.ru_msgsnd, "messages sent");
19203a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1939b50d902SRodney W. Grimes 			ru.ru_msgrcv, "messages received");
19403a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1959b50d902SRodney W. Grimes 			ru.ru_nsignals, "signals received");
19603a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1979b50d902SRodney W. Grimes 			ru.ru_nvcsw, "voluntary context switches");
19803a22248SPoul-Henning Kamp 		fprintf(out, "%10ld  %s\n",
1999b50d902SRodney W. Grimes 			ru.ru_nivcsw, "involuntary context switches");
2009b50d902SRodney W. Grimes 	}
2019a4902a9SMartin Cracauer 	if (exitonsig) {
2029a4902a9SMartin Cracauer 		if (signal(exitonsig, SIG_DFL) < 0)
2039a4902a9SMartin Cracauer 			perror("signal");
2049a4902a9SMartin Cracauer 		else
2059a4902a9SMartin Cracauer 			kill(getpid(), exitonsig);
2069a4902a9SMartin Cracauer 	}
207bc2c47dfSJordan K. Hubbard 	exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
2089b50d902SRodney W. Grimes }
209f0850246SJohn Polstra 
21080c486a4SPhilippe Charnier static void
21180c486a4SPhilippe Charnier usage()
21280c486a4SPhilippe Charnier {
213844338c2SOllivier Robert 	fprintf(stderr, "usage: time [-alp] [-o file] command\n");
2149b81ca85SDag-Erling Smørgrav 	exit(1);
21580c486a4SPhilippe Charnier }
21680c486a4SPhilippe Charnier 
217f0850246SJohn Polstra /*
218f0850246SJohn Polstra  * Return the frequency of the kernel's statistics clock.
219f0850246SJohn Polstra  */
220f0850246SJohn Polstra static int
221f0850246SJohn Polstra getstathz()
222f0850246SJohn Polstra {
223f0850246SJohn Polstra 	struct clockinfo clockrate;
224f0850246SJohn Polstra 	int mib[2];
225f0850246SJohn Polstra 	size_t size;
226f0850246SJohn Polstra 
227f0850246SJohn Polstra 	mib[0] = CTL_KERN;
228f0850246SJohn Polstra 	mib[1] = KERN_CLOCKRATE;
229f0850246SJohn Polstra 	size = sizeof clockrate;
230f0850246SJohn Polstra 	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
231f0850246SJohn Polstra 		err(1, "sysctl kern.clockrate");
232f0850246SJohn Polstra 	return clockrate.stathz;
233f0850246SJohn Polstra }
234