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>
53 static void humantime(FILE *, long, long);
54 static void showtime(FILE *, struct timeval
*, struct timeval
*,
56 static void siginfo(int);
58 static void usage(void);
60 static sig_atomic_t siginfo_recvd
;
61 static char decimal_point
;
62 struct timeval before_tv
;
63 static int hflag
, pflag
;
66 main(int argc
, char **argv
)
69 int aflag
, ch
, lflag
, status
;
74 const char *ofn
= NULL
;
76 setlocale(LC_NUMERIC
, "");
77 decimal_point
= localeconv()->decimal_point
[0];
79 aflag
= hflag
= lflag
= pflag
= 0;
80 while ((ch
= getopt(argc
, argv
, "ahlo:p")) != -1)
101 if (!(argc
-= optind
))
106 if ((out
= fopen(ofn
, aflag
? "ae" : "we")) == NULL
)
108 setvbuf(out
, NULL
, _IONBF
, (size_t)0);
111 if (gettimeofday(&before_tv
, NULL
) == -1)
112 err(1, "gettimeofday failed");
113 switch (pid
= fork()) {
115 err(1, "could not fork");
119 err(errno
== ENOENT
? 127 : 126, "%s", *argv
);
123 if (signal(SIGINT
, SIG_IGN
) == SIG_ERR
)
124 err(1, "signal failed");
125 if (signal(SIGQUIT
, SIG_IGN
) == SIG_ERR
)
126 err(1, "signal failed");
128 if (signal(SIGINFO
, siginfo
) == SIG_ERR
)
129 err(1, "signal failed");
130 siginterrupt(SIGINFO
,1 );
131 while (wait4(pid
, &status
, 0, &ru
) != pid
){
134 gettimeofday(&after
, NULL
);
135 getrusage(RUSAGE_CHILDREN
, &ru
);
136 showtime(stdout
, &before_tv
, &after
, &ru
);
139 if (gettimeofday(&after
, NULL
) == -1)
140 err(1, "gettimeofday failed");
141 if (!WIFEXITED(status
))
142 warnx("command terminated abnormally");
143 exit_on_sig
= WIFSIGNALED(status
) ? WTERMSIG(status
) : 0;
144 showtime(out
, &before_tv
, &after
, &ru
);
149 if (kinfo_get_sched_stathz(&hz
))
150 err(1, "kinfo_get_sched_stathz");
151 ticks
= hz
* (ru
.ru_utime
.tv_sec
+ ru
.ru_stime
.tv_sec
) +
152 hz
* (ru
.ru_utime
.tv_usec
+ ru
.ru_stime
.tv_usec
) / 1000000;
155 * If our round-off on the tick calculation still puts us at 0,
156 * then always assume at least one tick.
161 fprintf(out
, "%10ld %s\n",
162 ru
.ru_maxrss
, "maximum resident set size");
163 fprintf(out
, "%10ld %s\n",
164 ru
.ru_ixrss
/ ticks
, "average shared memory size");
165 fprintf(out
, "%10ld %s\n",
166 ru
.ru_idrss
/ ticks
, "average unshared data size");
167 fprintf(out
, "%10ld %s\n",
168 ru
.ru_isrss
/ ticks
, "average unshared stack size");
169 fprintf(out
, "%10ld %s\n",
170 ru
.ru_minflt
, "page reclaims");
171 fprintf(out
, "%10ld %s\n",
172 ru
.ru_majflt
, "page faults");
173 fprintf(out
, "%10ld %s\n",
174 ru
.ru_nswap
, "swaps");
175 fprintf(out
, "%10ld %s\n",
176 ru
.ru_inblock
, "block input operations");
177 fprintf(out
, "%10ld %s\n",
178 ru
.ru_oublock
, "block output operations");
179 fprintf(out
, "%10ld %s\n",
180 ru
.ru_msgsnd
, "messages sent");
181 fprintf(out
, "%10ld %s\n",
182 ru
.ru_msgrcv
, "messages received");
183 fprintf(out
, "%10ld %s\n",
184 ru
.ru_nsignals
, "signals received");
185 fprintf(out
, "%10ld %s\n",
186 ru
.ru_nvcsw
, "voluntary context switches");
187 fprintf(out
, "%10ld %s\n",
188 ru
.ru_nivcsw
, "involuntary context switches");
191 * If the child has exited on a signal, exit on the same
192 * signal, too, in order to reproduce the child's exit
193 * status. However, avoid actually dumping core from
197 if (signal(exit_on_sig
, SIG_DFL
) == SIG_ERR
)
198 warn("signal failed");
200 kill(getpid(), exit_on_sig
);
202 exit(WIFEXITED(status
) ? WEXITSTATUS(status
) : EXIT_FAILURE
);
209 "usage: time [-al] [-h|-p] [-o file] utility [argument ...]\n");
214 humantime(FILE *out
, long sec
, long usec
)
216 long days
, hrs
, mins
;
218 days
= sec
/ (60L * 60 * 24);
219 sec
%= (60L * 60 * 24);
220 hrs
= sec
/ (60L * 60);
227 fprintf(out
, "%ldd", days
);
229 fprintf(out
, "%ldh", hrs
);
231 fprintf(out
, "%ldm", mins
);
232 fprintf(out
, "%ld%c%02lds", sec
, decimal_point
, usec
);
236 showtime(FILE *out
, struct timeval
*before
, struct timeval
*after
,
240 after
->tv_sec
-= before
->tv_sec
;
241 after
->tv_usec
-= before
->tv_usec
;
242 if (after
->tv_usec
< 0)
243 after
->tv_sec
--, after
->tv_usec
+= 1000000;
246 /* POSIX wants output that must look like
247 "real %f\nuser %f\nsys %f\n" and requires
248 at least two digits after the radix. */
249 fprintf(out
, "real %jd%c%02ld\n",
250 (intmax_t)after
->tv_sec
, decimal_point
,
251 after
->tv_usec
/10000);
252 fprintf(out
, "user %jd%c%02ld\n",
253 (intmax_t)ru
->ru_utime
.tv_sec
, decimal_point
,
254 ru
->ru_utime
.tv_usec
/10000);
255 fprintf(out
, "sys %jd%c%02ld\n",
256 (intmax_t)ru
->ru_stime
.tv_sec
, decimal_point
,
257 ru
->ru_stime
.tv_usec
/10000);
259 humantime(out
, after
->tv_sec
, after
->tv_usec
/10000);
260 fprintf(out
, " real\t");
261 humantime(out
, ru
->ru_utime
.tv_sec
, ru
->ru_utime
.tv_usec
/10000);
262 fprintf(out
, " user\t");
263 humantime(out
, ru
->ru_stime
.tv_sec
, ru
->ru_stime
.tv_usec
/10000);
264 fprintf(out
, " sys\n");
266 fprintf(out
, "%9jd%c%02ld real ",
267 (intmax_t)after
->tv_sec
, decimal_point
,
268 after
->tv_usec
/10000);
269 fprintf(out
, "%9jd%c%02ld user ",
270 (intmax_t)ru
->ru_utime
.tv_sec
, decimal_point
,
271 ru
->ru_utime
.tv_usec
/10000);
272 fprintf(out
, "%9jd%c%02ld sys\n",
273 (intmax_t)ru
->ru_stime
.tv_sec
, decimal_point
,
274 ru
->ru_stime
.tv_usec
/10000);
279 siginfo(int sig __unused
)