Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/time PR/45592: Greg A. Woods: changes to get time(1)...



details:   https://anonhg.NetBSD.org/src/rev/3fdc2c699a8c
branches:  trunk
changeset: 771110:3fdc2c699a8c
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Nov 09 19:10:10 2011 +0000

description:
PR/45592: Greg A. Woods: changes to get time(1) to use CLOCK_MONOTONIC

diffstat:

 usr.bin/time/ext.h  |   7 ++++---
 usr.bin/time/time.1 |  13 +++++++------
 usr.bin/time/time.c |  35 +++++++++++++++++++++++------------
 3 files changed, 34 insertions(+), 21 deletions(-)

diffs (171 lines):

diff -r d9be3c5d90a7 -r 3fdc2c699a8c usr.bin/time/ext.h
--- a/usr.bin/time/ext.h        Wed Nov 09 19:08:59 2011 +0000
+++ b/usr.bin/time/ext.h        Wed Nov 09 19:10:10 2011 +0000
@@ -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 *);
diff -r d9be3c5d90a7 -r 3fdc2c699a8c usr.bin/time/time.1
--- a/usr.bin/time/time.1       Wed Nov 09 19:08:59 2011 +0000
+++ b/usr.bin/time/time.1       Wed Nov 09 19:10:10 2011 +0000
@@ -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 @@
 .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 @@
 .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 @@
 .Sh SEE ALSO
 .Xr csh 1 ,
 .Xr ksh 1 ,
+.Xr clock_gettime 2
 .Xr getrusage 2
 .Sh STANDARDS
 The
diff -r d9be3c5d90a7 -r 3fdc2c699a8c usr.bin/time/time.c
--- a/usr.bin/time/time.c       Wed Nov 09 19:08:59 2011 +0000
+++ b/usr.bin/time/time.c       Wed Nov 09 19:10:10 2011 +0000
@@ -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 @@
 #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 @@
 
 __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 @@
        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 @@
        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 @@
        (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 @@
        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 @@
 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 @@
 }
 
 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);
 }



Home | Main Index | Thread Index | Old Index