xref: /dragonfly/usr.bin/time/time.c (revision 2038fb68)
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  * @(#) Copyright (c) 1987, 1988, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)time.c	8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/time/time.c,v 1.14.2.5 2002/06/28 08:35:15 tjr Exp $
36  * $DragonFly: src/usr.bin/time/time.c,v 1.11 2005/03/04 16:54:37 liamfoy Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #include <sys/signal.h>
43 #include <sys/sysctl.h>
44 #include <sys/wait.h>
45 
46 #include <err.h>
47 #include <errno.h>
48 #include <kinfo.h>
49 #include <locale.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 static void humantime(FILE *, long, long);
57 static void usage(void);
58 
59 static char decimal_point;
60 
61 int
62 main(int argc, char **argv)
63 {
64 	pid_t pid;
65 	int aflag, ch, hflag, lflag, status, pflag;
66 	int exit_on_sig;
67 	struct timeval before, after;
68 	struct rusage ru;
69 	FILE *out = stderr;
70 	const char *ofn = NULL;
71 
72 	setlocale(LC_NUMERIC, "");
73 	decimal_point = localeconv()->decimal_point[0];
74 
75 	aflag = hflag = lflag = pflag = 0;
76 	while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
77 		switch (ch) {
78 		case 'a':
79 			aflag = 1;
80 			break;
81 		case 'h':
82 			hflag = 1;
83 			break;
84 		case 'l':
85 			lflag = 1;
86 			break;
87 		case 'o':
88 			ofn = optarg;
89 			break;
90 		case 'p':
91 			pflag = 1;
92 			break;
93 		default:
94 			usage();
95 		}
96 
97 	if (!(argc -= optind))
98 		exit(0);
99 	argv += optind;
100 
101 	if (ofn) {
102 	        if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL)
103 		        err(1, "%s", ofn);
104 		setvbuf(out, NULL, _IONBF, (size_t)0);
105 	}
106 
107 	if (gettimeofday(&before, NULL) == -1)
108 		err(1, "gettimeofday failed");
109 	switch (pid = fork()) {
110 	case -1:			/* error */
111 		err(1, "could not fork");
112 		/* NOTREACHED */
113 	case 0:				/* child */
114 		execvp(*argv, argv);
115 		err(errno == ENOENT ? 127 : 126, "%s", *argv);
116 		/* NOTREACHED */
117 	}
118 	/* parent */
119 	if (signal(SIGINT, SIG_IGN) == SIG_ERR)
120 		err(1, "signal failed");
121 	if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
122 		err(1, "signal failed");
123 	while (wait4(pid, &status, 0, &ru) != pid)		/* XXX use waitpid */
124 		;
125 	if (gettimeofday(&after, NULL) == -1)
126 		err(1, "gettimeofday failed");
127 	if (!WIFEXITED(status))
128 		warnx("command terminated abnormally");
129 	exit_on_sig = WIFSIGNALED(status) ? WTERMSIG(status) : 0;
130 	after.tv_sec -= before.tv_sec;
131 	after.tv_usec -= before.tv_usec;
132 	if (after.tv_usec < 0)
133 		after.tv_sec--, after.tv_usec += 1000000;
134 	if (pflag) {
135 		/* POSIX wants output that must look like
136 		"real %f\nuser %f\nsys %f\n" and requires
137 		at least two digits after the radix. */
138 		fprintf(out, "real %ld%c%02ld\n",
139 			after.tv_sec, decimal_point,
140 			after.tv_usec / 10000);
141 		fprintf(out, "user %ld%c%02ld\n",
142 			ru.ru_utime.tv_sec, decimal_point,
143 			ru.ru_utime.tv_usec / 10000);
144 		fprintf(out, "sys %ld%c%02ld\n",
145 			ru.ru_stime.tv_sec, decimal_point,
146 			ru.ru_stime.tv_usec / 10000);
147 	} else if (hflag) {
148 		humantime(out, after.tv_sec, after.tv_usec / 10000);
149 		fprintf(out, " real\t");
150 		humantime(out, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec / 10000);
151 		fprintf(out, " user\t");
152 		humantime(out, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec / 10000);
153 		fprintf(out, " sys\n");
154 	} else {
155 		fprintf(out, "%9ld%c%02ld real ",
156 			after.tv_sec, decimal_point,
157 			after.tv_usec / 10000);
158 		fprintf(out, "%9ld%c%02ld user ",
159 			ru.ru_utime.tv_sec, decimal_point,
160 			ru.ru_utime.tv_usec / 10000);
161 		fprintf(out, "%9ld%c%02ld sys\n",
162 			ru.ru_stime.tv_sec, decimal_point,
163 			ru.ru_stime.tv_usec / 10000);
164 	}
165 	if (lflag) {
166 		int hz;
167 		u_long ticks;
168 
169 		if (kinfo_get_sched_stathz(&hz))
170 			err(1, "kinfo_get_sched_stathz");
171 		ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
172 		     hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
173 
174 		/*
175 		 * If our round-off on the tick calculation still puts us at 0,
176 		 * then always assume at least one tick.
177 		 */
178 		if (ticks == 0)
179 			ticks = 1;
180 
181 		fprintf(out, "%10ld  %s\n",
182 			ru.ru_maxrss, "maximum resident set size");
183 		fprintf(out, "%10ld  %s\n",
184 			ru.ru_ixrss / ticks, "average shared memory size");
185 		fprintf(out, "%10ld  %s\n",
186 			ru.ru_idrss / ticks, "average unshared data size");
187 		fprintf(out, "%10ld  %s\n",
188 			ru.ru_isrss / ticks, "average unshared stack size");
189 		fprintf(out, "%10ld  %s\n",
190 			ru.ru_minflt, "page reclaims");
191 		fprintf(out, "%10ld  %s\n",
192 			ru.ru_majflt, "page faults");
193 		fprintf(out, "%10ld  %s\n",
194 			ru.ru_nswap, "swaps");
195 		fprintf(out, "%10ld  %s\n",
196 			ru.ru_inblock, "block input operations");
197 		fprintf(out, "%10ld  %s\n",
198 			ru.ru_oublock, "block output operations");
199 		fprintf(out, "%10ld  %s\n",
200 			ru.ru_msgsnd, "messages sent");
201 		fprintf(out, "%10ld  %s\n",
202 			ru.ru_msgrcv, "messages received");
203 		fprintf(out, "%10ld  %s\n",
204 			ru.ru_nsignals, "signals received");
205 		fprintf(out, "%10ld  %s\n",
206 			ru.ru_nvcsw, "voluntary context switches");
207 		fprintf(out, "%10ld  %s\n",
208 			ru.ru_nivcsw, "involuntary context switches");
209 	}
210 	/*
211 	 * If the child has exited on a signal, exit on the same
212 	 * signal, too, in order to reproduce the child's exit
213 	 * status.  However, avoid actually dumping core from
214 	 * current process.
215 	 */
216 	if (exit_on_sig) {
217 		if (signal(exit_on_sig, SIG_DFL) == SIG_ERR)
218 			warn("signal failed");
219 		else
220 			kill(getpid(), exit_on_sig);
221 	}
222 	exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
223 }
224 
225 static void
226 usage(void)
227 {
228 	fprintf(stderr,
229 	    "usage: time [-al] [-h|-p] [-o file] utility [argument ...]\n");
230 	exit(1);
231 }
232 
233 static void
234 humantime(FILE *out, long sec, long usec)
235 {
236 	long days, hrs, mins;
237 
238 	days = sec / (60L * 60 * 24);
239 	sec %= (60L * 60 * 24);
240 	hrs = sec / (60L * 60);
241 	sec %= (60L * 60);
242 	mins = sec / 60;
243 	sec %= 60;
244 
245 	fprintf(out, "\t");
246 	if (days)
247 		fprintf(out, "%ldd", days);
248 	if (hrs)
249 		fprintf(out, "%ldh", hrs);
250 	if (mins)
251 		fprintf(out, "%ldm", mins);
252 	fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);
253 }
254