Hi,

Another gettimeofday(2) -> clock_gettime(2) w/ CLOCK_MONOTONIC.

This is a statistics printout, so you naturally don't want the
timer subject to jumping.

Also, you're supposed to include <sys/time.h> to use gettimeofday(2),
which we were not doing in dd.c, so I guess this fixes that in a
bit of a roundabout sort of way.

Feedback?

--
Scott Cheloha

Index: bin/dd/dd.c
===================================================================
RCS file: /cvs/src/bin/dd/dd.c,v
retrieving revision 1.23
diff -u -p -r1.23 dd.c
--- bin/dd/dd.c 9 Oct 2015 01:37:06 -0000       1.23
+++ bin/dd/dd.c 13 Aug 2017 01:33:17 -0000
@@ -193,7 +193,7 @@ setup(void)
        }
 
        /* Statistics timestamp. */
-       (void)gettimeofday(&st.startv, (struct timezone *)NULL);
+       clock_gettime(CLOCK_MONOTONIC, &st.start);
 }
 
 static void
Index: bin/dd/dd.h
===================================================================
RCS file: /cvs/src/bin/dd/dd.h,v
retrieving revision 1.7
diff -u -p -r1.7 dd.h
--- bin/dd/dd.h 16 Aug 2016 16:44:55 -0000      1.7
+++ bin/dd/dd.h 13 Aug 2017 01:33:17 -0000
@@ -68,7 +68,7 @@ typedef struct {
        size_t  trunc;                  /* # of truncated records */
        size_t  swab;                   /* # of odd-length swab blocks */
        off_t   bytes;                  /* # of bytes written */
-       struct  timeval startv;         /* start time of dd */
+       struct  timespec start;         /* start time of dd */
 } STAT;
 
 /* Flags (in ddflags). */
Index: bin/dd/misc.c
===================================================================
RCS file: /cvs/src/bin/dd/misc.c,v
retrieving revision 1.20
diff -u -p -r1.20 misc.c
--- bin/dd/misc.c       25 Aug 2016 05:25:21 -0000      1.20
+++ bin/dd/misc.c       13 Aug 2017 01:33:17 -0000
@@ -52,20 +52,20 @@
 void
 summary(void)
 {
-       struct timeval nowtv;
+       struct timespec elapsed, now;
        char buf[4][100];
        struct iovec iov[4];
-       double microsecs;
+       double nanosecs;
        int i = 0;
 
        if (ddflags & C_NOINFO)
                return;
 
-       (void)gettimeofday(&nowtv, (struct timezone *)NULL);
-       timersub(&nowtv, &st.startv, &nowtv);
-       microsecs = ((double)nowtv.tv_sec * 1000000) + nowtv.tv_usec;
-       if (microsecs == 0)
-               microsecs = 1;
+       clock_gettime(CLOCK_MONOTONIC, &now);
+       timespecsub(&now, &st.start, &elapsed);
+       nanosecs = ((double)elapsed.tv_sec * 1000000000) + elapsed.tv_nsec;
+       if (nanosecs == 0)
+               nanosecs = 1;
 
        /* Use snprintf(3) so that we don't reenter stdio(3). */
        (void)snprintf(buf[0], sizeof(buf[0]),
@@ -92,8 +92,8 @@ summary(void)
                (void)snprintf(buf[3], sizeof(buf[3]),
                    "%lld bytes transferred in %lld.%03ld secs "
                    "(%0.0f bytes/sec)\n", (long long)st.bytes,
-                   (long long)nowtv.tv_sec, nowtv.tv_usec / 1000,
-                   ((double)st.bytes * 1000000) / microsecs);
+                   (long long)elapsed.tv_sec, elapsed.tv_nsec / 1000000,
+                   ((double)st.bytes * 1000000000) / nanosecs);
                iov[i].iov_base = buf[3];
                iov[i++].iov_len = strlen(buf[3]);
        }

Reply via email to