Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 2 | /* 'time' utility to display resource usage of processes. |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 3 | Copyright (C) 1990, 91, 92, 93, 96 Free Software Foundation, Inc. |
| 4 | |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | Licensed under GPLv2, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 6 | */ |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 7 | /* Originally written by David Keppel <pardo@cs.washington.edu>. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 8 | Heavily modified by David MacKenzie <djm@gnu.ai.mit.edu>. |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 9 | Heavily modified for busybox by Erik Andersen <andersen@codepoet.org> |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 10 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 11 | //config:config TIME |
| 12 | //config: bool "time" |
| 13 | //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 Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 18 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 19 | //applet:IF_TIME(APPLET(time, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 20 | |
| 21 | //kbuild:lib-$(CONFIG_TIME) += time.o |
| 22 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 23 | //usage:#define time_trivial_usage |
| 24 | //usage: "[-v] PROG ARGS" |
| 25 | //usage:#define time_full_usage "\n\n" |
| 26 | //usage: "Run PROG, display resource usage when it exits\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 27 | //usage: "\n -v Verbose" |
| 28 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 29 | #include "libbb.h" |
Mike Frysinger | c5fe9f7 | 2012-07-05 23:19:09 -0400 | [diff] [blame] | 30 | #include <sys/resource.h> /* getrusage */ |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 31 | |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 32 | /* Information on the resources used by a child process. */ |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 33 | typedef struct { |
| 34 | int waitstatus; |
| 35 | struct rusage ru; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 36 | unsigned elapsed_ms; /* Wallclock time of process. */ |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 37 | } resource_t; |
| 38 | |
| 39 | /* msec = milliseconds = 1/1,000 (1*10e-3) second. |
| 40 | usec = microseconds = 1/1,000,000 (1*10e-6) second. */ |
| 41 | |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 42 | #define UL unsigned long |
| 43 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 44 | static const char default_format[] ALIGN1 = "real\t%E\nuser\t%u\nsys\t%T"; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 45 | |
| 46 | /* The output format for the -p option .*/ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 47 | static const char posix_format[] ALIGN1 = "real %e\nuser %U\nsys %S"; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 48 | |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 49 | /* Format string for printing all statistics verbosely. |
| 50 | Keep this output to 24 lines so users on terminals can see it all.*/ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 51 | static const char long_format[] ALIGN1 = |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 52 | "\tCommand being timed: \"%C\"\n" |
| 53 | "\tUser time (seconds): %U\n" |
| 54 | "\tSystem time (seconds): %S\n" |
| 55 | "\tPercent of CPU this job got: %P\n" |
| 56 | "\tElapsed (wall clock) time (h:mm:ss or m:ss): %E\n" |
| 57 | "\tAverage shared text size (kbytes): %X\n" |
| 58 | "\tAverage unshared data size (kbytes): %D\n" |
| 59 | "\tAverage stack size (kbytes): %p\n" |
| 60 | "\tAverage total size (kbytes): %K\n" |
| 61 | "\tMaximum resident set size (kbytes): %M\n" |
| 62 | "\tAverage resident set size (kbytes): %t\n" |
| 63 | "\tMajor (requiring I/O) page faults: %F\n" |
| 64 | "\tMinor (reclaiming a frame) page faults: %R\n" |
| 65 | "\tVoluntary context switches: %w\n" |
| 66 | "\tInvoluntary context switches: %c\n" |
| 67 | "\tSwaps: %W\n" |
| 68 | "\tFile system inputs: %I\n" |
| 69 | "\tFile system outputs: %O\n" |
| 70 | "\tSocket messages sent: %s\n" |
| 71 | "\tSocket messages received: %r\n" |
| 72 | "\tSignals delivered: %k\n" |
Denis Vlasenko | f93ab47 | 2006-12-22 12:36:13 +0000 | [diff] [blame] | 73 | "\tPage size (bytes): %Z\n" |
| 74 | "\tExit status: %x"; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | f93ab47 | 2006-12-22 12:36:13 +0000 | [diff] [blame] | 76 | /* Wait for and fill in data on child process PID. |
| 77 | Return 0 on error, 1 if ok. */ |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 78 | /* pid_t is short on BSDI, so don't try to promote it. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 79 | static void resuse_end(pid_t pid, resource_t *resp) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 80 | { |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 81 | pid_t caught; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 82 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 83 | /* Ignore signals, but don't ignore the children. When wait3 |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 84 | * returns the child process, set the time the command finished. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 85 | while ((caught = wait3(&resp->waitstatus, 0, &resp->ru)) != pid) { |
| 86 | if (caught == -1 && errno != EINTR) { |
| 87 | bb_perror_msg("wait"); |
| 88 | return; |
| 89 | } |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 90 | } |
Denys Vlasenko | f2c8aa6 | 2010-01-12 12:52:30 +0100 | [diff] [blame] | 91 | resp->elapsed_ms = monotonic_ms() - resp->elapsed_ms; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 94 | static void printargv(char *const *argv) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 95 | { |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 96 | const char *fmt = " %s" + 1; |
| 97 | do { |
| 98 | printf(fmt, *argv); |
| 99 | fmt = " %s"; |
| 100 | } while (*++argv); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* Return the number of kilobytes corresponding to a number of pages PAGES. |
| 104 | (Actually, we use it to convert pages*ticks into kilobytes*ticks.) |
| 105 | |
| 106 | Try to do arithmetic so that the risk of overflow errors is minimized. |
| 107 | This is funky since the pagesize could be less than 1K. |
| 108 | Note: Some machines express getrusage statistics in terms of K, |
| 109 | others in terms of pages. */ |
Bernhard Reutner-Fischer | 0adf7f2 | 2009-02-23 16:51:25 +0000 | [diff] [blame] | 110 | static unsigned long ptok(const unsigned pagesize, const unsigned long pages) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 111 | { |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 112 | unsigned long tmp; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 113 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 114 | /* Conversion. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 115 | if (pages > (LONG_MAX / pagesize)) { /* Could overflow. */ |
| 116 | tmp = pages / 1024; /* Smaller first, */ |
| 117 | return tmp * pagesize; /* then larger. */ |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 118 | } |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 119 | /* Could underflow. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 120 | tmp = pages * pagesize; /* Larger first, */ |
| 121 | return tmp / 1024; /* then smaller. */ |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* summarize: Report on the system use of a command. |
| 125 | |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 126 | Print the FMT argument except that `%' sequences |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 127 | have special meaning, and `\n' and `\t' are translated into |
| 128 | newline and tab, respectively, and `\\' is translated into `\'. |
| 129 | |
| 130 | The character following a `%' can be: |
| 131 | (* means the tcsh time builtin also recognizes it) |
| 132 | % == a literal `%' |
| 133 | C == command name and arguments |
| 134 | * D == average unshared data size in K (ru_idrss+ru_isrss) |
| 135 | * E == elapsed real (wall clock) time in [hour:]min:sec |
| 136 | * F == major page faults (required physical I/O) (ru_majflt) |
| 137 | * I == file system inputs (ru_inblock) |
| 138 | * K == average total mem usage (ru_idrss+ru_isrss+ru_ixrss) |
| 139 | * M == maximum resident set size in K (ru_maxrss) |
| 140 | * O == file system outputs (ru_oublock) |
| 141 | * P == percent of CPU this job got (total cpu time / elapsed time) |
| 142 | * R == minor page faults (reclaims; no physical I/O involved) (ru_minflt) |
| 143 | * S == system (kernel) time (seconds) (ru_stime) |
| 144 | * T == system time in [hour:]min:sec |
| 145 | * U == user time (seconds) (ru_utime) |
| 146 | * u == user time in [hour:]min:sec |
| 147 | * W == times swapped out (ru_nswap) |
| 148 | * X == average amount of shared text in K (ru_ixrss) |
| 149 | Z == page size |
| 150 | * c == involuntary context switches (ru_nivcsw) |
| 151 | e == elapsed real time in seconds |
| 152 | * k == signals delivered (ru_nsignals) |
| 153 | p == average unshared stack size in K (ru_isrss) |
| 154 | * r == socket messages received (ru_msgrcv) |
| 155 | * s == socket messages sent (ru_msgsnd) |
| 156 | t == average resident set size in K (ru_idrss) |
| 157 | * w == voluntary context switches (ru_nvcsw) |
| 158 | x == exit status of command |
| 159 | |
| 160 | Various memory usages are found by converting from page-seconds |
| 161 | to kbytes by multiplying by the page size, dividing by 1024, |
| 162 | and dividing by elapsed real time. |
| 163 | |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 164 | FMT is the format string, interpreted as described above. |
| 165 | COMMAND is the command and args that are being summarized. |
| 166 | RESP is resource information on the command. */ |
| 167 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 168 | #ifndef TICKS_PER_SEC |
| 169 | #define TICKS_PER_SEC 100 |
| 170 | #endif |
| 171 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 172 | static void summarize(const char *fmt, char **command, resource_t *resp) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 173 | { |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 174 | unsigned vv_ms; /* Elapsed virtual (CPU) milliseconds */ |
| 175 | unsigned cpu_ticks; /* Same, in "CPU ticks" */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 176 | unsigned pagesize = getpagesize(); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 177 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 178 | /* Impossible: we do not use WUNTRACED flag in wait()... |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 179 | if (WIFSTOPPED(resp->waitstatus)) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 180 | printf("Command stopped by signal %u\n", |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 181 | WSTOPSIG(resp->waitstatus)); |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 182 | else */ |
| 183 | if (WIFSIGNALED(resp->waitstatus)) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 184 | printf("Command terminated by signal %u\n", |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 185 | WTERMSIG(resp->waitstatus)); |
| 186 | else if (WIFEXITED(resp->waitstatus) && WEXITSTATUS(resp->waitstatus)) |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 187 | printf("Command exited with non-zero status %u\n", |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 188 | WEXITSTATUS(resp->waitstatus)); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 189 | |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 190 | vv_ms = (resp->ru.ru_utime.tv_sec + resp->ru.ru_stime.tv_sec) * 1000 |
| 191 | + (resp->ru.ru_utime.tv_usec + resp->ru.ru_stime.tv_usec) / 1000; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 193 | #if (1000 / TICKS_PER_SEC) * TICKS_PER_SEC == 1000 |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 194 | /* 1000 is exactly divisible by TICKS_PER_SEC (typical) */ |
Denis Vlasenko | bd7bb29 | 2007-06-17 23:40:26 +0000 | [diff] [blame] | 195 | cpu_ticks = vv_ms / (1000 / TICKS_PER_SEC); |
| 196 | #else |
| 197 | cpu_ticks = vv_ms * (unsigned long long)TICKS_PER_SEC / 1000; |
| 198 | #endif |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 199 | if (!cpu_ticks) cpu_ticks = 1; /* we divide by it, must be nonzero */ |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 200 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 201 | while (*fmt) { |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 202 | /* Handle leading literal part */ |
| 203 | int n = strcspn(fmt, "%\\"); |
| 204 | if (n) { |
| 205 | printf("%.*s", n, fmt); |
| 206 | fmt += n; |
| 207 | continue; |
| 208 | } |
| 209 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 210 | switch (*fmt) { |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 211 | #ifdef NOT_NEEDED |
| 212 | /* Handle literal char */ |
| 213 | /* Usually we optimize for size, but there is a limit |
| 214 | * for everything. With this we do a lot of 1-byte writes */ |
| 215 | default: |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 216 | bb_putchar(*fmt); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 217 | break; |
| 218 | #endif |
| 219 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 220 | case '%': |
| 221 | switch (*++fmt) { |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 222 | #ifdef NOT_NEEDED_YET |
| 223 | /* Our format strings do not have these */ |
| 224 | /* and we do not take format str from user */ |
| 225 | default: |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 226 | bb_putchar('%'); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 227 | /*FALLTHROUGH*/ |
| 228 | case '%': |
| 229 | if (!*fmt) goto ret; |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 230 | bb_putchar(*fmt); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 231 | break; |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 232 | #endif |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 233 | case 'C': /* The command that got timed. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 234 | printargv(command); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 235 | break; |
| 236 | case 'D': /* Average unshared data size. */ |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 237 | printf("%lu", |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 238 | (ptok(pagesize, (UL) resp->ru.ru_idrss) + |
| 239 | ptok(pagesize, (UL) resp->ru.ru_isrss)) / cpu_ticks); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 240 | break; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 241 | case 'E': { /* Elapsed real (wall clock) time. */ |
| 242 | unsigned seconds = resp->elapsed_ms / 1000; |
| 243 | if (seconds >= 3600) /* One hour -> h:m:s. */ |
| 244 | printf("%uh %um %02us", |
| 245 | seconds / 3600, |
| 246 | (seconds % 3600) / 60, |
| 247 | seconds % 60); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 248 | else |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 249 | printf("%um %u.%02us", /* -> m:s. */ |
| 250 | seconds / 60, |
| 251 | seconds % 60, |
| 252 | (unsigned)(resp->elapsed_ms / 10) % 100); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 253 | break; |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 254 | } |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 255 | case 'F': /* Major page faults. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 256 | printf("%lu", resp->ru.ru_majflt); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 257 | break; |
| 258 | case 'I': /* Inputs. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 259 | printf("%lu", resp->ru.ru_inblock); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 260 | break; |
| 261 | case 'K': /* Average mem usage == data+stack+text. */ |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 262 | printf("%lu", |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 263 | (ptok(pagesize, (UL) resp->ru.ru_idrss) + |
| 264 | ptok(pagesize, (UL) resp->ru.ru_isrss) + |
| 265 | ptok(pagesize, (UL) resp->ru.ru_ixrss)) / cpu_ticks); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 266 | break; |
| 267 | case 'M': /* Maximum resident set size. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 268 | printf("%lu", ptok(pagesize, (UL) resp->ru.ru_maxrss)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 269 | break; |
| 270 | case 'O': /* Outputs. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 271 | printf("%lu", resp->ru.ru_oublock); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 272 | break; |
| 273 | case 'P': /* Percent of CPU this job got. */ |
| 274 | /* % cpu is (total cpu time)/(elapsed time). */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 275 | if (resp->elapsed_ms > 0) |
| 276 | printf("%u%%", (unsigned)(vv_ms * 100 / resp->elapsed_ms)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 277 | else |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 278 | printf("?%%"); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 279 | break; |
| 280 | case 'R': /* Minor page faults (reclaims). */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 281 | printf("%lu", resp->ru.ru_minflt); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 282 | break; |
| 283 | case 'S': /* System time. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 284 | printf("%u.%02u", |
| 285 | (unsigned)resp->ru.ru_stime.tv_sec, |
| 286 | (unsigned)(resp->ru.ru_stime.tv_usec / 10000)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 287 | break; |
| 288 | case 'T': /* System time. */ |
| 289 | if (resp->ru.ru_stime.tv_sec >= 3600) /* One hour -> h:m:s. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 290 | printf("%uh %um %02us", |
| 291 | (unsigned)(resp->ru.ru_stime.tv_sec / 3600), |
| 292 | (unsigned)(resp->ru.ru_stime.tv_sec % 3600) / 60, |
| 293 | (unsigned)(resp->ru.ru_stime.tv_sec % 60)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 294 | else |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 295 | printf("%um %u.%02us", /* -> m:s. */ |
| 296 | (unsigned)(resp->ru.ru_stime.tv_sec / 60), |
| 297 | (unsigned)(resp->ru.ru_stime.tv_sec % 60), |
| 298 | (unsigned)(resp->ru.ru_stime.tv_usec / 10000)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 299 | break; |
| 300 | case 'U': /* User time. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 301 | printf("%u.%02u", |
| 302 | (unsigned)resp->ru.ru_utime.tv_sec, |
| 303 | (unsigned)(resp->ru.ru_utime.tv_usec / 10000)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 304 | break; |
| 305 | case 'u': /* User time. */ |
| 306 | if (resp->ru.ru_utime.tv_sec >= 3600) /* One hour -> h:m:s. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 307 | printf("%uh %um %02us", |
| 308 | (unsigned)(resp->ru.ru_utime.tv_sec / 3600), |
| 309 | (unsigned)(resp->ru.ru_utime.tv_sec % 3600) / 60, |
| 310 | (unsigned)(resp->ru.ru_utime.tv_sec % 60)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 311 | else |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 312 | printf("%um %u.%02us", /* -> m:s. */ |
| 313 | (unsigned)(resp->ru.ru_utime.tv_sec / 60), |
| 314 | (unsigned)(resp->ru.ru_utime.tv_sec % 60), |
| 315 | (unsigned)(resp->ru.ru_utime.tv_usec / 10000)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 316 | break; |
| 317 | case 'W': /* Times swapped out. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 318 | printf("%lu", resp->ru.ru_nswap); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 319 | break; |
| 320 | case 'X': /* Average shared text size. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 321 | printf("%lu", ptok(pagesize, (UL) resp->ru.ru_ixrss) / cpu_ticks); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 322 | break; |
| 323 | case 'Z': /* Page size. */ |
Bernhard Reutner-Fischer | 0adf7f2 | 2009-02-23 16:51:25 +0000 | [diff] [blame] | 324 | printf("%u", pagesize); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 325 | break; |
| 326 | case 'c': /* Involuntary context switches. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 327 | printf("%lu", resp->ru.ru_nivcsw); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 328 | break; |
| 329 | case 'e': /* Elapsed real time in seconds. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 330 | printf("%u.%02u", |
| 331 | (unsigned)resp->elapsed_ms / 1000, |
| 332 | (unsigned)(resp->elapsed_ms / 10) % 100); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 333 | break; |
| 334 | case 'k': /* Signals delivered. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 335 | printf("%lu", resp->ru.ru_nsignals); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 336 | break; |
| 337 | case 'p': /* Average stack segment. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 338 | printf("%lu", ptok(pagesize, (UL) resp->ru.ru_isrss) / cpu_ticks); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 339 | break; |
| 340 | case 'r': /* Incoming socket messages received. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 341 | printf("%lu", resp->ru.ru_msgrcv); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 342 | break; |
| 343 | case 's': /* Outgoing socket messages sent. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 344 | printf("%lu", resp->ru.ru_msgsnd); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 345 | break; |
| 346 | case 't': /* Average resident set size. */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 347 | printf("%lu", ptok(pagesize, (UL) resp->ru.ru_idrss) / cpu_ticks); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 348 | break; |
| 349 | case 'w': /* Voluntary context switches. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 350 | printf("%lu", resp->ru.ru_nvcsw); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 351 | break; |
| 352 | case 'x': /* Exit status. */ |
Denis Vlasenko | 459be35 | 2007-06-17 19:09:05 +0000 | [diff] [blame] | 353 | printf("%u", WEXITSTATUS(resp->waitstatus)); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 354 | break; |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 355 | } |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 356 | break; |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 357 | |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 358 | #ifdef NOT_NEEDED_YET |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 359 | case '\\': /* Format escape. */ |
| 360 | switch (*++fmt) { |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 361 | default: |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 362 | bb_putchar('\\'); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 363 | /*FALLTHROUGH*/ |
| 364 | case '\\': |
| 365 | if (!*fmt) goto ret; |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 366 | bb_putchar(*fmt); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 367 | break; |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 368 | case 't': |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 369 | bb_putchar('\t'); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 370 | break; |
| 371 | case 'n': |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 372 | bb_putchar('\n'); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 373 | break; |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 374 | } |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 375 | break; |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 376 | #endif |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 377 | } |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 378 | ++fmt; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 379 | } |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 380 | /* ret: */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 381 | bb_putchar('\n'); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* Run command CMD and return statistics on it. |
| 385 | Put the statistics in *RESP. */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 386 | static void run_command(char *const *cmd, resource_t *resp) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 387 | { |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 388 | pid_t pid; |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 389 | void (*interrupt_signal)(int); |
| 390 | void (*quit_signal)(int); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 391 | |
Denys Vlasenko | f2c8aa6 | 2010-01-12 12:52:30 +0100 | [diff] [blame] | 392 | resp->elapsed_ms = monotonic_ms(); |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 393 | pid = xvfork(); |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 394 | if (pid == 0) { |
| 395 | /* Child */ |
| 396 | BB_EXECVP_or_die((char**)cmd); |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 397 | } |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 398 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 399 | /* Have signals kill the child but not self (if possible). */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 400 | //TODO: just block all sigs? and reenable them in the very end in main? |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 401 | interrupt_signal = signal(SIGINT, SIG_IGN); |
| 402 | quit_signal = signal(SIGQUIT, SIG_IGN); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 403 | |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 404 | resuse_end(pid, resp); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 405 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 406 | /* Re-enable signals. */ |
| 407 | signal(SIGINT, interrupt_signal); |
| 408 | signal(SIGQUIT, quit_signal); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 411 | int time_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 412 | int time_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 413 | { |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 414 | resource_t res; |
| 415 | const char *output_format = default_format; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 416 | int opt; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 417 | |
Denis Vlasenko | 94884eb | 2008-07-11 15:05:51 +0000 | [diff] [blame] | 418 | opt_complementary = "-1"; /* at least one arg */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 419 | /* "+": stop on first non-option */ |
| 420 | opt = getopt32(argv, "+vp"); |
| 421 | argv += optind; |
| 422 | if (opt & 1) |
| 423 | output_format = long_format; |
| 424 | if (opt & 2) |
| 425 | output_format = posix_format; |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 426 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 427 | run_command(argv, &res); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 428 | |
| 429 | /* Cheat. printf's are shorter :) */ |
Denys Vlasenko | c066472 | 2010-01-02 18:49:22 +0100 | [diff] [blame] | 430 | xdup2(STDERR_FILENO, STDOUT_FILENO); |
Denis Vlasenko | c5cb38f | 2006-12-22 13:43:19 +0000 | [diff] [blame] | 431 | summarize(output_format, argv, &res); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 432 | |
Bernhard Reutner-Fischer | b1312c9 | 2006-06-03 20:09:02 +0000 | [diff] [blame] | 433 | if (WIFSTOPPED(res.waitstatus)) |
Denis Vlasenko | f93ab47 | 2006-12-22 12:36:13 +0000 | [diff] [blame] | 434 | return WSTOPSIG(res.waitstatus); |
| 435 | if (WIFSIGNALED(res.waitstatus)) |
| 436 | return WTERMSIG(res.waitstatus); |
| 437 | if (WIFEXITED(res.waitstatus)) |
| 438 | return WEXITSTATUS(res.waitstatus); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 439 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Eric Andersen | c365742 | 2001-11-30 07:54:32 +0000 | [diff] [blame] | 440 | } |