diff options
author | Ted Unangst <tedu@cvs.openbsd.org> | 2017-07-13 06:39:55 +0000 |
---|---|---|
committer | Ted Unangst <tedu@cvs.openbsd.org> | 2017-07-13 06:39:55 +0000 |
commit | 5ba52eb4d92b5d9b39b8ab3b8c8b8ed18824638f (patch) | |
tree | 8614e6846ba3a80d5b0c81b8a0b5c59ba8235ebc /usr.bin/time | |
parent | 7af3013388ed18de6465a8f464379a4239b7fb28 (diff) |
use monotonic clock for computing elapsed time
from Scott Cheloha. ok dlg.
Diffstat (limited to 'usr.bin/time')
-rw-r--r-- | usr.bin/time/time.c | 15 |
1 files changed, 7 insertions, 8 deletions
diff --git a/usr.bin/time/time.c b/usr.bin/time/time.c index d3b4659439f..47eee9ee7d4 100644 --- a/usr.bin/time/time.c +++ b/usr.bin/time/time.c @@ -1,4 +1,4 @@ -/* $OpenBSD: time.c,v 1.21 2015/10/10 14:49:23 deraadt Exp $ */ +/* $OpenBSD: time.c,v 1.22 2017/07/13 06:39:54 tedu Exp $ */ /* $NetBSD: time.c,v 1.7 1995/06/27 00:34:00 jtc Exp $ */ /* @@ -52,7 +52,7 @@ main(int argc, char *argv[]) { pid_t pid; int ch, status; - struct timeval before, after; + struct timespec before, after, during; struct rusage ru; int exitonsig = 0; @@ -79,7 +79,7 @@ main(int argc, char *argv[]) if (argc < 1) usage(); - gettimeofday(&before, (struct timezone *)NULL); + clock_gettime(CLOCK_MONOTONIC, &before); switch(pid = vfork()) { case -1: /* error */ perror("time"); @@ -97,24 +97,23 @@ main(int argc, char *argv[]) (void)signal(SIGQUIT, SIG_IGN); while (wait3(&status, 0, &ru) != pid) ; - gettimeofday(&after, (struct timezone *)NULL); + clock_gettime(CLOCK_MONOTONIC, &after); if (WIFSIGNALED(status)) exitonsig = WTERMSIG(status); if (!WIFEXITED(status)) fprintf(stderr, "Command terminated abnormally.\n"); - timersub(&after, &before, &after); + timespecsub(&after, &before, &during); if (portableflag) { fprintf(stderr, "real %9lld.%02ld\n", - (long long)after.tv_sec, after.tv_usec/10000); + (long long)during.tv_sec, during.tv_nsec/10000000); fprintf(stderr, "user %9lld.%02ld\n", (long long)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); fprintf(stderr, "sys %9lld.%02ld\n", (long long)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec/10000); } else { - fprintf(stderr, "%9lld.%02ld real ", - (long long)after.tv_sec, after.tv_usec/10000); + (long long)during.tv_sec, during.tv_nsec/10000000); fprintf(stderr, "%9lld.%02ld user ", (long long)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec/10000); fprintf(stderr, "%9lld.%02ld sys\n", |