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