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 * 4. 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.
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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1987, 1988, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93";
40 static const char rcsid[] =
44 #include <sys/types.h>
45 #include <sys/resource.h>
46 #include <sys/signal.h>
47 #include <sys/sysctl.h>
61 static int getstathz(void);
62 static void humantime(FILE *, long, long);
63 static void showtime(FILE *, struct timeval *, struct timeval *,
65 static void siginfo(int);
66 static void usage(void);
68 static char decimal_point;
69 static struct timeval before_tv;
70 static int hflag, pflag;
73 main(int argc, char **argv)
75 int aflag, ch, lflag, status;
84 (void) setlocale(LC_NUMERIC, "");
85 decimal_point = localeconv()->decimal_point[0];
87 aflag = hflag = lflag = pflag = 0;
88 while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
110 if (!(argc -= optind))
115 if ((out = fopen(ofn, aflag ? "ae" : "we")) == NULL)
117 setvbuf(out, (char *)NULL, _IONBF, (size_t)0);
120 (void)gettimeofday(&before_tv, NULL);
121 switch(pid = fork()) {
127 err(errno == ENOENT ? 127 : 126, "%s", *argv);
131 (void)signal(SIGINT, SIG_IGN);
132 (void)signal(SIGQUIT, SIG_IGN);
133 (void)signal(SIGINFO, siginfo);
134 while (wait4(pid, &status, 0, &ru) != pid);
135 (void)gettimeofday(&after, NULL);
136 if ( ! WIFEXITED(status))
137 warnx("command terminated abnormally");
138 exitonsig = WIFSIGNALED(status) ? WTERMSIG(status) : 0;
139 showtime(out, &before_tv, &after, &ru);
141 int hz = getstathz();
144 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
145 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
148 * If our round-off on the tick calculation still puts us at 0,
149 * then always assume at least one tick.
154 fprintf(out, "%10ld %s\n",
155 ru.ru_maxrss, "maximum resident set size");
156 fprintf(out, "%10ld %s\n",
157 ru.ru_ixrss / ticks, "average shared memory size");
158 fprintf(out, "%10ld %s\n",
159 ru.ru_idrss / ticks, "average unshared data size");
160 fprintf(out, "%10ld %s\n",
161 ru.ru_isrss / ticks, "average unshared stack size");
162 fprintf(out, "%10ld %s\n",
163 ru.ru_minflt, "page reclaims");
164 fprintf(out, "%10ld %s\n",
165 ru.ru_majflt, "page faults");
166 fprintf(out, "%10ld %s\n",
167 ru.ru_nswap, "swaps");
168 fprintf(out, "%10ld %s\n",
169 ru.ru_inblock, "block input operations");
170 fprintf(out, "%10ld %s\n",
171 ru.ru_oublock, "block output operations");
172 fprintf(out, "%10ld %s\n",
173 ru.ru_msgsnd, "messages sent");
174 fprintf(out, "%10ld %s\n",
175 ru.ru_msgrcv, "messages received");
176 fprintf(out, "%10ld %s\n",
177 ru.ru_nsignals, "signals received");
178 fprintf(out, "%10ld %s\n",
179 ru.ru_nvcsw, "voluntary context switches");
180 fprintf(out, "%10ld %s\n",
181 ru.ru_nivcsw, "involuntary context switches");
184 * If the child has exited on a signal, exit on the same
185 * signal, too, in order to reproduce the child's exit status.
186 * However, avoid actually dumping core from the current process.
189 if (signal(exitonsig, SIG_DFL) == SIG_ERR)
192 rl.rlim_max = rl.rlim_cur = 0;
193 if (setrlimit(RLIMIT_CORE, &rl) == -1)
195 kill(getpid(), exitonsig);
198 exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
205 "usage: time [-al] [-h | -p] [-o file] utility [argument ...]\n");
210 * Return the frequency of the kernel's statistics clock.
217 struct clockinfo clockrate;
220 mib[1] = KERN_CLOCKRATE;
221 size = sizeof clockrate;
222 if (sysctl(mib, 2, &clockrate, &size, NULL, 0) == -1)
223 err(1, "sysctl kern.clockrate");
224 return clockrate.stathz;
228 humantime(FILE *out, long sec, long usec)
230 long days, hrs, mins;
232 days = sec / (60L * 60 * 24);
233 sec %= (60L * 60 * 24);
234 hrs = sec / (60L * 60);
241 fprintf(out, "%ldd", days);
243 fprintf(out, "%ldh", hrs);
245 fprintf(out, "%ldm", mins);
246 fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);
250 showtime(FILE *out, struct timeval *before, struct timeval *after,
254 after->tv_sec -= before->tv_sec;
255 after->tv_usec -= before->tv_usec;
256 if (after->tv_usec < 0)
257 after->tv_sec--, after->tv_usec += 1000000;
260 /* POSIX wants output that must look like
261 "real %f\nuser %f\nsys %f\n" and requires
262 at least two digits after the radix. */
263 fprintf(out, "real %jd%c%02ld\n",
264 (intmax_t)after->tv_sec, decimal_point,
265 after->tv_usec/10000);
266 fprintf(out, "user %jd%c%02ld\n",
267 (intmax_t)ru->ru_utime.tv_sec, decimal_point,
268 ru->ru_utime.tv_usec/10000);
269 fprintf(out, "sys %jd%c%02ld\n",
270 (intmax_t)ru->ru_stime.tv_sec, decimal_point,
271 ru->ru_stime.tv_usec/10000);
273 humantime(out, after->tv_sec, after->tv_usec/10000);
274 fprintf(out, " real\t");
275 humantime(out, ru->ru_utime.tv_sec, ru->ru_utime.tv_usec/10000);
276 fprintf(out, " user\t");
277 humantime(out, ru->ru_stime.tv_sec, ru->ru_stime.tv_usec/10000);
278 fprintf(out, " sys\n");
280 fprintf(out, "%9jd%c%02ld real ",
281 (intmax_t)after->tv_sec, decimal_point,
282 after->tv_usec/10000);
283 fprintf(out, "%9jd%c%02ld user ",
284 (intmax_t)ru->ru_utime.tv_sec, decimal_point,
285 ru->ru_utime.tv_usec/10000);
286 fprintf(out, "%9jd%c%02ld sys\n",
287 (intmax_t)ru->ru_stime.tv_sec, decimal_point,
288 ru->ru_stime.tv_usec/10000);
293 siginfo(int sig __unused)
295 struct timeval after;
298 (void)gettimeofday(&after, NULL);
299 getrusage(RUSAGE_CHILDREN, &ru);
300 showtime(stdout, &before_tv, &after, &ru);