gettime: even rounding, don't always round up
[fio.git] / gettime.c
index 035d275df60e2fc325be540818a130fc7835aba3..3011d7440738c050fc5ae696720e97bf896926bd 100644 (file)
--- a/gettime.c
+++ b/gettime.c
@@ -15,8 +15,9 @@
 
 #ifdef ARCH_HAVE_CPU_CLOCK
 static unsigned long cycles_per_usec;
-int tsc_reliable = 0;
+static unsigned long inv_cycles_per_usec;
 #endif
+int tsc_reliable = 0;
 
 struct tv_valid {
        struct timeval last_tv;
@@ -177,7 +178,7 @@ void fio_gettime(struct timeval *tp, void fio_unused *caller)
                } else if (tv)
                        tv->last_cycles = t;
 
-               usecs = t / cycles_per_usec;
+               usecs = (t * inv_cycles_per_usec) / 16777216UL;
                tp->tv_sec = usecs / 1000000;
                tp->tv_usec = usecs % 1000000;
                break;
@@ -272,11 +273,13 @@ static void calibrate_cpu_clock(void)
                dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
 
        avg /= samples;
-       avg = (avg + 9) / 10;
+       avg = (avg + 5) / 10;
        dprint(FD_TIME, "avg: %lu\n", avg);
        dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
 
        cycles_per_usec = avg;
+       inv_cycles_per_usec = 16777216UL / cycles_per_usec;
+       dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
 }
 #else
 static void calibrate_cpu_clock(void)