blob: 61f0787551b2a1eb45d14d9286db6b01a8c3ab47 [file] [log] [blame]
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02002/*
3 * 'time' utility to display resource usage of processes.
4 * Copyright (C) 1990, 91, 92, 93, 96 Free Software Foundation, Inc.
5 *
6 * Licensed under GPLv2, see file LICENSE in this source tree.
7 */
Eric Andersenc3657422001-11-30 07:54:32 +00008/* Originally written by David Keppel <pardo@cs.washington.edu>.
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02009 * Heavily modified by David MacKenzie <djm@gnu.ai.mit.edu>.
10 * Heavily modified for busybox by Erik Andersen <andersen@codepoet.org>
11 */
Denys Vlasenkofb4da162016-11-22 23:14:24 +010012//config:config TIME
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020013//config: bool "time (7 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010014//config: default y
15//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: The time command runs the specified program with the given arguments.
17//config: When the command finishes, time writes a message to standard output
18//config: giving timing statistics about this program run.
Eric Andersenc3657422001-11-30 07:54:32 +000019
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010020//applet:IF_TIME(APPLET(time, BB_DIR_USR_BIN, BB_SUID_DROP))
21
22//kbuild:lib-$(CONFIG_TIME) += time.o
23
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage:#define time_trivial_usage
Tommi Rantala5fe5be22017-04-28 17:54:14 +020025//usage: "[-vpa] [-o FILE] PROG ARGS"
Pere Orga5bc8c002011-04-11 03:29:49 +020026//usage:#define time_full_usage "\n\n"
27//usage: "Run PROG, display resource usage when it exits\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020028//usage: "\n -v Verbose"
Tommi Rantala854174f2017-04-24 19:08:53 +030029//usage: "\n -p POSIX output format"
Denys Vlasenkoa1a3b592017-04-28 18:01:18 +020030//usage: "\n -f FMT Custom format"
Tommi Rantala5fe5be22017-04-28 17:54:14 +020031//usage: "\n -o FILE Write result to FILE"
32//usage: "\n -a Append (else overwrite)"
Pere Orga5bc8c002011-04-11 03:29:49 +020033
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000034#include "libbb.h"
Mike Frysingerc5fe9f72012-07-05 23:19:09 -040035#include <sys/resource.h> /* getrusage */
Eric Andersenc3657422001-11-30 07:54:32 +000036
Eric Andersenc3657422001-11-30 07:54:32 +000037/* Information on the resources used by a child process. */
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000038typedef struct {
39 int waitstatus;
40 struct rusage ru;
Denis Vlasenko459be352007-06-17 19:09:05 +000041 unsigned elapsed_ms; /* Wallclock time of process. */
Eric Andersenc3657422001-11-30 07:54:32 +000042} resource_t;
43
44/* msec = milliseconds = 1/1,000 (1*10e-3) second.
45 usec = microseconds = 1/1,000,000 (1*10e-6) second. */
46
Eric Andersenc3657422001-11-30 07:54:32 +000047#define UL unsigned long
48
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000049static const char default_format[] ALIGN1 = "real\t%E\nuser\t%u\nsys\t%T";
Eric Andersenc3657422001-11-30 07:54:32 +000050
51/* The output format for the -p option .*/
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000052static const char posix_format[] ALIGN1 = "real %e\nuser %U\nsys %S";
Eric Andersenc3657422001-11-30 07:54:32 +000053
Eric Andersenc3657422001-11-30 07:54:32 +000054/* Format string for printing all statistics verbosely.
55 Keep this output to 24 lines so users on terminals can see it all.*/
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000056static const char long_format[] ALIGN1 =
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000057 "\tCommand being timed: \"%C\"\n"
58 "\tUser time (seconds): %U\n"
59 "\tSystem time (seconds): %S\n"
60 "\tPercent of CPU this job got: %P\n"
61 "\tElapsed (wall clock) time (h:mm:ss or m:ss): %E\n"
62 "\tAverage shared text size (kbytes): %X\n"
63 "\tAverage unshared data size (kbytes): %D\n"
64 "\tAverage stack size (kbytes): %p\n"
65 "\tAverage total size (kbytes): %K\n"
66 "\tMaximum resident set size (kbytes): %M\n"
67 "\tAverage resident set size (kbytes): %t\n"
68 "\tMajor (requiring I/O) page faults: %F\n"
69 "\tMinor (reclaiming a frame) page faults: %R\n"
70 "\tVoluntary context switches: %w\n"
71 "\tInvoluntary context switches: %c\n"
72 "\tSwaps: %W\n"
73 "\tFile system inputs: %I\n"
74 "\tFile system outputs: %O\n"
75 "\tSocket messages sent: %s\n"
76 "\tSocket messages received: %r\n"
77 "\tSignals delivered: %k\n"
Denis Vlasenkof93ab472006-12-22 12:36:13 +000078 "\tPage size (bytes): %Z\n"
79 "\tExit status: %x";
Eric Andersenc3657422001-11-30 07:54:32 +000080
Denis Vlasenkof93ab472006-12-22 12:36:13 +000081/* Wait for and fill in data on child process PID.
82 Return 0 on error, 1 if ok. */
Eric Andersenc3657422001-11-30 07:54:32 +000083/* pid_t is short on BSDI, so don't try to promote it. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +000084static void resuse_end(pid_t pid, resource_t *resp)
Eric Andersenc3657422001-11-30 07:54:32 +000085{
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000086 pid_t caught;
Eric Andersenc3657422001-11-30 07:54:32 +000087
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000088 /* Ignore signals, but don't ignore the children. When wait3
Denys Vlasenko6830ade2013-01-15 13:58:01 +010089 * returns the child process, set the time the command finished. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +000090 while ((caught = wait3(&resp->waitstatus, 0, &resp->ru)) != pid) {
91 if (caught == -1 && errno != EINTR) {
92 bb_perror_msg("wait");
93 return;
94 }
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000095 }
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +010096 resp->elapsed_ms = monotonic_ms() - resp->elapsed_ms;
Eric Andersenc3657422001-11-30 07:54:32 +000097}
98
Denis Vlasenkob44c7902008-03-17 09:29:43 +000099static void printargv(char *const *argv)
Eric Andersenc3657422001-11-30 07:54:32 +0000100{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000101 const char *fmt = " %s" + 1;
102 do {
103 printf(fmt, *argv);
104 fmt = " %s";
105 } while (*++argv);
Eric Andersenc3657422001-11-30 07:54:32 +0000106}
107
108/* Return the number of kilobytes corresponding to a number of pages PAGES.
109 (Actually, we use it to convert pages*ticks into kilobytes*ticks.)
110
111 Try to do arithmetic so that the risk of overflow errors is minimized.
112 This is funky since the pagesize could be less than 1K.
113 Note: Some machines express getrusage statistics in terms of K,
114 others in terms of pages. */
Bernhard Reutner-Fischer0adf7f22009-02-23 16:51:25 +0000115static unsigned long ptok(const unsigned pagesize, const unsigned long pages)
Eric Andersenc3657422001-11-30 07:54:32 +0000116{
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000117 unsigned long tmp;
Eric Andersenc3657422001-11-30 07:54:32 +0000118
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000119 /* Conversion. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000120 if (pages > (LONG_MAX / pagesize)) { /* Could overflow. */
121 tmp = pages / 1024; /* Smaller first, */
122 return tmp * pagesize; /* then larger. */
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000123 }
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000124 /* Could underflow. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000125 tmp = pages * pagesize; /* Larger first, */
126 return tmp / 1024; /* then smaller. */
Eric Andersenc3657422001-11-30 07:54:32 +0000127}
128
129/* summarize: Report on the system use of a command.
130
Denys Vlasenko95f79532017-08-02 14:26:33 +0200131 Print the FMT argument except that '%' sequences
132 have special meaning, and '\n' and '\t' are translated into
133 newline and tab, respectively, and '\\' is translated into '\'.
Eric Andersenc3657422001-11-30 07:54:32 +0000134
Denys Vlasenko95f79532017-08-02 14:26:33 +0200135 The character following a '%' can be:
Eric Andersenc3657422001-11-30 07:54:32 +0000136 (* means the tcsh time builtin also recognizes it)
Denys Vlasenko95f79532017-08-02 14:26:33 +0200137 % == a literal '%'
Eric Andersenc3657422001-11-30 07:54:32 +0000138 C == command name and arguments
139* D == average unshared data size in K (ru_idrss+ru_isrss)
140* E == elapsed real (wall clock) time in [hour:]min:sec
141* F == major page faults (required physical I/O) (ru_majflt)
142* I == file system inputs (ru_inblock)
143* K == average total mem usage (ru_idrss+ru_isrss+ru_ixrss)
144* M == maximum resident set size in K (ru_maxrss)
145* O == file system outputs (ru_oublock)
146* P == percent of CPU this job got (total cpu time / elapsed time)
147* R == minor page faults (reclaims; no physical I/O involved) (ru_minflt)
148* S == system (kernel) time (seconds) (ru_stime)
149* T == system time in [hour:]min:sec
150* U == user time (seconds) (ru_utime)
151* u == user time in [hour:]min:sec
152* W == times swapped out (ru_nswap)
153* X == average amount of shared text in K (ru_ixrss)
154 Z == page size
155* c == involuntary context switches (ru_nivcsw)
156 e == elapsed real time in seconds
157* k == signals delivered (ru_nsignals)
158 p == average unshared stack size in K (ru_isrss)
159* r == socket messages received (ru_msgrcv)
160* s == socket messages sent (ru_msgsnd)
161 t == average resident set size in K (ru_idrss)
162* w == voluntary context switches (ru_nvcsw)
163 x == exit status of command
164
165 Various memory usages are found by converting from page-seconds
166 to kbytes by multiplying by the page size, dividing by 1024,
167 and dividing by elapsed real time.
168
Eric Andersenc3657422001-11-30 07:54:32 +0000169 FMT is the format string, interpreted as described above.
170 COMMAND is the command and args that are being summarized.
171 RESP is resource information on the command. */
172
Denis Vlasenko459be352007-06-17 19:09:05 +0000173#ifndef TICKS_PER_SEC
174#define TICKS_PER_SEC 100
175#endif
176
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000177static void summarize(const char *fmt, char **command, resource_t *resp)
Eric Andersenc3657422001-11-30 07:54:32 +0000178{
Denis Vlasenko459be352007-06-17 19:09:05 +0000179 unsigned vv_ms; /* Elapsed virtual (CPU) milliseconds */
180 unsigned cpu_ticks; /* Same, in "CPU ticks" */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000181 unsigned pagesize = getpagesize();
Eric Andersenc3657422001-11-30 07:54:32 +0000182
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000183 /* Impossible: we do not use WUNTRACED flag in wait()...
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000184 if (WIFSTOPPED(resp->waitstatus))
Denis Vlasenko459be352007-06-17 19:09:05 +0000185 printf("Command stopped by signal %u\n",
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000186 WSTOPSIG(resp->waitstatus));
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000187 else */
188 if (WIFSIGNALED(resp->waitstatus))
Denis Vlasenko459be352007-06-17 19:09:05 +0000189 printf("Command terminated by signal %u\n",
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000190 WTERMSIG(resp->waitstatus));
191 else if (WIFEXITED(resp->waitstatus) && WEXITSTATUS(resp->waitstatus))
Denis Vlasenko459be352007-06-17 19:09:05 +0000192 printf("Command exited with non-zero status %u\n",
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000193 WEXITSTATUS(resp->waitstatus));
Eric Andersenc3657422001-11-30 07:54:32 +0000194
Denis Vlasenko459be352007-06-17 19:09:05 +0000195 vv_ms = (resp->ru.ru_utime.tv_sec + resp->ru.ru_stime.tv_sec) * 1000
196 + (resp->ru.ru_utime.tv_usec + resp->ru.ru_stime.tv_usec) / 1000;
Eric Andersenc3657422001-11-30 07:54:32 +0000197
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000198#if (1000 / TICKS_PER_SEC) * TICKS_PER_SEC == 1000
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000199 /* 1000 is exactly divisible by TICKS_PER_SEC (typical) */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000200 cpu_ticks = vv_ms / (1000 / TICKS_PER_SEC);
201#else
202 cpu_ticks = vv_ms * (unsigned long long)TICKS_PER_SEC / 1000;
203#endif
Denis Vlasenko459be352007-06-17 19:09:05 +0000204 if (!cpu_ticks) cpu_ticks = 1; /* we divide by it, must be nonzero */
Eric Andersenc3657422001-11-30 07:54:32 +0000205
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000206 while (*fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000207 /* Handle leading literal part */
208 int n = strcspn(fmt, "%\\");
209 if (n) {
210 printf("%.*s", n, fmt);
211 fmt += n;
212 continue;
213 }
214
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000215 switch (*fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000216#ifdef NOT_NEEDED
217 /* Handle literal char */
218 /* Usually we optimize for size, but there is a limit
219 * for everything. With this we do a lot of 1-byte writes */
220 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000221 bb_putchar(*fmt);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000222 break;
223#endif
224
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000225 case '%':
226 switch (*++fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000227#ifdef NOT_NEEDED_YET
228 /* Our format strings do not have these */
229 /* and we do not take format str from user */
230 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000231 bb_putchar('%');
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000232 /*FALLTHROUGH*/
233 case '%':
234 if (!*fmt) goto ret;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000235 bb_putchar(*fmt);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000236 break;
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000237#endif
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000238 case 'C': /* The command that got timed. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000239 printargv(command);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000240 break;
241 case 'D': /* Average unshared data size. */
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000242 printf("%lu",
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000243 (ptok(pagesize, (UL) resp->ru.ru_idrss) +
244 ptok(pagesize, (UL) resp->ru.ru_isrss)) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000245 break;
Denis Vlasenko459be352007-06-17 19:09:05 +0000246 case 'E': { /* Elapsed real (wall clock) time. */
247 unsigned seconds = resp->elapsed_ms / 1000;
248 if (seconds >= 3600) /* One hour -> h:m:s. */
249 printf("%uh %um %02us",
250 seconds / 3600,
251 (seconds % 3600) / 60,
252 seconds % 60);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000253 else
Denis Vlasenko459be352007-06-17 19:09:05 +0000254 printf("%um %u.%02us", /* -> m:s. */
255 seconds / 60,
256 seconds % 60,
257 (unsigned)(resp->elapsed_ms / 10) % 100);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000258 break;
Denis Vlasenko459be352007-06-17 19:09:05 +0000259 }
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000260 case 'F': /* Major page faults. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000261 printf("%lu", resp->ru.ru_majflt);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000262 break;
263 case 'I': /* Inputs. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000264 printf("%lu", resp->ru.ru_inblock);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000265 break;
266 case 'K': /* Average mem usage == data+stack+text. */
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000267 printf("%lu",
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000268 (ptok(pagesize, (UL) resp->ru.ru_idrss) +
269 ptok(pagesize, (UL) resp->ru.ru_isrss) +
270 ptok(pagesize, (UL) resp->ru.ru_ixrss)) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000271 break;
272 case 'M': /* Maximum resident set size. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000273 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_maxrss));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000274 break;
275 case 'O': /* Outputs. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000276 printf("%lu", resp->ru.ru_oublock);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000277 break;
278 case 'P': /* Percent of CPU this job got. */
279 /* % cpu is (total cpu time)/(elapsed time). */
Denis Vlasenko459be352007-06-17 19:09:05 +0000280 if (resp->elapsed_ms > 0)
281 printf("%u%%", (unsigned)(vv_ms * 100 / resp->elapsed_ms));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000282 else
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000283 printf("?%%");
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000284 break;
285 case 'R': /* Minor page faults (reclaims). */
Denis Vlasenko459be352007-06-17 19:09:05 +0000286 printf("%lu", resp->ru.ru_minflt);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000287 break;
288 case 'S': /* System time. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000289 printf("%u.%02u",
290 (unsigned)resp->ru.ru_stime.tv_sec,
291 (unsigned)(resp->ru.ru_stime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000292 break;
293 case 'T': /* System time. */
294 if (resp->ru.ru_stime.tv_sec >= 3600) /* One hour -> h:m:s. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000295 printf("%uh %um %02us",
296 (unsigned)(resp->ru.ru_stime.tv_sec / 3600),
297 (unsigned)(resp->ru.ru_stime.tv_sec % 3600) / 60,
298 (unsigned)(resp->ru.ru_stime.tv_sec % 60));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000299 else
Denis Vlasenko459be352007-06-17 19:09:05 +0000300 printf("%um %u.%02us", /* -> m:s. */
301 (unsigned)(resp->ru.ru_stime.tv_sec / 60),
302 (unsigned)(resp->ru.ru_stime.tv_sec % 60),
303 (unsigned)(resp->ru.ru_stime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000304 break;
305 case 'U': /* User time. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000306 printf("%u.%02u",
307 (unsigned)resp->ru.ru_utime.tv_sec,
308 (unsigned)(resp->ru.ru_utime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000309 break;
310 case 'u': /* User time. */
311 if (resp->ru.ru_utime.tv_sec >= 3600) /* One hour -> h:m:s. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000312 printf("%uh %um %02us",
313 (unsigned)(resp->ru.ru_utime.tv_sec / 3600),
314 (unsigned)(resp->ru.ru_utime.tv_sec % 3600) / 60,
315 (unsigned)(resp->ru.ru_utime.tv_sec % 60));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000316 else
Denis Vlasenko459be352007-06-17 19:09:05 +0000317 printf("%um %u.%02us", /* -> m:s. */
318 (unsigned)(resp->ru.ru_utime.tv_sec / 60),
319 (unsigned)(resp->ru.ru_utime.tv_sec % 60),
320 (unsigned)(resp->ru.ru_utime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000321 break;
322 case 'W': /* Times swapped out. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000323 printf("%lu", resp->ru.ru_nswap);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000324 break;
325 case 'X': /* Average shared text size. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000326 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_ixrss) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000327 break;
328 case 'Z': /* Page size. */
Bernhard Reutner-Fischer0adf7f22009-02-23 16:51:25 +0000329 printf("%u", pagesize);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000330 break;
331 case 'c': /* Involuntary context switches. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000332 printf("%lu", resp->ru.ru_nivcsw);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000333 break;
334 case 'e': /* Elapsed real time in seconds. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000335 printf("%u.%02u",
336 (unsigned)resp->elapsed_ms / 1000,
337 (unsigned)(resp->elapsed_ms / 10) % 100);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000338 break;
339 case 'k': /* Signals delivered. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000340 printf("%lu", resp->ru.ru_nsignals);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000341 break;
342 case 'p': /* Average stack segment. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000343 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_isrss) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000344 break;
345 case 'r': /* Incoming socket messages received. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000346 printf("%lu", resp->ru.ru_msgrcv);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000347 break;
348 case 's': /* Outgoing socket messages sent. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000349 printf("%lu", resp->ru.ru_msgsnd);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000350 break;
351 case 't': /* Average resident set size. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000352 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_idrss) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000353 break;
354 case 'w': /* Voluntary context switches. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000355 printf("%lu", resp->ru.ru_nvcsw);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000356 break;
357 case 'x': /* Exit status. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000358 printf("%u", WEXITSTATUS(resp->waitstatus));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000359 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000360 }
Eric Andersenc3657422001-11-30 07:54:32 +0000361 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000362
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000363#ifdef NOT_NEEDED_YET
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000364 case '\\': /* Format escape. */
365 switch (*++fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000366 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000367 bb_putchar('\\');
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000368 /*FALLTHROUGH*/
369 case '\\':
370 if (!*fmt) goto ret;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000371 bb_putchar(*fmt);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000372 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000373 case 't':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000374 bb_putchar('\t');
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000375 break;
376 case 'n':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000377 bb_putchar('\n');
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000378 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000379 }
Eric Andersenc3657422001-11-30 07:54:32 +0000380 break;
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000381#endif
Eric Andersenc3657422001-11-30 07:54:32 +0000382 }
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000383 ++fmt;
Eric Andersenc3657422001-11-30 07:54:32 +0000384 }
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000385 /* ret: */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000386 bb_putchar('\n');
Eric Andersenc3657422001-11-30 07:54:32 +0000387}
388
389/* Run command CMD and return statistics on it.
390 Put the statistics in *RESP. */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000391static void run_command(char *const *cmd, resource_t *resp)
Eric Andersenc3657422001-11-30 07:54:32 +0000392{
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200393 pid_t pid;
Denis Vlasenko25591c32008-02-16 22:58:56 +0000394 void (*interrupt_signal)(int);
395 void (*quit_signal)(int);
Eric Andersenc3657422001-11-30 07:54:32 +0000396
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100397 resp->elapsed_ms = monotonic_ms();
Pascal Bellard926031b2010-07-04 15:32:38 +0200398 pid = xvfork();
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200399 if (pid == 0) {
400 /* Child */
401 BB_EXECVP_or_die((char**)cmd);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000402 }
Eric Andersenc3657422001-11-30 07:54:32 +0000403
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000404 /* Have signals kill the child but not self (if possible). */
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200405//TODO: just block all sigs? and re-enable them in the very end in main?
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000406 interrupt_signal = signal(SIGINT, SIG_IGN);
407 quit_signal = signal(SIGQUIT, SIG_IGN);
Eric Andersenc3657422001-11-30 07:54:32 +0000408
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000409 resuse_end(pid, resp);
Eric Andersenc3657422001-11-30 07:54:32 +0000410
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000411 /* Re-enable signals. */
412 signal(SIGINT, interrupt_signal);
413 signal(SIGQUIT, quit_signal);
Eric Andersenc3657422001-11-30 07:54:32 +0000414}
415
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000416int time_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000417int time_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenc3657422001-11-30 07:54:32 +0000418{
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000419 resource_t res;
Denys Vlasenkoa1a3b592017-04-28 18:01:18 +0200420 /* $TIME has lowest prio (-v,-p,-f FMT overrride it) */
421 const char *output_format = getenv("TIME") ? : default_format;
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200422 char *output_filename;
423 int output_fd;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000424 int opt;
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200425 int ex;
426 enum {
427 OPT_v = (1 << 0),
428 OPT_p = (1 << 1),
429 OPT_a = (1 << 2),
430 OPT_o = (1 << 3),
Denys Vlasenkoa1a3b592017-04-28 18:01:18 +0200431 OPT_f = (1 << 4),
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200432 };
Eric Andersenc3657422001-11-30 07:54:32 +0000433
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000434 /* "+": stop on first non-option */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200435 opt = getopt32(argv, "^+" "vpao:f:" "\0" "-1"/*at least one arg*/,
436 &output_filename, &output_format
437 );
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000438 argv += optind;
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200439 if (opt & OPT_v)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000440 output_format = long_format;
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200441 if (opt & OPT_p)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000442 output_format = posix_format;
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200443 output_fd = STDERR_FILENO;
444 if (opt & OPT_o) {
Denys Vlasenkod3a7e882017-10-27 19:05:00 +0200445#ifndef O_CLOEXEC
446# define O_CLOEXEC 0
447#endif
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200448 output_fd = xopen(output_filename,
449 (opt & OPT_a) /* append? */
450 ? (O_CREAT | O_WRONLY | O_CLOEXEC | O_APPEND)
451 : (O_CREAT | O_WRONLY | O_CLOEXEC | O_TRUNC)
452 );
Denys Vlasenkod3a7e882017-10-27 19:05:00 +0200453 if (!O_CLOEXEC)
454 close_on_exec_on(output_fd);
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200455 }
Eric Andersenc3657422001-11-30 07:54:32 +0000456
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000457 run_command(argv, &res);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000458
459 /* Cheat. printf's are shorter :) */
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200460 xdup2(output_fd, STDOUT_FILENO);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000461 summarize(output_format, argv, &res);
Eric Andersenc3657422001-11-30 07:54:32 +0000462
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200463 ex = WEXITSTATUS(res.waitstatus);
464 /* Impossible: we do not use WUNTRACED flag in wait()...
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000465 if (WIFSTOPPED(res.waitstatus))
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200466 ex = WSTOPSIG(res.waitstatus);
467 */
Denis Vlasenkof93ab472006-12-22 12:36:13 +0000468 if (WIFSIGNALED(res.waitstatus))
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200469 ex = WTERMSIG(res.waitstatus);
470
471 fflush_stdout_and_exit(ex);
Eric Andersenc3657422001-11-30 07:54:32 +0000472}