+ if (fio_clock_source == fio_clock_source_inited)
+ return;
+
+#ifndef CONFIG_TLS_THREAD
+ if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
+ log_err("fio: can't create TLS key\n");
+#endif
+
+ fio_clock_source_inited = fio_clock_source;
+
+ if (calibrate_cpu_clock())
+ tsc_reliable = 0;
+
+ /*
+ * If the arch sets tsc_reliable != 0, then it must be good enough
+ * to use as THE clock source. For x86 CPUs, this means the TSC
+ * runs at a constant rate and is synced across CPU cores.
+ */
+ if (tsc_reliable) {
+ if (!fio_clock_source_set)
+ fio_clock_source = CS_CPUCLOCK;
+ } else if (fio_clock_source == CS_CPUCLOCK)
+ log_info("fio: clocksource=cpu may not be reliable\n");
+}
+
+uint64_t utime_since(const struct timeval *s, const struct timeval *e)
+{
+ long sec, usec;
+ uint64_t ret;
+
+ sec = e->tv_sec - s->tv_sec;
+ usec = e->tv_usec - s->tv_usec;
+ if (sec > 0 && usec < 0) {
+ sec--;
+ usec += 1000000;
+ }
+
+ /*
+ * time warp bug on some kernels?
+ */
+ if (sec < 0 || (sec == 0 && usec < 0))
+ return 0;
+
+ ret = sec * 1000000ULL + usec;
+
+ return ret;
+}
+
+uint64_t utime_since_now(const struct timeval *s)
+{
+ struct timeval t;
+
+ fio_gettime(&t, NULL);
+ return utime_since(s, &t);
+}
+
+uint64_t mtime_since(const struct timeval *s, const struct timeval *e)
+{
+ long sec, usec, ret;
+
+ sec = e->tv_sec - s->tv_sec;
+ usec = e->tv_usec - s->tv_usec;
+ if (sec > 0 && usec < 0) {
+ sec--;
+ usec += 1000000;
+ }
+
+ if (sec < 0 || (sec == 0 && usec < 0))
+ return 0;
+
+ sec *= 1000UL;
+ usec /= 1000UL;
+ ret = sec + usec;
+
+ return ret;
+}
+
+uint64_t mtime_since_now(const struct timeval *s)
+{
+ struct timeval t;
+ void *p = __builtin_return_address(0);
+
+ fio_gettime(&t, p);
+ return mtime_since(s, &t);