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