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