aboutsummaryrefslogtreecommitdiff
path: root/miscutils/time.c
diff options
context:
space:
mode:
authorGravatar Denis Vlasenko <vda.linux@googlemail.com>2008-03-17 09:29:43 +0000
committerGravatar Denis Vlasenko <vda.linux@googlemail.com>2008-03-17 09:29:43 +0000
commitb44c790e41e281965955a83408f35ea53ecdb3d2 (patch)
tree98514dba64f7b74b9d60882bfeca1e2c4976b033 /miscutils/time.c
parentd25c33f186c7cf3618c34ce79af94fe156f9ab02 (diff)
downloadbusybox-b44c790e41e281965955a83408f35ea53ecdb3d2.tar.gz
busybox-b44c790e41e281965955a83408f35ea53ecdb3d2.tar.bz2
*: fix various kinds of damage to letter 'c' in Arkadiusz Mickiewicz' surname.
chrt,taskset,dc,eject: shrink crontab: call "vi" with just name, no path watchdog: intercept all fatal signals function old new delta xstrtoull_range - 35 +35 static.sg_commands - 18 +18 packed_usage 23698 23712 +14 watchdog_main 148 151 +3 crontab_main 637 638 +1 base 1 - -1 static.ps 4 - -4 pointer 4 - -4 stack_machine 99 92 -7 run_command 273 260 -13 eject_main 360 343 -17 static.C 30 12 -18 ptok 61 38 -23 xstrtol_range 27 - -27 get_token 35 - -35 taskset_main 586 550 -36 chrt_main 411 372 -39 dc_main 158 117 -41 time_main 1127 1037 -90 ------------------------------------------------------------------------------ (add/remove: 2/5 grow/shrink: 3/9 up/down: 71/-355) Total: -284 bytes text data bss dec hex filename 793680 662 7420 801762 c3be2 busybox_old 793327 662 7412 801401 c3a79 busybox_unstripped
Diffstat (limited to 'miscutils/time.c')
-rw-r--r--miscutils/time.c118
1 files changed, 47 insertions, 71 deletions
diff --git a/miscutils/time.c b/miscutils/time.c
index 677ca6d8b..ed4385948 100644
--- a/miscutils/time.c
+++ b/miscutils/time.c
@@ -1,5 +1,5 @@
/* vi: set sw=4 ts=4: */
-/* `time' utility to display resource usage of processes.
+/* 'time' utility to display resource usage of processes.
Copyright (C) 1990, 91, 92, 93, 96 Free Software Foundation, Inc.
Licensed under GPL version 2, see file LICENSE in this tarball for details.
@@ -28,7 +28,6 @@ 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 =
@@ -56,35 +55,31 @@ static const char long_format[] ALIGN1 =
"\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 int resuse_end(pid_t pid, resource_t *resp)
+static void resuse_end(pid_t pid, resource_t *resp)
{
- int status;
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(&status, 0, &resp->ru)) != pid) {
- if (caught == -1 && errno != EINTR)
- return 0;
+ while ((caught = wait3(&resp->waitstatus, 0, &resp->ru)) != pid) {
+ if (caught == -1 && errno != EINTR) {
+ bb_perror_msg("wait");
+ return;
+ }
}
resp->elapsed_ms = (monotonic_us() / 1000) - resp->elapsed_ms;
- resp->waitstatus = status;
- return 1;
}
-/* Print ARGV, with each entry in ARGV separated by FILLER. */
-static void printargv(char *const *argv, const char *filler)
+static void printargv(char *const *argv)
{
- fputs(*argv, stdout);
- while (*++argv) {
- fputs(filler, stdout);
- fputs(*argv, stdout);
- }
+ 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.
@@ -94,24 +89,18 @@ static void printargv(char *const *argv, const char *filler)
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(unsigned long pages)
+static unsigned long ptok(unsigned pagesize, unsigned long pages)
{
- static unsigned long ps;
unsigned long tmp;
- /* Initialization. */
- if (ps == 0)
- ps = getpagesize();
-
/* Conversion. */
- if (pages > (LONG_MAX / ps)) { /* Could overflow. */
- tmp = pages / 1024; /* Smaller first, */
- return tmp * ps; /* then larger. */
+ if (pages > (LONG_MAX / pagesize)) { /* Could overflow. */
+ tmp = pages / 1024; /* Smaller first, */
+ return tmp * pagesize; /* then larger. */
}
/* Could underflow. */
- tmp = pages * ps; /* Larger first, */
- return tmp / 1024; /* then smaller. */
+ tmp = pages * pagesize; /* Larger first, */
+ return tmp / 1024; /* then smaller. */
}
/* summarize: Report on the system use of a command.
@@ -162,15 +151,18 @@ static unsigned long ptok(unsigned long pages)
#define TICKS_PER_SEC 100
#endif
-static void summarize(const char *fmt, char **command, resource_t * resp)
+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))
+ else */
+ if (WIFSIGNALED(resp->waitstatus))
printf("Command terminated by signal %u\n",
WTERMSIG(resp->waitstatus));
else if (WIFEXITED(resp->waitstatus) && WEXITSTATUS(resp->waitstatus))
@@ -181,7 +173,7 @@ static void summarize(const char *fmt, char **command, resource_t * resp)
+ (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 */
+ /* 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;
@@ -221,12 +213,12 @@ static void summarize(const char *fmt, char **command, resource_t * resp)
break;
#endif
case 'C': /* The command that got timed. */
- printargv(command, " ");
+ printargv(command);
break;
case 'D': /* Average unshared data size. */
printf("%lu",
- ptok((UL) resp->ru.ru_idrss) / cpu_ticks +
- ptok((UL) resp->ru.ru_isrss) / cpu_ticks);
+ (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;
@@ -250,12 +242,12 @@ static void summarize(const char *fmt, char **command, resource_t * resp)
break;
case 'K': /* Average mem usage == data+stack+text. */
printf("%lu",
- ptok((UL) resp->ru.ru_idrss) / cpu_ticks +
- ptok((UL) resp->ru.ru_isrss) / cpu_ticks +
- ptok((UL) resp->ru.ru_ixrss) / cpu_ticks);
+ (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((UL) resp->ru.ru_maxrss));
+ printf("%lu", ptok(pagesize, (UL) resp->ru.ru_maxrss));
break;
case 'O': /* Outputs. */
printf("%lu", resp->ru.ru_oublock);
@@ -308,7 +300,7 @@ static void summarize(const char *fmt, char **command, resource_t * resp)
printf("%lu", resp->ru.ru_nswap);
break;
case 'X': /* Average shared text size. */
- printf("%lu", ptok((UL) resp->ru.ru_ixrss) / cpu_ticks);
+ printf("%lu", ptok(pagesize, (UL) resp->ru.ru_ixrss) / cpu_ticks);
break;
case 'Z': /* Page size. */
printf("%u", getpagesize());
@@ -325,7 +317,7 @@ static void summarize(const char *fmt, char **command, resource_t * resp)
printf("%lu", resp->ru.ru_nsignals);
break;
case 'p': /* Average stack segment. */
- printf("%lu", ptok((UL) resp->ru.ru_isrss) / cpu_ticks);
+ printf("%lu", ptok(pagesize, (UL) resp->ru.ru_isrss) / cpu_ticks);
break;
case 'r': /* Incoming socket messages received. */
printf("%lu", resp->ru.ru_msgrcv);
@@ -334,7 +326,7 @@ static void summarize(const char *fmt, char **command, resource_t * resp)
printf("%lu", resp->ru.ru_msgsnd);
break;
case 't': /* Average resident set size. */
- printf("%lu", ptok((UL) resp->ru.ru_idrss) / cpu_ticks);
+ printf("%lu", ptok(pagesize, (UL) resp->ru.ru_idrss) / cpu_ticks);
break;
case 'w': /* Voluntary context switches. */
printf("%lu", resp->ru.ru_nvcsw);
@@ -396,8 +388,7 @@ static void run_command(char *const *cmd, resource_t *resp)
interrupt_signal = signal(SIGINT, SIG_IGN);
quit_signal = signal(SIGQUIT, SIG_IGN);
- if (resuse_end(pid, resp) == 0)
- bb_error_msg("error waiting for child process");
+ resuse_end(pid, resp);
/* Re-enable signals. */
signal(SIGINT, interrupt_signal);
@@ -405,41 +396,26 @@ static void run_command(char *const *cmd, resource_t *resp)
}
int time_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int time_main(int argc, char **argv)
+int time_main(int argc ATTRIBUTE_UNUSED, char **argv)
{
resource_t res;
const char *output_format = default_format;
- char c;
-
- goto next;
- /* Parse any options -- don't use getopt() here so we don't
- * consume the args of our client application... */
- while (argc > 0 && argv[0][0] == '-') {
- while ((c = *++*argv)) {
- switch (c) {
- case 'v':
- output_format = long_format;
- break;
- case 'p':
- output_format = posix_format;
- break;
- default:
- bb_show_usage();
- }
- }
- next:
- argv++;
- argc--;
- if (!argc)
- bb_show_usage();
- }
+ int opt;
+
+ /* "+": stop on first non-option */
+ opt = getopt32(argv, "+vp");
+ argv += optind;
+ if (opt & 1)
+ output_format = long_format;
+ if (opt & 2)
+ output_format = posix_format;
run_command(argv, &res);
/* Cheat. printf's are shorter :) */
/* (but see bb_putchar() body for additional wrinkle!) */
+ xdup2(2, 1); /* just in case libc does something silly :( */
stdout = stderr;
- dup2(2, 1); /* just in case libc does something silly :( */
summarize(output_format, argv, &res);
if (WIFSTOPPED(res.waitstatus))