atkbdc - Do not attach PS2 controller via legacy ISA bus, if FADT says so.
[dragonfly.git] / usr.bin / time / time.c
blob8958d395d8e28b4a5a6bc139b60a87dffc317bbd
1 /*
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
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. 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
27 * SUCH DAMAGE.
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 $
35 #include <sys/user.h>
36 #include <sys/time.h>
37 #include <sys/resource.h>
38 #include <sys/signal.h>
39 #include <sys/sysctl.h>
40 #include <sys/wait.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <kinfo.h>
45 #include <locale.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <stdint.h>
50 #include <string.h>
51 #include <unistd.h>
53 static void humantime(FILE *, long, long);
54 static void showtime(FILE *, struct timespec *, struct timespec *,
55 struct rusage *);
56 static void siginfo(int);
58 static void usage(void);
60 static sig_atomic_t siginfo_recvd;
61 static char decimal_point;
62 struct timespec before_ts;
63 static int hflag, pflag;
65 int
66 main(int argc, char **argv)
68 pid_t pid;
69 int aflag, ch, lflag, status;
70 int exit_on_sig;
71 struct timespec after;
72 struct rusage ru;
73 FILE *out = stderr;
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)
81 switch (ch) {
82 case 'a':
83 aflag = 1;
84 break;
85 case 'h':
86 hflag = 1;
87 break;
88 case 'l':
89 lflag = 1;
90 break;
91 case 'o':
92 ofn = optarg;
93 break;
94 case 'p':
95 pflag = 1;
96 break;
97 default:
98 usage();
101 if (!(argc -= optind))
102 exit(0);
103 argv += optind;
105 if (ofn) {
106 if ((out = fopen(ofn, aflag ? "ae" : "we")) == NULL)
107 err(1, "%s", ofn);
108 setvbuf(out, NULL, _IONBF, (size_t)0);
111 if (clock_gettime(CLOCK_MONOTONIC, &before_ts) == -1)
112 err(1, "clock_gettime failed");
113 switch (pid = fork()) {
114 case -1: /* error */
115 err(1, "could not fork");
116 /* NOTREACHED */
117 case 0: /* child */
118 execvp(*argv, argv);
119 err(errno == ENOENT ? 127 : 126, "%s", *argv);
120 /* NOTREACHED */
122 /* parent */
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");
127 siginfo_recvd = 0;
128 if (signal(SIGINFO, siginfo) == SIG_ERR)
129 err(1, "signal failed");
130 siginterrupt(SIGINFO,1 );
131 while (wait4(pid, &status, 0, &ru) != pid){
132 if (siginfo_recvd) {
133 siginfo_recvd = 0;
134 if (clock_gettime(CLOCK_MONOTONIC, &after) == -1)
135 err(1, "clock_gettime failed");
136 getrusage(RUSAGE_CHILDREN, &ru);
137 showtime(stdout, &before_ts, &after, &ru);
140 if (clock_gettime(CLOCK_MONOTONIC, &after) == -1)
141 err(1, "clock_gettime failed");
142 if (!WIFEXITED(status))
143 warnx("command terminated abnormally");
144 exit_on_sig = WIFSIGNALED(status) ? WTERMSIG(status) : 0;
145 showtime(out, &before_ts, &after, &ru);
146 if (lflag) {
147 int hz;
148 u_long ticks;
150 if (kinfo_get_sched_stathz(&hz))
151 err(1, "kinfo_get_sched_stathz");
152 ticks = hz * (ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
153 hz * (ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000;
156 * If our round-off on the tick calculation still puts us at 0,
157 * then always assume at least one tick.
159 if (ticks == 0)
160 ticks = 1;
162 fprintf(out, "%10ld %s\n",
163 ru.ru_maxrss, "maximum resident set size");
164 fprintf(out, "%10ld %s\n",
165 ru.ru_ixrss / ticks, "average shared memory size");
166 fprintf(out, "%10ld %s\n",
167 ru.ru_idrss / ticks, "average unshared data size");
168 fprintf(out, "%10ld %s\n",
169 ru.ru_isrss / ticks, "average unshared stack size");
170 fprintf(out, "%10ld %s\n",
171 ru.ru_minflt, "page reclaims");
172 fprintf(out, "%10ld %s\n",
173 ru.ru_majflt, "page faults");
174 fprintf(out, "%10ld %s\n",
175 ru.ru_nswap, "swaps");
176 fprintf(out, "%10ld %s\n",
177 ru.ru_inblock, "block input operations");
178 fprintf(out, "%10ld %s\n",
179 ru.ru_oublock, "block output operations");
180 fprintf(out, "%10ld %s\n",
181 ru.ru_msgsnd, "messages sent");
182 fprintf(out, "%10ld %s\n",
183 ru.ru_msgrcv, "messages received");
184 fprintf(out, "%10ld %s\n",
185 ru.ru_nsignals, "signals received");
186 fprintf(out, "%10ld %s\n",
187 ru.ru_nvcsw, "voluntary context switches");
188 fprintf(out, "%10ld %s\n",
189 ru.ru_nivcsw, "involuntary context switches");
192 * If the child has exited on a signal, exit on the same
193 * signal, too, in order to reproduce the child's exit
194 * status. However, avoid actually dumping core from
195 * current process.
197 if (exit_on_sig) {
198 if (signal(exit_on_sig, SIG_DFL) == SIG_ERR)
199 warn("signal failed");
200 else
201 kill(getpid(), exit_on_sig);
203 exit(WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
206 static void
207 usage(void)
209 fprintf(stderr,
210 "usage: time [-al] [-h|-p] [-o file] utility [argument ...]\n");
211 exit(1);
214 static void
215 humantime(FILE *out, long sec, long usec)
217 long days, hrs, mins;
219 days = sec / (60L * 60 * 24);
220 sec %= (60L * 60 * 24);
221 hrs = sec / (60L * 60);
222 sec %= (60L * 60);
223 mins = sec / 60;
224 sec %= 60;
226 fprintf(out, "\t");
227 if (days)
228 fprintf(out, "%ldd", days);
229 if (hrs)
230 fprintf(out, "%ldh", hrs);
231 if (mins)
232 fprintf(out, "%ldm", mins);
233 fprintf(out, "%ld%c%02lds", sec, decimal_point, usec);
236 static void
237 showtime(FILE *out, struct timespec *before, struct timespec *after,
238 struct rusage *ru)
241 after->tv_sec -= before->tv_sec;
242 after->tv_nsec -= before->tv_nsec;
243 if (after->tv_nsec < 0)
244 after->tv_sec--, after->tv_nsec += 1000000000L;
246 if (pflag) {
247 /* POSIX wants output that must look like
248 "real %f\nuser %f\nsys %f\n" and requires
249 at least two digits after the radix. */
250 fprintf(out, "real %jd%c%02ld\n",
251 (intmax_t)after->tv_sec, decimal_point,
252 after->tv_nsec/10000000L);
253 fprintf(out, "user %jd%c%02ld\n",
254 (intmax_t)ru->ru_utime.tv_sec, decimal_point,
255 ru->ru_utime.tv_usec/10000);
256 fprintf(out, "sys %jd%c%02ld\n",
257 (intmax_t)ru->ru_stime.tv_sec, decimal_point,
258 ru->ru_stime.tv_usec/10000);
259 } else if (hflag) {
260 humantime(out, after->tv_sec, after->tv_nsec/10000000);
261 fprintf(out, " real\t");
262 humantime(out, ru->ru_utime.tv_sec, ru->ru_utime.tv_usec/10000);
263 fprintf(out, " user\t");
264 humantime(out, ru->ru_stime.tv_sec, ru->ru_stime.tv_usec/10000);
265 fprintf(out, " sys\n");
266 } else {
267 fprintf(out, "%9jd%c%02ld real ",
268 (intmax_t)after->tv_sec, decimal_point,
269 after->tv_nsec/10000000);
270 fprintf(out, "%9jd%c%02ld user ",
271 (intmax_t)ru->ru_utime.tv_sec, decimal_point,
272 ru->ru_utime.tv_usec/10000);
273 fprintf(out, "%9jd%c%02ld sys\n",
274 (intmax_t)ru->ru_stime.tv_sec, decimal_point,
275 ru->ru_stime.tv_usec/10000);
279 static void
280 siginfo(int sig __unused)
282 siginfo_recvd = 1;