time.c revision 50477
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 50477 1999-08-28 01:08:13Z peter $";
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 <string.h>
60#include <unistd.h>
61#include <signal.h>
62
63static int getstathz __P((void));
64static void usage __P((void));
65
66int
67main(argc, argv)
68	int argc;
69	char **argv;
70{
71	extern char *optarg;
72	extern int optind;
73
74	register int pid;
75	int aflag, ch, lflag, status, pflag;
76	struct timeval before, after;
77	struct rusage ru;
78	FILE *out = stderr;
79	char *ofn = NULL;
80	int exitonsig = 0; /* Die with same signal as child */
81
82	aflag = lflag = pflag = 0;
83	while ((ch = getopt(argc, argv, "alo:p")) != -1)
84		switch((char)ch) {
85		case 'a':
86			aflag = 1;
87			break;
88		case 'l':
89			lflag = 1;
90			break;
91		case 'o':
92			ofn = optarg;
93			break;
94		case 'p':
95			pflag = 1;
96			break;
97		case '?':
98		default:
99			usage();
100		}
101
102	if (!(argc -= optind))
103		exit(0);
104	argv += optind;
105
106	if (ofn) {
107	        if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL)
108		        err(1, "%s", ofn);
109		setvbuf(out, (char *)NULL, _IONBF, (size_t)0);
110	}
111
112	gettimeofday(&before, (struct timezone *)NULL);
113	switch(pid = fork()) {
114	case -1:			/* error */
115		err(1, "time");
116		/* NOTREACHED */
117	case 0:				/* child */
118		errno = 0;
119		execvp(*argv, argv);
120		warn("%s", *argv);
121		if (errno == ENOENT)
122			_exit(127); /* POSIX: utility could not be found */
123		else
124			_exit(126); /* POSIX: utility could not be invoked */
125		/* NOTREACHED */
126	}
127	/* parent */
128	(void)signal(SIGINT, SIG_IGN);
129	(void)signal(SIGQUIT, SIG_IGN);
130	while (wait3(&status, 0, &ru) != pid);		/* XXX use waitpid */
131	gettimeofday(&after, (struct timezone *)NULL);
132	if ( ! WIFEXITED(status))
133		warnx("command terminated abnormally");
134	if (WIFSIGNALED(status))
135		exitonsig = WTERMSIG(status);
136	after.tv_sec -= before.tv_sec;
137	after.tv_usec -= before.tv_usec;
138	if (after.tv_usec < 0)
139		after.tv_sec--, after.tv_usec += 1000000;
140	if (pflag) {
141		/* POSIX wants output that must look like
142		"real %f\nuser %f\nsys %f\n" and requires
143		at least two digits after the radix. */
144		fprintf(out, "real %ld.%02ld\n",
145			after.tv_sec, after.tv_usec/10000);
146		fprintf(out, "user %ld.%02ld\n",
147			ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
148		fprintf(out, "sys %ld.%02ld\n",
149			ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
150	} else {
151		fprintf(out, "%9ld.%02ld real ",
152			after.tv_sec, after.tv_usec/10000);
153		fprintf(out, "%9ld.%02ld user ",
154			ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
155		fprintf(out, "%9ld.%02ld sys\n",
156			ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
157	}
158	if (lflag) {
159		int hz = getstathz();
160		u_long ticks;
161
162		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
163		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
164
165		/*
166		 * If our round-off on the tick calculation still puts us at 0,
167		 * then always assume at least one tick.
168		 */
169		if (ticks == 0)
170			ticks = 1;
171
172		fprintf(out, "%10ld  %s\n",
173			ru.ru_maxrss, "maximum resident set size");
174		fprintf(out, "%10ld  %s\n",
175			ru.ru_ixrss / ticks, "average shared memory size");
176		fprintf(out, "%10ld  %s\n",
177			ru.ru_idrss / ticks, "average unshared data size");
178		fprintf(out, "%10ld  %s\n",
179			ru.ru_isrss / ticks, "average unshared stack size");
180		fprintf(out, "%10ld  %s\n",
181			ru.ru_minflt, "page reclaims");
182		fprintf(out, "%10ld  %s\n",
183			ru.ru_majflt, "page faults");
184		fprintf(out, "%10ld  %s\n",
185			ru.ru_nswap, "swaps");
186		fprintf(out, "%10ld  %s\n",
187			ru.ru_inblock, "block input operations");
188		fprintf(out, "%10ld  %s\n",
189			ru.ru_oublock, "block output operations");
190		fprintf(out, "%10ld  %s\n",
191			ru.ru_msgsnd, "messages sent");
192		fprintf(out, "%10ld  %s\n",
193			ru.ru_msgrcv, "messages received");
194		fprintf(out, "%10ld  %s\n",
195			ru.ru_nsignals, "signals received");
196		fprintf(out, "%10ld  %s\n",
197			ru.ru_nvcsw, "voluntary context switches");
198		fprintf(out, "%10ld  %s\n",
199			ru.ru_nivcsw, "involuntary context switches");
200	}
201	if (exitonsig) {
202		if (signal(exitonsig, SIG_DFL) < 0)
203			perror("signal");
204		else
205			kill(getpid(), exitonsig);
206	}
207	exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
208}
209
210static void
211usage()
212{
213	fprintf(stderr, "usage: time [-alp] [-o file] command\n");
214	exit(1);
215}
216
217/*
218 * Return the frequency of the kernel's statistics clock.
219 */
220static int
221getstathz()
222{
223	struct clockinfo clockrate;
224	int mib[2];
225	size_t size;
226
227	mib[0] = CTL_KERN;
228	mib[1] = KERN_CLOCKRATE;
229	size = sizeof clockrate;
230	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
231		err(1, "sysctl kern.clockrate");
232	return clockrate.stathz;
233}
234