Add -p option to time(1).
This commit is contained in:
parent
cbf16b4891
commit
c3a3ff86ec
54
utils/time.c
54
utils/time.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2013, 2015 Jonas 'Sortie' Termansen.
|
* Copyright (c) 2013, 2015, 2022 Jonas 'Sortie' Termansen.
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
@ -20,8 +20,8 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
#include <err.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <error.h>
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -30,23 +30,39 @@
|
||||||
#include <timespec.h>
|
#include <timespec.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
static bool p;
|
||||||
static pid_t child_pid;
|
static pid_t child_pid;
|
||||||
static struct timespec start_time, end_time;
|
static struct timespec start_time, end_time;
|
||||||
|
|
||||||
void statistics(void)
|
void statistics(void)
|
||||||
{
|
{
|
||||||
clock_gettime(CLOCK_MONOTONIC, &end_time);
|
clock_gettime(CLOCK_MONOTONIC, &end_time);
|
||||||
struct tmns tmns;
|
struct tmns ts;
|
||||||
timens(&tmns);
|
timens(&ts);
|
||||||
struct timespec real_time = timespec_sub(end_time, start_time);
|
struct timespec real_time = timespec_sub(end_time, start_time);
|
||||||
struct timespec execute_time = timespec_sub(tmns.tmns_cutime, tmns.tmns_utime);
|
struct timespec execute_time =timespec_sub(ts.tmns_cutime, ts.tmns_utime);
|
||||||
struct timespec system_time = timespec_sub(tmns.tmns_cstime, tmns.tmns_stime);
|
struct timespec system_time = timespec_sub(ts.tmns_cstime, ts.tmns_stime);
|
||||||
struct timespec user_time = timespec_sub(execute_time, system_time);
|
struct timespec user_time = timespec_sub(execute_time, system_time);
|
||||||
|
|
||||||
fprintf(stderr, "\n");
|
if ( p )
|
||||||
fprintf(stderr, "real\t%lim%li.%03li\n", (long) real_time.tv_sec / 60, (long) real_time.tv_sec % 60, real_time.tv_nsec / 1000000L);
|
{
|
||||||
fprintf(stderr, "user\t%lim%li.%03li\n", (long) user_time.tv_sec / 60, (long) user_time.tv_sec % 60, user_time.tv_nsec / 1000000);
|
fprintf(stderr, "real %li.%03li\n",
|
||||||
fprintf(stderr, "sys\t%lim%li.%03li\n", (long) system_time.tv_sec / 60, (long) system_time.tv_sec % 60, system_time.tv_nsec / 1000000);
|
(long) real_time.tv_sec, real_time.tv_nsec / 1000000L);
|
||||||
|
fprintf(stderr, "user %li.%03li\n",
|
||||||
|
(long) user_time.tv_sec, user_time.tv_nsec / 1000000);
|
||||||
|
fprintf(stderr, "sys %li.%03li\n",
|
||||||
|
(long) system_time.tv_sec, system_time.tv_nsec / 1000000);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
fprintf(stderr, "real\t%lim%li.%03li\n", (long) real_time.tv_sec / 60,
|
||||||
|
(long) real_time.tv_sec % 60, real_time.tv_nsec / 1000000L);
|
||||||
|
fprintf(stderr, "user\t%lim%li.%03li\n", (long) user_time.tv_sec / 60,
|
||||||
|
(long) user_time.tv_sec % 60, user_time.tv_nsec / 1000000);
|
||||||
|
fprintf(stderr, "sys\t%lim%li.%03li\n", (long) system_time.tv_sec / 60,
|
||||||
|
(long) system_time.tv_sec % 60, system_time.tv_nsec / 1000000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void signal_handler(int signum)
|
void signal_handler(int signum)
|
||||||
|
@ -60,6 +76,16 @@ void signal_handler(int signum)
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
int opt;
|
||||||
|
while ( (opt = getopt(argc, argv, "p")) != -1 )
|
||||||
|
{
|
||||||
|
switch ( opt )
|
||||||
|
{
|
||||||
|
case 'p': p = true; break;
|
||||||
|
default: return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
clock_gettime(CLOCK_MONOTONIC, &start_time);
|
clock_gettime(CLOCK_MONOTONIC, &start_time);
|
||||||
sigset_t handleset, oldset;
|
sigset_t handleset, oldset;
|
||||||
sigemptyset(&handleset);
|
sigemptyset(&handleset);
|
||||||
|
@ -67,14 +93,14 @@ int main(int argc, char* argv[])
|
||||||
sigaddset(&handleset, SIGQUIT);
|
sigaddset(&handleset, SIGQUIT);
|
||||||
sigprocmask(SIG_BLOCK, &handleset, &oldset);
|
sigprocmask(SIG_BLOCK, &handleset, &oldset);
|
||||||
if ( (child_pid = fork()) < 0 )
|
if ( (child_pid = fork()) < 0 )
|
||||||
error(1, errno, "fork");
|
err(1, "fork");
|
||||||
if ( !child_pid )
|
if ( !child_pid )
|
||||||
{
|
{
|
||||||
sigprocmask(SIG_SETMASK, &oldset, NULL);
|
sigprocmask(SIG_SETMASK, &oldset, NULL);
|
||||||
if ( argc <= 1 )
|
if ( argc <= optind )
|
||||||
exit(0);
|
exit(0);
|
||||||
execvp(argv[1], argv+1);
|
execvp(argv[optind], argv + optind);
|
||||||
error(127, errno, "%s", argv[1]);
|
err(127, "%s", argv[optind]);
|
||||||
}
|
}
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
memset(&sa, 0, sizeof(sa));
|
memset(&sa, 0, sizeof(sa));
|
||||||
|
|
Loading…
Reference in New Issue