xref: /dflybsd-src/usr.bin/time/time.c (revision 225cb38fefe493d6b322d9e7699d9987ec2c56e5)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1987, 1988, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)time.c	8.1 (Berkeley) 6/6/93
31  * $FreeBSD: src/usr.bin/time/time.c,v 1.14.2.5 2002/06/28 08:35:15 tjr Exp $
32  */
33 
34 #include <sys/user.h>
35 #include <sys/time.h>
36 #include <sys/resource.h>
37 #include <sys/signal.h>
38 #include <sys/wait.h>
39 
40 #include <err.h>
41 #include <errno.h>
42 #ifndef BOOTSTRAPPING
43 #include <kinfo.h>
44 #endif
45 #include <locale.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <stdint.h>
50 #include <string.h>
51 #include <time.h>
52 #include <unistd.h>
53 
54 static void humantime(FILE *, long, long);
55 static void showtime(FILE *, struct timespec *, struct timespec *,
56     struct rusage *);
57 static void siginfo(int);
58 
59 static void usage(void);
60 
61 static sig_atomic_t siginfo_recvd;
62 static char decimal_point;
63 struct timespec before_ts;
64 static int hflag, pflag;
65 
66 int
67 main(int argc, char **argv)
68 {
69 	pid_t pid;
70 	int aflag, ch, lflag, status;
71 	int exit_on_sig;
72 	struct timespec after;
73 	struct rusage ru;
74 	FILE *out = stderr;
75 	const char *ofn = NULL;
76 
77 	setlocale(LC_NUMERIC, "");
78 	decimal_point = localeconv()->decimal_point[0];
79 
80 	aflag = hflag = lflag = pflag = 0;
81 	while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
82 		switch (ch) {
83 		case 'a':
84 			aflag = 1;
85 			break;
86 		case 'h':
87 			hflag = 1;
88 			break;
89 		case 'l':
90 #ifndef BOOTSTRAPPING
91 			lflag = 1;
92 #endif
93 			break;
94 		case 'o':
95 			ofn = optarg;
96 			break;
97 		case 'p':
98 			pflag = 1;
99 			break;
100 		default:
101 			usage();
102 		}
103 
104 	if (!(argc -= optind))
105 		exit(0);
106 	argv += optind;
107 
108 	if (ofn) {
109 	        if ((out = fopen(ofn, aflag ? "ae" : "we")) == NULL)
110 		        err(1, "%s", ofn);
111 		setvbuf(out, NULL, _IONBF, (size_t)0);
112 	}
113 
114 	if (clock_gettime(CLOCK_MONOTONIC, &before_ts) == -1)
115 		err(1, "clock_gettime failed");
116 	switch (pid = fork()) {
117 	case -1:			/* error */
118 		err(1, "could not fork");
119 		/* NOTREACHED */
120 	case 0:				/* child */
121 		execvp(*argv, argv);
122 		err(errno == ENOENT ? 127 : 126, "%s", *argv);
123 		/* NOTREACHED */
124 	}
125 	/* parent */
126 	if (signal(SIGINT, SIG_IGN) == SIG_ERR)
127 		err(1, "signal failed");
128 	if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
129 		err(1, "signal failed");
130 	siginfo_recvd = 0;
131 #ifdef SIGINFO
132 	if (signal(SIGINFO, siginfo) == SIG_ERR)
133 		err(1, "signal failed");
134 	siginterrupt(SIGINFO,1 );
135 #endif
136 	while (wait4(pid, &status, 0, &ru) != pid){
137 		if (siginfo_recvd) {
138 			siginfo_recvd = 0;
139 			if (clock_gettime(CLOCK_MONOTONIC, &after) == -1)
140 				err(1, "clock_gettime failed");
141 			getrusage(RUSAGE_CHILDREN, &ru);
142 			showtime(stdout, &before_ts, &after, &ru);
143 		}
144 	}
145 	if (clock_gettime(CLOCK_MONOTONIC, &after) == -1)
146 		err(1, "clock_gettime failed");
147 	if (!WIFEXITED(status))
148 		warnx("command terminated abnormally");
149 	exit_on_sig = WIFSIGNALED(status) ? WTERMSIG(status) : 0;
150 	showtime(out, &before_ts, &after, &ru);
151 #ifndef BOOTSTRAPPING
152 	if (lflag) {
153 		int hz;
154 		u_long ticks;
155 
156 		if (kinfo_get_sched_stathz(&hz))
157 			err(1, "kinfo_get_sched_stathz");
158 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
159 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
160 
161 		/*
162 		 * If our round-off on the tick calculation still puts us at 0,
163 		 * then always assume at least one tick.
164 		 */
165 		if (ticks == 0)
166 			ticks = 1;
167 
168 		fprintf(out, "%10ld  %s\n",
169 			ru.ru_maxrss, "maximum resident set size");
170 		fprintf(out, "%10ld  %s\n",
171 			ru.ru_ixrss / ticks, "average shared memory size");
172 		fprintf(out, "%10ld  %s\n",
173 			ru.ru_idrss / ticks, "average unshared data size");
174 		fprintf(out, "%10ld  %s\n",
175 			ru.ru_isrss / ticks, "average unshared stack size");
176 		fprintf(out, "%10ld  %s\n",
177 			ru.ru_minflt, "page reclaims");
178 		fprintf(out, "%10ld  %s\n",
179 			ru.ru_majflt, "page faults");
180 		fprintf(out, "%10ld  %s\n",
181 			ru.ru_nswap, "swaps");
182 		fprintf(out, "%10ld  %s\n",
183 			ru.ru_inblock, "block input operations");
184 		fprintf(out, "%10ld  %s\n",
185 			ru.ru_oublock, "block output operations");
186 		fprintf(out, "%10ld  %s\n",
187 			ru.ru_msgsnd, "messages sent");
188 		fprintf(out, "%10ld  %s\n",
189 			ru.ru_msgrcv, "messages received");
190 		fprintf(out, "%10ld  %s\n",
191 			ru.ru_nsignals, "signals received");
192 		fprintf(out, "%10ld  %s\n",
193 			ru.ru_nvcsw, "voluntary context switches");
194 		fprintf(out, "%10ld  %s\n",
195 			ru.ru_nivcsw, "involuntary context switches");
196 	}
197 #endif
198 	/*
199 	 * If the child has exited on a signal, exit on the same
200 	 * signal, too, in order to reproduce the child's exit
201 	 * status.  However, avoid actually dumping core from
202 	 * current process.
203 	 */
204 	if (exit_on_sig) {
205 		if (signal(exit_on_sig, SIG_DFL) == SIG_ERR)
206 			warn("signal failed");
207 		else
208 			kill(getpid(), exit_on_sig);
209 	}
210 	exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
211 }
212 
213 static void
214 usage(void)
215 {
216 	fprintf(stderr,
217 	    "usage: time [-al] [-h|-p] [-o file] utility [argument ...]\n");
218 	exit(1);
219 }
220 
221 static void
222 humantime(FILE *out, long sec, long usec)
223 {
224 	long days, hrs, mins;
225 
226 	days = sec / (60L * 60 * 24);
227 	sec %= (60L * 60 * 24);
228 	hrs = sec / (60L * 60);
229 	sec %= (60L * 60);
230 	mins = sec / 60;
231 	sec %= 60;
232 
233 	fprintf(out, "\t");
234 	if (days)
235 		fprintf(out, "%ldd", days);
236 	if (hrs)
237 		fprintf(out, "%ldh", hrs);
238 	if (mins)
239 		fprintf(out, "%ldm", mins);
240 	fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);
241 }
242 
243 static void
244 showtime(FILE *out, struct timespec *before, struct timespec *after,
245     struct rusage *ru)
246 {
247 
248 	after->tv_sec -= before->tv_sec;
249 	after->tv_nsec -= before->tv_nsec;
250 	if (after->tv_nsec < 0)
251 		after->tv_sec--, after->tv_nsec += 1000000000L;
252 
253 	if (pflag) {
254 		/* POSIX wants output that must look like
255 		"real %f\nuser %f\nsys %f\n" and requires
256 		at least two digits after the radix. */
257 		fprintf(out, "real %jd%c%02ld\n",
258 			(intmax_t)after->tv_sec, decimal_point,
259 			after->tv_nsec/10000000L);
260 		fprintf(out, "user %jd%c%02ld\n",
261 			(intmax_t)ru->ru_utime.tv_sec, decimal_point,
262 			ru->ru_utime.tv_usec/10000);
263 		fprintf(out, "sys %jd%c%02ld\n",
264 			(intmax_t)ru->ru_stime.tv_sec, decimal_point,
265 			ru->ru_stime.tv_usec/10000);
266 	} else if (hflag) {
267 		humantime(out, after->tv_sec, after->tv_nsec/10000000);
268 		fprintf(out, " real\t");
269 		humantime(out, ru->ru_utime.tv_sec, ru->ru_utime.tv_usec/10000);
270 		fprintf(out, " user\t");
271 		humantime(out, ru->ru_stime.tv_sec, ru->ru_stime.tv_usec/10000);
272 		fprintf(out, " sys\n");
273 	} else {
274 		fprintf(out, "%9jd%c%02ld real ",
275 			(intmax_t)after->tv_sec, decimal_point,
276 			after->tv_nsec/10000000);
277 		fprintf(out, "%9jd%c%02ld user ",
278 			(intmax_t)ru->ru_utime.tv_sec, decimal_point,
279 			ru->ru_utime.tv_usec/10000);
280 		fprintf(out, "%9jd%c%02ld sys\n",
281 			(intmax_t)ru->ru_stime.tv_sec, decimal_point,
282 			ru->ru_stime.tv_usec/10000);
283 	}
284 }
285 
286 static void
287 siginfo(int sig __unused)
288 {
289 	siginfo_recvd = 1;
290 }
291