blob: c443ef909756a793d8d37875fe4345996f785e59 [file] [log] [blame]
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +00001/* vi: set sw=4 ts=4: */
Denis Vlasenkob44c7902008-03-17 09:29:43 +00002/* 'time' utility to display resource usage of processes.
Eric Andersenc3657422001-11-30 07:54:32 +00003 Copyright (C) 1990, 91, 92, 93, 96 Free Software Foundation, Inc.
4
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 Licensed under GPLv2, see file LICENSE in this source tree.
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +00006*/
Eric Andersenc3657422001-11-30 07:54:32 +00007/* Originally written by David Keppel <pardo@cs.washington.edu>.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00008 Heavily modified by David MacKenzie <djm@gnu.ai.mit.edu>.
Eric Andersenc3657422001-11-30 07:54:32 +00009 Heavily modified for busybox by Erik Andersen <andersen@codepoet.org>
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000010*/
Denys Vlasenkofb4da162016-11-22 23:14:24 +010011//config:config TIME
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020012//config: bool "time (7 kb)"
Denys Vlasenkofb4da162016-11-22 23:14:24 +010013//config: default y
14//config: help
15//config: The time command runs the specified program with the given arguments.
16//config: When the command finishes, time writes a message to standard output
17//config: giving timing statistics about this program run.
Eric Andersenc3657422001-11-30 07:54:32 +000018
Denys Vlasenkof88e3bf2016-11-22 23:54:17 +010019//applet:IF_TIME(APPLET(time, BB_DIR_USR_BIN, BB_SUID_DROP))
20
21//kbuild:lib-$(CONFIG_TIME) += time.o
22
Pere Orga5bc8c002011-04-11 03:29:49 +020023//usage:#define time_trivial_usage
Tommi Rantala5fe5be22017-04-28 17:54:14 +020024//usage: "[-vpa] [-o FILE] PROG ARGS"
Pere Orga5bc8c002011-04-11 03:29:49 +020025//usage:#define time_full_usage "\n\n"
26//usage: "Run PROG, display resource usage when it exits\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020027//usage: "\n -v Verbose"
Tommi Rantala854174f2017-04-24 19:08:53 +030028//usage: "\n -p POSIX output format"
Denys Vlasenkoa1a3b592017-04-28 18:01:18 +020029//usage: "\n -f FMT Custom format"
Tommi Rantala5fe5be22017-04-28 17:54:14 +020030//usage: "\n -o FILE Write result to FILE"
31//usage: "\n -a Append (else overwrite)"
Pere Orga5bc8c002011-04-11 03:29:49 +020032
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000033#include "libbb.h"
Mike Frysingerc5fe9f72012-07-05 23:19:09 -040034#include <sys/resource.h> /* getrusage */
Eric Andersenc3657422001-11-30 07:54:32 +000035
Eric Andersenc3657422001-11-30 07:54:32 +000036/* Information on the resources used by a child process. */
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000037typedef struct {
38 int waitstatus;
39 struct rusage ru;
Denis Vlasenko459be352007-06-17 19:09:05 +000040 unsigned elapsed_ms; /* Wallclock time of process. */
Eric Andersenc3657422001-11-30 07:54:32 +000041} resource_t;
42
43/* msec = milliseconds = 1/1,000 (1*10e-3) second.
44 usec = microseconds = 1/1,000,000 (1*10e-6) second. */
45
Eric Andersenc3657422001-11-30 07:54:32 +000046#define UL unsigned long
47
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000048static const char default_format[] ALIGN1 = "real\t%E\nuser\t%u\nsys\t%T";
Eric Andersenc3657422001-11-30 07:54:32 +000049
50/* The output format for the -p option .*/
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000051static const char posix_format[] ALIGN1 = "real %e\nuser %U\nsys %S";
Eric Andersenc3657422001-11-30 07:54:32 +000052
Eric Andersenc3657422001-11-30 07:54:32 +000053/* Format string for printing all statistics verbosely.
54 Keep this output to 24 lines so users on terminals can see it all.*/
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000055static const char long_format[] ALIGN1 =
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000056 "\tCommand being timed: \"%C\"\n"
57 "\tUser time (seconds): %U\n"
58 "\tSystem time (seconds): %S\n"
59 "\tPercent of CPU this job got: %P\n"
60 "\tElapsed (wall clock) time (h:mm:ss or m:ss): %E\n"
61 "\tAverage shared text size (kbytes): %X\n"
62 "\tAverage unshared data size (kbytes): %D\n"
63 "\tAverage stack size (kbytes): %p\n"
64 "\tAverage total size (kbytes): %K\n"
65 "\tMaximum resident set size (kbytes): %M\n"
66 "\tAverage resident set size (kbytes): %t\n"
67 "\tMajor (requiring I/O) page faults: %F\n"
68 "\tMinor (reclaiming a frame) page faults: %R\n"
69 "\tVoluntary context switches: %w\n"
70 "\tInvoluntary context switches: %c\n"
71 "\tSwaps: %W\n"
72 "\tFile system inputs: %I\n"
73 "\tFile system outputs: %O\n"
74 "\tSocket messages sent: %s\n"
75 "\tSocket messages received: %r\n"
76 "\tSignals delivered: %k\n"
Denis Vlasenkof93ab472006-12-22 12:36:13 +000077 "\tPage size (bytes): %Z\n"
78 "\tExit status: %x";
Eric Andersenc3657422001-11-30 07:54:32 +000079
Denis Vlasenkof93ab472006-12-22 12:36:13 +000080/* Wait for and fill in data on child process PID.
81 Return 0 on error, 1 if ok. */
Eric Andersenc3657422001-11-30 07:54:32 +000082/* pid_t is short on BSDI, so don't try to promote it. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +000083static void resuse_end(pid_t pid, resource_t *resp)
Eric Andersenc3657422001-11-30 07:54:32 +000084{
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000085 pid_t caught;
Eric Andersenc3657422001-11-30 07:54:32 +000086
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000087 /* Ignore signals, but don't ignore the children. When wait3
Denys Vlasenko6830ade2013-01-15 13:58:01 +010088 * returns the child process, set the time the command finished. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +000089 while ((caught = wait3(&resp->waitstatus, 0, &resp->ru)) != pid) {
90 if (caught == -1 && errno != EINTR) {
91 bb_perror_msg("wait");
92 return;
93 }
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000094 }
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +010095 resp->elapsed_ms = monotonic_ms() - resp->elapsed_ms;
Eric Andersenc3657422001-11-30 07:54:32 +000096}
97
Denis Vlasenkob44c7902008-03-17 09:29:43 +000098static void printargv(char *const *argv)
Eric Andersenc3657422001-11-30 07:54:32 +000099{
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000100 const char *fmt = " %s" + 1;
101 do {
102 printf(fmt, *argv);
103 fmt = " %s";
104 } while (*++argv);
Eric Andersenc3657422001-11-30 07:54:32 +0000105}
106
107/* Return the number of kilobytes corresponding to a number of pages PAGES.
108 (Actually, we use it to convert pages*ticks into kilobytes*ticks.)
109
110 Try to do arithmetic so that the risk of overflow errors is minimized.
111 This is funky since the pagesize could be less than 1K.
112 Note: Some machines express getrusage statistics in terms of K,
113 others in terms of pages. */
Bernhard Reutner-Fischer0adf7f22009-02-23 16:51:25 +0000114static unsigned long ptok(const unsigned pagesize, const unsigned long pages)
Eric Andersenc3657422001-11-30 07:54:32 +0000115{
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000116 unsigned long tmp;
Eric Andersenc3657422001-11-30 07:54:32 +0000117
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000118 /* Conversion. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000119 if (pages > (LONG_MAX / pagesize)) { /* Could overflow. */
120 tmp = pages / 1024; /* Smaller first, */
121 return tmp * pagesize; /* then larger. */
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000122 }
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000123 /* Could underflow. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000124 tmp = pages * pagesize; /* Larger first, */
125 return tmp / 1024; /* then smaller. */
Eric Andersenc3657422001-11-30 07:54:32 +0000126}
127
128/* summarize: Report on the system use of a command.
129
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000130 Print the FMT argument except that `%' sequences
Eric Andersenc3657422001-11-30 07:54:32 +0000131 have special meaning, and `\n' and `\t' are translated into
132 newline and tab, respectively, and `\\' is translated into `\'.
133
134 The character following a `%' can be:
135 (* means the tcsh time builtin also recognizes it)
136 % == a literal `%'
137 C == command name and arguments
138* D == average unshared data size in K (ru_idrss+ru_isrss)
139* E == elapsed real (wall clock) time in [hour:]min:sec
140* F == major page faults (required physical I/O) (ru_majflt)
141* I == file system inputs (ru_inblock)
142* K == average total mem usage (ru_idrss+ru_isrss+ru_ixrss)
143* M == maximum resident set size in K (ru_maxrss)
144* O == file system outputs (ru_oublock)
145* P == percent of CPU this job got (total cpu time / elapsed time)
146* R == minor page faults (reclaims; no physical I/O involved) (ru_minflt)
147* S == system (kernel) time (seconds) (ru_stime)
148* T == system time in [hour:]min:sec
149* U == user time (seconds) (ru_utime)
150* u == user time in [hour:]min:sec
151* W == times swapped out (ru_nswap)
152* X == average amount of shared text in K (ru_ixrss)
153 Z == page size
154* c == involuntary context switches (ru_nivcsw)
155 e == elapsed real time in seconds
156* k == signals delivered (ru_nsignals)
157 p == average unshared stack size in K (ru_isrss)
158* r == socket messages received (ru_msgrcv)
159* s == socket messages sent (ru_msgsnd)
160 t == average resident set size in K (ru_idrss)
161* w == voluntary context switches (ru_nvcsw)
162 x == exit status of command
163
164 Various memory usages are found by converting from page-seconds
165 to kbytes by multiplying by the page size, dividing by 1024,
166 and dividing by elapsed real time.
167
Eric Andersenc3657422001-11-30 07:54:32 +0000168 FMT is the format string, interpreted as described above.
169 COMMAND is the command and args that are being summarized.
170 RESP is resource information on the command. */
171
Denis Vlasenko459be352007-06-17 19:09:05 +0000172#ifndef TICKS_PER_SEC
173#define TICKS_PER_SEC 100
174#endif
175
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000176static void summarize(const char *fmt, char **command, resource_t *resp)
Eric Andersenc3657422001-11-30 07:54:32 +0000177{
Denis Vlasenko459be352007-06-17 19:09:05 +0000178 unsigned vv_ms; /* Elapsed virtual (CPU) milliseconds */
179 unsigned cpu_ticks; /* Same, in "CPU ticks" */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000180 unsigned pagesize = getpagesize();
Eric Andersenc3657422001-11-30 07:54:32 +0000181
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000182 /* Impossible: we do not use WUNTRACED flag in wait()...
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000183 if (WIFSTOPPED(resp->waitstatus))
Denis Vlasenko459be352007-06-17 19:09:05 +0000184 printf("Command stopped by signal %u\n",
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000185 WSTOPSIG(resp->waitstatus));
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000186 else */
187 if (WIFSIGNALED(resp->waitstatus))
Denis Vlasenko459be352007-06-17 19:09:05 +0000188 printf("Command terminated by signal %u\n",
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000189 WTERMSIG(resp->waitstatus));
190 else if (WIFEXITED(resp->waitstatus) && WEXITSTATUS(resp->waitstatus))
Denis Vlasenko459be352007-06-17 19:09:05 +0000191 printf("Command exited with non-zero status %u\n",
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000192 WEXITSTATUS(resp->waitstatus));
Eric Andersenc3657422001-11-30 07:54:32 +0000193
Denis Vlasenko459be352007-06-17 19:09:05 +0000194 vv_ms = (resp->ru.ru_utime.tv_sec + resp->ru.ru_stime.tv_sec) * 1000
195 + (resp->ru.ru_utime.tv_usec + resp->ru.ru_stime.tv_usec) / 1000;
Eric Andersenc3657422001-11-30 07:54:32 +0000196
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000197#if (1000 / TICKS_PER_SEC) * TICKS_PER_SEC == 1000
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000198 /* 1000 is exactly divisible by TICKS_PER_SEC (typical) */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000199 cpu_ticks = vv_ms / (1000 / TICKS_PER_SEC);
200#else
201 cpu_ticks = vv_ms * (unsigned long long)TICKS_PER_SEC / 1000;
202#endif
Denis Vlasenko459be352007-06-17 19:09:05 +0000203 if (!cpu_ticks) cpu_ticks = 1; /* we divide by it, must be nonzero */
Eric Andersenc3657422001-11-30 07:54:32 +0000204
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000205 while (*fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000206 /* Handle leading literal part */
207 int n = strcspn(fmt, "%\\");
208 if (n) {
209 printf("%.*s", n, fmt);
210 fmt += n;
211 continue;
212 }
213
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000214 switch (*fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000215#ifdef NOT_NEEDED
216 /* Handle literal char */
217 /* Usually we optimize for size, but there is a limit
218 * for everything. With this we do a lot of 1-byte writes */
219 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000220 bb_putchar(*fmt);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000221 break;
222#endif
223
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000224 case '%':
225 switch (*++fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000226#ifdef NOT_NEEDED_YET
227 /* Our format strings do not have these */
228 /* and we do not take format str from user */
229 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000230 bb_putchar('%');
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000231 /*FALLTHROUGH*/
232 case '%':
233 if (!*fmt) goto ret;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000234 bb_putchar(*fmt);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000235 break;
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000236#endif
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000237 case 'C': /* The command that got timed. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000238 printargv(command);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000239 break;
240 case 'D': /* Average unshared data size. */
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000241 printf("%lu",
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000242 (ptok(pagesize, (UL) resp->ru.ru_idrss) +
243 ptok(pagesize, (UL) resp->ru.ru_isrss)) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000244 break;
Denis Vlasenko459be352007-06-17 19:09:05 +0000245 case 'E': { /* Elapsed real (wall clock) time. */
246 unsigned seconds = resp->elapsed_ms / 1000;
247 if (seconds >= 3600) /* One hour -> h:m:s. */
248 printf("%uh %um %02us",
249 seconds / 3600,
250 (seconds % 3600) / 60,
251 seconds % 60);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000252 else
Denis Vlasenko459be352007-06-17 19:09:05 +0000253 printf("%um %u.%02us", /* -> m:s. */
254 seconds / 60,
255 seconds % 60,
256 (unsigned)(resp->elapsed_ms / 10) % 100);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000257 break;
Denis Vlasenko459be352007-06-17 19:09:05 +0000258 }
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000259 case 'F': /* Major page faults. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000260 printf("%lu", resp->ru.ru_majflt);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000261 break;
262 case 'I': /* Inputs. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000263 printf("%lu", resp->ru.ru_inblock);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000264 break;
265 case 'K': /* Average mem usage == data+stack+text. */
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000266 printf("%lu",
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000267 (ptok(pagesize, (UL) resp->ru.ru_idrss) +
268 ptok(pagesize, (UL) resp->ru.ru_isrss) +
269 ptok(pagesize, (UL) resp->ru.ru_ixrss)) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000270 break;
271 case 'M': /* Maximum resident set size. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000272 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_maxrss));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000273 break;
274 case 'O': /* Outputs. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000275 printf("%lu", resp->ru.ru_oublock);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000276 break;
277 case 'P': /* Percent of CPU this job got. */
278 /* % cpu is (total cpu time)/(elapsed time). */
Denis Vlasenko459be352007-06-17 19:09:05 +0000279 if (resp->elapsed_ms > 0)
280 printf("%u%%", (unsigned)(vv_ms * 100 / resp->elapsed_ms));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000281 else
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000282 printf("?%%");
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000283 break;
284 case 'R': /* Minor page faults (reclaims). */
Denis Vlasenko459be352007-06-17 19:09:05 +0000285 printf("%lu", resp->ru.ru_minflt);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000286 break;
287 case 'S': /* System time. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000288 printf("%u.%02u",
289 (unsigned)resp->ru.ru_stime.tv_sec,
290 (unsigned)(resp->ru.ru_stime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000291 break;
292 case 'T': /* System time. */
293 if (resp->ru.ru_stime.tv_sec >= 3600) /* One hour -> h:m:s. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000294 printf("%uh %um %02us",
295 (unsigned)(resp->ru.ru_stime.tv_sec / 3600),
296 (unsigned)(resp->ru.ru_stime.tv_sec % 3600) / 60,
297 (unsigned)(resp->ru.ru_stime.tv_sec % 60));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000298 else
Denis Vlasenko459be352007-06-17 19:09:05 +0000299 printf("%um %u.%02us", /* -> m:s. */
300 (unsigned)(resp->ru.ru_stime.tv_sec / 60),
301 (unsigned)(resp->ru.ru_stime.tv_sec % 60),
302 (unsigned)(resp->ru.ru_stime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000303 break;
304 case 'U': /* User time. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000305 printf("%u.%02u",
306 (unsigned)resp->ru.ru_utime.tv_sec,
307 (unsigned)(resp->ru.ru_utime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000308 break;
309 case 'u': /* User time. */
310 if (resp->ru.ru_utime.tv_sec >= 3600) /* One hour -> h:m:s. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000311 printf("%uh %um %02us",
312 (unsigned)(resp->ru.ru_utime.tv_sec / 3600),
313 (unsigned)(resp->ru.ru_utime.tv_sec % 3600) / 60,
314 (unsigned)(resp->ru.ru_utime.tv_sec % 60));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000315 else
Denis Vlasenko459be352007-06-17 19:09:05 +0000316 printf("%um %u.%02us", /* -> m:s. */
317 (unsigned)(resp->ru.ru_utime.tv_sec / 60),
318 (unsigned)(resp->ru.ru_utime.tv_sec % 60),
319 (unsigned)(resp->ru.ru_utime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000320 break;
321 case 'W': /* Times swapped out. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000322 printf("%lu", resp->ru.ru_nswap);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000323 break;
324 case 'X': /* Average shared text size. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000325 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_ixrss) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000326 break;
327 case 'Z': /* Page size. */
Bernhard Reutner-Fischer0adf7f22009-02-23 16:51:25 +0000328 printf("%u", pagesize);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000329 break;
330 case 'c': /* Involuntary context switches. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000331 printf("%lu", resp->ru.ru_nivcsw);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000332 break;
333 case 'e': /* Elapsed real time in seconds. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000334 printf("%u.%02u",
335 (unsigned)resp->elapsed_ms / 1000,
336 (unsigned)(resp->elapsed_ms / 10) % 100);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000337 break;
338 case 'k': /* Signals delivered. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000339 printf("%lu", resp->ru.ru_nsignals);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000340 break;
341 case 'p': /* Average stack segment. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000342 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_isrss) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000343 break;
344 case 'r': /* Incoming socket messages received. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000345 printf("%lu", resp->ru.ru_msgrcv);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000346 break;
347 case 's': /* Outgoing socket messages sent. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000348 printf("%lu", resp->ru.ru_msgsnd);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000349 break;
350 case 't': /* Average resident set size. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000351 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_idrss) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000352 break;
353 case 'w': /* Voluntary context switches. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000354 printf("%lu", resp->ru.ru_nvcsw);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000355 break;
356 case 'x': /* Exit status. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000357 printf("%u", WEXITSTATUS(resp->waitstatus));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000358 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000359 }
Eric Andersenc3657422001-11-30 07:54:32 +0000360 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000361
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000362#ifdef NOT_NEEDED_YET
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000363 case '\\': /* Format escape. */
364 switch (*++fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000365 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000366 bb_putchar('\\');
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000367 /*FALLTHROUGH*/
368 case '\\':
369 if (!*fmt) goto ret;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000370 bb_putchar(*fmt);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000371 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000372 case 't':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000373 bb_putchar('\t');
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000374 break;
375 case 'n':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000376 bb_putchar('\n');
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000377 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000378 }
Eric Andersenc3657422001-11-30 07:54:32 +0000379 break;
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000380#endif
Eric Andersenc3657422001-11-30 07:54:32 +0000381 }
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000382 ++fmt;
Eric Andersenc3657422001-11-30 07:54:32 +0000383 }
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000384 /* ret: */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000385 bb_putchar('\n');
Eric Andersenc3657422001-11-30 07:54:32 +0000386}
387
388/* Run command CMD and return statistics on it.
389 Put the statistics in *RESP. */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000390static void run_command(char *const *cmd, resource_t *resp)
Eric Andersenc3657422001-11-30 07:54:32 +0000391{
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200392 pid_t pid;
Denis Vlasenko25591c32008-02-16 22:58:56 +0000393 void (*interrupt_signal)(int);
394 void (*quit_signal)(int);
Eric Andersenc3657422001-11-30 07:54:32 +0000395
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100396 resp->elapsed_ms = monotonic_ms();
Pascal Bellard926031b2010-07-04 15:32:38 +0200397 pid = xvfork();
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200398 if (pid == 0) {
399 /* Child */
400 BB_EXECVP_or_die((char**)cmd);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000401 }
Eric Andersenc3657422001-11-30 07:54:32 +0000402
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000403 /* Have signals kill the child but not self (if possible). */
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200404//TODO: just block all sigs? and re-enable them in the very end in main?
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000405 interrupt_signal = signal(SIGINT, SIG_IGN);
406 quit_signal = signal(SIGQUIT, SIG_IGN);
Eric Andersenc3657422001-11-30 07:54:32 +0000407
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000408 resuse_end(pid, resp);
Eric Andersenc3657422001-11-30 07:54:32 +0000409
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000410 /* Re-enable signals. */
411 signal(SIGINT, interrupt_signal);
412 signal(SIGQUIT, quit_signal);
Eric Andersenc3657422001-11-30 07:54:32 +0000413}
414
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000415int time_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000416int time_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenc3657422001-11-30 07:54:32 +0000417{
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000418 resource_t res;
Denys Vlasenkoa1a3b592017-04-28 18:01:18 +0200419 /* $TIME has lowest prio (-v,-p,-f FMT overrride it) */
420 const char *output_format = getenv("TIME") ? : default_format;
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200421 char *output_filename;
422 int output_fd;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000423 int opt;
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200424 int ex;
425 enum {
426 OPT_v = (1 << 0),
427 OPT_p = (1 << 1),
428 OPT_a = (1 << 2),
429 OPT_o = (1 << 3),
Denys Vlasenkoa1a3b592017-04-28 18:01:18 +0200430 OPT_f = (1 << 4),
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200431 };
Eric Andersenc3657422001-11-30 07:54:32 +0000432
Denis Vlasenko94884eb2008-07-11 15:05:51 +0000433 opt_complementary = "-1"; /* at least one arg */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000434 /* "+": stop on first non-option */
Denys Vlasenkoa1a3b592017-04-28 18:01:18 +0200435 opt = getopt32(argv, "+vpao:f:", &output_filename, &output_format);
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000436 argv += optind;
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200437 if (opt & OPT_v)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000438 output_format = long_format;
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200439 if (opt & OPT_p)
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000440 output_format = posix_format;
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200441 output_fd = STDERR_FILENO;
442 if (opt & OPT_o) {
443 output_fd = xopen(output_filename,
444 (opt & OPT_a) /* append? */
445 ? (O_CREAT | O_WRONLY | O_CLOEXEC | O_APPEND)
446 : (O_CREAT | O_WRONLY | O_CLOEXEC | O_TRUNC)
447 );
448 }
Eric Andersenc3657422001-11-30 07:54:32 +0000449
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000450 run_command(argv, &res);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000451
452 /* Cheat. printf's are shorter :) */
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200453 xdup2(output_fd, STDOUT_FILENO);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000454 summarize(output_format, argv, &res);
Eric Andersenc3657422001-11-30 07:54:32 +0000455
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200456 ex = WEXITSTATUS(res.waitstatus);
457 /* Impossible: we do not use WUNTRACED flag in wait()...
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000458 if (WIFSTOPPED(res.waitstatus))
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200459 ex = WSTOPSIG(res.waitstatus);
460 */
Denis Vlasenkof93ab472006-12-22 12:36:13 +0000461 if (WIFSIGNALED(res.waitstatus))
Tommi Rantala5fe5be22017-04-28 17:54:14 +0200462 ex = WTERMSIG(res.waitstatus);
463
464 fflush_stdout_and_exit(ex);
Eric Andersenc3657422001-11-30 07:54:32 +0000465}