blob: 945f15f0d56c7eee322dbfdfdc68dcc4c6b44c83 [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*/
Eric Andersenc3657422001-11-30 07:54:32 +000011
Pere Orga5bc8c002011-04-11 03:29:49 +020012//usage:#define time_trivial_usage
13//usage: "[-v] PROG ARGS"
14//usage:#define time_full_usage "\n\n"
15//usage: "Run PROG, display resource usage when it exits\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020016//usage: "\n -v Verbose"
17
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Eric Andersenc3657422001-11-30 07:54:32 +000019
Eric Andersenc3657422001-11-30 07:54:32 +000020/* Information on the resources used by a child process. */
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000021typedef struct {
22 int waitstatus;
23 struct rusage ru;
Denis Vlasenko459be352007-06-17 19:09:05 +000024 unsigned elapsed_ms; /* Wallclock time of process. */
Eric Andersenc3657422001-11-30 07:54:32 +000025} resource_t;
26
27/* msec = milliseconds = 1/1,000 (1*10e-3) second.
28 usec = microseconds = 1/1,000,000 (1*10e-6) second. */
29
Eric Andersenc3657422001-11-30 07:54:32 +000030#define UL unsigned long
31
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000032static const char default_format[] ALIGN1 = "real\t%E\nuser\t%u\nsys\t%T";
Eric Andersenc3657422001-11-30 07:54:32 +000033
34/* The output format for the -p option .*/
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000035static const char posix_format[] ALIGN1 = "real %e\nuser %U\nsys %S";
Eric Andersenc3657422001-11-30 07:54:32 +000036
Eric Andersenc3657422001-11-30 07:54:32 +000037/* Format string for printing all statistics verbosely.
38 Keep this output to 24 lines so users on terminals can see it all.*/
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000039static const char long_format[] ALIGN1 =
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000040 "\tCommand being timed: \"%C\"\n"
41 "\tUser time (seconds): %U\n"
42 "\tSystem time (seconds): %S\n"
43 "\tPercent of CPU this job got: %P\n"
44 "\tElapsed (wall clock) time (h:mm:ss or m:ss): %E\n"
45 "\tAverage shared text size (kbytes): %X\n"
46 "\tAverage unshared data size (kbytes): %D\n"
47 "\tAverage stack size (kbytes): %p\n"
48 "\tAverage total size (kbytes): %K\n"
49 "\tMaximum resident set size (kbytes): %M\n"
50 "\tAverage resident set size (kbytes): %t\n"
51 "\tMajor (requiring I/O) page faults: %F\n"
52 "\tMinor (reclaiming a frame) page faults: %R\n"
53 "\tVoluntary context switches: %w\n"
54 "\tInvoluntary context switches: %c\n"
55 "\tSwaps: %W\n"
56 "\tFile system inputs: %I\n"
57 "\tFile system outputs: %O\n"
58 "\tSocket messages sent: %s\n"
59 "\tSocket messages received: %r\n"
60 "\tSignals delivered: %k\n"
Denis Vlasenkof93ab472006-12-22 12:36:13 +000061 "\tPage size (bytes): %Z\n"
62 "\tExit status: %x";
Eric Andersenc3657422001-11-30 07:54:32 +000063
Denis Vlasenkof93ab472006-12-22 12:36:13 +000064/* Wait for and fill in data on child process PID.
65 Return 0 on error, 1 if ok. */
Eric Andersenc3657422001-11-30 07:54:32 +000066/* pid_t is short on BSDI, so don't try to promote it. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +000067static void resuse_end(pid_t pid, resource_t *resp)
Eric Andersenc3657422001-11-30 07:54:32 +000068{
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000069 pid_t caught;
Eric Andersenc3657422001-11-30 07:54:32 +000070
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000071 /* Ignore signals, but don't ignore the children. When wait3
72 returns the child process, set the time the command finished. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +000073 while ((caught = wait3(&resp->waitstatus, 0, &resp->ru)) != pid) {
74 if (caught == -1 && errno != EINTR) {
75 bb_perror_msg("wait");
76 return;
77 }
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +000078 }
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +010079 resp->elapsed_ms = monotonic_ms() - resp->elapsed_ms;
Eric Andersenc3657422001-11-30 07:54:32 +000080}
81
Denis Vlasenkob44c7902008-03-17 09:29:43 +000082static void printargv(char *const *argv)
Eric Andersenc3657422001-11-30 07:54:32 +000083{
Denis Vlasenkob44c7902008-03-17 09:29:43 +000084 const char *fmt = " %s" + 1;
85 do {
86 printf(fmt, *argv);
87 fmt = " %s";
88 } while (*++argv);
Eric Andersenc3657422001-11-30 07:54:32 +000089}
90
91/* Return the number of kilobytes corresponding to a number of pages PAGES.
92 (Actually, we use it to convert pages*ticks into kilobytes*ticks.)
93
94 Try to do arithmetic so that the risk of overflow errors is minimized.
95 This is funky since the pagesize could be less than 1K.
96 Note: Some machines express getrusage statistics in terms of K,
97 others in terms of pages. */
Bernhard Reutner-Fischer0adf7f22009-02-23 16:51:25 +000098static unsigned long ptok(const unsigned pagesize, const unsigned long pages)
Eric Andersenc3657422001-11-30 07:54:32 +000099{
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000100 unsigned long tmp;
Eric Andersenc3657422001-11-30 07:54:32 +0000101
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000102 /* Conversion. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000103 if (pages > (LONG_MAX / pagesize)) { /* Could overflow. */
104 tmp = pages / 1024; /* Smaller first, */
105 return tmp * pagesize; /* then larger. */
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000106 }
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000107 /* Could underflow. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000108 tmp = pages * pagesize; /* Larger first, */
109 return tmp / 1024; /* then smaller. */
Eric Andersenc3657422001-11-30 07:54:32 +0000110}
111
112/* summarize: Report on the system use of a command.
113
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000114 Print the FMT argument except that `%' sequences
Eric Andersenc3657422001-11-30 07:54:32 +0000115 have special meaning, and `\n' and `\t' are translated into
116 newline and tab, respectively, and `\\' is translated into `\'.
117
118 The character following a `%' can be:
119 (* means the tcsh time builtin also recognizes it)
120 % == a literal `%'
121 C == command name and arguments
122* D == average unshared data size in K (ru_idrss+ru_isrss)
123* E == elapsed real (wall clock) time in [hour:]min:sec
124* F == major page faults (required physical I/O) (ru_majflt)
125* I == file system inputs (ru_inblock)
126* K == average total mem usage (ru_idrss+ru_isrss+ru_ixrss)
127* M == maximum resident set size in K (ru_maxrss)
128* O == file system outputs (ru_oublock)
129* P == percent of CPU this job got (total cpu time / elapsed time)
130* R == minor page faults (reclaims; no physical I/O involved) (ru_minflt)
131* S == system (kernel) time (seconds) (ru_stime)
132* T == system time in [hour:]min:sec
133* U == user time (seconds) (ru_utime)
134* u == user time in [hour:]min:sec
135* W == times swapped out (ru_nswap)
136* X == average amount of shared text in K (ru_ixrss)
137 Z == page size
138* c == involuntary context switches (ru_nivcsw)
139 e == elapsed real time in seconds
140* k == signals delivered (ru_nsignals)
141 p == average unshared stack size in K (ru_isrss)
142* r == socket messages received (ru_msgrcv)
143* s == socket messages sent (ru_msgsnd)
144 t == average resident set size in K (ru_idrss)
145* w == voluntary context switches (ru_nvcsw)
146 x == exit status of command
147
148 Various memory usages are found by converting from page-seconds
149 to kbytes by multiplying by the page size, dividing by 1024,
150 and dividing by elapsed real time.
151
Eric Andersenc3657422001-11-30 07:54:32 +0000152 FMT is the format string, interpreted as described above.
153 COMMAND is the command and args that are being summarized.
154 RESP is resource information on the command. */
155
Denis Vlasenko459be352007-06-17 19:09:05 +0000156#ifndef TICKS_PER_SEC
157#define TICKS_PER_SEC 100
158#endif
159
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000160static void summarize(const char *fmt, char **command, resource_t *resp)
Eric Andersenc3657422001-11-30 07:54:32 +0000161{
Denis Vlasenko459be352007-06-17 19:09:05 +0000162 unsigned vv_ms; /* Elapsed virtual (CPU) milliseconds */
163 unsigned cpu_ticks; /* Same, in "CPU ticks" */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000164 unsigned pagesize = getpagesize();
Eric Andersenc3657422001-11-30 07:54:32 +0000165
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000166 /* Impossible: we do not use WUNTRACED flag in wait()...
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000167 if (WIFSTOPPED(resp->waitstatus))
Denis Vlasenko459be352007-06-17 19:09:05 +0000168 printf("Command stopped by signal %u\n",
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000169 WSTOPSIG(resp->waitstatus));
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000170 else */
171 if (WIFSIGNALED(resp->waitstatus))
Denis Vlasenko459be352007-06-17 19:09:05 +0000172 printf("Command terminated by signal %u\n",
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000173 WTERMSIG(resp->waitstatus));
174 else if (WIFEXITED(resp->waitstatus) && WEXITSTATUS(resp->waitstatus))
Denis Vlasenko459be352007-06-17 19:09:05 +0000175 printf("Command exited with non-zero status %u\n",
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000176 WEXITSTATUS(resp->waitstatus));
Eric Andersenc3657422001-11-30 07:54:32 +0000177
Denis Vlasenko459be352007-06-17 19:09:05 +0000178 vv_ms = (resp->ru.ru_utime.tv_sec + resp->ru.ru_stime.tv_sec) * 1000
179 + (resp->ru.ru_utime.tv_usec + resp->ru.ru_stime.tv_usec) / 1000;
Eric Andersenc3657422001-11-30 07:54:32 +0000180
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000181#if (1000 / TICKS_PER_SEC) * TICKS_PER_SEC == 1000
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000182 /* 1000 is exactly divisible by TICKS_PER_SEC (typical) */
Denis Vlasenkobd7bb292007-06-17 23:40:26 +0000183 cpu_ticks = vv_ms / (1000 / TICKS_PER_SEC);
184#else
185 cpu_ticks = vv_ms * (unsigned long long)TICKS_PER_SEC / 1000;
186#endif
Denis Vlasenko459be352007-06-17 19:09:05 +0000187 if (!cpu_ticks) cpu_ticks = 1; /* we divide by it, must be nonzero */
Eric Andersenc3657422001-11-30 07:54:32 +0000188
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000189 while (*fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000190 /* Handle leading literal part */
191 int n = strcspn(fmt, "%\\");
192 if (n) {
193 printf("%.*s", n, fmt);
194 fmt += n;
195 continue;
196 }
197
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000198 switch (*fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000199#ifdef NOT_NEEDED
200 /* Handle literal char */
201 /* Usually we optimize for size, but there is a limit
202 * for everything. With this we do a lot of 1-byte writes */
203 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000204 bb_putchar(*fmt);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000205 break;
206#endif
207
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000208 case '%':
209 switch (*++fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000210#ifdef NOT_NEEDED_YET
211 /* Our format strings do not have these */
212 /* and we do not take format str from user */
213 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000214 bb_putchar('%');
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000215 /*FALLTHROUGH*/
216 case '%':
217 if (!*fmt) goto ret;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000218 bb_putchar(*fmt);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000219 break;
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000220#endif
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000221 case 'C': /* The command that got timed. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000222 printargv(command);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000223 break;
224 case 'D': /* Average unshared data size. */
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000225 printf("%lu",
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000226 (ptok(pagesize, (UL) resp->ru.ru_idrss) +
227 ptok(pagesize, (UL) resp->ru.ru_isrss)) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000228 break;
Denis Vlasenko459be352007-06-17 19:09:05 +0000229 case 'E': { /* Elapsed real (wall clock) time. */
230 unsigned seconds = resp->elapsed_ms / 1000;
231 if (seconds >= 3600) /* One hour -> h:m:s. */
232 printf("%uh %um %02us",
233 seconds / 3600,
234 (seconds % 3600) / 60,
235 seconds % 60);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000236 else
Denis Vlasenko459be352007-06-17 19:09:05 +0000237 printf("%um %u.%02us", /* -> m:s. */
238 seconds / 60,
239 seconds % 60,
240 (unsigned)(resp->elapsed_ms / 10) % 100);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000241 break;
Denis Vlasenko459be352007-06-17 19:09:05 +0000242 }
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000243 case 'F': /* Major page faults. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000244 printf("%lu", resp->ru.ru_majflt);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000245 break;
246 case 'I': /* Inputs. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000247 printf("%lu", resp->ru.ru_inblock);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000248 break;
249 case 'K': /* Average mem usage == data+stack+text. */
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000250 printf("%lu",
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000251 (ptok(pagesize, (UL) resp->ru.ru_idrss) +
252 ptok(pagesize, (UL) resp->ru.ru_isrss) +
253 ptok(pagesize, (UL) resp->ru.ru_ixrss)) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000254 break;
255 case 'M': /* Maximum resident set size. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000256 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_maxrss));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000257 break;
258 case 'O': /* Outputs. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000259 printf("%lu", resp->ru.ru_oublock);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000260 break;
261 case 'P': /* Percent of CPU this job got. */
262 /* % cpu is (total cpu time)/(elapsed time). */
Denis Vlasenko459be352007-06-17 19:09:05 +0000263 if (resp->elapsed_ms > 0)
264 printf("%u%%", (unsigned)(vv_ms * 100 / resp->elapsed_ms));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000265 else
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000266 printf("?%%");
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000267 break;
268 case 'R': /* Minor page faults (reclaims). */
Denis Vlasenko459be352007-06-17 19:09:05 +0000269 printf("%lu", resp->ru.ru_minflt);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000270 break;
271 case 'S': /* System time. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000272 printf("%u.%02u",
273 (unsigned)resp->ru.ru_stime.tv_sec,
274 (unsigned)(resp->ru.ru_stime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000275 break;
276 case 'T': /* System time. */
277 if (resp->ru.ru_stime.tv_sec >= 3600) /* One hour -> h:m:s. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000278 printf("%uh %um %02us",
279 (unsigned)(resp->ru.ru_stime.tv_sec / 3600),
280 (unsigned)(resp->ru.ru_stime.tv_sec % 3600) / 60,
281 (unsigned)(resp->ru.ru_stime.tv_sec % 60));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000282 else
Denis Vlasenko459be352007-06-17 19:09:05 +0000283 printf("%um %u.%02us", /* -> m:s. */
284 (unsigned)(resp->ru.ru_stime.tv_sec / 60),
285 (unsigned)(resp->ru.ru_stime.tv_sec % 60),
286 (unsigned)(resp->ru.ru_stime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000287 break;
288 case 'U': /* User time. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000289 printf("%u.%02u",
290 (unsigned)resp->ru.ru_utime.tv_sec,
291 (unsigned)(resp->ru.ru_utime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000292 break;
293 case 'u': /* User time. */
294 if (resp->ru.ru_utime.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_utime.tv_sec / 3600),
297 (unsigned)(resp->ru.ru_utime.tv_sec % 3600) / 60,
298 (unsigned)(resp->ru.ru_utime.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_utime.tv_sec / 60),
302 (unsigned)(resp->ru.ru_utime.tv_sec % 60),
303 (unsigned)(resp->ru.ru_utime.tv_usec / 10000));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000304 break;
305 case 'W': /* Times swapped out. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000306 printf("%lu", resp->ru.ru_nswap);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000307 break;
308 case 'X': /* Average shared text size. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000309 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_ixrss) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000310 break;
311 case 'Z': /* Page size. */
Bernhard Reutner-Fischer0adf7f22009-02-23 16:51:25 +0000312 printf("%u", pagesize);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000313 break;
314 case 'c': /* Involuntary context switches. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000315 printf("%lu", resp->ru.ru_nivcsw);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000316 break;
317 case 'e': /* Elapsed real time in seconds. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000318 printf("%u.%02u",
319 (unsigned)resp->elapsed_ms / 1000,
320 (unsigned)(resp->elapsed_ms / 10) % 100);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000321 break;
322 case 'k': /* Signals delivered. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000323 printf("%lu", resp->ru.ru_nsignals);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000324 break;
325 case 'p': /* Average stack segment. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000326 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_isrss) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000327 break;
328 case 'r': /* Incoming socket messages received. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000329 printf("%lu", resp->ru.ru_msgrcv);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000330 break;
331 case 's': /* Outgoing socket messages sent. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000332 printf("%lu", resp->ru.ru_msgsnd);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000333 break;
334 case 't': /* Average resident set size. */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000335 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_idrss) / cpu_ticks);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000336 break;
337 case 'w': /* Voluntary context switches. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000338 printf("%lu", resp->ru.ru_nvcsw);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000339 break;
340 case 'x': /* Exit status. */
Denis Vlasenko459be352007-06-17 19:09:05 +0000341 printf("%u", WEXITSTATUS(resp->waitstatus));
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000342 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000343 }
Eric Andersenc3657422001-11-30 07:54:32 +0000344 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000345
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000346#ifdef NOT_NEEDED_YET
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000347 case '\\': /* Format escape. */
348 switch (*++fmt) {
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000349 default:
Denis Vlasenko4daad902007-09-27 10:20:47 +0000350 bb_putchar('\\');
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000351 /*FALLTHROUGH*/
352 case '\\':
353 if (!*fmt) goto ret;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000354 bb_putchar(*fmt);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000355 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000356 case 't':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000357 bb_putchar('\t');
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000358 break;
359 case 'n':
Denis Vlasenko4daad902007-09-27 10:20:47 +0000360 bb_putchar('\n');
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000361 break;
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000362 }
Eric Andersenc3657422001-11-30 07:54:32 +0000363 break;
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000364#endif
Eric Andersenc3657422001-11-30 07:54:32 +0000365 }
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000366 ++fmt;
Eric Andersenc3657422001-11-30 07:54:32 +0000367 }
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000368 /* ret: */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000369 bb_putchar('\n');
Eric Andersenc3657422001-11-30 07:54:32 +0000370}
371
372/* Run command CMD and return statistics on it.
373 Put the statistics in *RESP. */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000374static void run_command(char *const *cmd, resource_t *resp)
Eric Andersenc3657422001-11-30 07:54:32 +0000375{
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200376 pid_t pid;
Denis Vlasenko25591c32008-02-16 22:58:56 +0000377 void (*interrupt_signal)(int);
378 void (*quit_signal)(int);
Eric Andersenc3657422001-11-30 07:54:32 +0000379
Denys Vlasenkof2c8aa62010-01-12 12:52:30 +0100380 resp->elapsed_ms = monotonic_ms();
Pascal Bellard926031b2010-07-04 15:32:38 +0200381 pid = xvfork();
Pascal Bellard21e8e8d2010-07-04 00:57:03 +0200382 if (pid == 0) {
383 /* Child */
384 BB_EXECVP_or_die((char**)cmd);
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000385 }
Eric Andersenc3657422001-11-30 07:54:32 +0000386
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000387 /* Have signals kill the child but not self (if possible). */
Denis Vlasenko25591c32008-02-16 22:58:56 +0000388//TODO: just block all sigs? and reenable them in the very end in main?
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000389 interrupt_signal = signal(SIGINT, SIG_IGN);
390 quit_signal = signal(SIGQUIT, SIG_IGN);
Eric Andersenc3657422001-11-30 07:54:32 +0000391
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000392 resuse_end(pid, resp);
Eric Andersenc3657422001-11-30 07:54:32 +0000393
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000394 /* Re-enable signals. */
395 signal(SIGINT, interrupt_signal);
396 signal(SIGQUIT, quit_signal);
Eric Andersenc3657422001-11-30 07:54:32 +0000397}
398
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000399int time_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000400int time_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenc3657422001-11-30 07:54:32 +0000401{
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000402 resource_t res;
403 const char *output_format = default_format;
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000404 int opt;
Eric Andersenc3657422001-11-30 07:54:32 +0000405
Denis Vlasenko94884eb2008-07-11 15:05:51 +0000406 opt_complementary = "-1"; /* at least one arg */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000407 /* "+": stop on first non-option */
408 opt = getopt32(argv, "+vp");
409 argv += optind;
410 if (opt & 1)
411 output_format = long_format;
412 if (opt & 2)
413 output_format = posix_format;
Eric Andersenc3657422001-11-30 07:54:32 +0000414
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000415 run_command(argv, &res);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000416
417 /* Cheat. printf's are shorter :) */
Denys Vlasenkoc0664722010-01-02 18:49:22 +0100418 xdup2(STDERR_FILENO, STDOUT_FILENO);
Denis Vlasenkoc5cb38f2006-12-22 13:43:19 +0000419 summarize(output_format, argv, &res);
Eric Andersenc3657422001-11-30 07:54:32 +0000420
Bernhard Reutner-Fischerb1312c92006-06-03 20:09:02 +0000421 if (WIFSTOPPED(res.waitstatus))
Denis Vlasenkof93ab472006-12-22 12:36:13 +0000422 return WSTOPSIG(res.waitstatus);
423 if (WIFSIGNALED(res.waitstatus))
424 return WTERMSIG(res.waitstatus);
425 if (WIFEXITED(res.waitstatus))
426 return WEXITSTATUS(res.waitstatus);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000427 fflush_stdout_and_exit(EXIT_SUCCESS);
Eric Andersenc3657422001-11-30 07:54:32 +0000428}