1 /* vi: set sw=4 ts=4: */
2 /* 'time' utility to display resource usage of processes.
3 Copyright (C) 1990, 91, 92, 93, 96 Free Software Foundation, Inc.
5 Licensed under GPLv2, see file LICENSE in this source tree.
7 /* Originally written by David Keppel <pardo@cs.washington.edu>.
8 Heavily modified by David MacKenzie <djm@gnu.ai.mit.edu>.
9 Heavily modified for busybox by Erik Andersen <andersen@codepoet.org>
12 //config: bool "time (7 kb)"
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.
19 //applet:IF_TIME(APPLET(time, BB_DIR_USR_BIN, BB_SUID_DROP))
21 //kbuild:lib-$(CONFIG_TIME) += time.o
23 //usage:#define time_trivial_usage
24 //usage: "[-vpa] [-o FILE] PROG ARGS"
25 //usage:#define time_full_usage "\n\n"
26 //usage: "Run PROG, display resource usage when it exits\n"
27 //usage: "\n -v Verbose"
28 //usage: "\n -p POSIX output format"
29 //usage: "\n -f FMT Custom format"
30 //usage: "\n -o FILE Write result to FILE"
31 //usage: "\n -a Append (else overwrite)"
34 #include <sys/resource.h> /* getrusage */
36 /* Information on the resources used by a child process. */
40 unsigned elapsed_ms; /* Wallclock time of process. */
43 /* msec = milliseconds = 1/1,000 (1*10e-3) second.
44 usec = microseconds = 1/1,000,000 (1*10e-6) second. */
46 #define UL unsigned long
48 static const char default_format[] ALIGN1 = "real\t%E\nuser\t%u\nsys\t%T";
50 /* The output format for the -p option .*/
51 static const char posix_format[] ALIGN1 = "real %e\nuser %U\nsys %S";
53 /* Format string for printing all statistics verbosely.
54 Keep this output to 24 lines so users on terminals can see it all.*/
55 static const char long_format[] ALIGN1 =
56 "\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"
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"
77 "\tPage size (bytes): %Z\n"
80 /* Wait for and fill in data on child process PID.
81 Return 0 on error, 1 if ok. */
82 /* pid_t is short on BSDI, so don't try to promote it. */
83 static void resuse_end(pid_t pid, resource_t *resp)
87 /* Ignore signals, but don't ignore the children. When wait3
88 * returns the child process, set the time the command finished. */
89 while ((caught = wait3(&resp->waitstatus, 0, &resp->ru)) != pid) {
90 if (caught == -1 && errno != EINTR) {
91 bb_perror_msg("wait");
95 resp->elapsed_ms = monotonic_ms() - resp->elapsed_ms;
98 static void printargv(char *const *argv)
100 const char *fmt = " %s" + 1;
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.)
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. */
114 static unsigned long ptok(const unsigned pagesize, const unsigned long pages)
119 if (pages > (LONG_MAX / pagesize)) { /* Could overflow. */
120 tmp = pages / 1024; /* Smaller first, */
121 return tmp * pagesize; /* then larger. */
123 /* Could underflow. */
124 tmp = pages * pagesize; /* Larger first, */
125 return tmp / 1024; /* then smaller. */
128 /* summarize: Report on the system use of a command.
130 Print the FMT argument except that '%' sequences
131 have special meaning, and '\n' and '\t' are translated into
132 newline and tab, respectively, and '\\' is translated into '\'.
134 The character following a '%' can be:
135 (* means the tcsh time builtin also recognizes it)
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)
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
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.
168 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. */
172 #ifndef TICKS_PER_SEC
173 #define TICKS_PER_SEC 100
176 static void summarize(const char *fmt, char **command, resource_t *resp)
178 unsigned vv_ms; /* Elapsed virtual (CPU) milliseconds */
179 unsigned cpu_ticks; /* Same, in "CPU ticks" */
180 unsigned pagesize = getpagesize();
182 /* Impossible: we do not use WUNTRACED flag in wait()...
183 if (WIFSTOPPED(resp->waitstatus))
184 printf("Command stopped by signal %u\n",
185 WSTOPSIG(resp->waitstatus));
187 if (WIFSIGNALED(resp->waitstatus))
188 printf("Command terminated by signal %u\n",
189 WTERMSIG(resp->waitstatus));
190 else if (WIFEXITED(resp->waitstatus) && WEXITSTATUS(resp->waitstatus))
191 printf("Command exited with non-zero status %u\n",
192 WEXITSTATUS(resp->waitstatus));
194 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;
197 #if (1000 / TICKS_PER_SEC) * TICKS_PER_SEC == 1000
198 /* 1000 is exactly divisible by TICKS_PER_SEC (typical) */
199 cpu_ticks = vv_ms / (1000 / TICKS_PER_SEC);
201 cpu_ticks = vv_ms * (unsigned long long)TICKS_PER_SEC / 1000;
203 if (!cpu_ticks) cpu_ticks = 1; /* we divide by it, must be nonzero */
206 /* Handle leading literal part */
207 int n = strcspn(fmt, "%\\");
209 printf("%.*s", n, fmt);
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 */
226 #ifdef NOT_NEEDED_YET
227 /* Our format strings do not have these */
228 /* and we do not take format str from user */
237 case 'C': /* The command that got timed. */
240 case 'D': /* Average unshared data size. */
242 (ptok(pagesize, (UL) resp->ru.ru_idrss) +
243 ptok(pagesize, (UL) resp->ru.ru_isrss)) / cpu_ticks);
245 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",
250 (seconds % 3600) / 60,
253 printf("%um %u.%02us", /* -> m:s. */
256 (unsigned)(resp->elapsed_ms / 10) % 100);
259 case 'F': /* Major page faults. */
260 printf("%lu", resp->ru.ru_majflt);
262 case 'I': /* Inputs. */
263 printf("%lu", resp->ru.ru_inblock);
265 case 'K': /* Average mem usage == data+stack+text. */
267 (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);
271 case 'M': /* Maximum resident set size. */
272 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_maxrss));
274 case 'O': /* Outputs. */
275 printf("%lu", resp->ru.ru_oublock);
277 case 'P': /* Percent of CPU this job got. */
278 /* % cpu is (total cpu time)/(elapsed time). */
279 if (resp->elapsed_ms > 0)
280 printf("%u%%", (unsigned)(vv_ms * 100 / resp->elapsed_ms));
284 case 'R': /* Minor page faults (reclaims). */
285 printf("%lu", resp->ru.ru_minflt);
287 case 'S': /* System time. */
289 (unsigned)resp->ru.ru_stime.tv_sec,
290 (unsigned)(resp->ru.ru_stime.tv_usec / 10000));
292 case 'T': /* System time. */
293 if (resp->ru.ru_stime.tv_sec >= 3600) /* One hour -> h:m:s. */
294 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));
299 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));
304 case 'U': /* User time. */
306 (unsigned)resp->ru.ru_utime.tv_sec,
307 (unsigned)(resp->ru.ru_utime.tv_usec / 10000));
309 case 'u': /* User time. */
310 if (resp->ru.ru_utime.tv_sec >= 3600) /* One hour -> h:m:s. */
311 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));
316 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));
321 case 'W': /* Times swapped out. */
322 printf("%lu", resp->ru.ru_nswap);
324 case 'X': /* Average shared text size. */
325 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_ixrss) / cpu_ticks);
327 case 'Z': /* Page size. */
328 printf("%u", pagesize);
330 case 'c': /* Involuntary context switches. */
331 printf("%lu", resp->ru.ru_nivcsw);
333 case 'e': /* Elapsed real time in seconds. */
335 (unsigned)resp->elapsed_ms / 1000,
336 (unsigned)(resp->elapsed_ms / 10) % 100);
338 case 'k': /* Signals delivered. */
339 printf("%lu", resp->ru.ru_nsignals);
341 case 'p': /* Average stack segment. */
342 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_isrss) / cpu_ticks);
344 case 'r': /* Incoming socket messages received. */
345 printf("%lu", resp->ru.ru_msgrcv);
347 case 's': /* Outgoing socket messages sent. */
348 printf("%lu", resp->ru.ru_msgsnd);
350 case 't': /* Average resident set size. */
351 printf("%lu", ptok(pagesize, (UL) resp->ru.ru_idrss) / cpu_ticks);
353 case 'w': /* Voluntary context switches. */
354 printf("%lu", resp->ru.ru_nvcsw);
356 case 'x': /* Exit status. */
357 printf("%u", WEXITSTATUS(resp->waitstatus));
362 #ifdef NOT_NEEDED_YET
363 case '\\': /* Format escape. */
388 /* Run command CMD and return statistics on it.
389 Put the statistics in *RESP. */
390 static void run_command(char *const *cmd, resource_t *resp)
393 void (*interrupt_signal)(int);
394 void (*quit_signal)(int);
396 resp->elapsed_ms = monotonic_ms();
400 BB_EXECVP_or_die((char**)cmd);
403 /* Have signals kill the child but not self (if possible). */
404 //TODO: just block all sigs? and re-enable them in the very end in main?
405 interrupt_signal = signal(SIGINT, SIG_IGN);
406 quit_signal = signal(SIGQUIT, SIG_IGN);
408 resuse_end(pid, resp);
410 /* Re-enable signals. */
411 signal(SIGINT, interrupt_signal);
412 signal(SIGQUIT, quit_signal);
415 int time_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
416 int time_main(int argc UNUSED_PARAM, char **argv)
419 /* $TIME has lowest prio (-v,-p,-f FMT overrride it) */
420 const char *output_format = getenv("TIME") ? : default_format;
421 char *output_filename;
433 /* "+": stop on first non-option */
434 opt = getopt32(argv, "^+" "vpao:f:" "\0" "-1"/*at least one arg*/,
435 &output_filename, &output_format
439 output_format = long_format;
441 output_format = posix_format;
442 output_fd = STDERR_FILENO;
444 output_fd = xopen(output_filename,
445 (opt & OPT_a) /* append? */
446 ? (O_CREAT | O_WRONLY | O_CLOEXEC | O_APPEND)
447 : (O_CREAT | O_WRONLY | O_CLOEXEC | O_TRUNC)
451 run_command(argv, &res);
453 /* Cheat. printf's are shorter :) */
454 xdup2(output_fd, STDOUT_FILENO);
455 summarize(output_format, argv, &res);
457 ex = WEXITSTATUS(res.waitstatus);
458 /* Impossible: we do not use WUNTRACED flag in wait()...
459 if (WIFSTOPPED(res.waitstatus))
460 ex = WSTOPSIG(res.waitstatus);
462 if (WIFSIGNALED(res.waitstatus))
463 ex = WTERMSIG(res.waitstatus);
465 fflush_stdout_and_exit(ex);