time.c revision 37913
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	"$Id: time.c,v 1.9 1998/07/27 16:54:05 des Exp $";
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 <string.h>
59#include <unistd.h>
60
61static int getstathz __P((void));
62static void usage __P((void));
63
64int
65main(argc, argv)
66	int argc;
67	char **argv;
68{
69	extern char *optarg;
70	extern int optind;
71
72	register int pid;
73	int aflag, ch, lflag, status;
74	struct timeval before, after;
75	struct rusage ru;
76	FILE *out = stderr;
77	char *ofn = NULL;
78
79	aflag = lflag = 0;
80	while ((ch = getopt(argc, argv, "alo:")) != -1)
81		switch((char)ch) {
82		case 'a':
83			aflag = 1;
84			break;
85		case 'l':
86			lflag = 1;
87			break;
88		case 'o':
89			ofn = optarg;
90			break;
91		case '?':
92		default:
93			usage();
94		}
95
96	if (!(argc -= optind))
97		exit(0);
98	argv += optind;
99
100	if (ofn) {
101	        if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL)
102		        err(1, "%s", ofn);
103		setvbuf(out, (char *)NULL, _IONBF, (size_t)0);
104	}
105
106	gettimeofday(&before, (struct timezone *)NULL);
107	switch(pid = vfork()) {
108	case -1:			/* error */
109		err(1, "time");
110		/* NOTREACHED */
111	case 0:				/* child */
112		execvp(*argv, argv);
113		warn("%s", *argv);
114		_exit(1);
115		/* NOTREACHED */
116	}
117	/* parent */
118	(void)signal(SIGINT, SIG_IGN);
119	(void)signal(SIGQUIT, SIG_IGN);
120	while (wait3(&status, 0, &ru) != pid);		/* XXX use waitpid */
121	gettimeofday(&after, (struct timezone *)NULL);
122	if (status&0377)
123		warnx("command terminated abnormally");
124	after.tv_sec -= before.tv_sec;
125	after.tv_usec -= before.tv_usec;
126	if (after.tv_usec < 0)
127		after.tv_sec--, after.tv_usec += 1000000;
128	fprintf(out, "%9ld.%02ld real ", after.tv_sec, after.tv_usec/10000);
129	fprintf(out, "%9ld.%02ld user ",
130	    ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
131	fprintf(out, "%9ld.%02ld sys\n",
132	    ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
133	if (lflag) {
134		int hz = getstathz();
135		u_long ticks;
136
137		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
138		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
139
140		/*
141		 * If our round-off on the tick calculation still puts us at 0,
142		 * then always assume at least one tick.
143		 */
144		if (ticks == 0)
145			ticks = 1;
146
147		fprintf(out, "%10ld  %s\n",
148			ru.ru_maxrss, "maximum resident set size");
149		fprintf(out, "%10ld  %s\n",
150			ru.ru_ixrss / ticks, "average shared memory size");
151		fprintf(out, "%10ld  %s\n",
152			ru.ru_idrss / ticks, "average unshared data size");
153		fprintf(out, "%10ld  %s\n",
154			ru.ru_isrss / ticks, "average unshared stack size");
155		fprintf(out, "%10ld  %s\n",
156			ru.ru_minflt, "page reclaims");
157		fprintf(out, "%10ld  %s\n",
158			ru.ru_majflt, "page faults");
159		fprintf(out, "%10ld  %s\n",
160			ru.ru_nswap, "swaps");
161		fprintf(out, "%10ld  %s\n",
162			ru.ru_inblock, "block input operations");
163		fprintf(out, "%10ld  %s\n",
164			ru.ru_oublock, "block output operations");
165		fprintf(out, "%10ld  %s\n",
166			ru.ru_msgsnd, "messages sent");
167		fprintf(out, "%10ld  %s\n",
168			ru.ru_msgrcv, "messages received");
169		fprintf(out, "%10ld  %s\n",
170			ru.ru_nsignals, "signals received");
171		fprintf(out, "%10ld  %s\n",
172			ru.ru_nvcsw, "voluntary context switches");
173		fprintf(out, "%10ld  %s\n",
174			ru.ru_nivcsw, "involuntary context switches");
175	}
176	exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
177}
178
179static void
180usage()
181{
182	fprintf(stderr, "usage: time [-al] [-o file] command\n");
183	exit(1);
184}
185
186/*
187 * Return the frequency of the kernel's statistics clock.
188 */
189static int
190getstathz()
191{
192	struct clockinfo clockrate;
193	int mib[2];
194	size_t size;
195
196	mib[0] = CTL_KERN;
197	mib[1] = KERN_CLOCKRATE;
198	size = sizeof clockrate;
199	if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
200		err(1, "sysctl kern.clockrate");
201	return clockrate.stathz;
202}
203