2 * Copyright (c) 1987, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.2 2003/06/17 04:29:32 dillon Exp $
39 #include <sys/types.h>
41 #include <sys/resource.h>
42 #include <sys/signal.h>
43 #include <sys/sysctl.h>
55 static int getstathz(void);
56 static void humantime(FILE *, long, long);
57 static void usage(void);
59 static char decimal_point;
67 int aflag, ch, hflag, lflag, status, pflag;
68 struct timeval before, after;
72 int exitonsig = 0; /* Die with same signal as child */
74 (void) setlocale(LC_NUMERIC, "");
75 decimal_point = localeconv()->decimal_point[0];
77 aflag = hflag = lflag = pflag = 0;
78 while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
100 if (!(argc -= optind))
105 if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL)
107 setvbuf(out, (char *)NULL, _IONBF, (size_t)0);
110 gettimeofday(&before, (struct timezone *)NULL);
111 switch(pid = fork()) {
117 err(errno == ENOENT ? 127 : 126, "%s", *argv);
121 (void)signal(SIGINT, SIG_IGN);
122 (void)signal(SIGQUIT, SIG_IGN);
123 while (wait3(&status, 0, &ru) != pid); /* XXX use waitpid */
124 gettimeofday(&after, (struct timezone *)NULL);
125 if ( ! WIFEXITED(status))
126 warnx("command terminated abnormally");
127 if (WIFSIGNALED(status))
128 exitonsig = WTERMSIG(status);
129 after.tv_sec -= before.tv_sec;
130 after.tv_usec -= before.tv_usec;
131 if (after.tv_usec < 0)
132 after.tv_sec--, after.tv_usec += 1000000;
134 /* POSIX wants output that must look like
135 "real %f\nuser %f\nsys %f\n" and requires
136 at least two digits after the radix. */
137 fprintf(out, "real %ld%c%02ld\n",
138 after.tv_sec, decimal_point,
139 after.tv_usec/10000);
140 fprintf(out, "user %ld%c%02ld\n",
141 ru.ru_utime.tv_sec, decimal_point,
142 ru.ru_utime.tv_usec/10000);
143 fprintf(out, "sys %ld%c%02ld\n",
144 ru.ru_stime.tv_sec, decimal_point,
145 ru.ru_stime.tv_usec/10000);
147 humantime(out, after.tv_sec, after.tv_usec/10000);
148 fprintf(out, " real\t");
149 humantime(out, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000);
150 fprintf(out, " user\t");
151 humantime(out, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000);
152 fprintf(out, " sys\n");
154 fprintf(out, "%9ld%c%02ld real ",
155 after.tv_sec, decimal_point,
156 after.tv_usec/10000);
157 fprintf(out, "%9ld%c%02ld user ",
158 ru.ru_utime.tv_sec, decimal_point,
159 ru.ru_utime.tv_usec/10000);
160 fprintf(out, "%9ld%c%02ld sys\n",
161 ru.ru_stime.tv_sec, decimal_point,
162 ru.ru_stime.tv_usec/10000);
165 int hz = getstathz();
168 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
169 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
172 * If our round-off on the tick calculation still puts us at 0,
173 * then always assume at least one tick.
178 fprintf(out, "%10ld %s\n",
179 ru.ru_maxrss, "maximum resident set size");
180 fprintf(out, "%10ld %s\n",
181 ru.ru_ixrss / ticks, "average shared memory size");
182 fprintf(out, "%10ld %s\n",
183 ru.ru_idrss / ticks, "average unshared data size");
184 fprintf(out, "%10ld %s\n",
185 ru.ru_isrss / ticks, "average unshared stack size");
186 fprintf(out, "%10ld %s\n",
187 ru.ru_minflt, "page reclaims");
188 fprintf(out, "%10ld %s\n",
189 ru.ru_majflt, "page faults");
190 fprintf(out, "%10ld %s\n",
191 ru.ru_nswap, "swaps");
192 fprintf(out, "%10ld %s\n",
193 ru.ru_inblock, "block input operations");
194 fprintf(out, "%10ld %s\n",
195 ru.ru_oublock, "block output operations");
196 fprintf(out, "%10ld %s\n",
197 ru.ru_msgsnd, "messages sent");
198 fprintf(out, "%10ld %s\n",
199 ru.ru_msgrcv, "messages received");
200 fprintf(out, "%10ld %s\n",
201 ru.ru_nsignals, "signals received");
202 fprintf(out, "%10ld %s\n",
203 ru.ru_nvcsw, "voluntary context switches");
204 fprintf(out, "%10ld %s\n",
205 ru.ru_nivcsw, "involuntary context switches");
208 if (signal(exitonsig, SIG_DFL) == SIG_ERR)
211 kill(getpid(), exitonsig);
213 exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
220 "usage: time [-al] [-h|-p] [-o file] utility [argument ...]\n");
225 * Return the frequency of the kernel's statistics clock.
230 struct clockinfo clockrate;
235 mib[1] = KERN_CLOCKRATE;
236 size = sizeof clockrate;
237 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
238 err(1, "sysctl kern.clockrate");
239 return clockrate.stathz;
243 humantime(out, sec, usec)
248 long days, hrs, mins;
250 days = sec / (60L * 60 * 24);
251 sec %= (60L * 60 * 24);
252 hrs = sec / (60L * 60);
259 fprintf(out, "%ldd", days);
261 fprintf(out, "%ldh", hrs);
263 fprintf(out, "%ldm", mins);
264 fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);