misc: use monotonic time rater than gettimeofday

Based on patch Alexander Samilovskih <alexsamilovskih@gmail.com>

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2014-11-18 14:35:21 +01:00
parent 570a930014
commit 700031ade7
5 changed files with 38 additions and 9 deletions

View 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 */

View 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
}

View 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);

View 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;

View 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);