Module Name: src Committed By: christos Date: Wed Nov 9 19:10:10 UTC 2011
Modified Files: src/usr.bin/time: ext.h time.1 time.c Log Message: PR/45592: Greg A. Woods: changes to get time(1) to use CLOCK_MONOTONIC To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/usr.bin/time/ext.h cvs rdiff -u -r1.23 -r1.24 src/usr.bin/time/time.1 cvs rdiff -u -r1.21 -r1.22 src/usr.bin/time/time.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/time/ext.h diff -u src/usr.bin/time/ext.h:1.1 src/usr.bin/time/ext.h:1.2 --- src/usr.bin/time/ext.h:1.1 Sat Feb 24 16:30:27 2007 +++ src/usr.bin/time/ext.h Wed Nov 9 14:10:10 2011 @@ -1,4 +1,5 @@ -/* $NetBSD: ext.h,v 1.1 2007/02/24 21:30:27 matt Exp $ */ +/* $NetBSD: ext.h,v 1.2 2011/11/09 19:10:10 christos Exp $ */ -void prusage(FILE *, struct rusage *, struct rusage *, struct timeval *, - struct timeval *); +/* borrowed from ../../bin/csh/extern.h */ +void prusage(FILE *, struct rusage *, struct rusage *, struct timespec *, + struct timespec *); Index: src/usr.bin/time/time.1 diff -u src/usr.bin/time/time.1:1.23 src/usr.bin/time/time.1:1.24 --- src/usr.bin/time/time.1:1.23 Sat Oct 1 21:51:00 2011 +++ src/usr.bin/time/time.1 Wed Nov 9 14:10:10 2011 @@ -1,4 +1,4 @@ -.\" $NetBSD: time.1,v 1.23 2011/10/02 01:51:00 dholland Exp $ +.\" $NetBSD: time.1,v 1.24 2011/11/09 19:10:10 christos Exp $ .\" .\" Copyright (c) 1980, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -29,7 +29,7 @@ .\" .\" @(#)time.1 8.1 (Berkeley) 6/6/93 .\" -.Dd October 1, 2011 +.Dd November 9, 2011 .Dt TIME 1 .Os .Sh NAME @@ -49,13 +49,13 @@ times .Ar command . After the command finishes, .Nm -writes the total elapsed time +writes the total elapsed time (wall clock time), .Pq Dq real , -the time spent executing +the CPU time spent executing .Ar command at user level .Pq Dq user , -and the time spent executing in the operating system kernel +and the CPU time spent executing in the operating system kernel .Pq Dq sys , to the standard error stream. Times are reported in seconds. @@ -83,7 +83,7 @@ Some shells, such as .Xr csh 1 and .Xr ksh 1 , -have their own and syntactically different builtin version of +have their own and syntactically different built-in version of .Nm . The utility described here is available as @@ -159,6 +159,7 @@ will be that of .Sh SEE ALSO .Xr csh 1 , .Xr ksh 1 , +.Xr clock_gettime 2 .Xr getrusage 2 .Sh STANDARDS The Index: src/usr.bin/time/time.c diff -u src/usr.bin/time/time.c:1.21 src/usr.bin/time/time.c:1.22 --- src/usr.bin/time/time.c:1.21 Wed Aug 31 12:24:58 2011 +++ src/usr.bin/time/time.c Wed Nov 9 14:10:10 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: time.c,v 1.21 2011/08/31 16:24:58 plunky Exp $ */ +/* $NetBSD: time.c,v 1.22 2011/11/09 19:10:10 christos Exp $ */ /* * Copyright (c) 1987, 1988, 1993 @@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1987, 19 #if 0 static char sccsid[] = "@(#)time.c 8.1 (Berkeley) 6/6/93"; #endif -__RCSID("$NetBSD: time.c,v 1.21 2011/08/31 16:24:58 plunky Exp $"); +__RCSID("$NetBSD: time.c,v 1.22 2011/11/09 19:10:10 christos Exp $"); #endif /* not lint */ #include <sys/types.h> @@ -58,6 +58,8 @@ __RCSID("$NetBSD: time.c,v 1.21 2011/08/ __dead static void usage(void); static void prl(long, const char *); +static void prts(const char *, const char *, const struct timespec *, + const char *); static void prtv(const char *, const char *, const struct timeval *, const char *); @@ -71,7 +73,7 @@ main(int argc, char ** volatile argv) int volatile cshflag; const char *decpt; const struct lconv *lconv; - struct timeval before, after; + struct timespec before, after; struct rusage ru; (void)setlocale(LC_ALL, ""); @@ -105,7 +107,7 @@ main(int argc, char ** volatile argv) if (argc < 1) usage(); - gettimeofday(&before, NULL); + (void)clock_gettime(CLOCK_MONOTONIC, &before); switch(pid = vfork()) { case -1: /* error */ err(EXIT_FAILURE, "Vfork failed"); @@ -122,10 +124,10 @@ main(int argc, char ** volatile argv) (void)signal(SIGQUIT, SIG_IGN); if ((pid = wait4(pid, &status, 0, &ru)) == -1) err(EXIT_FAILURE, "wait4 %d failed", pid); - (void)gettimeofday(&after, NULL); + (void)clock_gettime(CLOCK_MONOTONIC, &after); if (!WIFEXITED(status)) warnx("Command terminated abnormally."); - timersub(&after, &before, &after); + timespecsub(&after, &before, &after); if ((lconv = localeconv()) == NULL || (decpt = lconv->decimal_point) == NULL) @@ -134,14 +136,14 @@ main(int argc, char ** volatile argv) if (cshflag) { static struct rusage null_ru; before.tv_sec = 0; - before.tv_usec = 0; + before.tv_nsec = 0; prusage(stderr, &null_ru, &ru, &after, &before); } else if (portableflag) { - prtv("real ", decpt, &after, "\n"); + prts("real ", decpt, &after, "\n"); prtv("user ", decpt, &ru.ru_utime, "\n"); prtv("sys ", decpt, &ru.ru_stime, "\n"); } else { - prtv("", decpt, &after, " real "); + prts("", decpt, &after, " real "); prtv("", decpt, &ru.ru_utime, " user "); prtv("", decpt, &ru.ru_stime, " sys\n"); } @@ -176,7 +178,7 @@ static void usage(void) { - (void)fprintf(stderr, "usage: %s [-clp] utility [argument ...]\n", + (void)fprintf(stderr, "Usage: %s [-clp] utility [argument ...]\n", getprogname()); exit(EXIT_FAILURE); } @@ -189,10 +191,19 @@ prl(long val, const char *expn) } static void +prts(const char *pre, const char *decpt, const struct timespec *ts, + const char *post) +{ + + (void)fprintf(stderr, "%s%9lld%s%02ld%s", pre, (long long)ts->tv_sec, + decpt, (long)ts->tv_nsec / 10000000, post); +} + +static void prtv(const char *pre, const char *decpt, const struct timeval *tv, const char *post) { - (void)fprintf(stderr, "%s%9ld%s%02ld%s", pre, (long)tv->tv_sec, decpt, - (long)tv->tv_usec / 10000, post); + (void)fprintf(stderr, "%s%9lld%s%02ld%s", pre, (long long)tv->tv_sec, + decpt, (long)tv->tv_usec / 10000, post); }