time.c revision 87300
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
35static 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
42static char sccsid[] = "@(#)time.c	8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
45  "$FreeBSD: head/usr.bin/time/time.c 87300 2001-12-03 21:32:01Z dwmalone $";
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
64static int getstathz __P((void));
65static void humantime __P((FILE *, long, long));
66static void usage __P((void));
67
68static char decimal_point;
69
70int
71main(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		errno = 0;
126		execvp(*argv, argv);
127		warn("%s", *argv);
128		if (errno == ENOENT)
129			_exit(127); /* POSIX: utility could not be found */
130		else
131			_exit(126); /* POSIX: utility could not be invoked */
132		/* NOTREACHED */
133	}
134	/* parent */
135	(void)signal(SIGINT, SIG_IGN);
136	(void)signal(SIGQUIT, SIG_IGN);
137	while (wait3(&status, 0, &ru) != pid);		/* XXX use waitpid */
138	gettimeofday(&after, (struct timezone *)NULL);
139	if ( ! WIFEXITED(status))
140		warnx("command terminated abnormally");
141	if (WIFSIGNALED(status))
142		exitonsig = WTERMSIG(status);
143	after.tv_sec -= before.tv_sec;
144	after.tv_usec -= before.tv_usec;
145	if (after.tv_usec < 0)
146		after.tv_sec--, after.tv_usec += 1000000;
147	if (pflag) {
148		/* POSIX wants output that must look like
149		"real %f\nuser %f\nsys %f\n" and requires
150		at least two digits after the radix. */
151		fprintf(out, "real %ld%c%02ld\n",
152			after.tv_sec, decimal_point,
153			after.tv_usec/10000);
154		fprintf(out, "user %ld%c%02ld\n",
155			ru.ru_utime.tv_sec, decimal_point,
156			ru.ru_utime.tv_usec/10000);
157		fprintf(out, "sys %ld%c%02ld\n",
158			ru.ru_stime.tv_sec, decimal_point,
159			ru.ru_stime.tv_usec/10000);
160	} else if (hflag) {
161		humantime(out, after.tv_sec, after.tv_usec/10000);
162		fprintf(out, " real\t");
163		humantime(out, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
164		fprintf(out, " user\t");
165		humantime(out, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
166		fprintf(out, " sys\n");
167	} else {
168		fprintf(out, "%9ld%c%02ld real ",
169			after.tv_sec, decimal_point,
170			after.tv_usec/10000);
171		fprintf(out, "%9ld%c%02ld user ",
172			ru.ru_utime.tv_sec, decimal_point,
173			ru.ru_utime.tv_usec/10000);
174		fprintf(out, "%9ld%c%02ld sys\n",
175			ru.ru_stime.tv_sec, decimal_point,
176			ru.ru_stime.tv_usec/10000);
177	}
178	if (lflag) {
179		int hz = getstathz();
180		u_long ticks;
181
182		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
183		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
184
185		/*
186		 * If our round-off on the tick calculation still puts us at 0,
187		 * then always assume at least one tick.
188		 */
189		if (ticks == 0)
190			ticks = 1;
191
192		fprintf(out, "%10ld  %s\n",
193			ru.ru_maxrss, "maximum resident set size");
194		fprintf(out, "%10ld  %s\n",
195			ru.ru_ixrss / ticks, "average shared memory size");
196		fprintf(out, "%10ld  %s\n",
197			ru.ru_idrss / ticks, "average unshared data size");
198		fprintf(out, "%10ld  %s\n",
199			ru.ru_isrss / ticks, "average unshared stack size");
200		fprintf(out, "%10ld  %s\n",
201			ru.ru_minflt, "page reclaims");
202		fprintf(out, "%10ld  %s\n",
203			ru.ru_majflt, "page faults");
204		fprintf(out, "%10ld  %s\n",
205			ru.ru_nswap, "swaps");
206		fprintf(out, "%10ld  %s\n",
207			ru.ru_inblock, "block input operations");
208		fprintf(out, "%10ld  %s\n",
209			ru.ru_oublock, "block output operations");
210		fprintf(out, "%10ld  %s\n",
211			ru.ru_msgsnd, "messages sent");
212		fprintf(out, "%10ld  %s\n",
213			ru.ru_msgrcv, "messages received");
214		fprintf(out, "%10ld  %s\n",
215			ru.ru_nsignals, "signals received");
216		fprintf(out, "%10ld  %s\n",
217			ru.ru_nvcsw, "voluntary context switches");
218		fprintf(out, "%10ld  %s\n",
219			ru.ru_nivcsw, "involuntary context switches");
220	}
221	if (exitonsig) {
222		if (signal(exitonsig, SIG_DFL) == SIG_ERR)
223			perror("signal");
224		else
225			kill(getpid(), exitonsig);
226	}
227	exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
228}
229
230static void
231usage()
232{
233	fprintf(stderr, "usage: time [-al] [-h|-p] [-o file] command\n");
234	exit(1);
235}
236
237/*
238 * Return the frequency of the kernel's statistics clock.
239 */
240static int
241getstathz()
242{
243	struct clockinfo clockrate;
244	int mib[2];
245	size_t size;
246
247	mib[0] = CTL_KERN;
248	mib[1] = KERN_CLOCKRATE;
249	size = sizeof clockrate;
250	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
251		err(1, "sysctl kern.clockrate");
252	return clockrate.stathz;
253}
254
255static void
256humantime(out, sec, usec)
257	FILE *out;
258	long sec;
259	long usec;
260{
261	long days, hrs, mins;
262
263	days = sec / (60L * 60 * 24);
264	sec %= (60L * 60 * 24);
265	hrs = sec / (60L * 60);
266	sec %= (60L * 60);
267	mins = sec / 60;
268	sec %= 60;
269
270	fprintf(out, "\t");
271	if (days)
272		fprintf(out, "%ldd", days);
273	if (hrs)
274		fprintf(out, "%ldh", hrs);
275	if (mins)
276		fprintf(out, "%ldm", mins);
277	fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);
278}
279