/* 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.
/* 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 =
"\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.
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(const unsigned pagesize, const 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.
#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))
+ (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;
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;
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);
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());
+ printf("%u", pagesize);
break;
case 'c': /* Involuntary context switches. */
printf("%lu", resp->ru.ru_nivcsw);
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);
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);
resp->elapsed_ms = monotonic_us() / 1000;
pid = vfork(); /* Run CMD as child process. */
if (pid < 0)
- bb_error_msg_and_die("cannot fork");
+ bb_perror_msg_and_die("fork");
if (pid == 0) { /* If child. */
/* Don't cast execvp arguments; that causes errors on some systems,
versus merely warnings if the cast is left off. */
BB_EXECVP(cmd[0], cmd);
xfunc_error_retval = (errno == ENOENT ? 127 : 126);
- bb_error_msg_and_die("cannot run %s", cmd[0]);
+ bb_error_msg_and_die("can't run %s", cmd[0]);
}
/* Have signals kill the child but not self (if possible). */
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);
}
int time_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int time_main(int argc, char **argv)
+int time_main(int argc UNUSED_PARAM, 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;
+
+ opt_complementary = "-1"; /* at least one arg */
+ /* "+": 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))
return WTERMSIG(res.waitstatus);
if (WIFEXITED(res.waitstatus))
return WEXITSTATUS(res.waitstatus);
- fflush_stdout_and_exit(0);
+ fflush_stdout_and_exit(EXIT_SUCCESS);
}