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. 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
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 * $DragonFly: src/usr.bin/time/time.c,v 1.11 2005/03/04 16:54:37 liamfoy Exp $
37 #include <sys/resource.h>
38 #include <sys/signal.h>
39 #include <sys/sysctl.h>
52 static void humantime(FILE *, long, long);
53 static void usage(void);
55 static char decimal_point;
58 main(int argc, char **argv)
61 int aflag, ch, hflag, lflag, status, pflag;
63 struct timeval before, after;
66 const char *ofn = NULL;
68 setlocale(LC_NUMERIC, "");
69 decimal_point = localeconv()->decimal_point[0];
71 aflag = hflag = lflag = pflag = 0;
72 while ((ch = getopt(argc, argv, "ahlo:p")) != -1)
93 if (!(argc -= optind))
98 if ((out = fopen(ofn, aflag ? "a" : "w")) == NULL)
100 setvbuf(out, NULL, _IONBF, (size_t)0);
103 if (gettimeofday(&before, NULL) == -1)
104 err(1, "gettimeofday failed");
105 switch (pid = fork()) {
107 err(1, "could not fork");
111 err(errno == ENOENT ? 127 : 126, "%s", *argv);
115 if (signal(SIGINT, SIG_IGN) == SIG_ERR)
116 err(1, "signal failed");
117 if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
118 err(1, "signal failed");
119 while (wait4(pid, &status, 0, &ru) != pid) /* XXX use waitpid */
121 if (gettimeofday(&after, NULL) == -1)
122 err(1, "gettimeofday failed");
123 if (!WIFEXITED(status))
124 warnx("command terminated abnormally");
125 exit_on_sig = WIFSIGNALED(status) ? WTERMSIG(status) : 0;
126 after.tv_sec -= before.tv_sec;
127 after.tv_usec -= before.tv_usec;
128 if (after.tv_usec < 0)
129 after.tv_sec--, after.tv_usec += 1000000;
131 /* POSIX wants output that must look like
132 "real %f\nuser %f\nsys %f\n" and requires
133 at least two digits after the radix. */
134 fprintf(out, "real %ld%c%02ld\n",
135 after.tv_sec, decimal_point,
136 after.tv_usec / 10000);
137 fprintf(out, "user %ld%c%02ld\n",
138 ru.ru_utime.tv_sec, decimal_point,
139 ru.ru_utime.tv_usec / 10000);
140 fprintf(out, "sys %ld%c%02ld\n",
141 ru.ru_stime.tv_sec, decimal_point,
142 ru.ru_stime.tv_usec / 10000);
144 humantime(out, after.tv_sec, after.tv_usec / 10000);
145 fprintf(out, " real\t");
146 humantime(out, ru.ru_utime.tv_sec, ru.ru_utime.tv_usec / 10000);
147 fprintf(out, " user\t");
148 humantime(out, ru.ru_stime.tv_sec, ru.ru_stime.tv_usec / 10000);
149 fprintf(out, " sys\n");
151 fprintf(out, "%9ld%c%02ld real ",
152 after.tv_sec, decimal_point,
153 after.tv_usec / 10000);
154 fprintf(out, "%9ld%c%02ld user ",
155 ru.ru_utime.tv_sec, decimal_point,
156 ru.ru_utime.tv_usec / 10000);
157 fprintf(out, "%9ld%c%02ld sys\n",
158 ru.ru_stime.tv_sec, decimal_point,
159 ru.ru_stime.tv_usec / 10000);
165 if (kinfo_get_sched_stathz(&hz))
166 err(1, "kinfo_get_sched_stathz");
167 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
168 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
171 * If our round-off on the tick calculation still puts us at 0,
172 * then always assume at least one tick.
177 fprintf(out, "%10ld %s\n",
178 ru.ru_maxrss, "maximum resident set size");
179 fprintf(out, "%10ld %s\n",
180 ru.ru_ixrss / ticks, "average shared memory size");
181 fprintf(out, "%10ld %s\n",
182 ru.ru_idrss / ticks, "average unshared data size");
183 fprintf(out, "%10ld %s\n",
184 ru.ru_isrss / ticks, "average unshared stack size");
185 fprintf(out, "%10ld %s\n",
186 ru.ru_minflt, "page reclaims");
187 fprintf(out, "%10ld %s\n",
188 ru.ru_majflt, "page faults");
189 fprintf(out, "%10ld %s\n",
190 ru.ru_nswap, "swaps");
191 fprintf(out, "%10ld %s\n",
192 ru.ru_inblock, "block input operations");
193 fprintf(out, "%10ld %s\n",
194 ru.ru_oublock, "block output operations");
195 fprintf(out, "%10ld %s\n",
196 ru.ru_msgsnd, "messages sent");
197 fprintf(out, "%10ld %s\n",
198 ru.ru_msgrcv, "messages received");
199 fprintf(out, "%10ld %s\n",
200 ru.ru_nsignals, "signals received");
201 fprintf(out, "%10ld %s\n",
202 ru.ru_nvcsw, "voluntary context switches");
203 fprintf(out, "%10ld %s\n",
204 ru.ru_nivcsw, "involuntary context switches");
207 * If the child has exited on a signal, exit on the same
208 * signal, too, in order to reproduce the child's exit
209 * status. However, avoid actually dumping core from
213 if (signal(exit_on_sig, SIG_DFL) == SIG_ERR)
214 warn("signal failed");
216 kill(getpid(), exit_on_sig);
218 exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
225 "usage: time [-al] [-h|-p] [-o file] utility [argument ...]\n");
230 humantime(FILE *out, long sec, long usec)
232 long days, hrs, mins;
234 days = sec / (60L * 60 * 24);
235 sec %= (60L * 60 * 24);
236 hrs = sec / (60L * 60);
243 fprintf(out, "%ldd", days);
245 fprintf(out, "%ldh", hrs);
247 fprintf(out, "%ldm", mins);
248 fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);