]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
misc: use monotonic time rater than gettimeofday
authorKarel Zak <kzak@redhat.com>
Tue, 18 Nov 2014 13:35:21 +0000 (14:35 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 18 Nov 2014 13:35:21 +0000 (14:35 +0100)
Based on patch Alexander Samilovskih <alexsamilovskih@gmail.com>

Signed-off-by: Karel Zak <kzak@redhat.com>
include/timeutils.h
lib/timeutils.c
libmount/src/lock.c
sys-utils/blkdiscard.c
sys-utils/eject.c

index bcae6137cfdcaa8b85cc74e4480440d665266925..97f054489efb3cea2dafc1b6c7110aad0d694e27 100644 (file)
@@ -3,6 +3,7 @@
   copied to util-linux at August 2013.
 
   Copyright 2010 Lennart Poettering
+  Copyright (C) 2014 Karel Zak <kzak@redhat.com>
 
   systemd is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as published by
@@ -52,4 +53,6 @@ typedef uint64_t nsec_t;
 
 int parse_timestamp(const char *t, usec_t *usec);
 
+int gettime_monotonic(struct timeval *tv);
+
 #endif /* UTIL_LINUX_TIME_UTIL_H */
index b811041e453b6feff4a480553b5bdce2ef1b2f88..bd3d402488e1dde15a7221f8e1aae9e3567cfb57 100644 (file)
@@ -340,3 +340,26 @@ int parse_timestamp(const char *t, usec_t *usec)
 
        return 0;
 }
+
+
+int gettime_monotonic(struct timeval *tv)
+{
+#ifdef CLOCK_MONOTONIC
+       /* Can slew only by ntp and adjtime */
+       int ret;
+       struct timespec ts;
+
+# ifdef CLOCK_MONOTONIC_RAW
+       /* Linux specific, cant slew */
+       if (!(ret = clock_gettime(CLOCK_MONOTONIC_RAW, &ts))) {
+# else
+       if (!(ret = clock_gettime(CLOCK_MONOTONIC, &ts))) {
+# endif
+               tv->tv_sec = ts.tv_sec;
+               tv->tv_usec = ts.tv_nsec / 1000;
+       }
+       return ret;
+#else
+       return gettimeofday(tv, NULL);
+#endif
+}
index 8667db999e0c975b7125fafe79cb93a7e11a753c..0a07e9b0322840ab59614b3e34b5492738060bc6 100644 (file)
@@ -25,6 +25,7 @@
 #include "closestream.h"
 #include "pathnames.h"
 #include "mountP.h"
+#include "timeutils.h"
 
 /*
  * lock handler
@@ -258,7 +259,7 @@ static int mnt_wait_mtab_lock(struct libmnt_lock *ml, struct flock *fl, time_t m
        struct sigaction sa, osa;
        int ret = 0;
 
-       gettimeofday(&now, NULL);
+       gettime_monotonic(&now);
 
        if (now.tv_sec >= maxtime)
                return 1;               /* timeout */
@@ -408,7 +409,7 @@ static int lock_mtab(struct libmnt_lock *ml)
        }
        close(i);
 
-       gettimeofday(&maxtime, NULL);
+       gettime_monotonic(&maxtime);
        maxtime.tv_sec += MOUNTLOCK_MAXTIME;
 
        waittime.tv_sec = 0;
@@ -434,7 +435,7 @@ static int lock_mtab(struct libmnt_lock *ml)
                if (ml->lockfile_fd < 0) {
                        /* Strange... Maybe the file was just deleted? */
                        int errsv = errno;
-                       gettimeofday(&now, NULL);
+                       gettime_monotonic(&now);
                        if (errsv == ENOENT && now.tv_sec < maxtime.tv_sec) {
                                ml->locked = 0;
                                continue;
@@ -646,7 +647,7 @@ int test_lock(struct libmnt_test *ts, int argc, char *argv[])
 
        /* start the test in exactly defined time */
        if (synctime) {
-               gettimeofday(&tv, NULL);
+               gettime_monotonic(&tv);
                if (synctime && synctime - tv.tv_sec > 1) {
                        usecs = ((synctime - tv.tv_sec) * 1000000UL) -
                                                (1000000UL - tv.tv_usec);
index 92ca52ae4b9ad873a6078f947dce81f370a46ed3..094f4e2ffc14e6ea39deb6c66ef4923c39ac7b62 100644 (file)
@@ -42,6 +42,7 @@
 #include "strutils.h"
 #include "c.h"
 #include "closestream.h"
+#include "timeutils.h"
 
 #ifndef BLKDISCARD
 #define BLKDISCARD     _IO(0x12,119)
@@ -80,7 +81,7 @@ int main(int argc, char **argv)
        int c, fd, verbose = 0, secure = 0, secsize;
        uint64_t end, blksize, step, range[2], stats[2];
        struct stat sb;
-       struct timespec now, last;
+       struct timeval now, last;
 
        static const struct option longopts[] = {
            { "help",      0, 0, 'h' },
@@ -178,7 +179,7 @@ int main(int argc, char **argv)
                         "to sector size %i"), path, range[1], secsize);
 
        stats[0] = range[0], stats[1] = 0;
-       clock_gettime(CLOCK_MONOTONIC, &last);
+       gettime_monotonic(&last);
 
        for (range[0] = range[0]; range[0] < end; range[0] += range[1]) {
                if (range[0] + range[1] > end)
@@ -194,7 +195,7 @@ int main(int argc, char **argv)
 
                /* reporting progress */
                if (verbose && step) {
-                       clock_gettime(CLOCK_MONOTONIC, &now);
+                       gettime_monotonic(&now);
                        if (last.tv_sec < now.tv_sec) {
                                print_stats(path, stats);
                                stats[0] = range[0], stats[1] = 0;
index 860610f5ae4faac8e74fccbd6eec5ebfd5670c2f..5a181644d2cefee12234baf8dee77d48cb5d37ec 100644 (file)
@@ -52,6 +52,7 @@
 #include "xalloc.h"
 #include "pathnames.h"
 #include "sysfs.h"
+#include "timeutils.h"
 
 /*
  * sg_io_hdr_t driver_status -- see kernel include/scsi/scsi.h
@@ -462,7 +463,7 @@ static void toggle_tray(int fd)
         * needed.  In my experience the function needs less than 0.05
         * seconds if the tray was already open, and at least 1.5 seconds
         * if it was closed.  */
-       gettimeofday(&time_start, NULL);
+       gettime_monotonic(&time_start);
 
        /* Send the CDROMEJECT command to the device. */
        if (!eject_cdrom(fd))
@@ -470,7 +471,7 @@ static void toggle_tray(int fd)
 
        /* Get the second timestamp, to measure the time needed to open
         * the tray.  */
-       gettimeofday(&time_stop, NULL);
+       gettime_monotonic(&time_stop);
 
        time_elapsed = (time_stop.tv_sec * 1000000 + time_stop.tv_usec) -
                (time_start.tv_sec * 1000000 + time_start.tv_usec);