X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=gettime.c;h=b1431f329bec2e3ef9e32439035d896e9f8545ca;hp=2753faecb02d0596b1898346a46bf7f1d9e44873;hb=814452bd47e0f2bb4825b8042b664f32de0aff8d;hpb=01743ee1718e7ec4b16ae3e53c8f64900c6052cc diff --git a/gettime.c b/gettime.c index 2753faec..b1431f32 100644 --- a/gettime.c +++ b/gettime.c @@ -6,10 +6,17 @@ #include #include "fio.h" +#include "smalloc.h" #include "hash.h" static int clock_gettime_works; +static struct timeval last_tv; +static int last_tv_valid; + +static struct timeval *fio_tv; +int fio_gtod_offload = 0; +int fio_gtod_cpu = -1; #ifdef FIO_DEBUG_TIME @@ -114,7 +121,10 @@ void fio_gettime(struct timeval *tp, void fio_unused *caller) gtod_log_caller(caller); #endif - if (!clock_gettime_works) { + if (fio_tv) { + memcpy(tp, fio_tv, sizeof(*tp)); + return; + } else if (!clock_gettime_works) { gtod: gettimeofday(tp, NULL); } else { @@ -128,4 +138,29 @@ gtod: tp->tv_sec = ts.tv_sec; tp->tv_usec = ts.tv_nsec / 1000; } + + /* + * If Linux is using the tsc clock on non-synced processors, + * sometimes time can appear to drift backwards. Fix that up. + */ + if (last_tv_valid) { + if (tp->tv_sec < last_tv.tv_sec) + tp->tv_sec = last_tv.tv_sec; + else if (last_tv.tv_sec == tp->tv_sec && + tp->tv_usec < last_tv.tv_usec) + tp->tv_usec = last_tv.tv_usec; + } + last_tv_valid = 1; + memcpy(&last_tv, tp, sizeof(*tp)); +} + +void fio_gtod_init(void) +{ + fio_tv = smalloc(sizeof(struct timeval)); + assert(fio_tv); +} + +void fio_gtod_update(void) +{ + gettimeofday(fio_tv, NULL); }