2006-06-04 01:39:02 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2017-10-05 18:10:24 +05:30
|
|
|
/*
|
|
|
|
* '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.
|
|
|
|
*/
|
2001-11-30 13:24:32 +05:30
|
|
|
/* Originally written by David Keppel <pardo@cs.washington.edu>.
|
2017-10-05 18:10:24 +05:30
|
|
|
* Heavily modified by David MacKenzie <djm@gnu.ai.mit.edu>.
|
|
|
|
* Heavily modified for busybox by Erik Andersen <andersen@codepoet.org>
|
|
|
|
*/
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:config TIME
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "time (6.8 kb)"
|
2016-11-23 03:44:24 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//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.
|
2001-11-30 13:24:32 +05:30
|
|
|
|
2016-11-23 04:24:17 +05:30
|
|
|
//applet:IF_TIME(APPLET(time, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_TIME) += time.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define time_trivial_usage
|
2017-04-28 21:24:14 +05:30
|
|
|
//usage: "[-vpa] [-o FILE] PROG ARGS"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define time_full_usage "\n\n"
|
|
|
|
//usage: "Run PROG, display resource usage when it exits\n"
|
|
|
|
//usage: "\n -v Verbose"
|
2017-04-24 21:38:53 +05:30
|
|
|
//usage: "\n -p POSIX output format"
|
2017-04-28 21:31:18 +05:30
|
|
|
//usage: "\n -f FMT Custom format"
|
2017-04-28 21:24:14 +05:30
|
|
|
//usage: "\n -o FILE Write result to FILE"
|
|
|
|
//usage: "\n -a Append (else overwrite)"
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2001-11-30 13:24:32 +05:30
|
|
|
|
2021-04-15 01:44:36 +05:30
|
|
|
#ifndef HAVE_WAIT3
|
|
|
|
static pid_t wait3(int *status, int options, struct rusage *rusage)
|
|
|
|
{
|
|
|
|
return wait4(-1, status, options, rusage);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-11-30 13:24:32 +05:30
|
|
|
/* Information on the resources used by a child process. */
|
2006-06-04 01:39:02 +05:30
|
|
|
typedef struct {
|
|
|
|
int waitstatus;
|
|
|
|
struct rusage ru;
|
2007-06-18 00:39:05 +05:30
|
|
|
unsigned elapsed_ms; /* Wallclock time of process. */
|
2001-11-30 13:24:32 +05:30
|
|
|
} 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
|
|
|
|
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char default_format[] ALIGN1 = "real\t%E\nuser\t%u\nsys\t%T";
|
2001-11-30 13:24:32 +05:30
|
|
|
|
|
|
|
/* The output format for the -p option .*/
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char posix_format[] ALIGN1 = "real %e\nuser %U\nsys %S";
|
2001-11-30 13:24:32 +05:30
|
|
|
|
|
|
|
/* Format string for printing all statistics verbosely.
|
|
|
|
Keep this output to 24 lines so users on terminals can see it all.*/
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char long_format[] ALIGN1 =
|
2006-06-04 01:39:02 +05:30
|
|
|
"\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 18:06:13 +05:30
|
|
|
"\tPage size (bytes): %Z\n"
|
|
|
|
"\tExit status: %x";
|
2001-11-30 13:24:32 +05:30
|
|
|
|
2006-12-22 18:06:13 +05:30
|
|
|
/* Wait for and fill in data on child process PID.
|
|
|
|
Return 0 on error, 1 if ok. */
|
2001-11-30 13:24:32 +05:30
|
|
|
/* pid_t is short on BSDI, so don't try to promote it. */
|
2008-03-17 14:59:43 +05:30
|
|
|
static void resuse_end(pid_t pid, resource_t *resp)
|
2001-11-30 13:24:32 +05:30
|
|
|
{
|
2006-06-04 01:39:02 +05:30
|
|
|
pid_t caught;
|
|
|
|
|
|
|
|
/* Ignore signals, but don't ignore the children. When wait3
|
2013-01-15 18:28:01 +05:30
|
|
|
* returns the child process, set the time the command finished. */
|
2008-03-17 14:59:43 +05:30
|
|
|
while ((caught = wait3(&resp->waitstatus, 0, &resp->ru)) != pid) {
|
|
|
|
if (caught == -1 && errno != EINTR) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg("wait");
|
2008-03-17 14:59:43 +05:30
|
|
|
return;
|
|
|
|
}
|
2006-06-04 01:39:02 +05:30
|
|
|
}
|
2010-01-12 17:22:30 +05:30
|
|
|
resp->elapsed_ms = monotonic_ms() - resp->elapsed_ms;
|
2001-11-30 13:24:32 +05:30
|
|
|
}
|
|
|
|
|
2008-03-17 14:59:43 +05:30
|
|
|
static void printargv(char *const *argv)
|
2001-11-30 13:24:32 +05:30
|
|
|
{
|
2008-03-17 14:59:43 +05:30
|
|
|
const char *fmt = " %s" + 1;
|
|
|
|
do {
|
|
|
|
printf(fmt, *argv);
|
|
|
|
fmt = " %s";
|
|
|
|
} while (*++argv);
|
2001-11-30 13:24:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
2020-12-14 23:19:23 +05:30
|
|
|
#ifdef BB_ARCH_FIXED_PAGESIZE
|
|
|
|
# define pagesize BB_ARCH_FIXED_PAGESIZE
|
|
|
|
# define ptok(pagesize, pages) ptok(pages)
|
|
|
|
#endif
|
2009-02-23 22:21:25 +05:30
|
|
|
static unsigned long ptok(const unsigned pagesize, const unsigned long pages)
|
2001-11-30 13:24:32 +05:30
|
|
|
{
|
2006-06-04 01:39:02 +05:30
|
|
|
unsigned long tmp;
|
|
|
|
|
|
|
|
/* Conversion. */
|
2008-03-17 14:59:43 +05:30
|
|
|
if (pages > (LONG_MAX / pagesize)) { /* Could overflow. */
|
|
|
|
tmp = pages / 1024; /* Smaller first, */
|
|
|
|
return tmp * pagesize; /* then larger. */
|
2006-06-04 01:39:02 +05:30
|
|
|
}
|
2006-12-22 19:13:19 +05:30
|
|
|
/* Could underflow. */
|
2008-03-17 14:59:43 +05:30
|
|
|
tmp = pages * pagesize; /* Larger first, */
|
|
|
|
return tmp / 1024; /* then smaller. */
|
2001-11-30 13:24:32 +05:30
|
|
|
}
|
2020-12-14 23:19:23 +05:30
|
|
|
#undef pagesize
|
2001-11-30 13:24:32 +05:30
|
|
|
|
|
|
|
/* summarize: Report on the system use of a command.
|
|
|
|
|
2017-08-02 17:56:33 +05:30
|
|
|
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 '\'.
|
2001-11-30 13:24:32 +05:30
|
|
|
|
2017-08-02 17:56:33 +05:30
|
|
|
The character following a '%' can be:
|
2001-11-30 13:24:32 +05:30
|
|
|
(* means the tcsh time builtin also recognizes it)
|
2017-08-02 17:56:33 +05:30
|
|
|
% == a literal '%'
|
2001-11-30 13:24:32 +05:30
|
|
|
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. */
|
|
|
|
|
2007-06-18 00:39:05 +05:30
|
|
|
#ifndef TICKS_PER_SEC
|
|
|
|
#define TICKS_PER_SEC 100
|
|
|
|
#endif
|
|
|
|
|
2008-03-17 14:59:43 +05:30
|
|
|
static void summarize(const char *fmt, char **command, resource_t *resp)
|
2001-11-30 13:24:32 +05:30
|
|
|
{
|
2007-06-18 00:39:05 +05:30
|
|
|
unsigned vv_ms; /* Elapsed virtual (CPU) milliseconds */
|
|
|
|
unsigned cpu_ticks; /* Same, in "CPU ticks" */
|
2020-12-14 23:19:23 +05:30
|
|
|
unsigned pagesize = bb_getpagesize();
|
2006-06-04 01:39:02 +05:30
|
|
|
|
2008-03-17 14:59:43 +05:30
|
|
|
/* Impossible: we do not use WUNTRACED flag in wait()...
|
2006-06-04 01:39:02 +05:30
|
|
|
if (WIFSTOPPED(resp->waitstatus))
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("Command stopped by signal %u\n",
|
2006-06-04 01:39:02 +05:30
|
|
|
WSTOPSIG(resp->waitstatus));
|
2008-03-17 14:59:43 +05:30
|
|
|
else */
|
|
|
|
if (WIFSIGNALED(resp->waitstatus))
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("Command terminated by signal %u\n",
|
2006-06-04 01:39:02 +05:30
|
|
|
WTERMSIG(resp->waitstatus));
|
|
|
|
else if (WIFEXITED(resp->waitstatus) && WEXITSTATUS(resp->waitstatus))
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("Command exited with non-zero status %u\n",
|
2006-06-04 01:39:02 +05:30
|
|
|
WEXITSTATUS(resp->waitstatus));
|
|
|
|
|
2007-06-18 00:39:05 +05:30
|
|
|
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;
|
2006-06-04 01:39:02 +05:30
|
|
|
|
2007-06-18 05:10:26 +05:30
|
|
|
#if (1000 / TICKS_PER_SEC) * TICKS_PER_SEC == 1000
|
2008-03-17 14:59:43 +05:30
|
|
|
/* 1000 is exactly divisible by TICKS_PER_SEC (typical) */
|
2007-06-18 05:10:26 +05:30
|
|
|
cpu_ticks = vv_ms / (1000 / TICKS_PER_SEC);
|
|
|
|
#else
|
|
|
|
cpu_ticks = vv_ms * (unsigned long long)TICKS_PER_SEC / 1000;
|
|
|
|
#endif
|
2007-06-18 00:39:05 +05:30
|
|
|
if (!cpu_ticks) cpu_ticks = 1; /* we divide by it, must be nonzero */
|
2006-06-04 01:39:02 +05:30
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-06-04 01:39:02 +05:30
|
|
|
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:
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar(*fmt);
|
2006-12-22 19:13:19 +05:30
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2006-06-04 01:39:02 +05:30
|
|
|
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:
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('%');
|
2006-12-22 19:13:19 +05:30
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case '%':
|
|
|
|
if (!*fmt) goto ret;
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar(*fmt);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
2006-12-22 19:13:19 +05:30
|
|
|
#endif
|
2006-06-04 01:39:02 +05:30
|
|
|
case 'C': /* The command that got timed. */
|
2008-03-17 14:59:43 +05:30
|
|
|
printargv(command);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'D': /* Average unshared data size. */
|
2006-12-22 19:13:19 +05:30
|
|
|
printf("%lu",
|
2008-03-17 14:59:43 +05:30
|
|
|
(ptok(pagesize, (UL) resp->ru.ru_idrss) +
|
|
|
|
ptok(pagesize, (UL) resp->ru.ru_isrss)) / cpu_ticks);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
2007-06-18 00:39:05 +05:30
|
|
|
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);
|
2006-06-04 01:39:02 +05:30
|
|
|
else
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%um %u.%02us", /* -> m:s. */
|
|
|
|
seconds / 60,
|
|
|
|
seconds % 60,
|
|
|
|
(unsigned)(resp->elapsed_ms / 10) % 100);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
2007-06-18 00:39:05 +05:30
|
|
|
}
|
2006-06-04 01:39:02 +05:30
|
|
|
case 'F': /* Major page faults. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%lu", resp->ru.ru_majflt);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'I': /* Inputs. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%lu", resp->ru.ru_inblock);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'K': /* Average mem usage == data+stack+text. */
|
2006-12-22 19:13:19 +05:30
|
|
|
printf("%lu",
|
2008-03-17 14:59:43 +05:30
|
|
|
(ptok(pagesize, (UL) resp->ru.ru_idrss) +
|
|
|
|
ptok(pagesize, (UL) resp->ru.ru_isrss) +
|
|
|
|
ptok(pagesize, (UL) resp->ru.ru_ixrss)) / cpu_ticks);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'M': /* Maximum resident set size. */
|
2008-03-17 14:59:43 +05:30
|
|
|
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_maxrss));
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'O': /* Outputs. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%lu", resp->ru.ru_oublock);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'P': /* Percent of CPU this job got. */
|
|
|
|
/* % cpu is (total cpu time)/(elapsed time). */
|
2007-06-18 00:39:05 +05:30
|
|
|
if (resp->elapsed_ms > 0)
|
|
|
|
printf("%u%%", (unsigned)(vv_ms * 100 / resp->elapsed_ms));
|
2006-06-04 01:39:02 +05:30
|
|
|
else
|
2006-12-22 19:13:19 +05:30
|
|
|
printf("?%%");
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'R': /* Minor page faults (reclaims). */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%lu", resp->ru.ru_minflt);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'S': /* System time. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%u.%02u",
|
|
|
|
(unsigned)resp->ru.ru_stime.tv_sec,
|
|
|
|
(unsigned)(resp->ru.ru_stime.tv_usec / 10000));
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'T': /* System time. */
|
|
|
|
if (resp->ru.ru_stime.tv_sec >= 3600) /* One hour -> h:m:s. */
|
2007-06-18 00:39:05 +05:30
|
|
|
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));
|
2006-06-04 01:39:02 +05:30
|
|
|
else
|
2007-06-18 00:39:05 +05:30
|
|
|
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));
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'U': /* User time. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%u.%02u",
|
|
|
|
(unsigned)resp->ru.ru_utime.tv_sec,
|
|
|
|
(unsigned)(resp->ru.ru_utime.tv_usec / 10000));
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'u': /* User time. */
|
|
|
|
if (resp->ru.ru_utime.tv_sec >= 3600) /* One hour -> h:m:s. */
|
2007-06-18 00:39:05 +05:30
|
|
|
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));
|
2006-06-04 01:39:02 +05:30
|
|
|
else
|
2007-06-18 00:39:05 +05:30
|
|
|
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));
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'W': /* Times swapped out. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%lu", resp->ru.ru_nswap);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'X': /* Average shared text size. */
|
2008-03-17 14:59:43 +05:30
|
|
|
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_ixrss) / cpu_ticks);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'Z': /* Page size. */
|
2009-02-23 22:21:25 +05:30
|
|
|
printf("%u", pagesize);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'c': /* Involuntary context switches. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%lu", resp->ru.ru_nivcsw);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'e': /* Elapsed real time in seconds. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%u.%02u",
|
|
|
|
(unsigned)resp->elapsed_ms / 1000,
|
|
|
|
(unsigned)(resp->elapsed_ms / 10) % 100);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'k': /* Signals delivered. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%lu", resp->ru.ru_nsignals);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'p': /* Average stack segment. */
|
2008-03-17 14:59:43 +05:30
|
|
|
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_isrss) / cpu_ticks);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'r': /* Incoming socket messages received. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%lu", resp->ru.ru_msgrcv);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 's': /* Outgoing socket messages sent. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%lu", resp->ru.ru_msgsnd);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 't': /* Average resident set size. */
|
2008-03-17 14:59:43 +05:30
|
|
|
printf("%lu", ptok(pagesize, (UL) resp->ru.ru_idrss) / cpu_ticks);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'w': /* Voluntary context switches. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%lu", resp->ru.ru_nvcsw);
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'x': /* Exit status. */
|
2007-06-18 00:39:05 +05:30
|
|
|
printf("%u", WEXITSTATUS(resp->waitstatus));
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
}
|
2001-11-30 13:24:32 +05:30
|
|
|
break;
|
2006-06-04 01:39:02 +05:30
|
|
|
|
2006-12-22 19:13:19 +05:30
|
|
|
#ifdef NOT_NEEDED_YET
|
2006-06-04 01:39:02 +05:30
|
|
|
case '\\': /* Format escape. */
|
|
|
|
switch (*++fmt) {
|
2006-12-22 19:13:19 +05:30
|
|
|
default:
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\\');
|
2006-12-22 19:13:19 +05:30
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case '\\':
|
|
|
|
if (!*fmt) goto ret;
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar(*fmt);
|
2006-12-22 19:13:19 +05:30
|
|
|
break;
|
2006-06-04 01:39:02 +05:30
|
|
|
case 't':
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\t');
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
case 'n':
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\n');
|
2006-06-04 01:39:02 +05:30
|
|
|
break;
|
|
|
|
}
|
2001-11-30 13:24:32 +05:30
|
|
|
break;
|
2006-12-22 19:13:19 +05:30
|
|
|
#endif
|
2001-11-30 13:24:32 +05:30
|
|
|
}
|
2006-12-22 19:13:19 +05:30
|
|
|
++fmt;
|
2001-11-30 13:24:32 +05:30
|
|
|
}
|
2006-12-22 19:13:19 +05:30
|
|
|
/* ret: */
|
2007-09-27 15:50:47 +05:30
|
|
|
bb_putchar('\n');
|
2001-11-30 13:24:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Run command CMD and return statistics on it.
|
|
|
|
Put the statistics in *RESP. */
|
2008-02-17 04:28:56 +05:30
|
|
|
static void run_command(char *const *cmd, resource_t *resp)
|
2001-11-30 13:24:32 +05:30
|
|
|
{
|
2010-07-04 04:27:03 +05:30
|
|
|
pid_t pid;
|
2008-02-17 04:28:56 +05:30
|
|
|
void (*interrupt_signal)(int);
|
|
|
|
void (*quit_signal)(int);
|
2006-06-04 01:39:02 +05:30
|
|
|
|
2010-01-12 17:22:30 +05:30
|
|
|
resp->elapsed_ms = monotonic_ms();
|
2010-07-04 19:02:38 +05:30
|
|
|
pid = xvfork();
|
2010-07-04 04:27:03 +05:30
|
|
|
if (pid == 0) {
|
|
|
|
/* Child */
|
|
|
|
BB_EXECVP_or_die((char**)cmd);
|
2006-06-04 01:39:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Have signals kill the child but not self (if possible). */
|
2017-04-17 19:43:32 +05:30
|
|
|
//TODO: just block all sigs? and re-enable them in the very end in main?
|
2006-06-04 01:39:02 +05:30
|
|
|
interrupt_signal = signal(SIGINT, SIG_IGN);
|
|
|
|
quit_signal = signal(SIGQUIT, SIG_IGN);
|
|
|
|
|
2008-03-17 14:59:43 +05:30
|
|
|
resuse_end(pid, resp);
|
2006-06-04 01:39:02 +05:30
|
|
|
|
|
|
|
/* Re-enable signals. */
|
|
|
|
signal(SIGINT, interrupt_signal);
|
|
|
|
signal(SIGQUIT, quit_signal);
|
2001-11-30 13:24:32 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
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)
|
2001-11-30 13:24:32 +05:30
|
|
|
{
|
2006-06-04 01:39:02 +05:30
|
|
|
resource_t res;
|
2017-04-28 21:31:18 +05:30
|
|
|
/* $TIME has lowest prio (-v,-p,-f FMT overrride it) */
|
|
|
|
const char *output_format = getenv("TIME") ? : default_format;
|
2017-04-28 21:24:14 +05:30
|
|
|
char *output_filename;
|
|
|
|
int output_fd;
|
2008-03-17 14:59:43 +05:30
|
|
|
int opt;
|
2017-04-28 21:24:14 +05:30
|
|
|
int ex;
|
|
|
|
enum {
|
|
|
|
OPT_v = (1 << 0),
|
|
|
|
OPT_p = (1 << 1),
|
|
|
|
OPT_a = (1 << 2),
|
|
|
|
OPT_o = (1 << 3),
|
2017-04-28 21:31:18 +05:30
|
|
|
OPT_f = (1 << 4),
|
2017-04-28 21:24:14 +05:30
|
|
|
};
|
2008-03-17 14:59:43 +05:30
|
|
|
|
|
|
|
/* "+": stop on first non-option */
|
2017-08-09 01:25:02 +05:30
|
|
|
opt = getopt32(argv, "^+" "vpao:f:" "\0" "-1"/*at least one arg*/,
|
|
|
|
&output_filename, &output_format
|
|
|
|
);
|
2008-03-17 14:59:43 +05:30
|
|
|
argv += optind;
|
2017-04-28 21:24:14 +05:30
|
|
|
if (opt & OPT_v)
|
2008-03-17 14:59:43 +05:30
|
|
|
output_format = long_format;
|
2017-04-28 21:24:14 +05:30
|
|
|
if (opt & OPT_p)
|
2008-03-17 14:59:43 +05:30
|
|
|
output_format = posix_format;
|
2017-04-28 21:24:14 +05:30
|
|
|
output_fd = STDERR_FILENO;
|
|
|
|
if (opt & OPT_o) {
|
2017-10-27 22:35:00 +05:30
|
|
|
#ifndef O_CLOEXEC
|
|
|
|
# define O_CLOEXEC 0
|
|
|
|
#endif
|
2017-04-28 21:24:14 +05:30
|
|
|
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)
|
|
|
|
);
|
2017-10-27 22:35:00 +05:30
|
|
|
if (!O_CLOEXEC)
|
|
|
|
close_on_exec_on(output_fd);
|
2017-04-28 21:24:14 +05:30
|
|
|
}
|
2006-06-04 01:39:02 +05:30
|
|
|
|
|
|
|
run_command(argv, &res);
|
2006-12-22 19:13:19 +05:30
|
|
|
|
|
|
|
/* Cheat. printf's are shorter :) */
|
2017-04-28 21:24:14 +05:30
|
|
|
xdup2(output_fd, STDOUT_FILENO);
|
2006-12-22 19:13:19 +05:30
|
|
|
summarize(output_format, argv, &res);
|
2006-06-04 01:39:02 +05:30
|
|
|
|
2017-04-28 21:24:14 +05:30
|
|
|
ex = WEXITSTATUS(res.waitstatus);
|
|
|
|
/* Impossible: we do not use WUNTRACED flag in wait()...
|
2006-06-04 01:39:02 +05:30
|
|
|
if (WIFSTOPPED(res.waitstatus))
|
2017-04-28 21:24:14 +05:30
|
|
|
ex = WSTOPSIG(res.waitstatus);
|
|
|
|
*/
|
2006-12-22 18:06:13 +05:30
|
|
|
if (WIFSIGNALED(res.waitstatus))
|
2017-04-28 21:24:14 +05:30
|
|
|
ex = WTERMSIG(res.waitstatus);
|
|
|
|
|
|
|
|
fflush_stdout_and_exit(ex);
|
2001-11-30 13:24:32 +05:30
|
|
|
}
|