blob: 7e3e2e5b4375724dcb826c2c34523b994b0d3034 [file] [log] [blame]
Jens Axboe02bcaa82006-11-24 10:42:00 +01001/*
Jens Axboef5cc0242006-11-24 10:47:40 +01002 * Clock functions
Jens Axboe02bcaa82006-11-24 10:42:00 +01003 */
Jens Axboef5cc0242006-11-24 10:47:40 +01004
Jens Axboe02bcaa82006-11-24 10:42:00 +01005#include <unistd.h>
Jens Axboec223da82010-03-24 13:23:53 +01006#include <math.h>
Jens Axboe02bcaa82006-11-24 10:42:00 +01007#include <sys/time.h>
Bruce Cran03e20d62011-01-02 20:14:54 +01008#include <time.h>
Jens Axboe02bcaa82006-11-24 10:42:00 +01009
10#include "fio.h"
Jens Axboebe4ecfd2008-12-08 14:10:52 +010011#include "smalloc.h"
Jens Axboe02bcaa82006-11-24 10:42:00 +010012
13#include "hash.h"
Jens Axboe7d11f872012-12-17 12:03:29 +010014#include "os/os.h"
Jens Axboe02bcaa82006-11-24 10:42:00 +010015
Christian Ehrhardt919e7892014-04-08 17:52:00 +020016#if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
Jens Axboec223da82010-03-24 13:23:53 +010017static unsigned long cycles_per_usec;
Jens Axboe71339112012-12-21 22:54:56 +010018static unsigned long inv_cycles_per_usec;
Jens Axboe400531d2014-12-16 22:40:37 -070019static uint64_t max_cycles_for_mult;
Jens Axboe09a32402010-08-15 15:01:51 -040020#endif
Jens Axboe4de98eb2013-01-01 10:59:04 +010021int tsc_reliable = 0;
Jens Axboe5d879392012-12-18 09:12:27 +010022
23struct tv_valid {
Jens Axboeba458c22013-01-21 05:38:22 -070024 uint64_t last_cycles;
Jens Axboe02c7e292014-12-16 15:37:25 -070025 int last_tv_valid;
26 int warned;
Jens Axboe5d879392012-12-18 09:12:27 +010027};
Jens Axboe67bf9822013-01-10 11:23:19 +010028#ifdef CONFIG_TLS_THREAD
Jens Axboeb4ea84d2013-04-11 14:20:33 +020029static __thread struct tv_valid static_tv_valid;
Jens Axboe67bf9822013-01-10 11:23:19 +010030#else
Jens Axboe5d879392012-12-18 09:12:27 +010031static pthread_key_t tv_tls_key;
Jens Axboe67bf9822013-01-10 11:23:19 +010032#endif
Jens Axboe02bcaa82006-11-24 10:42:00 +010033
Bruce Cran16de1bf2012-02-20 17:07:32 +000034enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
Jens Axboefa80fea2012-12-09 20:29:00 +010035int fio_clock_source_set = 0;
Jens Axboe10aa1362014-04-01 21:10:36 -060036static enum fio_cs fio_clock_source_inited = CS_INVAL;
Jens Axboec223da82010-03-24 13:23:53 +010037
Jens Axboe02bcaa82006-11-24 10:42:00 +010038#ifdef FIO_DEBUG_TIME
39
40#define HASH_BITS 8
41#define HASH_SIZE (1 << HASH_BITS)
42
Jens Axboe01743ee2008-06-02 12:19:19 +020043static struct flist_head hash[HASH_SIZE];
Jens Axboe02bcaa82006-11-24 10:42:00 +010044static int gtod_inited;
45
46struct gtod_log {
Jens Axboe01743ee2008-06-02 12:19:19 +020047 struct flist_head list;
Jens Axboe02bcaa82006-11-24 10:42:00 +010048 void *caller;
49 unsigned long calls;
50};
51
52static struct gtod_log *find_hash(void *caller)
53{
54 unsigned long h = hash_ptr(caller, HASH_BITS);
Jens Axboe01743ee2008-06-02 12:19:19 +020055 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +010056
Jens Axboe01743ee2008-06-02 12:19:19 +020057 flist_for_each(entry, &hash[h]) {
58 struct gtod_log *log = flist_entry(entry, struct gtod_log,
59 list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010060
61 if (log->caller == caller)
62 return log;
63 }
64
65 return NULL;
66}
67
Jens Axboe5194e342014-12-16 23:03:54 -070068static void inc_caller(void *caller)
Jens Axboe02bcaa82006-11-24 10:42:00 +010069{
70 struct gtod_log *log = find_hash(caller);
71
72 if (!log) {
73 unsigned long h;
74
75 log = malloc(sizeof(*log));
Jens Axboe01743ee2008-06-02 12:19:19 +020076 INIT_FLIST_HEAD(&log->list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010077 log->caller = caller;
78 log->calls = 0;
79
80 h = hash_ptr(caller, HASH_BITS);
Jens Axboe01743ee2008-06-02 12:19:19 +020081 flist_add_tail(&log->list, &hash[h]);
Jens Axboe02bcaa82006-11-24 10:42:00 +010082 }
83
Jens Axboe5194e342014-12-16 23:03:54 -070084 log->calls++;
Jens Axboe02bcaa82006-11-24 10:42:00 +010085}
86
87static void gtod_log_caller(void *caller)
88{
Jens Axboe5194e342014-12-16 23:03:54 -070089 if (gtod_inited)
90 inc_caller(caller);
Jens Axboe02bcaa82006-11-24 10:42:00 +010091}
92
93static void fio_exit fio_dump_gtod(void)
94{
95 unsigned long total_calls = 0;
96 int i;
97
98 for (i = 0; i < HASH_SIZE; i++) {
Jens Axboe01743ee2008-06-02 12:19:19 +020099 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100100 struct gtod_log *log;
101
Jens Axboe01743ee2008-06-02 12:19:19 +0200102 flist_for_each(entry, &hash[i]) {
103 log = flist_entry(entry, struct gtod_log, list);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100104
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100105 printf("function %p, calls %lu\n", log->caller,
106 log->calls);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100107 total_calls += log->calls;
108 }
109 }
110
111 printf("Total %lu gettimeofday\n", total_calls);
112}
113
114static void fio_init gtod_init(void)
115{
116 int i;
117
118 for (i = 0; i < HASH_SIZE; i++)
Jens Axboe01743ee2008-06-02 12:19:19 +0200119 INIT_FLIST_HEAD(&hash[i]);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100120
121 gtod_inited = 1;
122}
123
124#endif /* FIO_DEBUG_TIME */
125
Jens Axboe67bf9822013-01-10 11:23:19 +0100126#ifdef CONFIG_CLOCK_GETTIME
Jens Axboe9ff1c072012-12-20 15:17:57 +0100127static int fill_clock_gettime(struct timespec *ts)
128{
Jens Axboe67bf9822013-01-10 11:23:19 +0100129#ifdef CONFIG_CLOCK_MONOTONIC
Jens Axboe9ff1c072012-12-20 15:17:57 +0100130 return clock_gettime(CLOCK_MONOTONIC, ts);
131#else
132 return clock_gettime(CLOCK_REALTIME, ts);
133#endif
134}
Jens Axboe1e97cce2006-12-05 11:44:16 +0100135#endif
Jens Axboe67bf9822013-01-10 11:23:19 +0100136
Jens Axboea7d78f22014-12-16 15:13:45 -0700137static void __fio_gettime(struct timeval *tp)
Jens Axboe02bcaa82006-11-24 10:42:00 +0100138{
Jens Axboe93f0b092012-12-18 09:41:59 +0100139 struct tv_valid *tv;
Jens Axboe5d879392012-12-18 09:12:27 +0100140
Jens Axboe67bf9822013-01-10 11:23:19 +0100141#ifdef CONFIG_TLS_THREAD
142 tv = &static_tv_valid;
143#else
Jens Axboe93f0b092012-12-18 09:41:59 +0100144 tv = pthread_getspecific(tv_tls_key);
Jens Axboe67bf9822013-01-10 11:23:19 +0100145#endif
Jens Axboe93f0b092012-12-18 09:41:59 +0100146
Jens Axboec223da82010-03-24 13:23:53 +0100147 switch (fio_clock_source) {
Jens Axboe67bf9822013-01-10 11:23:19 +0100148#ifdef CONFIG_GETTIMEOFDAY
Jens Axboec223da82010-03-24 13:23:53 +0100149 case CS_GTOD:
Jens Axboe02bcaa82006-11-24 10:42:00 +0100150 gettimeofday(tp, NULL);
Jens Axboec223da82010-03-24 13:23:53 +0100151 break;
Jens Axboe67bf9822013-01-10 11:23:19 +0100152#endif
153#ifdef CONFIG_CLOCK_GETTIME
Jens Axboec223da82010-03-24 13:23:53 +0100154 case CS_CGETTIME: {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100155 struct timespec ts;
156
Jens Axboe9ff1c072012-12-20 15:17:57 +0100157 if (fill_clock_gettime(&ts) < 0) {
Jens Axboec223da82010-03-24 13:23:53 +0100158 log_err("fio: clock_gettime fails\n");
159 assert(0);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100160 }
161
162 tp->tv_sec = ts.tv_sec;
163 tp->tv_usec = ts.tv_nsec / 1000;
Jens Axboec223da82010-03-24 13:23:53 +0100164 break;
165 }
Jens Axboe67bf9822013-01-10 11:23:19 +0100166#endif
Jens Axboec223da82010-03-24 13:23:53 +0100167#ifdef ARCH_HAVE_CPU_CLOCK
168 case CS_CPUCLOCK: {
Jens Axboeba458c22013-01-21 05:38:22 -0700169 uint64_t usecs, t;
Jens Axboec223da82010-03-24 13:23:53 +0100170
171 t = get_cpu_clock();
Jens Axboe02c7e292014-12-16 15:37:25 -0700172 if (t < tv->last_cycles && tv->last_tv_valid &&
173 !tv->warned) {
Jens Axboea7d78f22014-12-16 15:13:45 -0700174 log_err("fio: CPU clock going back in time\n");
Jens Axboe02c7e292014-12-16 15:37:25 -0700175 tv->warned = 1;
176 }
Jens Axboec223da82010-03-24 13:23:53 +0100177
Jens Axboea7d78f22014-12-16 15:13:45 -0700178 tv->last_cycles = t;
179 tv->last_tv_valid = 1;
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200180#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
181 usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
182#else
Jens Axboe400531d2014-12-16 22:40:37 -0700183 if (t < max_cycles_for_mult)
184 usecs = (t * inv_cycles_per_usec) / 16777216UL;
185 else
186 usecs = t / cycles_per_usec;
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200187#endif
Jens Axboec223da82010-03-24 13:23:53 +0100188 tp->tv_sec = usecs / 1000000;
189 tp->tv_usec = usecs % 1000000;
Jens Axboec223da82010-03-24 13:23:53 +0100190 break;
191 }
192#endif
193 default:
194 log_err("fio: invalid clock source %d\n", fio_clock_source);
195 break;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100196 }
Jens Axboe67bf9822013-01-10 11:23:19 +0100197}
198
199#ifdef FIO_DEBUG_TIME
200void fio_gettime(struct timeval *tp, void *caller)
201#else
202void fio_gettime(struct timeval *tp, void fio_unused *caller)
203#endif
204{
Jens Axboe67bf9822013-01-10 11:23:19 +0100205#ifdef FIO_DEBUG_TIME
206 if (!caller)
207 caller = __builtin_return_address(0);
208
209 gtod_log_caller(caller);
210#endif
Jens Axboe04194192014-12-16 19:43:55 -0700211 if (fio_unlikely(fio_gettime_offload(tp)))
Jens Axboe67bf9822013-01-10 11:23:19 +0100212 return;
Jens Axboe67bf9822013-01-10 11:23:19 +0100213
Jens Axboea7d78f22014-12-16 15:13:45 -0700214 __fio_gettime(tp);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100215}
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100216
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200217#if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
Jens Axboec223da82010-03-24 13:23:53 +0100218static unsigned long get_cycles_per_usec(void)
219{
220 struct timeval s, e;
Jens Axboeba458c22013-01-21 05:38:22 -0700221 uint64_t c_s, c_e;
Jens Axboe67bf9822013-01-10 11:23:19 +0100222 enum fio_cs old_cs = fio_clock_source;
Jens Axboec223da82010-03-24 13:23:53 +0100223
Jens Axboe67bf9822013-01-10 11:23:19 +0100224#ifdef CONFIG_CLOCK_GETTIME
225 fio_clock_source = CS_CGETTIME;
226#else
227 fio_clock_source = CS_GTOD;
228#endif
229 __fio_gettime(&s);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100230
Jens Axboec223da82010-03-24 13:23:53 +0100231 c_s = get_cpu_clock();
232 do {
Jens Axboeba458c22013-01-21 05:38:22 -0700233 uint64_t elapsed;
Jens Axboec223da82010-03-24 13:23:53 +0100234
Jens Axboe67bf9822013-01-10 11:23:19 +0100235 __fio_gettime(&e);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100236
Jens Axboec223da82010-03-24 13:23:53 +0100237 elapsed = utime_since(&s, &e);
Jens Axboe486332e2012-12-10 08:07:14 +0100238 if (elapsed >= 1280) {
Jens Axboec223da82010-03-24 13:23:53 +0100239 c_e = get_cpu_clock();
240 break;
241 }
242 } while (1);
243
Jens Axboe67bf9822013-01-10 11:23:19 +0100244 fio_clock_source = old_cs;
Jens Axboec0110002012-12-10 08:29:03 +0100245 return (c_e - c_s + 127) >> 7;
Jens Axboec223da82010-03-24 13:23:53 +0100246}
247
Jens Axboefa80fea2012-12-09 20:29:00 +0100248#define NR_TIME_ITERS 50
249
Jens Axboee2598792013-02-24 21:29:35 +0100250static int calibrate_cpu_clock(void)
Jens Axboec223da82010-03-24 13:23:53 +0100251{
252 double delta, mean, S;
Jens Axboeba458c22013-01-21 05:38:22 -0700253 uint64_t avg, cycles[NR_TIME_ITERS];
Jens Axboec223da82010-03-24 13:23:53 +0100254 int i, samples;
255
Jens Axboec223da82010-03-24 13:23:53 +0100256 cycles[0] = get_cycles_per_usec();
257 S = delta = mean = 0.0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100258 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100259 cycles[i] = get_cycles_per_usec();
260 delta = cycles[i] - mean;
261 if (delta) {
262 mean += delta / (i + 1.0);
263 S += delta * (cycles[i] - mean);
264 }
265 }
266
Jens Axboee2598792013-02-24 21:29:35 +0100267 /*
268 * The most common platform clock breakage is returning zero
269 * indefinitely. Check for that and return failure.
270 */
271 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
272 return 1;
273
Jens Axboefa80fea2012-12-09 20:29:00 +0100274 S = sqrt(S / (NR_TIME_ITERS - 1.0));
Jens Axboec223da82010-03-24 13:23:53 +0100275
276 samples = avg = 0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100277 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100278 double this = cycles[i];
279
Bruce Cran03e20d62011-01-02 20:14:54 +0100280 if ((fmax(this, mean) - fmin(this, mean)) > S)
Jens Axboec223da82010-03-24 13:23:53 +0100281 continue;
282 samples++;
283 avg += this;
284 }
285
Jens Axboefa80fea2012-12-09 20:29:00 +0100286 S /= (double) NR_TIME_ITERS;
Jens Axboe89db7272012-12-10 08:29:56 +0100287 mean /= 10.0;
Jens Axboec223da82010-03-24 13:23:53 +0100288
Jens Axboefa80fea2012-12-09 20:29:00 +0100289 for (i = 0; i < NR_TIME_ITERS; i++)
Jens Axboe4b91ee82013-02-25 10:18:33 +0100290 dprint(FD_TIME, "cycles[%d]=%llu\n", i,
291 (unsigned long long) cycles[i] / 10);
Jens Axboec223da82010-03-24 13:23:53 +0100292
Jens Axboed7abad32012-12-10 10:15:59 +0100293 avg /= samples;
Jens Axboeb0ff22d2013-01-01 13:38:18 +0100294 avg = (avg + 5) / 10;
Jens Axboe4b91ee82013-02-25 10:18:33 +0100295 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
Jens Axboec223da82010-03-24 13:23:53 +0100296 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
297
298 cycles_per_usec = avg;
Jens Axboe71339112012-12-21 22:54:56 +0100299 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
Jens Axboe400531d2014-12-16 22:40:37 -0700300 max_cycles_for_mult = ~0ULL / inv_cycles_per_usec;
Jens Axboe71339112012-12-21 22:54:56 +0100301 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
Jens Axboee2598792013-02-24 21:29:35 +0100302 return 0;
Jens Axboe09a32402010-08-15 15:01:51 -0400303}
304#else
Jens Axboee2598792013-02-24 21:29:35 +0100305static int calibrate_cpu_clock(void)
Jens Axboe09a32402010-08-15 15:01:51 -0400306{
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200307#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
308 return 0;
309#else
Jens Axboee2598792013-02-24 21:29:35 +0100310 return 1;
Jens Axboe09a32402010-08-15 15:01:51 -0400311#endif
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200312}
313#endif // ARCH_HAVE_CPU_CLOCK
Jens Axboe09a32402010-08-15 15:01:51 -0400314
Jens Axboe67bf9822013-01-10 11:23:19 +0100315#ifndef CONFIG_TLS_THREAD
Jens Axboe5d879392012-12-18 09:12:27 +0100316void fio_local_clock_init(int is_thread)
317{
318 struct tv_valid *t;
319
Jens Axboe572cfb32013-12-06 12:02:10 -0700320 t = calloc(1, sizeof(*t));
Jens Axboea7d78f22014-12-16 15:13:45 -0700321 if (pthread_setspecific(tv_tls_key, t)) {
Jens Axboe5d879392012-12-18 09:12:27 +0100322 log_err("fio: can't set TLS key\n");
Jens Axboea7d78f22014-12-16 15:13:45 -0700323 assert(0);
324 }
Jens Axboe5d879392012-12-18 09:12:27 +0100325}
326
327static void kill_tv_tls_key(void *data)
328{
329 free(data);
330}
Jens Axboe67bf9822013-01-10 11:23:19 +0100331#else
332void fio_local_clock_init(int is_thread)
333{
334}
335#endif
Jens Axboe5d879392012-12-18 09:12:27 +0100336
Jens Axboe09a32402010-08-15 15:01:51 -0400337void fio_clock_init(void)
338{
Jens Axboe01423ea2012-12-14 20:37:06 +0100339 if (fio_clock_source == fio_clock_source_inited)
340 return;
341
Jens Axboe67bf9822013-01-10 11:23:19 +0100342#ifndef CONFIG_TLS_THREAD
Jens Axboe5d879392012-12-18 09:12:27 +0100343 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
344 log_err("fio: can't create TLS key\n");
Jens Axboe67bf9822013-01-10 11:23:19 +0100345#endif
Jens Axboe5d879392012-12-18 09:12:27 +0100346
Jens Axboe01423ea2012-12-14 20:37:06 +0100347 fio_clock_source_inited = fio_clock_source;
Jens Axboee2598792013-02-24 21:29:35 +0100348
349 if (calibrate_cpu_clock())
350 tsc_reliable = 0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100351
352 /*
353 * If the arch sets tsc_reliable != 0, then it must be good enough
354 * to use as THE clock source. For x86 CPUs, this means the TSC
355 * runs at a constant rate and is synced across CPU cores.
356 */
357 if (tsc_reliable) {
358 if (!fio_clock_source_set)
359 fio_clock_source = CS_CPUCLOCK;
360 } else if (fio_clock_source == CS_CPUCLOCK)
361 log_info("fio: clocksource=cpu may not be reliable\n");
Jens Axboec223da82010-03-24 13:23:53 +0100362}
363
Jens Axboe0cbbc392014-09-30 16:04:12 -0600364uint64_t utime_since(const struct timeval *s, const struct timeval *e)
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100365{
Jens Axboe39ab7da2012-11-06 22:10:43 +0100366 long sec, usec;
Jens Axboeaa60bc52013-01-04 13:24:52 +0100367 uint64_t ret;
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100368
Jens Axboe39ab7da2012-11-06 22:10:43 +0100369 sec = e->tv_sec - s->tv_sec;
370 usec = e->tv_usec - s->tv_usec;
371 if (sec > 0 && usec < 0) {
372 sec--;
373 usec += 1000000;
374 }
Jens Axboe783a3eb2012-02-09 10:27:10 +0100375
376 /*
Jens Axboe39ab7da2012-11-06 22:10:43 +0100377 * time warp bug on some kernels?
Jens Axboe783a3eb2012-02-09 10:27:10 +0100378 */
Jens Axboe39ab7da2012-11-06 22:10:43 +0100379 if (sec < 0 || (sec == 0 && usec < 0))
380 return 0;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100381
Jens Axboe39ab7da2012-11-06 22:10:43 +0100382 ret = sec * 1000000ULL + usec;
383
384 return ret;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100385}
386
Jens Axboe0cbbc392014-09-30 16:04:12 -0600387uint64_t utime_since_now(const struct timeval *s)
Jens Axboe783a3eb2012-02-09 10:27:10 +0100388{
Jens Axboe39ab7da2012-11-06 22:10:43 +0100389 struct timeval t;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100390
Jens Axboe39ab7da2012-11-06 22:10:43 +0100391 fio_gettime(&t, NULL);
392 return utime_since(s, &t);
393}
Jens Axboe783a3eb2012-02-09 10:27:10 +0100394
Jens Axboe0cbbc392014-09-30 16:04:12 -0600395uint64_t mtime_since(const struct timeval *s, const struct timeval *e)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100396{
397 long sec, usec, ret;
398
399 sec = e->tv_sec - s->tv_sec;
400 usec = e->tv_usec - s->tv_usec;
401 if (sec > 0 && usec < 0) {
402 sec--;
403 usec += 1000000;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100404 }
405
Jens Axboe39ab7da2012-11-06 22:10:43 +0100406 if (sec < 0 || (sec == 0 && usec < 0))
407 return 0;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100408
Jens Axboe39ab7da2012-11-06 22:10:43 +0100409 sec *= 1000UL;
410 usec /= 1000UL;
411 ret = sec + usec;
412
Jens Axboe783a3eb2012-02-09 10:27:10 +0100413 return ret;
414}
Jens Axboe39ab7da2012-11-06 22:10:43 +0100415
Jens Axboe0cbbc392014-09-30 16:04:12 -0600416uint64_t mtime_since_now(const struct timeval *s)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100417{
418 struct timeval t;
419 void *p = __builtin_return_address(0);
420
421 fio_gettime(&t, p);
422 return mtime_since(s, &t);
423}
424
Jens Axboe0cbbc392014-09-30 16:04:12 -0600425uint64_t time_since_now(const struct timeval *s)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100426{
427 return mtime_since_now(s) / 1000;
428}
Jens Axboe7d11f872012-12-17 12:03:29 +0100429
Jens Axboe67bf9822013-01-10 11:23:19 +0100430#if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
431 defined(CONFIG_SFAA)
Jens Axboe7d11f872012-12-17 12:03:29 +0100432
433#define CLOCK_ENTRIES 100000
434
435struct clock_entry {
Jens Axboe58002f92013-02-25 10:23:58 +0100436 uint32_t seq;
437 uint32_t cpu;
Jens Axboeba458c22013-01-21 05:38:22 -0700438 uint64_t tsc;
Jens Axboe7d11f872012-12-17 12:03:29 +0100439};
440
441struct clock_thread {
442 pthread_t thread;
443 int cpu;
444 pthread_mutex_t lock;
445 pthread_mutex_t started;
Jens Axboe58002f92013-02-25 10:23:58 +0100446 uint32_t *seq;
Jens Axboe7d11f872012-12-17 12:03:29 +0100447 struct clock_entry *entries;
448};
449
Jens Axboe58002f92013-02-25 10:23:58 +0100450static inline uint32_t atomic32_inc_return(uint32_t *seq)
Jens Axboe7d11f872012-12-17 12:03:29 +0100451{
452 return 1 + __sync_fetch_and_add(seq, 1);
453}
454
455static void *clock_thread_fn(void *data)
456{
457 struct clock_thread *t = data;
458 struct clock_entry *c;
459 os_cpu_mask_t cpu_mask;
Jens Axboe58002f92013-02-25 10:23:58 +0100460 uint32_t last_seq;
Jens Axboe7d11f872012-12-17 12:03:29 +0100461 int i;
462
463 memset(&cpu_mask, 0, sizeof(cpu_mask));
464 fio_cpu_set(&cpu_mask, t->cpu);
465
466 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
467 log_err("clock setaffinity failed\n");
468 return (void *) 1;
469 }
470
Jens Axboe7d11f872012-12-17 12:03:29 +0100471 pthread_mutex_lock(&t->lock);
Jens Axboeb9b34982012-12-17 14:23:47 +0100472 pthread_mutex_unlock(&t->started);
Jens Axboe7d11f872012-12-17 12:03:29 +0100473
Jens Axboe58002f92013-02-25 10:23:58 +0100474 last_seq = 0;
Jens Axboe7d11f872012-12-17 12:03:29 +0100475 c = &t->entries[0];
476 for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
Jens Axboe58002f92013-02-25 10:23:58 +0100477 uint32_t seq;
478 uint64_t tsc;
Jens Axboe7d11f872012-12-17 12:03:29 +0100479
480 c->cpu = t->cpu;
481 do {
Jens Axboe58002f92013-02-25 10:23:58 +0100482 seq = atomic32_inc_return(t->seq);
483 if (seq < last_seq)
484 break;
Jens Axboe7d11f872012-12-17 12:03:29 +0100485 tsc = get_cpu_clock();
486 } while (seq != *t->seq);
487
488 c->seq = seq;
489 c->tsc = tsc;
490 }
491
Jens Axboe0f78b222013-02-26 13:54:20 +0100492 log_info("cs: cpu%3d: %llu clocks seen\n", t->cpu,
493 (unsigned long long) t->entries[i - 1].tsc - t->entries[0].tsc);
Jens Axboe58002f92013-02-25 10:23:58 +0100494
Jens Axboee2598792013-02-24 21:29:35 +0100495 /*
496 * The most common platform clock breakage is returning zero
497 * indefinitely. Check for that and return failure.
498 */
Jens Axboe58002f92013-02-25 10:23:58 +0100499 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
Jens Axboee2598792013-02-24 21:29:35 +0100500 return (void *) 1;
501
Jens Axboe7d11f872012-12-17 12:03:29 +0100502 return NULL;
503}
504
505static int clock_cmp(const void *p1, const void *p2)
506{
507 const struct clock_entry *c1 = p1;
508 const struct clock_entry *c2 = p2;
509
Jens Axboeb9b34982012-12-17 14:23:47 +0100510 if (c1->seq == c2->seq)
511 log_err("cs: bug in atomic sequence!\n");
512
Jens Axboe7d11f872012-12-17 12:03:29 +0100513 return c1->seq - c2->seq;
514}
515
516int fio_monotonic_clocktest(void)
517{
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700518 struct clock_thread *cthreads;
Jens Axboe7d11f872012-12-17 12:03:29 +0100519 unsigned int nr_cpus = cpus_online();
520 struct clock_entry *entries;
Jens Axboe814917b2014-04-14 12:20:04 -0600521 unsigned long tentries, failed = 0;
Bruce Cran80da8a82013-02-21 14:16:17 +0100522 struct clock_entry *prev, *this;
Jens Axboe58002f92013-02-25 10:23:58 +0100523 uint32_t seq = 0;
Jens Axboecaa3eb12014-04-14 09:05:46 -0600524 unsigned int i;
Jens Axboe7d11f872012-12-17 12:03:29 +0100525
Jens Axboed5e3f5d2013-01-18 20:13:45 +0100526 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
527
Jens Axboefc3f3e42014-09-24 09:54:24 -0600528#ifdef FIO_INC_DEBUG
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100529 fio_debug |= 1U << FD_TIME;
Jens Axboefc3f3e42014-09-24 09:54:24 -0600530#endif
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100531 calibrate_cpu_clock();
Jens Axboefc3f3e42014-09-24 09:54:24 -0600532#ifdef FIO_INC_DEBUG
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100533 fio_debug &= ~(1U << FD_TIME);
Jens Axboefc3f3e42014-09-24 09:54:24 -0600534#endif
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100535
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700536 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
Jens Axboe7d11f872012-12-17 12:03:29 +0100537 tentries = CLOCK_ENTRIES * nr_cpus;
538 entries = malloc(tentries * sizeof(struct clock_entry));
539
540 log_info("cs: Testing %u CPUs\n", nr_cpus);
541
542 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700543 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100544
545 t->cpu = i;
546 t->seq = &seq;
547 t->entries = &entries[i * CLOCK_ENTRIES];
548 pthread_mutex_init(&t->lock, NULL);
549 pthread_mutex_init(&t->started, NULL);
550 pthread_mutex_lock(&t->lock);
Jens Axboe6b0110c2014-04-14 12:14:17 -0600551 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
552 failed++;
553 nr_cpus = i;
554 break;
555 }
Jens Axboe7d11f872012-12-17 12:03:29 +0100556 }
557
558 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700559 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100560
561 pthread_mutex_lock(&t->started);
562 }
563
564 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700565 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100566
567 pthread_mutex_unlock(&t->lock);
568 }
569
Jens Axboe814917b2014-04-14 12:20:04 -0600570 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700571 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100572 void *ret;
573
574 pthread_join(t->thread, &ret);
575 if (ret)
576 failed++;
577 }
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700578 free(cthreads);
Jens Axboe7d11f872012-12-17 12:03:29 +0100579
580 if (failed) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200581 log_err("Clocksource test: %lu threads failed\n", failed);
Jens Axboe7d11f872012-12-17 12:03:29 +0100582 goto err;
583 }
584
585 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
586
587 for (failed = i = 0; i < tentries; i++) {
Bruce Cran80da8a82013-02-21 14:16:17 +0100588 this = &entries[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100589
590 if (!i) {
591 prev = this;
592 continue;
593 }
594
595 if (prev->tsc > this->tsc) {
596 uint64_t diff = prev->tsc - this->tsc;
597
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200598 log_info("cs: CPU clock mismatch (diff=%llu):\n",
599 (unsigned long long) diff);
600 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
601 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
Jens Axboe7d11f872012-12-17 12:03:29 +0100602 failed++;
603 }
604
605 prev = this;
606 }
607
608 if (failed)
609 log_info("cs: Failed: %lu\n", failed);
610 else
611 log_info("cs: Pass!\n");
612
613err:
614 free(entries);
615 return !!failed;
616}
617
618#else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
619
620int fio_monotonic_clocktest(void)
621{
622 log_info("cs: current platform does not support CPU clocks\n");
623 return 0;
624}
625
626#endif