Fix overflow caused by signed long division by unsigned long.
[fio.git] / gettime.c
CommitLineData
02bcaa8c 1/*
f5cc024a 2 * Clock functions
02bcaa8c 3 */
f5cc024a 4
02bcaa8c 5#include <unistd.h>
c223da83 6#include <math.h>
02bcaa8c 7#include <sys/time.h>
03e20d68 8#include <time.h>
02bcaa8c
JA
9
10#include "fio.h"
be4ecfdf 11#include "smalloc.h"
02bcaa8c
JA
12
13#include "hash.h"
7d11f871 14#include "os/os.h"
02bcaa8c 15
dac499a0
AV
16#if defined(ARCH_HAVE_CPU_CLOCK)
17#ifndef ARCH_CPU_CLOCK_CYCLES_PER_USEC
c223da83 18static unsigned long cycles_per_usec;
71339117 19static unsigned long inv_cycles_per_usec;
b3fa625b 20static uint64_t max_cycles_for_mult;
96170421
CE
21#endif
22#ifdef ARCH_CPU_CLOCK_WRAPS
73df3e07 23static unsigned long long cycles_start, cycles_wrap;
09a32402 24#endif
dac499a0 25#endif
4de98eb0 26int tsc_reliable = 0;
5d879392
JA
27
28struct tv_valid {
ba458c2f 29 uint64_t last_cycles;
02dcf81c
JA
30 int last_tv_valid;
31 int warned;
5d879392 32};
03be65c9 33#ifdef ARCH_HAVE_CPU_CLOCK
67bf9823 34#ifdef CONFIG_TLS_THREAD
b4ea84da 35static __thread struct tv_valid static_tv_valid;
67bf9823 36#else
5d879392 37static pthread_key_t tv_tls_key;
67bf9823 38#endif
03be65c9 39#endif
02bcaa8c 40
16de1bf9 41enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
fa80feae 42int fio_clock_source_set = 0;
10aa136b 43static enum fio_cs fio_clock_source_inited = CS_INVAL;
c223da83 44
02bcaa8c
JA
45#ifdef FIO_DEBUG_TIME
46
47#define HASH_BITS 8
48#define HASH_SIZE (1 << HASH_BITS)
49
01743ee1 50static struct flist_head hash[HASH_SIZE];
02bcaa8c
JA
51static int gtod_inited;
52
53struct gtod_log {
01743ee1 54 struct flist_head list;
02bcaa8c
JA
55 void *caller;
56 unsigned long calls;
57};
58
59static struct gtod_log *find_hash(void *caller)
60{
61 unsigned long h = hash_ptr(caller, HASH_BITS);
01743ee1 62 struct flist_head *entry;
02bcaa8c 63
01743ee1
JA
64 flist_for_each(entry, &hash[h]) {
65 struct gtod_log *log = flist_entry(entry, struct gtod_log,
66 list);
02bcaa8c
JA
67
68 if (log->caller == caller)
69 return log;
70 }
71
72 return NULL;
73}
74
d5e16441 75static void inc_caller(void *caller)
02bcaa8c
JA
76{
77 struct gtod_log *log = find_hash(caller);
78
79 if (!log) {
80 unsigned long h;
81
82 log = malloc(sizeof(*log));
01743ee1 83 INIT_FLIST_HEAD(&log->list);
02bcaa8c
JA
84 log->caller = caller;
85 log->calls = 0;
86
87 h = hash_ptr(caller, HASH_BITS);
01743ee1 88 flist_add_tail(&log->list, &hash[h]);
02bcaa8c
JA
89 }
90
d5e16441 91 log->calls++;
02bcaa8c
JA
92}
93
94static void gtod_log_caller(void *caller)
95{
d5e16441
JA
96 if (gtod_inited)
97 inc_caller(caller);
02bcaa8c
JA
98}
99
100static void fio_exit fio_dump_gtod(void)
101{
102 unsigned long total_calls = 0;
103 int i;
104
105 for (i = 0; i < HASH_SIZE; i++) {
01743ee1 106 struct flist_head *entry;
02bcaa8c
JA
107 struct gtod_log *log;
108
01743ee1
JA
109 flist_for_each(entry, &hash[i]) {
110 log = flist_entry(entry, struct gtod_log, list);
02bcaa8c 111
5ec10eaa
JA
112 printf("function %p, calls %lu\n", log->caller,
113 log->calls);
02bcaa8c
JA
114 total_calls += log->calls;
115 }
116 }
117
118 printf("Total %lu gettimeofday\n", total_calls);
119}
120
121static void fio_init gtod_init(void)
122{
123 int i;
124
125 for (i = 0; i < HASH_SIZE; i++)
01743ee1 126 INIT_FLIST_HEAD(&hash[i]);
02bcaa8c
JA
127
128 gtod_inited = 1;
129}
130
131#endif /* FIO_DEBUG_TIME */
132
67bf9823 133#ifdef CONFIG_CLOCK_GETTIME
9ff1c070
JA
134static int fill_clock_gettime(struct timespec *ts)
135{
c544f604
SN
136#if defined(CONFIG_CLOCK_MONOTONIC_RAW)
137 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
138#elif defined(CONFIG_CLOCK_MONOTONIC)
9ff1c070
JA
139 return clock_gettime(CLOCK_MONOTONIC, ts);
140#else
141 return clock_gettime(CLOCK_REALTIME, ts);
142#endif
143}
1e97cce9 144#endif
67bf9823 145
9eb271b9 146static void __fio_gettime(struct timeval *tp)
02bcaa8c 147{
c223da83 148 switch (fio_clock_source) {
67bf9823 149#ifdef CONFIG_GETTIMEOFDAY
c223da83 150 case CS_GTOD:
02bcaa8c 151 gettimeofday(tp, NULL);
c223da83 152 break;
67bf9823
JA
153#endif
154#ifdef CONFIG_CLOCK_GETTIME
c223da83 155 case CS_CGETTIME: {
02bcaa8c
JA
156 struct timespec ts;
157
9ff1c070 158 if (fill_clock_gettime(&ts) < 0) {
c223da83
JA
159 log_err("fio: clock_gettime fails\n");
160 assert(0);
02bcaa8c
JA
161 }
162
163 tp->tv_sec = ts.tv_sec;
164 tp->tv_usec = ts.tv_nsec / 1000;
c223da83
JA
165 break;
166 }
67bf9823 167#endif
c223da83
JA
168#ifdef ARCH_HAVE_CPU_CLOCK
169 case CS_CPUCLOCK: {
ba458c2f 170 uint64_t usecs, t;
03be65c9
JA
171 struct tv_valid *tv;
172
173#ifdef CONFIG_TLS_THREAD
174 tv = &static_tv_valid;
175#else
176 tv = pthread_getspecific(tv_tls_key);
177#endif
c223da83
JA
178
179 t = get_cpu_clock();
96170421 180#ifdef ARCH_CPU_CLOCK_WRAPS
73df3e07
JA
181 if (t < cycles_start && !cycles_wrap)
182 cycles_wrap = 1;
03be65c9
JA
183 else if (cycles_wrap && t >= cycles_start && !tv->warned) {
184 log_err("fio: double CPU clock wrap\n");
185 tv->warned = 1;
02dcf81c 186 }
c223da83 187
73df3e07 188 t -= cycles_start;
96170421 189#endif
9eb271b9
JA
190 tv->last_cycles = t;
191 tv->last_tv_valid = 1;
919e789d
CE
192#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
193 usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
194#else
b3fa625b
JA
195 if (t < max_cycles_for_mult)
196 usecs = (t * inv_cycles_per_usec) / 16777216UL;
197 else
198 usecs = t / cycles_per_usec;
919e789d 199#endif
c223da83
JA
200 tp->tv_sec = usecs / 1000000;
201 tp->tv_usec = usecs % 1000000;
c223da83
JA
202 break;
203 }
204#endif
205 default:
206 log_err("fio: invalid clock source %d\n", fio_clock_source);
207 break;
02bcaa8c 208 }
67bf9823
JA
209}
210
211#ifdef FIO_DEBUG_TIME
212void fio_gettime(struct timeval *tp, void *caller)
213#else
214void fio_gettime(struct timeval *tp, void fio_unused *caller)
215#endif
216{
67bf9823
JA
217#ifdef FIO_DEBUG_TIME
218 if (!caller)
219 caller = __builtin_return_address(0);
220
221 gtod_log_caller(caller);
222#endif
27325ed5 223 if (fio_unlikely(fio_gettime_offload(tp)))
67bf9823 224 return;
67bf9823 225
9eb271b9 226 __fio_gettime(tp);
02bcaa8c 227}
be4ecfdf 228
919e789d 229#if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
c223da83
JA
230static unsigned long get_cycles_per_usec(void)
231{
232 struct timeval s, e;
ba458c2f 233 uint64_t c_s, c_e;
67bf9823 234 enum fio_cs old_cs = fio_clock_source;
99afcdb5 235 uint64_t elapsed;
c223da83 236
67bf9823
JA
237#ifdef CONFIG_CLOCK_GETTIME
238 fio_clock_source = CS_CGETTIME;
239#else
240 fio_clock_source = CS_GTOD;
241#endif
242 __fio_gettime(&s);
9ff1c070 243
c223da83
JA
244 c_s = get_cpu_clock();
245 do {
67bf9823 246 __fio_gettime(&e);
9ff1c070 247
c223da83 248 elapsed = utime_since(&s, &e);
486332e5 249 if (elapsed >= 1280) {
c223da83
JA
250 c_e = get_cpu_clock();
251 break;
252 }
253 } while (1);
254
67bf9823 255 fio_clock_source = old_cs;
13aa415a 256 return (c_e - c_s) / elapsed;
c223da83
JA
257}
258
fa80feae
JA
259#define NR_TIME_ITERS 50
260
e259879e 261static int calibrate_cpu_clock(void)
c223da83
JA
262{
263 double delta, mean, S;
e51a6629 264 uint64_t minc, maxc, avg, cycles[NR_TIME_ITERS];
c223da83
JA
265 int i, samples;
266
c223da83
JA
267 cycles[0] = get_cycles_per_usec();
268 S = delta = mean = 0.0;
fa80feae 269 for (i = 0; i < NR_TIME_ITERS; i++) {
c223da83
JA
270 cycles[i] = get_cycles_per_usec();
271 delta = cycles[i] - mean;
272 if (delta) {
273 mean += delta / (i + 1.0);
274 S += delta * (cycles[i] - mean);
275 }
276 }
277
e259879e
JA
278 /*
279 * The most common platform clock breakage is returning zero
280 * indefinitely. Check for that and return failure.
281 */
282 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
283 return 1;
284
fa80feae 285 S = sqrt(S / (NR_TIME_ITERS - 1.0));
c223da83 286
e51a6629
JA
287 minc = -1ULL;
288 maxc = samples = avg = 0;
fa80feae 289 for (i = 0; i < NR_TIME_ITERS; i++) {
c223da83
JA
290 double this = cycles[i];
291
e51a6629
JA
292 minc = min(cycles[i], minc);
293 maxc = max(cycles[i], maxc);
294
03e20d68 295 if ((fmax(this, mean) - fmin(this, mean)) > S)
c223da83
JA
296 continue;
297 samples++;
298 avg += this;
299 }
300
fa80feae 301 S /= (double) NR_TIME_ITERS;
c223da83 302
fa80feae 303 for (i = 0; i < NR_TIME_ITERS; i++)
13aa415a 304 dprint(FD_TIME, "cycles[%d]=%llu\n", i, (unsigned long long) cycles[i]);
c223da83 305
d7abad3d 306 avg /= samples;
4b91ee8f 307 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
e51a6629
JA
308 dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f\n",
309 (unsigned long long) minc,
310 (unsigned long long) maxc, mean, S);
c223da83
JA
311
312 cycles_per_usec = avg;
71339117 313 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
b3fa625b 314 max_cycles_for_mult = ~0ULL / inv_cycles_per_usec;
71339117 315 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
96170421 316#ifdef ARCH_CPU_CLOCK_WRAPS
73df3e07
JA
317 cycles_start = get_cpu_clock();
318 dprint(FD_TIME, "cycles_start=%llu\n", cycles_start);
96170421 319#endif
e259879e 320 return 0;
09a32402
JA
321}
322#else
e259879e 323static int calibrate_cpu_clock(void)
09a32402 324{
919e789d
CE
325#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
326 return 0;
327#else
e259879e 328 return 1;
09a32402 329#endif
919e789d
CE
330}
331#endif // ARCH_HAVE_CPU_CLOCK
09a32402 332
67bf9823 333#ifndef CONFIG_TLS_THREAD
5d879392
JA
334void fio_local_clock_init(int is_thread)
335{
336 struct tv_valid *t;
337
572cfb3f 338 t = calloc(1, sizeof(*t));
9eb271b9 339 if (pthread_setspecific(tv_tls_key, t)) {
5d879392 340 log_err("fio: can't set TLS key\n");
9eb271b9
JA
341 assert(0);
342 }
5d879392
JA
343}
344
345static void kill_tv_tls_key(void *data)
346{
347 free(data);
348}
67bf9823
JA
349#else
350void fio_local_clock_init(int is_thread)
351{
352}
353#endif
5d879392 354
09a32402
JA
355void fio_clock_init(void)
356{
01423eae
JA
357 if (fio_clock_source == fio_clock_source_inited)
358 return;
359
67bf9823 360#ifndef CONFIG_TLS_THREAD
5d879392
JA
361 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
362 log_err("fio: can't create TLS key\n");
67bf9823 363#endif
5d879392 364
01423eae 365 fio_clock_source_inited = fio_clock_source;
e259879e
JA
366
367 if (calibrate_cpu_clock())
368 tsc_reliable = 0;
fa80feae
JA
369
370 /*
371 * If the arch sets tsc_reliable != 0, then it must be good enough
372 * to use as THE clock source. For x86 CPUs, this means the TSC
373 * runs at a constant rate and is synced across CPU cores.
374 */
375 if (tsc_reliable) {
aad918e4 376 if (!fio_clock_source_set && !fio_monotonic_clocktest(0))
fa80feae
JA
377 fio_clock_source = CS_CPUCLOCK;
378 } else if (fio_clock_source == CS_CPUCLOCK)
379 log_info("fio: clocksource=cpu may not be reliable\n");
c223da83
JA
380}
381
1f440ece 382uint64_t utime_since(const struct timeval *s, const struct timeval *e)
be4ecfdf 383{
39ab7da2 384 long sec, usec;
aa60bc58 385 uint64_t ret;
39ab7da2
JA
386
387 sec = e->tv_sec - s->tv_sec;
388 usec = e->tv_usec - s->tv_usec;
389 if (sec > 0 && usec < 0) {
390 sec--;
391 usec += 1000000;
392 }
393
394 /*
395 * time warp bug on some kernels?
396 */
397 if (sec < 0 || (sec == 0 && usec < 0))
398 return 0;
399
400 ret = sec * 1000000ULL + usec;
401
402 return ret;
be4ecfdf
JA
403}
404
1f440ece 405uint64_t utime_since_now(const struct timeval *s)
be4ecfdf 406{
39ab7da2
JA
407 struct timeval t;
408
409 fio_gettime(&t, NULL);
410 return utime_since(s, &t);
be4ecfdf 411}
783a3eb1 412
1f440ece 413uint64_t mtime_since(const struct timeval *s, const struct timeval *e)
783a3eb1 414{
39ab7da2 415 long sec, usec, ret;
783a3eb1 416
39ab7da2
JA
417 sec = e->tv_sec - s->tv_sec;
418 usec = e->tv_usec - s->tv_usec;
419 if (sec > 0 && usec < 0) {
420 sec--;
421 usec += 1000000;
783a3eb1
JA
422 }
423
39ab7da2
JA
424 if (sec < 0 || (sec == 0 && usec < 0))
425 return 0;
426
be6bb2b7
Y
427 sec *= 1000;
428 usec /= 1000;
39ab7da2
JA
429 ret = sec + usec;
430
431 return ret;
783a3eb1
JA
432}
433
1f440ece 434uint64_t mtime_since_now(const struct timeval *s)
783a3eb1 435{
39ab7da2
JA
436 struct timeval t;
437 void *p = __builtin_return_address(0);
783a3eb1 438
39ab7da2
JA
439 fio_gettime(&t, p);
440 return mtime_since(s, &t);
441}
783a3eb1 442
1f440ece 443uint64_t time_since_now(const struct timeval *s)
39ab7da2
JA
444{
445 return mtime_since_now(s) / 1000;
783a3eb1 446}
7d11f871 447
67bf9823
JA
448#if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
449 defined(CONFIG_SFAA)
7d11f871 450
aad918e4
JA
451#define CLOCK_ENTRIES_DEBUG 100000
452#define CLOCK_ENTRIES_TEST 10000
7d11f871
JA
453
454struct clock_entry {
58002f9a
JA
455 uint32_t seq;
456 uint32_t cpu;
ba458c2f 457 uint64_t tsc;
7d11f871
JA
458};
459
460struct clock_thread {
461 pthread_t thread;
462 int cpu;
aad918e4 463 int debug;
7d11f871
JA
464 pthread_mutex_t lock;
465 pthread_mutex_t started;
aad918e4 466 unsigned long nr_entries;
58002f9a 467 uint32_t *seq;
7d11f871
JA
468 struct clock_entry *entries;
469};
470
58002f9a 471static inline uint32_t atomic32_inc_return(uint32_t *seq)
7d11f871
JA
472{
473 return 1 + __sync_fetch_and_add(seq, 1);
474}
475
476static void *clock_thread_fn(void *data)
477{
478 struct clock_thread *t = data;
479 struct clock_entry *c;
480 os_cpu_mask_t cpu_mask;
58002f9a 481 uint32_t last_seq;
5896d827 482 unsigned long long first;
7d11f871
JA
483 int i;
484
c763aea6 485 if (fio_cpuset_init(&cpu_mask)) {
37e20021 486 int __err = errno;
c763aea6
JA
487
488 log_err("clock cpuset init failed: %s\n", strerror(__err));
489 goto err_out;
490 }
491
7d11f871
JA
492 fio_cpu_set(&cpu_mask, t->cpu);
493
494 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
e66d7f90
JA
495 int __err = errno;
496
497 log_err("clock setaffinity failed: %s\n", strerror(__err));
c763aea6 498 goto err;
7d11f871
JA
499 }
500
7d11f871 501 pthread_mutex_lock(&t->lock);
b9b3498e 502 pthread_mutex_unlock(&t->started);
7d11f871 503
5896d827 504 first = get_cpu_clock();
58002f9a 505 last_seq = 0;
7d11f871 506 c = &t->entries[0];
aad918e4 507 for (i = 0; i < t->nr_entries; i++, c++) {
58002f9a
JA
508 uint32_t seq;
509 uint64_t tsc;
7d11f871
JA
510
511 c->cpu = t->cpu;
512 do {
58002f9a
JA
513 seq = atomic32_inc_return(t->seq);
514 if (seq < last_seq)
515 break;
7d11f871
JA
516 tsc = get_cpu_clock();
517 } while (seq != *t->seq);
518
519 c->seq = seq;
520 c->tsc = tsc;
521 }
522
aad918e4
JA
523 if (t->debug) {
524 unsigned long long clocks;
525
526 clocks = t->entries[i - 1].tsc - t->entries[0].tsc;
5896d827
JA
527 log_info("cs: cpu%3d: %llu clocks seen, first %llu\n", t->cpu,
528 clocks, first);
aad918e4 529 }
58002f9a 530
e259879e
JA
531 /*
532 * The most common platform clock breakage is returning zero
533 * indefinitely. Check for that and return failure.
534 */
58002f9a 535 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
c763aea6 536 goto err;
e259879e 537
d47d7cb3 538 fio_cpuset_exit(&cpu_mask);
7d11f871 539 return NULL;
c763aea6
JA
540err:
541 fio_cpuset_exit(&cpu_mask);
542err_out:
543 return (void *) 1;
7d11f871
JA
544}
545
546static int clock_cmp(const void *p1, const void *p2)
547{
548 const struct clock_entry *c1 = p1;
549 const struct clock_entry *c2 = p2;
550
b9b3498e
JA
551 if (c1->seq == c2->seq)
552 log_err("cs: bug in atomic sequence!\n");
553
7d11f871
JA
554 return c1->seq - c2->seq;
555}
556
aad918e4 557int fio_monotonic_clocktest(int debug)
7d11f871 558{
8a1db9a1 559 struct clock_thread *cthreads;
7d11f871
JA
560 unsigned int nr_cpus = cpus_online();
561 struct clock_entry *entries;
aad918e4 562 unsigned long nr_entries, tentries, failed = 0;
80da8a8f 563 struct clock_entry *prev, *this;
58002f9a 564 uint32_t seq = 0;
caa3eb1c 565 unsigned int i;
7d11f871 566
aad918e4
JA
567 if (debug) {
568 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
d5e3f5d8 569
b5b571a3 570#ifdef FIO_INC_DEBUG
aad918e4 571 fio_debug |= 1U << FD_TIME;
b5b571a3 572#endif
aad918e4
JA
573 nr_entries = CLOCK_ENTRIES_DEBUG;
574 } else
575 nr_entries = CLOCK_ENTRIES_TEST;
576
4f1d43c2 577 calibrate_cpu_clock();
aad918e4
JA
578
579 if (debug) {
b5b571a3 580#ifdef FIO_INC_DEBUG
aad918e4 581 fio_debug &= ~(1U << FD_TIME);
b5b571a3 582#endif
aad918e4 583 }
4f1d43c2 584
8a1db9a1 585 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
aad918e4 586 tentries = nr_entries * nr_cpus;
7d11f871
JA
587 entries = malloc(tentries * sizeof(struct clock_entry));
588
aad918e4
JA
589 if (debug)
590 log_info("cs: Testing %u CPUs\n", nr_cpus);
7d11f871
JA
591
592 for (i = 0; i < nr_cpus; i++) {
8a1db9a1 593 struct clock_thread *t = &cthreads[i];
7d11f871
JA
594
595 t->cpu = i;
aad918e4 596 t->debug = debug;
7d11f871 597 t->seq = &seq;
aad918e4
JA
598 t->nr_entries = nr_entries;
599 t->entries = &entries[i * nr_entries];
7d11f871
JA
600 pthread_mutex_init(&t->lock, NULL);
601 pthread_mutex_init(&t->started, NULL);
602 pthread_mutex_lock(&t->lock);
6b0110cd
JA
603 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
604 failed++;
605 nr_cpus = i;
606 break;
607 }
7d11f871
JA
608 }
609
610 for (i = 0; i < nr_cpus; i++) {
8a1db9a1 611 struct clock_thread *t = &cthreads[i];
7d11f871
JA
612
613 pthread_mutex_lock(&t->started);
614 }
615
616 for (i = 0; i < nr_cpus; i++) {
8a1db9a1 617 struct clock_thread *t = &cthreads[i];
7d11f871
JA
618
619 pthread_mutex_unlock(&t->lock);
620 }
621
814917be 622 for (i = 0; i < nr_cpus; i++) {
8a1db9a1 623 struct clock_thread *t = &cthreads[i];
7d11f871
JA
624 void *ret;
625
626 pthread_join(t->thread, &ret);
627 if (ret)
628 failed++;
629 }
8a1db9a1 630 free(cthreads);
7d11f871
JA
631
632 if (failed) {
aad918e4
JA
633 if (debug)
634 log_err("Clocksource test: %lu threads failed\n", failed);
7d11f871
JA
635 goto err;
636 }
637
638 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
639
a4aa3dc0
JA
640 /* silence silly gcc */
641 prev = NULL;
7d11f871 642 for (failed = i = 0; i < tentries; i++) {
80da8a8f 643 this = &entries[i];
7d11f871
JA
644
645 if (!i) {
646 prev = this;
647 continue;
648 }
649
650 if (prev->tsc > this->tsc) {
651 uint64_t diff = prev->tsc - this->tsc;
652
aad918e4
JA
653 if (!debug) {
654 failed++;
655 break;
656 }
657
4e0a8fa2
JA
658 log_info("cs: CPU clock mismatch (diff=%llu):\n",
659 (unsigned long long) diff);
660 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
661 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
7d11f871
JA
662 failed++;
663 }
664
665 prev = this;
666 }
667
aad918e4
JA
668 if (debug) {
669 if (failed)
670 log_info("cs: Failed: %lu\n", failed);
671 else
672 log_info("cs: Pass!\n");
673 }
7d11f871
JA
674err:
675 free(entries);
676 return !!failed;
677}
678
679#else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
680
aad918e4 681int fio_monotonic_clocktest(int debug)
7d11f871 682{
aad918e4
JA
683 if (debug)
684 log_info("cs: current platform does not support CPU clocks\n");
685 return 1;
7d11f871
JA
686}
687
688#endif