busybox/miscutils/time.c

472 lines
14 KiB
C
Raw Normal View History

/* vi: set sw=4 ts=4: */
/*
* 'time' utility to display resource usage of processes.
* Copyright (C) 1990, 91, 92, 93, 96 Free Software Foundation, Inc.
*
* Licensed under GPLv2, see file LICENSE in this source tree.
*/
/* Originally written by David Keppel <pardo@cs.washington.edu>.
* Heavily modified by David MacKenzie <djm@gnu.ai.mit.edu>.
* Heavily modified for busybox by Erik Andersen <andersen@codepoet.org>
*/
//config:config TIME
//config: bool "time (7 kb)"
//config: default y
//config: help
//config: The time command runs the specified program with the given arguments.
//config: When the command finishes, time writes a message to standard output
//config: giving timing statistics about this program run.
//applet:IF_TIME(APPLET(time, BB_DIR_USR_BIN, BB_SUID_DROP))
//kbuild:lib-$(CONFIG_TIME) += time.o
//usage:#define time_trivial_usage
//usage: "[-vpa] [-o FILE] PROG ARGS"
//usage:#define time_full_usage "\n\n"
//usage: "Run PROG, display resource usage when it exits\n"
//usage: "\n -v Verbose"
//usage: "\n -p POSIX output format"
//usage: "\n -f FMT Custom format"
//usage: "\n -o FILE Write result to FILE"
//usage: "\n -a Append (else overwrite)"
#include "libbb.h"
/* Information on the resources used by a child process. */
typedef struct {
int waitstatus;
struct rusage ru;
unsigned elapsed_ms; /* Wallclock time of process. */
} resource_t;
/* msec = milliseconds = 1/1,000 (1*10e-3) second.
usec = microseconds = 1/1,000,000 (1*10e-6) second. */
#define UL unsigned long
static const char default_format[] ALIGN1 = "real\t%E\nuser\t%u\nsys\t%T";
/* The output format for the -p option .*/
static const char posix_format[] ALIGN1 = "real %e\nuser %U\nsys %S";
/* Format string for printing all statistics verbosely.
Keep this output to 24 lines so users on terminals can see it all.*/
static const char long_format[] ALIGN1 =
"\tCommand being timed: \"%C\"\n"
"\tUser time (seconds): %U\n"
"\tSystem time (seconds): %S\n"
"\tPercent of CPU this job got: %P\n"
"\tElapsed (wall clock) time (h:mm:ss or m:ss): %E\n"
"\tAverage shared text size (kbytes): %X\n"
"\tAverage unshared data size (kbytes): %D\n"
"\tAverage stack size (kbytes): %p\n"
"\tAverage total size (kbytes): %K\n"
"\tMaximum resident set size (kbytes): %M\n"
"\tAverage resident set size (kbytes): %t\n"
"\tMajor (requiring I/O) page faults: %F\n"
"\tMinor (reclaiming a frame) page faults: %R\n"
"\tVoluntary context switches: %w\n"
"\tInvoluntary context switches: %c\n"
"\tSwaps: %W\n"
"\tFile system inputs: %I\n"
"\tFile system outputs: %O\n"
"\tSocket messages sent: %s\n"
"\tSocket messages received: %r\n"
"\tSignals delivered: %k\n"
"\tPage size (bytes): %Z\n"
"\tExit status: %x";
/* Wait for and fill in data on child process PID.
Return 0 on error, 1 if ok. */
/* pid_t is short on BSDI, so don't try to promote it. */
static void resuse_end(pid_t pid, resource_t *resp)
{
pid_t caught;
/* Ignore signals, but don't ignore the children. When wait3
* returns the child process, set the time the command finished. */
while ((caught = wait3(&resp->waitstatus, 0, &resp->ru)) != pid) {
if (caught == -1 && errno != EINTR) {
bb_perror_msg("wait");
return;
}
}
resp->elapsed_ms = monotonic_ms() - resp->elapsed_ms;
}
static void printargv(char *const *argv)
{
const char *fmt = " %s" + 1;
do {
printf(fmt, *argv);
fmt = " %s";
} while (*++argv);
}
/* Return the number of kilobytes corresponding to a number of pages PAGES.
(Actually, we use it to convert pages*ticks into kilobytes*ticks.)
Try to do arithmetic so that the risk of overflow errors is minimized.
This is funky since the pagesize could be less than 1K.
Note: Some machines express getrusage statistics in terms of K,
others in terms of pages. */
static unsigned long ptok(const unsigned pagesize, const unsigned long pages)
{
unsigned long tmp;
/* Conversion. */
if (pages > (LONG_MAX / pagesize)) { /* Could overflow. */
tmp = pages / 1024; /* Smaller first, */
return tmp * pagesize; /* then larger. */
}
2006-12-22 19:13:19 +05:30
/* Could underflow. */
tmp = pages * pagesize; /* Larger first, */
return tmp / 1024; /* then smaller. */
}
/* summarize: Report on the system use of a command.
Print the FMT argument except that '%' sequences
have special meaning, and '\n' and '\t' are translated into
newline and tab, respectively, and '\\' is translated into '\'.
The character following a '%' can be:
(* means the tcsh time builtin also recognizes it)
% == a literal '%'
C == command name and arguments
* D == average unshared data size in K (ru_idrss+ru_isrss)
* E == elapsed real (wall clock) time in [hour:]min:sec
* F == major page faults (required physical I/O) (ru_majflt)
* I == file system inputs (ru_inblock)
* K == average total mem usage (ru_idrss+ru_isrss+ru_ixrss)
* M == maximum resident set size in K (ru_maxrss)
* O == file system outputs (ru_oublock)
* P == percent of CPU this job got (total cpu time / elapsed time)
* R == minor page faults (reclaims; no physical I/O involved) (ru_minflt)
* S == system (kernel) time (seconds) (ru_stime)
* T == system time in [hour:]min:sec
* U == user time (seconds) (ru_utime)
* u == user time in [hour:]min:sec
* W == times swapped out (ru_nswap)
* X == average amount of shared text in K (ru_ixrss)
Z == page size
* c == involuntary context switches (ru_nivcsw)
e == elapsed real time in seconds
* k == signals delivered (ru_nsignals)
p == average unshared stack size in K (ru_isrss)
* r == socket messages received (ru_msgrcv)
* s == socket messages sent (ru_msgsnd)
t == average resident set size in K (ru_idrss)
* w == voluntary context switches (ru_nvcsw)
x == exit status of command
Various memory usages are found by converting from page-seconds
to kbytes by multiplying by the page size, dividing by 1024,
and dividing by elapsed real time.
FMT is the format string, interpreted as described above.
COMMAND is the command and args that are being summarized.
RESP is resource information on the command. */
#ifndef TICKS_PER_SEC
#define TICKS_PER_SEC 100
#endif
static void summarize(const char *fmt, char **command, resource_t *resp)
{
unsigned vv_ms; /* Elapsed virtual (CPU) milliseconds */
unsigned cpu_ticks; /* Same, in "CPU ticks" */
unsigned pagesize = getpagesize();
/* Impossible: we do not use WUNTRACED flag in wait()...
if (WIFSTOPPED(resp->waitstatus))
printf("Command stopped by signal %u\n",
WSTOPSIG(resp->waitstatus));
else */
if (WIFSIGNALED(resp->waitstatus))
printf("Command terminated by signal %u\n",
WTERMSIG(resp->waitstatus));
else if (WIFEXITED(resp->waitstatus) && WEXITSTATUS(resp->waitstatus))
printf("Command exited with non-zero status %u\n",
WEXITSTATUS(resp->waitstatus));
vv_ms = (resp->ru.ru_utime.tv_sec + resp->ru.ru_stime.tv_sec) * 1000
+ (resp->ru.ru_utime.tv_usec + resp->ru.ru_stime.tv_usec) / 1000;
#if (1000 / TICKS_PER_SEC) * TICKS_PER_SEC == 1000
/* 1000 is exactly divisible by TICKS_PER_SEC (typical) */
cpu_ticks = vv_ms / (1000 / TICKS_PER_SEC);
#else
cpu_ticks = vv_ms * (unsigned long long)TICKS_PER_SEC / 1000;
#endif
if (!cpu_ticks) cpu_ticks = 1; /* we divide by it, must be nonzero */
while (*fmt) {
2006-12-22 19:13:19 +05:30
/* Handle leading literal part */
int n = strcspn(fmt, "%\\");
if (n) {
printf("%.*s", n, fmt);
fmt += n;
continue;
}
switch (*fmt) {
2006-12-22 19:13:19 +05:30
#ifdef NOT_NEEDED
/* Handle literal char */
/* Usually we optimize for size, but there is a limit
* for everything. With this we do a lot of 1-byte writes */
default:
bb_putchar(*fmt);
2006-12-22 19:13:19 +05:30
break;
#endif
case '%':
switch (*++fmt) {
2006-12-22 19:13:19 +05:30
#ifdef NOT_NEEDED_YET
/* Our format strings do not have these */
/* and we do not take format str from user */
default:
bb_putchar('%');
2006-12-22 19:13:19 +05:30
/*FALLTHROUGH*/
case '%':
if (!*fmt) goto ret;
bb_putchar(*fmt);
break;
2006-12-22 19:13:19 +05:30
#endif
case 'C': /* The command that got timed. */
printargv(command);
break;
case 'D': /* Average unshared data size. */
2006-12-22 19:13:19 +05:30
printf("%lu",
(ptok(pagesize, (UL) resp->ru.ru_idrss) +
ptok(pagesize, (UL) resp->ru.ru_isrss)) / cpu_ticks);
break;
case 'E': { /* Elapsed real (wall clock) time. */
unsigned seconds = resp->elapsed_ms / 1000;
if (seconds >= 3600) /* One hour -> h:m:s. */
printf("%uh %um %02us",
seconds / 3600,
(seconds % 3600) / 60,
seconds % 60);
else
printf("%um %u.%02us", /* -> m:s. */
seconds / 60,
seconds % 60,
(unsigned)(resp->elapsed_ms / 10) % 100);
break;
}
case 'F': /* Major page faults. */
printf("%lu", resp->ru.ru_majflt);
break;
case 'I': /* Inputs. */
printf("%lu", resp->ru.ru_inblock);
break;
case 'K': /* Average mem usage == data+stack+text. */
2006-12-22 19:13:19 +05:30
printf("%lu",
(ptok(pagesize, (UL) resp->ru.ru_idrss) +
ptok(pagesize, (UL) resp->ru.ru_isrss) +
ptok(pagesize, (UL) resp->ru.ru_ixrss)) / cpu_ticks);
break;
case 'M': /* Maximum resident set size. */
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_maxrss));
break;
case 'O': /* Outputs. */
printf("%lu", resp->ru.ru_oublock);
break;
case 'P': /* Percent of CPU this job got. */
/* % cpu is (total cpu time)/(elapsed time). */
if (resp->elapsed_ms > 0)
printf("%u%%", (unsigned)(vv_ms * 100 / resp->elapsed_ms));
else
2006-12-22 19:13:19 +05:30
printf("?%%");
break;
case 'R': /* Minor page faults (reclaims). */
printf("%lu", resp->ru.ru_minflt);
break;
case 'S': /* System time. */
printf("%u.%02u",
(unsigned)resp->ru.ru_stime.tv_sec,
(unsigned)(resp->ru.ru_stime.tv_usec / 10000));
break;
case 'T': /* System time. */
if (resp->ru.ru_stime.tv_sec >= 3600) /* One hour -> h:m:s. */
printf("%uh %um %02us",
(unsigned)(resp->ru.ru_stime.tv_sec / 3600),
(unsigned)(resp->ru.ru_stime.tv_sec % 3600) / 60,
(unsigned)(resp->ru.ru_stime.tv_sec % 60));
else
printf("%um %u.%02us", /* -> m:s. */
(unsigned)(resp->ru.ru_stime.tv_sec / 60),
(unsigned)(resp->ru.ru_stime.tv_sec % 60),
(unsigned)(resp->ru.ru_stime.tv_usec / 10000));
break;
case 'U': /* User time. */
printf("%u.%02u",
(unsigned)resp->ru.ru_utime.tv_sec,
(unsigned)(resp->ru.ru_utime.tv_usec / 10000));
break;
case 'u': /* User time. */
if (resp->ru.ru_utime.tv_sec >= 3600) /* One hour -> h:m:s. */
printf("%uh %um %02us",
(unsigned)(resp->ru.ru_utime.tv_sec / 3600),
(unsigned)(resp->ru.ru_utime.tv_sec % 3600) / 60,
(unsigned)(resp->ru.ru_utime.tv_sec % 60));
else
printf("%um %u.%02us", /* -> m:s. */
(unsigned)(resp->ru.ru_utime.tv_sec / 60),
(unsigned)(resp->ru.ru_utime.tv_sec % 60),
(unsigned)(resp->ru.ru_utime.tv_usec / 10000));
break;
case 'W': /* Times swapped out. */
printf("%lu", resp->ru.ru_nswap);
break;
case 'X': /* Average shared text size. */
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_ixrss) / cpu_ticks);
break;
case 'Z': /* Page size. */
printf("%u", pagesize);
break;
case 'c': /* Involuntary context switches. */
printf("%lu", resp->ru.ru_nivcsw);
break;
case 'e': /* Elapsed real time in seconds. */
printf("%u.%02u",
(unsigned)resp->elapsed_ms / 1000,
(unsigned)(resp->elapsed_ms / 10) % 100);
break;
case 'k': /* Signals delivered. */
printf("%lu", resp->ru.ru_nsignals);
break;
case 'p': /* Average stack segment. */
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_isrss) / cpu_ticks);
break;
case 'r': /* Incoming socket messages received. */
printf("%lu", resp->ru.ru_msgrcv);
break;
case 's': /* Outgoing socket messages sent. */
printf("%lu", resp->ru.ru_msgsnd);
break;
case 't': /* Average resident set size. */
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_idrss) / cpu_ticks);
break;
case 'w': /* Voluntary context switches. */
printf("%lu", resp->ru.ru_nvcsw);
break;
case 'x': /* Exit status. */
printf("%u", WEXITSTATUS(resp->waitstatus));
break;
}
break;
2006-12-22 19:13:19 +05:30
#ifdef NOT_NEEDED_YET
case '\\': /* Format escape. */
switch (*++fmt) {
2006-12-22 19:13:19 +05:30
default:
bb_putchar('\\');
2006-12-22 19:13:19 +05:30
/*FALLTHROUGH*/
case '\\':
if (!*fmt) goto ret;
bb_putchar(*fmt);
2006-12-22 19:13:19 +05:30
break;
case 't':
bb_putchar('\t');
break;
case 'n':
bb_putchar('\n');
break;
}
break;
2006-12-22 19:13:19 +05:30
#endif
}
2006-12-22 19:13:19 +05:30
++fmt;
}
2006-12-22 19:13:19 +05:30
/* ret: */
bb_putchar('\n');
}
/* Run command CMD and return statistics on it.
Put the statistics in *RESP. */
static void run_command(char *const *cmd, resource_t *resp)
{
pid_t pid;
void (*interrupt_signal)(int);
void (*quit_signal)(int);
resp->elapsed_ms = monotonic_ms();
pid = xvfork();
if (pid == 0) {
/* Child */
BB_EXECVP_or_die((char**)cmd);
}
/* Have signals kill the child but not self (if possible). */
//TODO: just block all sigs? and re-enable them in the very end in main?
interrupt_signal = signal(SIGINT, SIG_IGN);
quit_signal = signal(SIGQUIT, SIG_IGN);
resuse_end(pid, resp);
/* Re-enable signals. */
signal(SIGINT, interrupt_signal);
signal(SIGQUIT, quit_signal);
}
int time_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
2008-07-05 14:48:54 +05:30
int time_main(int argc UNUSED_PARAM, char **argv)
{
resource_t res;
/* $TIME has lowest prio (-v,-p,-f FMT overrride it) */
const char *output_format = getenv("TIME") ? : default_format;
char *output_filename;
int output_fd;
int opt;
int ex;
enum {
OPT_v = (1 << 0),
OPT_p = (1 << 1),
OPT_a = (1 << 2),
OPT_o = (1 << 3),
OPT_f = (1 << 4),
};
/* "+": stop on first non-option */
getopt32: remove opt_complementary function old new delta vgetopt32 1318 1392 +74 runsvdir_main 703 713 +10 bb_make_directory 423 425 +2 collect_cpu 546 545 -1 opt_chars 3 - -3 opt_complementary 4 - -4 tftpd_main 567 562 -5 ntp_init 476 471 -5 zcip_main 1266 1256 -10 xxd_main 428 418 -10 whois_main 140 130 -10 who_main 463 453 -10 which_main 212 202 -10 wget_main 2535 2525 -10 watchdog_main 291 281 -10 watch_main 222 212 -10 vlock_main 399 389 -10 uuencode_main 332 322 -10 uudecode_main 316 306 -10 unlink_main 45 35 -10 udhcpd_main 1482 1472 -10 udhcpc_main 2762 2752 -10 tune2fs_main 290 280 -10 tunctl_main 366 356 -10 truncate_main 218 208 -10 tr_main 518 508 -10 time_main 1134 1124 -10 tftp_main 286 276 -10 telnetd_main 1873 1863 -10 tcpudpsvd_main 1785 1775 -10 taskset_main 521 511 -10 tar_main 1009 999 -10 tail_main 1644 1634 -10 syslogd_main 1967 1957 -10 switch_root_main 368 358 -10 svlogd_main 1454 1444 -10 sv 1296 1286 -10 stat_main 104 94 -10 start_stop_daemon_main 1028 1018 -10 split_main 542 532 -10 sort_main 796 786 -10 slattach_main 624 614 -10 shuf_main 504 494 -10 setsid_main 96 86 -10 setserial_main 1132 1122 -10 setfont_main 388 378 -10 setconsole_main 78 68 -10 sendmail_main 1209 1199 -10 sed_main 677 667 -10 script_main 1077 1067 -10 run_parts_main 325 315 -10 rtcwake_main 454 444 -10 rm_main 175 165 -10 reformime_main 119 109 -10 readlink_main 123 113 -10 rdate_main 246 236 -10 pwdx_main 189 179 -10 pstree_main 317 307 -10 pscan_main 663 653 -10 popmaildir_main 818 808 -10 pmap_main 80 70 -10 nc_main 1042 1032 -10 mv_main 558 548 -10 mountpoint_main 477 467 -10 mount_main 1264 1254 -10 modprobe_main 768 758 -10 modinfo_main 333 323 -10 mktemp_main 200 190 -10 mkswap_main 324 314 -10 mkfs_vfat_main 1489 1479 -10 microcom_main 715 705 -10 md5_sha1_sum_main 521 511 -10 man_main 867 857 -10 makedevs_main 1052 1042 -10 ls_main 563 553 -10 losetup_main 432 422 -10 loadfont_main 89 79 -10 ln_main 524 514 -10 link_main 75 65 -10 ipcalc_main 544 534 -10 iostat_main 2397 2387 -10 install_main 768 758 -10 id_main 480 470 -10 i2cset_main 1239 1229 -10 i2cget_main 380 370 -10 i2cdump_main 1482 1472 -10 i2cdetect_main 682 672 -10 hwclock_main 406 396 -10 httpd_main 741 731 -10 grep_main 837 827 -10 getty_main 1559 1549 -10 fuser_main 297 287 -10 ftpgetput_main 345 335 -10 ftpd_main 2232 2222 -10 fstrim_main 251 241 -10 fsfreeze_main 77 67 -10 fsck_minix_main 2921 2911 -10 flock_main 314 304 -10 flashcp_main 740 730 -10 flash_eraseall_main 833 823 -10 fdformat_main 532 522 -10 expand_main 680 670 -10 eject_main 335 325 -10 dumpleases_main 630 620 -10 du_main 314 304 -10 dos2unix_main 441 431 -10 diff_main 1350 1340 -10 df_main 1064 1054 -10 date_main 1095 1085 -10 cut_main 961 951 -10 cryptpw_main 228 218 -10 crontab_main 575 565 -10 crond_main 1149 1139 -10 cp_main 370 360 -10 common_traceroute_main 3834 3824 -10 common_ping_main 1767 1757 -10 comm_main 239 229 -10 cmp_main 655 645 -10 chrt_main 379 369 -10 chpst_main 704 694 -10 chpasswd_main 308 298 -10 chown_main 171 161 -10 chmod_main 158 148 -10 cat_main 428 418 -10 bzip2_main 120 110 -10 blkdiscard_main 264 254 -10 base64_main 221 211 -10 arping_main 1665 1655 -10 ar_main 556 546 -10 adjtimex_main 406 396 -10 adduser_main 882 872 -10 addgroup_main 411 401 -10 acpid_main 1198 1188 -10 optstring 11 - -11 opt_string 18 - -18 OPT_STR 25 - -25 ubi_tools_main 1288 1258 -30 ls_options 31 - -31 ------------------------------------------------------------------------------ (add/remove: 0/6 grow/shrink: 3/129 up/down: 86/-1383) Total: -1297 bytes text data bss dec hex filename 915428 485 6876 922789 e14a5 busybox_old 914629 485 6872 921986 e1182 busybox_unstripped Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-09 01:25:02 +05:30
opt = getopt32(argv, "^+" "vpao:f:" "\0" "-1"/*at least one arg*/,
&output_filename, &output_format
);
argv += optind;
if (opt & OPT_v)
output_format = long_format;
if (opt & OPT_p)
output_format = posix_format;
output_fd = STDERR_FILENO;
if (opt & OPT_o) {
#ifndef O_CLOEXEC
# define O_CLOEXEC 0
#endif
output_fd = xopen(output_filename,
(opt & OPT_a) /* append? */
? (O_CREAT | O_WRONLY | O_CLOEXEC | O_APPEND)
: (O_CREAT | O_WRONLY | O_CLOEXEC | O_TRUNC)
);
if (!O_CLOEXEC)
close_on_exec_on(output_fd);
}
run_command(argv, &res);
2006-12-22 19:13:19 +05:30
/* Cheat. printf's are shorter :) */
xdup2(output_fd, STDOUT_FILENO);
2006-12-22 19:13:19 +05:30
summarize(output_format, argv, &res);
ex = WEXITSTATUS(res.waitstatus);
/* Impossible: we do not use WUNTRACED flag in wait()...
if (WIFSTOPPED(res.waitstatus))
ex = WSTOPSIG(res.waitstatus);
*/
if (WIFSIGNALED(res.waitstatus))
ex = WTERMSIG(res.waitstatus);
fflush_stdout_and_exit(ex);
}