NetBSD-Bugs archive

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

bin/45592: changes to get time(1) to use clock_gettime(CLOCK_MONOTONIC)



>Number:         45592
>Category:       bin
>Synopsis:       changes to get time(1) to use clock_gettime(CLOCK_MONOTONIC)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Tue Nov 08 20:15:00 +0000 2011
>Originator:     Greg A. Woods
>Release:        all
>Organization:
Planix, Inc.; Kelowna, BC; Canada
>Environment:
System: NetBSD
Architecture: all
Machine: all
>Description:

        time(1) uses gettimeofday() and so can be subject to time
        adjustments and such

        It could use clock_gettime() with CLOCK_MONOTONIC instead

>How-To-Repeat:


>Fix:

        these changes are incomplete -- they hack some common code
        stolen from csh but don't fix the rest of csh to adapt similarly
        as I don't build, use, or otherwise support or care about csh.  :-)

        these diffs are against the netbsd-5 branch as of 2011/11/08


Index: usr.bin/time/time.c
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/usr.bin/time/time.c,v
retrieving revision 1.19
diff -u -r1.19 time.c
--- usr.bin/time/time.c 21 Jul 2008 14:19:26 -0000      1.19
+++ usr.bin/time/time.c 27 Oct 2011 02:39:41 -0000
@@ -59,6 +59,8 @@
 int            main(int, char **);
 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 *);
 
@@ -72,7 +74,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, "");
@@ -106,7 +108,7 @@
        if (argc < 1)
                usage();
 
-       gettimeofday(&before, (struct timezone *)NULL);
+       (void)clock_gettime(CLOCK_MONOTONIC, &before);
        switch(pid = vfork()) {
        case -1:                        /* error */
                err(EXIT_FAILURE, "Vfork failed");
@@ -123,10 +125,10 @@
        (void)signal(SIGQUIT, SIG_IGN);
        if ((pid = wait4(pid, &status, 0, &ru)) == -1)
                err(EXIT_FAILURE, "wait4 %d failed", pid);
-       (void)gettimeofday(&after, (struct timezone *)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)
@@ -135,14 +137,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");
        }
@@ -190,6 +192,15 @@
 }
 
 static void
+prts(const char *pre, const char *decpt, const struct timespec *ts,
+    const char *post)
+{
+
+       (void)fprintf(stderr, "%s%9ld%s%02ld%s", pre, (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)
 {
Index: usr.bin/time/ext.h
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/usr.bin/time/ext.h,v
retrieving revision 1.1
diff -u -r1.1 ext.h
--- usr.bin/time/ext.h  24 Feb 2007 21:30:27 -0000      1.1
+++ usr.bin/time/ext.h  27 Oct 2011 02:49:36 -0000
@@ -1,4 +1,5 @@
 /*     $NetBSD: ext.h,v 1.1 2007/02/24 21:30:27 matt 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: usr.bin/time/time.1
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/usr.bin/time/time.1,v
retrieving revision 1.18.40.1
diff -u -r1.18.40.1 time.1
--- usr.bin/time/time.1 26 Mar 2009 17:07:30 -0000      1.18.40.1
+++ usr.bin/time/time.1 8 Nov 2011 20:11:01 -0000
@@ -51,10 +51,10 @@
 .Ar utility
 finishes,
 .Nm
-writes the total time elapsed,
-the time consumed by system overhead,
-and the time used to execute
-.Ar utility
+writes the total time elapsed (wall clock time),
+the CPU time consumed by system overhead,
+and the CPU time used to execute
+.Ar utility ,
 to the standard error stream.
 Times are reported in seconds.
 .Pp
@@ -80,7 +80,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
@@ -152,6 +152,7 @@
 .Sh SEE ALSO
 .Xr csh 1 ,
 .Xr ksh 1 ,
+.Xr clock_gettime 2
 .Xr getrusage 2
 .Sh STANDARDS
 The
Index: bin/csh/time.c
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/bin/csh/time.c,v
retrieving revision 1.17
diff -u -r1.17 time.c
--- bin/csh/time.c      24 Feb 2008 05:20:17 -0000      1.17
+++ bin/csh/time.c      27 Oct 2011 02:42:19 -0000
@@ -127,8 +127,8 @@
 #endif /* NOT_CSH */
 
 void
-prusage(FILE *fp, struct rusage *r0, struct rusage *r1, struct timeval *e,
-        struct timeval *b)
+prusage(FILE *fp, struct rusage *r0, struct rusage *r1, struct timespec *e,
+        struct timespec *b)
 {
 #ifndef NOT_CSH
     struct varent *vp;
@@ -139,7 +139,7 @@
     int ms;
 
     cp = "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww";
-    ms = (e->tv_sec - b->tv_sec) * 100 + (e->tv_usec - b->tv_usec) / 10000;
+    ms = (e->tv_sec - b->tv_sec) * 100 + (e->tv_nsec - b->tv_nsec) / 10000000;
     t = (r1->ru_utime.tv_sec - r0->ru_utime.tv_sec) * 100 +
         (r1->ru_utime.tv_usec - r0->ru_utime.tv_usec) / 10000 +
         (r1->ru_stime.tv_sec - r0->ru_stime.tv_sec) * 100 +
Index: bin/csh/extern.h
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/bin/csh/extern.h,v
retrieving revision 1.22
diff -u -r1.22 extern.h
--- bin/csh/extern.h    24 Dec 2007 16:11:50 -0000      1.22
+++ bin/csh/extern.h    27 Oct 2011 02:40:08 -0000
@@ -288,8 +288,8 @@
  */
 void donice(Char **, struct command *);
 void dotime(Char **, struct command *);
-void prusage(FILE *, struct rusage *, struct rusage *, struct timeval *,
-             struct timeval *);
+void prusage(FILE *, struct rusage *, struct rusage *, struct timespec *,
+             struct timespec *);
 void ruadd(struct rusage *, struct rusage *);
 void settimes(void);
 void psecs(long);



Home | Main Index | Thread Index | Old Index