2006-06-03 20:09:02 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2001-11-30 07:54:32 +00:00
|
|
|
/* `time' utility to display resource usage of processes.
|
|
|
|
Copyright (C) 1990, 91, 92, 93, 96 Free Software Foundation, Inc.
|
|
|
|
|
2006-09-22 02:52:41 +00:00
|
|
|
Licensed under GPL version 2, see file LICENSE in this tarball for details.
|
2006-06-03 20:09:02 +00:00
|
|
|
*/
|
2001-11-30 07:54:32 +00:00
|
|
|
/* Originally written by David Keppel <pardo@cs.washington.edu>.
|
2004-03-15 08:29:22 +00:00
|
|
|
Heavily modified by David MacKenzie <djm@gnu.ai.mit.edu>.
|
2001-11-30 07:54:32 +00:00
|
|
|
Heavily modified for busybox by Erik Andersen <andersen@codepoet.org>
|
2006-06-03 20:09:02 +00:00
|
|
|
*/
|
2001-11-30 07:54:32 +00:00
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2001-11-30 07:54:32 +00:00
|
|
|
|
|
|
|
#define TV_MSEC tv_usec / 1000
|
|
|
|
|
|
|
|
/* Information on the resources used by a child process. */
|
2006-06-03 20:09:02 +00:00
|
|
|
typedef struct {
|
|
|
|
int waitstatus;
|
|
|
|
struct rusage ru;
|
|
|
|
struct timeval start, elapsed; /* Wallclock time of process. */
|
2001-11-30 07:54:32 +00:00
|
|
|
} resource_t;
|
|
|
|
|
|
|
|
/* msec = milliseconds = 1/1,000 (1*10e-3) second.
|
|
|
|
usec = microseconds = 1/1,000,000 (1*10e-6) second. */
|
|
|
|
|
|
|
|
#ifndef TICKS_PER_SEC
|
|
|
|
#define TICKS_PER_SEC 100
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* The number of milliseconds in one `tick' used by the `rusage' structure. */
|
|
|
|
#define MSEC_PER_TICK (1000 / TICKS_PER_SEC)
|
|
|
|
|
|
|
|
/* Return the number of clock ticks that occur in M milliseconds. */
|
|
|
|
#define MSEC_TO_TICKS(m) ((m) / MSEC_PER_TICK)
|
|
|
|
|
|
|
|
#define UL unsigned long
|
|
|
|
|
|
|
|
static const char *const default_format = "real\t%E\nuser\t%u\nsys\t%T";
|
|
|
|
|
|
|
|
/* The output format for the -p option .*/
|
|
|
|
static const char *const posix_format = "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 *const long_format =
|
2006-06-03 20:09:02 +00:00
|
|
|
"\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"
|
2006-12-22 12:36:13 +00:00
|
|
|
"\tPage size (bytes): %Z\n"
|
|
|
|
"\tExit status: %x";
|
2001-11-30 07:54:32 +00:00
|
|
|
|
|
|
|
|
2006-12-22 12:36:13 +00:00
|
|
|
/* Wait for and fill in data on child process PID.
|
|
|
|
Return 0 on error, 1 if ok. */
|
2001-11-30 07:54:32 +00:00
|
|
|
|
|
|
|
/* pid_t is short on BSDI, so don't try to promote it. */
|
2006-06-03 20:09:02 +00:00
|
|
|
static int resuse_end(pid_t pid, resource_t * resp)
|
2001-11-30 07:54:32 +00:00
|
|
|
{
|
2006-06-03 20:09:02 +00:00
|
|
|
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)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
gettimeofday(&resp->elapsed, (struct timezone *) 0);
|
|
|
|
resp->elapsed.tv_sec -= resp->start.tv_sec;
|
|
|
|
if (resp->elapsed.tv_usec < resp->start.tv_usec) {
|
|
|
|
/* Manually carry a one from the seconds field. */
|
|
|
|
resp->elapsed.tv_usec += 1000000;
|
|
|
|
--resp->elapsed.tv_sec;
|
|
|
|
}
|
|
|
|
resp->elapsed.tv_usec -= resp->start.tv_usec;
|
|
|
|
|
|
|
|
resp->waitstatus = status;
|
|
|
|
|
|
|
|
return 1;
|
2001-11-30 07:54:32 +00:00
|
|
|
}
|
|
|
|
|
2006-12-22 13:43:19 +00:00
|
|
|
/* Print ARGV, with each entry in ARGV separated by FILLER. */
|
|
|
|
static void printargv(char *const *argv, const char *filler)
|
2001-11-30 07:54:32 +00:00
|
|
|
{
|
2006-12-22 13:43:19 +00:00
|
|
|
fputs(*argv, stdout);
|
|
|
|
while (*++argv) {
|
|
|
|
fputs(filler, stdout);
|
|
|
|
fputs(*argv, stdout);
|
2006-06-03 20:09:02 +00:00
|
|
|
}
|
2001-11-30 07:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
|
2006-06-03 20:09:02 +00:00
|
|
|
static unsigned long ptok(unsigned long pages)
|
2001-11-30 07:54:32 +00:00
|
|
|
{
|
2006-12-22 13:43:19 +00:00
|
|
|
static unsigned long ps;
|
2006-06-03 20:09:02 +00:00
|
|
|
unsigned long tmp;
|
|
|
|
|
|
|
|
/* Initialization. */
|
|
|
|
if (ps == 0)
|
2006-12-22 13:43:19 +00:00
|
|
|
ps = getpagesize();
|
2006-06-03 20:09:02 +00:00
|
|
|
|
|
|
|
/* Conversion. */
|
|
|
|
if (pages > (LONG_MAX / ps)) { /* Could overflow. */
|
|
|
|
tmp = pages / 1024; /* Smaller first, */
|
2006-12-22 13:43:19 +00:00
|
|
|
return tmp * ps; /* then larger. */
|
2006-06-03 20:09:02 +00:00
|
|
|
}
|
2006-12-22 13:43:19 +00:00
|
|
|
/* Could underflow. */
|
|
|
|
tmp = pages * ps; /* Larger first, */
|
|
|
|
return tmp / 1024; /* then smaller. */
|
2001-11-30 07:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* summarize: Report on the system use of a command.
|
|
|
|
|
2006-12-22 13:43:19 +00:00
|
|
|
Print the FMT argument except that `%' sequences
|
2001-11-30 07:54:32 +00:00
|
|
|
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. */
|
|
|
|
|
2006-12-22 13:43:19 +00:00
|
|
|
static void summarize(const char *fmt, char **command, resource_t * resp)
|
2001-11-30 07:54:32 +00:00
|
|
|
{
|
2006-06-03 20:09:02 +00:00
|
|
|
unsigned long r; /* Elapsed real milliseconds. */
|
|
|
|
unsigned long v; /* Elapsed virtual (CPU) milliseconds. */
|
|
|
|
|
|
|
|
if (WIFSTOPPED(resp->waitstatus))
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("Command stopped by signal %d\n",
|
2006-06-03 20:09:02 +00:00
|
|
|
WSTOPSIG(resp->waitstatus));
|
|
|
|
else if (WIFSIGNALED(resp->waitstatus))
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("Command terminated by signal %d\n",
|
2006-06-03 20:09:02 +00:00
|
|
|
WTERMSIG(resp->waitstatus));
|
|
|
|
else if (WIFEXITED(resp->waitstatus) && WEXITSTATUS(resp->waitstatus))
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("Command exited with non-zero status %d\n",
|
2006-06-03 20:09:02 +00:00
|
|
|
WEXITSTATUS(resp->waitstatus));
|
|
|
|
|
|
|
|
/* Convert all times to milliseconds. Occasionally, one of these values
|
|
|
|
comes out as zero. Dividing by zero causes problems, so we first
|
|
|
|
check the time value. If it is zero, then we take `evasive action'
|
|
|
|
instead of calculating a value. */
|
|
|
|
|
|
|
|
r = resp->elapsed.tv_sec * 1000 + resp->elapsed.tv_usec / 1000;
|
|
|
|
|
|
|
|
v = resp->ru.ru_utime.tv_sec * 1000 + resp->ru.ru_utime.TV_MSEC +
|
|
|
|
resp->ru.ru_stime.tv_sec * 1000 + resp->ru.ru_stime.TV_MSEC;
|
|
|
|
|
2006-12-22 13:43:19 +00:00
|
|
|
/* putchar() != putc(stdout) in glibc! */
|
|
|
|
|
2006-06-03 20:09:02 +00:00
|
|
|
while (*fmt) {
|
2006-12-22 13:43:19 +00:00
|
|
|
/* Handle leading literal part */
|
|
|
|
int n = strcspn(fmt, "%\\");
|
|
|
|
if (n) {
|
|
|
|
printf("%.*s", n, fmt);
|
|
|
|
fmt += n;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-06-03 20:09:02 +00:00
|
|
|
switch (*fmt) {
|
2006-12-22 13:43:19 +00:00
|
|
|
#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:
|
|
|
|
putc(*fmt, stdout);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2006-06-03 20:09:02 +00:00
|
|
|
case '%':
|
|
|
|
switch (*++fmt) {
|
2006-12-22 13:43:19 +00:00
|
|
|
#ifdef NOT_NEEDED_YET
|
|
|
|
/* Our format strings do not have these */
|
|
|
|
/* and we do not take format str from user */
|
|
|
|
default:
|
|
|
|
putc('%', stdout);
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case '%':
|
|
|
|
if (!*fmt) goto ret;
|
|
|
|
putc(*fmt, stdout);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
2006-12-22 13:43:19 +00:00
|
|
|
#endif
|
2006-06-03 20:09:02 +00:00
|
|
|
case 'C': /* The command that got timed. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printargv(command, " ");
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'D': /* Average unshared data size. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%lu",
|
2006-06-03 20:09:02 +00:00
|
|
|
MSEC_TO_TICKS(v) == 0 ? 0 :
|
|
|
|
ptok((UL) resp->ru.ru_idrss) / MSEC_TO_TICKS(v) +
|
|
|
|
ptok((UL) resp->ru.ru_isrss) / MSEC_TO_TICKS(v));
|
|
|
|
break;
|
|
|
|
case 'E': /* Elapsed real (wall clock) time. */
|
|
|
|
if (resp->elapsed.tv_sec >= 3600) /* One hour -> h:m:s. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ldh %ldm %02lds",
|
2006-06-03 20:09:02 +00:00
|
|
|
resp->elapsed.tv_sec / 3600,
|
|
|
|
(resp->elapsed.tv_sec % 3600) / 60,
|
|
|
|
resp->elapsed.tv_sec % 60);
|
|
|
|
else
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ldm %ld.%02lds", /* -> m:s. */
|
2006-06-03 20:09:02 +00:00
|
|
|
resp->elapsed.tv_sec / 60,
|
|
|
|
resp->elapsed.tv_sec % 60,
|
|
|
|
resp->elapsed.tv_usec / 10000);
|
|
|
|
break;
|
|
|
|
case 'F': /* Major page faults. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld", resp->ru.ru_majflt);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'I': /* Inputs. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld", resp->ru.ru_inblock);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'K': /* Average mem usage == data+stack+text. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%lu",
|
2006-06-03 20:09:02 +00:00
|
|
|
MSEC_TO_TICKS(v) == 0 ? 0 :
|
|
|
|
ptok((UL) resp->ru.ru_idrss) / MSEC_TO_TICKS(v) +
|
|
|
|
ptok((UL) resp->ru.ru_isrss) / MSEC_TO_TICKS(v) +
|
|
|
|
ptok((UL) resp->ru.ru_ixrss) / MSEC_TO_TICKS(v));
|
|
|
|
break;
|
|
|
|
case 'M': /* Maximum resident set size. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%lu", ptok((UL) resp->ru.ru_maxrss));
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'O': /* Outputs. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld", resp->ru.ru_oublock);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'P': /* Percent of CPU this job got. */
|
|
|
|
/* % cpu is (total cpu time)/(elapsed time). */
|
|
|
|
if (r > 0)
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%lu%%", (v * 100 / r));
|
2006-06-03 20:09:02 +00:00
|
|
|
else
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("?%%");
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'R': /* Minor page faults (reclaims). */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld", resp->ru.ru_minflt);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'S': /* System time. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld.%02ld",
|
2006-06-03 20:09:02 +00:00
|
|
|
resp->ru.ru_stime.tv_sec,
|
|
|
|
resp->ru.ru_stime.TV_MSEC / 10);
|
|
|
|
break;
|
|
|
|
case 'T': /* System time. */
|
|
|
|
if (resp->ru.ru_stime.tv_sec >= 3600) /* One hour -> h:m:s. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ldh %ldm %02lds",
|
2006-06-03 20:09:02 +00:00
|
|
|
resp->ru.ru_stime.tv_sec / 3600,
|
|
|
|
(resp->ru.ru_stime.tv_sec % 3600) / 60,
|
|
|
|
resp->ru.ru_stime.tv_sec % 60);
|
|
|
|
else
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ldm %ld.%02lds", /* -> m:s. */
|
2006-06-03 20:09:02 +00:00
|
|
|
resp->ru.ru_stime.tv_sec / 60,
|
|
|
|
resp->ru.ru_stime.tv_sec % 60,
|
|
|
|
resp->ru.ru_stime.tv_usec / 10000);
|
|
|
|
break;
|
|
|
|
case 'U': /* User time. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld.%02ld",
|
2006-06-03 20:09:02 +00:00
|
|
|
resp->ru.ru_utime.tv_sec,
|
|
|
|
resp->ru.ru_utime.TV_MSEC / 10);
|
|
|
|
break;
|
|
|
|
case 'u': /* User time. */
|
|
|
|
if (resp->ru.ru_utime.tv_sec >= 3600) /* One hour -> h:m:s. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ldh %ldm %02lds",
|
2006-06-03 20:09:02 +00:00
|
|
|
resp->ru.ru_utime.tv_sec / 3600,
|
|
|
|
(resp->ru.ru_utime.tv_sec % 3600) / 60,
|
|
|
|
resp->ru.ru_utime.tv_sec % 60);
|
|
|
|
else
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ldm %ld.%02lds", /* -> m:s. */
|
2006-06-03 20:09:02 +00:00
|
|
|
resp->ru.ru_utime.tv_sec / 60,
|
|
|
|
resp->ru.ru_utime.tv_sec % 60,
|
|
|
|
resp->ru.ru_utime.tv_usec / 10000);
|
|
|
|
break;
|
|
|
|
case 'W': /* Times swapped out. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld", resp->ru.ru_nswap);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'X': /* Average shared text size. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%lu",
|
2006-06-03 20:09:02 +00:00
|
|
|
MSEC_TO_TICKS(v) == 0 ? 0 :
|
|
|
|
ptok((UL) resp->ru.ru_ixrss) / MSEC_TO_TICKS(v));
|
|
|
|
break;
|
|
|
|
case 'Z': /* Page size. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%d", getpagesize());
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'c': /* Involuntary context switches. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld", resp->ru.ru_nivcsw);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'e': /* Elapsed real time in seconds. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld.%02ld",
|
2006-06-03 20:09:02 +00:00
|
|
|
resp->elapsed.tv_sec, resp->elapsed.tv_usec / 10000);
|
|
|
|
break;
|
|
|
|
case 'k': /* Signals delivered. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld", resp->ru.ru_nsignals);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'p': /* Average stack segment. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%lu",
|
2006-06-03 20:09:02 +00:00
|
|
|
MSEC_TO_TICKS(v) == 0 ? 0 :
|
|
|
|
ptok((UL) resp->ru.ru_isrss) / MSEC_TO_TICKS(v));
|
|
|
|
break;
|
|
|
|
case 'r': /* Incoming socket messages received. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld", resp->ru.ru_msgrcv);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 's': /* Outgoing socket messages sent. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld", resp->ru.ru_msgsnd);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 't': /* Average resident set size. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%lu",
|
2006-06-03 20:09:02 +00:00
|
|
|
MSEC_TO_TICKS(v) == 0 ? 0 :
|
|
|
|
ptok((UL) resp->ru.ru_idrss) / MSEC_TO_TICKS(v));
|
|
|
|
break;
|
|
|
|
case 'w': /* Voluntary context switches. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%ld", resp->ru.ru_nvcsw);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'x': /* Exit status. */
|
2006-12-22 13:43:19 +00:00
|
|
|
printf("%d", WEXITSTATUS(resp->waitstatus));
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-11-30 07:54:32 +00:00
|
|
|
break;
|
2006-06-03 20:09:02 +00:00
|
|
|
|
2006-12-22 13:43:19 +00:00
|
|
|
#ifdef NOT_NEEDED_YET
|
2006-06-03 20:09:02 +00:00
|
|
|
case '\\': /* Format escape. */
|
|
|
|
switch (*++fmt) {
|
2006-12-22 13:43:19 +00:00
|
|
|
default:
|
|
|
|
putc('\\', stdout);
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case '\\':
|
|
|
|
if (!*fmt) goto ret;
|
|
|
|
putc(*fmt, stdout);
|
|
|
|
break;
|
2006-06-03 20:09:02 +00:00
|
|
|
case 't':
|
2006-12-22 13:43:19 +00:00
|
|
|
putc('\t', stdout);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2006-12-22 13:43:19 +00:00
|
|
|
putc('\n', stdout);
|
2006-06-03 20:09:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-11-30 07:54:32 +00:00
|
|
|
break;
|
2006-12-22 13:43:19 +00:00
|
|
|
#endif
|
2001-11-30 07:54:32 +00:00
|
|
|
}
|
2006-12-22 13:43:19 +00:00
|
|
|
++fmt;
|
2001-11-30 07:54:32 +00:00
|
|
|
}
|
2006-12-22 13:43:19 +00:00
|
|
|
/* ret: */
|
|
|
|
putc('\n', stdout);
|
2001-11-30 07:54:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Run command CMD and return statistics on it.
|
|
|
|
Put the statistics in *RESP. */
|
2006-06-03 20:09:02 +00:00
|
|
|
static void run_command(char *const *cmd, resource_t * resp)
|
2001-11-30 07:54:32 +00:00
|
|
|
{
|
2006-06-03 20:09:02 +00:00
|
|
|
pid_t pid; /* Pid of child. */
|
|
|
|
__sighandler_t interrupt_signal, quit_signal;
|
|
|
|
|
|
|
|
gettimeofday(&resp->start, (struct timezone *) 0);
|
2006-06-06 06:08:34 +00:00
|
|
|
pid = vfork(); /* Run CMD as child process. */
|
2006-06-03 20:09:02 +00:00
|
|
|
if (pid < 0)
|
|
|
|
bb_error_msg_and_die("cannot fork");
|
|
|
|
else 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. */
|
2007-02-06 01:20:12 +00:00
|
|
|
BB_EXECVP(cmd[0], cmd);
|
2006-06-03 20:09:02 +00:00
|
|
|
bb_error_msg("cannot run %s", cmd[0]);
|
|
|
|
_exit(errno == ENOENT ? 127 : 126);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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");
|
|
|
|
|
|
|
|
/* Re-enable signals. */
|
|
|
|
signal(SIGINT, interrupt_signal);
|
|
|
|
signal(SIGQUIT, quit_signal);
|
2001-11-30 07:54:32 +00:00
|
|
|
}
|
|
|
|
|
2007-02-03 17:28:39 +00:00
|
|
|
int time_main(int argc, char **argv);
|
2006-06-03 20:09:02 +00:00
|
|
|
int time_main(int argc, char **argv)
|
2001-11-30 07:54:32 +00:00
|
|
|
{
|
2006-06-03 20:09:02 +00:00
|
|
|
resource_t res;
|
|
|
|
const char *output_format = default_format;
|
2006-12-22 12:36:13 +00:00
|
|
|
char c;
|
2006-06-03 20:09:02 +00:00
|
|
|
|
2006-12-22 12:36:13 +00:00
|
|
|
goto next;
|
2006-06-03 20:09:02 +00:00
|
|
|
/* Parse any options -- don't use getopt() here so we don't
|
|
|
|
* consume the args of our client application... */
|
2006-12-22 12:36:13 +00:00
|
|
|
while (argc > 0 && argv[0][0] == '-') {
|
|
|
|
while ((c = *++*argv)) {
|
|
|
|
switch (c) {
|
2006-06-03 20:09:02 +00:00
|
|
|
case 'v':
|
|
|
|
output_format = long_format;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
output_format = posix_format;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
}
|
2006-12-22 12:36:13 +00:00
|
|
|
next:
|
|
|
|
argv++;
|
|
|
|
argc--;
|
|
|
|
if (!argc)
|
|
|
|
bb_show_usage();
|
2001-11-30 07:54:32 +00:00
|
|
|
}
|
2006-06-03 20:09:02 +00:00
|
|
|
|
|
|
|
run_command(argv, &res);
|
2006-12-22 13:43:19 +00:00
|
|
|
|
|
|
|
/* Cheat. printf's are shorter :) */
|
|
|
|
stdout = stderr;
|
|
|
|
dup2(2, 1); /* just in case libc does something silly :( */
|
|
|
|
summarize(output_format, argv, &res);
|
2006-06-03 20:09:02 +00:00
|
|
|
|
|
|
|
if (WIFSTOPPED(res.waitstatus))
|
2006-12-22 12:36:13 +00:00
|
|
|
return WSTOPSIG(res.waitstatus);
|
|
|
|
if (WIFSIGNALED(res.waitstatus))
|
|
|
|
return WTERMSIG(res.waitstatus);
|
|
|
|
if (WIFEXITED(res.waitstatus))
|
|
|
|
return WEXITSTATUS(res.waitstatus);
|
2006-12-22 13:43:19 +00:00
|
|
|
fflush_stdout_and_exit(0);
|
2001-11-30 07:54:32 +00:00
|
|
|
}
|