Revert "ioengines: Make td_io_queue print log_err when got error "
[fio.git] / gettime.c
1 /*
2  * Clock functions
3  */
4
5 #include <math.h>
6
7 #include "fio.h"
8 #include "os/os.h"
9
10 #if defined(ARCH_HAVE_CPU_CLOCK)
11 #ifndef ARCH_CPU_CLOCK_CYCLES_PER_USEC
12 static unsigned long long cycles_per_msec;
13 static unsigned long long cycles_start;
14 static unsigned long long clock_mult;
15 static unsigned long long max_cycles_mask;
16 static unsigned long long nsecs_for_max_cycles;
17 static unsigned int clock_shift;
18 static unsigned int max_cycles_shift;
19 #define MAX_CLOCK_SEC 60*60
20 #endif
21 #ifdef ARCH_CPU_CLOCK_WRAPS
22 static unsigned int cycles_wrap;
23 #endif
24 #endif
25 bool tsc_reliable = false;
26
27 struct tv_valid {
28         int warned;
29 };
30 #ifdef ARCH_HAVE_CPU_CLOCK
31 #ifdef CONFIG_TLS_THREAD
32 static __thread struct tv_valid static_tv_valid;
33 #else
34 static pthread_key_t tv_tls_key;
35 #endif
36 #endif
37
38 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
39 int fio_clock_source_set = 0;
40 static enum fio_cs fio_clock_source_inited = CS_INVAL;
41
42 #ifdef FIO_DEBUG_TIME
43
44 #define HASH_BITS       8
45 #define HASH_SIZE       (1 << HASH_BITS)
46
47 static struct flist_head hash[HASH_SIZE];
48 static int gtod_inited;
49
50 struct gtod_log {
51         struct flist_head list;
52         void *caller;
53         unsigned long calls;
54 };
55
56 static struct gtod_log *find_hash(void *caller)
57 {
58         unsigned long h = hash_ptr(caller, HASH_BITS);
59         struct flist_head *entry;
60
61         flist_for_each(entry, &hash[h]) {
62                 struct gtod_log *log = flist_entry(entry, struct gtod_log,
63                                                                         list);
64
65                 if (log->caller == caller)
66                         return log;
67         }
68
69         return NULL;
70 }
71
72 static void inc_caller(void *caller)
73 {
74         struct gtod_log *log = find_hash(caller);
75
76         if (!log) {
77                 unsigned long h;
78
79                 log = malloc(sizeof(*log));
80                 INIT_FLIST_HEAD(&log->list);
81                 log->caller = caller;
82                 log->calls = 0;
83
84                 h = hash_ptr(caller, HASH_BITS);
85                 flist_add_tail(&log->list, &hash[h]);
86         }
87
88         log->calls++;
89 }
90
91 static void gtod_log_caller(void *caller)
92 {
93         if (gtod_inited)
94                 inc_caller(caller);
95 }
96
97 static void fio_exit fio_dump_gtod(void)
98 {
99         unsigned long total_calls = 0;
100         int i;
101
102         for (i = 0; i < HASH_SIZE; i++) {
103                 struct flist_head *entry;
104                 struct gtod_log *log;
105
106                 flist_for_each(entry, &hash[i]) {
107                         log = flist_entry(entry, struct gtod_log, list);
108
109                         printf("function %p, calls %lu\n", log->caller,
110                                                                 log->calls);
111                         total_calls += log->calls;
112                 }
113         }
114
115         printf("Total %lu gettimeofday\n", total_calls);
116 }
117
118 static void fio_init gtod_init(void)
119 {
120         int i;
121
122         for (i = 0; i < HASH_SIZE; i++)
123                 INIT_FLIST_HEAD(&hash[i]);
124
125         gtod_inited = 1;
126 }
127
128 #endif /* FIO_DEBUG_TIME */
129
130 /*
131  * Queries the value of the monotonic clock if a monotonic clock is available
132  * or the wall clock time if no monotonic clock is available. Returns 0 if
133  * querying the clock succeeded or -1 if querying the clock failed.
134  */
135 int fio_get_mono_time(struct timespec *ts)
136 {
137         int ret;
138
139 #ifdef CONFIG_CLOCK_GETTIME
140 #if defined(CONFIG_CLOCK_MONOTONIC)
141         ret = clock_gettime(CLOCK_MONOTONIC, ts);
142 #else
143         ret = clock_gettime(CLOCK_REALTIME, ts);
144 #endif
145 #else
146         struct timeval tv;
147
148         ret = gettimeofday(&tv, NULL);
149         if (ret == 0) {
150                 ts->tv_sec = tv.tv_sec;
151                 ts->tv_nsec = tv.tv_usec * 1000;
152         }
153 #endif
154         assert(ret <= 0);
155         return ret;
156 }
157
158 static void __fio_gettime(struct timespec *tp)
159 {
160         switch (fio_clock_source) {
161 #ifdef CONFIG_GETTIMEOFDAY
162         case CS_GTOD: {
163                 struct timeval tv;
164                 gettimeofday(&tv, NULL);
165
166                 tp->tv_sec = tv.tv_sec;
167                 tp->tv_nsec = tv.tv_usec * 1000;
168                 break;
169                 }
170 #endif
171 #ifdef CONFIG_CLOCK_GETTIME
172         case CS_CGETTIME: {
173                 if (fio_get_mono_time(tp) < 0) {
174                         log_err("fio: fio_get_mono_time() fails\n");
175                         assert(0);
176                 }
177                 break;
178                 }
179 #endif
180 #ifdef ARCH_HAVE_CPU_CLOCK
181         case CS_CPUCLOCK: {
182                 uint64_t nsecs, t, multiples;
183                 struct tv_valid *tv;
184
185 #ifdef CONFIG_TLS_THREAD
186                 tv = &static_tv_valid;
187 #else
188                 tv = pthread_getspecific(tv_tls_key);
189 #endif
190
191                 t = get_cpu_clock();
192 #ifdef ARCH_CPU_CLOCK_WRAPS
193                 if (t < cycles_start && !cycles_wrap)
194                         cycles_wrap = 1;
195                 else if (cycles_wrap && t >= cycles_start && !tv->warned) {
196                         log_err("fio: double CPU clock wrap\n");
197                         tv->warned = 1;
198                 }
199 #endif
200 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
201                 nsecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC * 1000;
202 #else
203                 t -= cycles_start;
204                 multiples = t >> max_cycles_shift;
205                 nsecs = multiples * nsecs_for_max_cycles;
206                 nsecs += ((t & max_cycles_mask) * clock_mult) >> clock_shift;
207 #endif
208                 tp->tv_sec = nsecs / 1000000000ULL;
209                 tp->tv_nsec = nsecs % 1000000000ULL;
210                 break;
211                 }
212 #endif
213         default:
214                 log_err("fio: invalid clock source %d\n", fio_clock_source);
215                 break;
216         }
217 }
218
219 #ifdef FIO_DEBUG_TIME
220 void fio_gettime(struct timespec *tp, void *caller)
221 #else
222 void fio_gettime(struct timespec *tp, void fio_unused *caller)
223 #endif
224 {
225 #ifdef FIO_DEBUG_TIME
226         if (!caller)
227                 caller = __builtin_return_address(0);
228
229         gtod_log_caller(caller);
230 #endif
231         if (fio_unlikely(fio_gettime_offload(tp)))
232                 return;
233
234         __fio_gettime(tp);
235 }
236
237 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
238 static unsigned long get_cycles_per_msec(void)
239 {
240         struct timespec s, e;
241         uint64_t c_s, c_e;
242         uint64_t elapsed;
243
244         fio_get_mono_time(&s);
245
246         c_s = get_cpu_clock();
247         do {
248                 fio_get_mono_time(&e);
249                 c_e = get_cpu_clock();
250
251                 elapsed = ntime_since(&s, &e);
252                 if (elapsed >= 1280000)
253                         break;
254         } while (1);
255
256         return (c_e - c_s) * 1000000 / elapsed;
257 }
258
259 #define NR_TIME_ITERS   50
260
261 static int calibrate_cpu_clock(void)
262 {
263         double delta, mean, S;
264         uint64_t minc, maxc, avg, cycles[NR_TIME_ITERS];
265         int i, samples, sft = 0;
266         unsigned long long tmp, max_ticks, max_mult;
267
268         cycles[0] = get_cycles_per_msec();
269         S = delta = mean = 0.0;
270         for (i = 0; i < NR_TIME_ITERS; i++) {
271                 cycles[i] = get_cycles_per_msec();
272                 delta = cycles[i] - mean;
273                 if (delta) {
274                         mean += delta / (i + 1.0);
275                         S += delta * (cycles[i] - mean);
276                 }
277         }
278
279         /*
280          * The most common platform clock breakage is returning zero
281          * indefinitely. Check for that and return failure.
282          */
283         if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
284                 return 1;
285
286         S = sqrt(S / (NR_TIME_ITERS - 1.0));
287
288         minc = -1ULL;
289         maxc = samples = avg = 0;
290         for (i = 0; i < NR_TIME_ITERS; i++) {
291                 double this = cycles[i];
292
293                 minc = min(cycles[i], minc);
294                 maxc = max(cycles[i], maxc);
295
296                 if ((fmax(this, mean) - fmin(this, mean)) > S)
297                         continue;
298                 samples++;
299                 avg += this;
300         }
301
302         S /= (double) NR_TIME_ITERS;
303
304         for (i = 0; i < NR_TIME_ITERS; i++)
305                 dprint(FD_TIME, "cycles[%d]=%llu\n", i, (unsigned long long) cycles[i]);
306
307         avg /= samples;
308         cycles_per_msec = avg;
309         dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f, N=%d\n",
310                         (unsigned long long) minc,
311                         (unsigned long long) maxc, mean, S, NR_TIME_ITERS);
312         dprint(FD_TIME, "trimmed mean=%llu, N=%d\n", (unsigned long long) avg, samples);
313
314         max_ticks = MAX_CLOCK_SEC * cycles_per_msec * 1000ULL;
315         max_mult = ULLONG_MAX / max_ticks;
316         dprint(FD_TIME, "\n\nmax_ticks=%llu, __builtin_clzll=%d, "
317                         "max_mult=%llu\n", max_ticks,
318                         __builtin_clzll(max_ticks), max_mult);
319
320         /*
321          * Find the largest shift count that will produce
322          * a multiplier that does not exceed max_mult
323          */
324         tmp = max_mult * cycles_per_msec / 1000000;
325         while (tmp > 1) {
326                 tmp >>= 1;
327                 sft++;
328                 dprint(FD_TIME, "tmp=%llu, sft=%u\n", tmp, sft);
329         }
330
331         clock_shift = sft;
332         clock_mult = (1ULL << sft) * 1000000 / cycles_per_msec;
333         dprint(FD_TIME, "clock_shift=%u, clock_mult=%llu\n", clock_shift,
334                                                         clock_mult);
335
336         /*
337          * Find the greatest power of 2 clock ticks that is less than the
338          * ticks in MAX_CLOCK_SEC_2STAGE
339          */
340         max_cycles_shift = max_cycles_mask = 0;
341         tmp = MAX_CLOCK_SEC * 1000ULL * cycles_per_msec;
342         dprint(FD_TIME, "tmp=%llu, max_cycles_shift=%u\n", tmp,
343                                                         max_cycles_shift);
344         while (tmp > 1) {
345                 tmp >>= 1;
346                 max_cycles_shift++;
347                 dprint(FD_TIME, "tmp=%llu, max_cycles_shift=%u\n", tmp, max_cycles_shift);
348         }
349         /*
350          * if use use (1ULL << max_cycles_shift) * 1000 / cycles_per_msec
351          * here we will have a discontinuity every
352          * (1ULL << max_cycles_shift) cycles
353          */
354         nsecs_for_max_cycles = ((1ULL << max_cycles_shift) * clock_mult)
355                                         >> clock_shift;
356
357         /* Use a bitmask to calculate ticks % (1ULL << max_cycles_shift) */
358         for (tmp = 0; tmp < max_cycles_shift; tmp++)
359                 max_cycles_mask |= 1ULL << tmp;
360
361         dprint(FD_TIME, "max_cycles_shift=%u, 2^max_cycles_shift=%llu, "
362                         "nsecs_for_max_cycles=%llu, "
363                         "max_cycles_mask=%016llx\n",
364                         max_cycles_shift, (1ULL << max_cycles_shift),
365                         nsecs_for_max_cycles, max_cycles_mask);
366
367         cycles_start = get_cpu_clock();
368         dprint(FD_TIME, "cycles_start=%llu\n", cycles_start);
369         return 0;
370 }
371 #else
372 static int calibrate_cpu_clock(void)
373 {
374 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
375         return 0;
376 #else
377         return 1;
378 #endif
379 }
380 #endif // ARCH_HAVE_CPU_CLOCK
381
382 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(CONFIG_TLS_THREAD)
383 void fio_local_clock_init(void)
384 {
385         struct tv_valid *t;
386
387         t = calloc(1, sizeof(*t));
388         if (pthread_setspecific(tv_tls_key, t)) {
389                 log_err("fio: can't set TLS key\n");
390                 assert(0);
391         }
392 }
393
394 static void kill_tv_tls_key(void *data)
395 {
396         free(data);
397 }
398 #else
399 void fio_local_clock_init(void)
400 {
401 }
402 #endif
403
404 void fio_clock_init(void)
405 {
406         if (fio_clock_source == fio_clock_source_inited)
407                 return;
408
409 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(CONFIG_TLS_THREAD)
410         if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
411                 log_err("fio: can't create TLS key\n");
412 #endif
413
414         fio_clock_source_inited = fio_clock_source;
415
416         if (calibrate_cpu_clock())
417                 tsc_reliable = false;
418
419         /*
420          * If the arch sets tsc_reliable != 0, then it must be good enough
421          * to use as THE clock source. For x86 CPUs, this means the TSC
422          * runs at a constant rate and is synced across CPU cores.
423          */
424         if (tsc_reliable) {
425                 if (!fio_clock_source_set && !fio_monotonic_clocktest(0))
426                         fio_clock_source = CS_CPUCLOCK;
427         } else if (fio_clock_source == CS_CPUCLOCK)
428                 log_info("fio: clocksource=cpu may not be reliable\n");
429         dprint(FD_TIME, "gettime: clocksource=%d\n", (int) fio_clock_source);
430 }
431
432 uint64_t ntime_since(const struct timespec *s, const struct timespec *e)
433 {
434        int64_t sec, nsec;
435
436        sec = e->tv_sec - s->tv_sec;
437        nsec = e->tv_nsec - s->tv_nsec;
438        if (sec > 0 && nsec < 0) {
439                sec--;
440                nsec += 1000000000LL;
441        }
442
443        /*
444         * time warp bug on some kernels?
445         */
446        if (sec < 0 || (sec == 0 && nsec < 0))
447                return 0;
448
449        return nsec + (sec * 1000000000LL);
450 }
451
452 uint64_t ntime_since_now(const struct timespec *s)
453 {
454         struct timespec now;
455
456         fio_gettime(&now, NULL);
457         return ntime_since(s, &now);
458 }
459
460 uint64_t utime_since(const struct timespec *s, const struct timespec *e)
461 {
462         int64_t sec, usec;
463
464         sec = e->tv_sec - s->tv_sec;
465         usec = (e->tv_nsec - s->tv_nsec) / 1000;
466         if (sec > 0 && usec < 0) {
467                 sec--;
468                 usec += 1000000;
469         }
470
471         /*
472          * time warp bug on some kernels?
473          */
474         if (sec < 0 || (sec == 0 && usec < 0))
475                 return 0;
476
477         return usec + (sec * 1000000);
478 }
479
480 uint64_t utime_since_now(const struct timespec *s)
481 {
482         struct timespec t;
483 #ifdef FIO_DEBUG_TIME
484         void *p = __builtin_return_address(0);
485
486         fio_gettime(&t, p);
487 #else
488         fio_gettime(&t, NULL);
489 #endif
490
491         return utime_since(s, &t);
492 }
493
494 uint64_t mtime_since_tv(const struct timeval *s, const struct timeval *e)
495 {
496         int64_t sec, usec;
497
498         sec = e->tv_sec - s->tv_sec;
499         usec = (e->tv_usec - s->tv_usec);
500         if (sec > 0 && usec < 0) {
501                 sec--;
502                 usec += 1000000;
503         }
504
505         if (sec < 0 || (sec == 0 && usec < 0))
506                 return 0;
507
508         sec *= 1000;
509         usec /= 1000;
510         return sec + usec;
511 }
512
513 uint64_t mtime_since_now(const struct timespec *s)
514 {
515         struct timespec t;
516 #ifdef FIO_DEBUG_TIME
517         void *p = __builtin_return_address(0);
518
519         fio_gettime(&t, p);
520 #else
521         fio_gettime(&t, NULL);
522 #endif
523
524         return mtime_since(s, &t);
525 }
526
527 /*
528  * Returns *e - *s in milliseconds as a signed integer. Note: rounding is
529  * asymmetric. If the difference yields +1 ns then 0 is returned. If the
530  * difference yields -1 ns then -1 is returned.
531  */
532 int64_t rel_time_since(const struct timespec *s, const struct timespec *e)
533 {
534         int64_t sec, nsec;
535
536         sec = e->tv_sec - s->tv_sec;
537         nsec = e->tv_nsec - s->tv_nsec;
538         if (nsec < 0) {
539                 sec--;
540                 nsec += 1000ULL * 1000 * 1000;
541         }
542         assert(0 <= nsec && nsec < 1000ULL * 1000 * 1000);
543
544         return sec * 1000 + nsec / (1000 * 1000);
545 }
546
547 /*
548  * Returns *e - *s in milliseconds as an unsigned integer. Returns 0 if
549  * *e < *s.
550  */
551 uint64_t mtime_since(const struct timespec *s, const struct timespec *e)
552 {
553         return max(rel_time_since(s, e), (int64_t)0);
554 }
555
556 uint64_t time_since_now(const struct timespec *s)
557 {
558         return mtime_since_now(s) / 1000;
559 }
560
561 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK)  && \
562     defined(CONFIG_SYNC_SYNC) && defined(CONFIG_CMP_SWAP)
563
564 #define CLOCK_ENTRIES_DEBUG     100000
565 #define CLOCK_ENTRIES_TEST      1000
566
567 struct clock_entry {
568         uint32_t seq;
569         uint32_t cpu;
570         uint64_t tsc;
571 };
572
573 struct clock_thread {
574         pthread_t thread;
575         int cpu;
576         int debug;
577         struct fio_sem lock;
578         unsigned long nr_entries;
579         uint32_t *seq;
580         struct clock_entry *entries;
581 };
582
583 static inline uint32_t atomic32_compare_and_swap(uint32_t *ptr, uint32_t old,
584                                                  uint32_t new)
585 {
586         return __sync_val_compare_and_swap(ptr, old, new);
587 }
588
589 static void *clock_thread_fn(void *data)
590 {
591         struct clock_thread *t = data;
592         struct clock_entry *c;
593         os_cpu_mask_t cpu_mask;
594         unsigned long long first;
595         int i;
596
597         if (fio_cpuset_init(&cpu_mask)) {
598                 int __err = errno;
599
600                 log_err("clock cpuset init failed: %s\n", strerror(__err));
601                 goto err_out;
602         }
603
604         fio_cpu_set(&cpu_mask, t->cpu);
605
606         if (fio_setaffinity(gettid(), cpu_mask) == -1) {
607                 int __err = errno;
608
609                 log_err("clock setaffinity failed: %s\n", strerror(__err));
610                 goto err;
611         }
612
613         fio_sem_down(&t->lock);
614
615         first = get_cpu_clock();
616         c = &t->entries[0];
617         for (i = 0; i < t->nr_entries; i++, c++) {
618                 uint32_t seq;
619                 uint64_t tsc;
620
621                 c->cpu = t->cpu;
622                 do {
623                         seq = *t->seq;
624                         if (seq == UINT_MAX)
625                                 break;
626                         __sync_synchronize();
627                         tsc = get_cpu_clock();
628                 } while (seq != atomic32_compare_and_swap(t->seq, seq, seq + 1));
629
630                 if (seq == UINT_MAX)
631                         break;
632
633                 c->seq = seq;
634                 c->tsc = tsc;
635         }
636
637         if (t->debug) {
638                 unsigned long long clocks;
639
640                 clocks = t->entries[i - 1].tsc - t->entries[0].tsc;
641                 log_info("cs: cpu%3d: %llu clocks seen, first %llu\n", t->cpu,
642                                                         clocks, first);
643         }
644
645         /*
646          * The most common platform clock breakage is returning zero
647          * indefinitely. Check for that and return failure.
648          */
649         if (i > 1 && !t->entries[i - 1].tsc && !t->entries[0].tsc)
650                 goto err;
651
652         fio_cpuset_exit(&cpu_mask);
653         return NULL;
654 err:
655         fio_cpuset_exit(&cpu_mask);
656 err_out:
657         return (void *) 1;
658 }
659
660 static int clock_cmp(const void *p1, const void *p2)
661 {
662         const struct clock_entry *c1 = p1;
663         const struct clock_entry *c2 = p2;
664
665         if (c1->seq == c2->seq)
666                 log_err("cs: bug in atomic sequence!\n");
667
668         return c1->seq - c2->seq;
669 }
670
671 int fio_monotonic_clocktest(int debug)
672 {
673         struct clock_thread *cthreads;
674         unsigned int seen_cpus, nr_cpus = cpus_online();
675         struct clock_entry *entries;
676         unsigned long nr_entries, tentries, failed = 0;
677         struct clock_entry *prev, *this;
678         uint32_t seq = 0;
679         unsigned int i;
680         os_cpu_mask_t mask;
681
682 #ifdef CONFIG_PTHREAD_GETAFFINITY
683         fio_get_thread_affinity(mask);
684 #else
685         memset(&mask, 0, sizeof(mask));
686         for (i = 0; i < nr_cpus; i++)
687                 fio_cpu_set(&mask, i);
688 #endif
689
690         if (debug) {
691                 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
692
693 #ifdef FIO_INC_DEBUG
694                 fio_debug |= 1U << FD_TIME;
695 #endif
696                 nr_entries = CLOCK_ENTRIES_DEBUG;
697         } else
698                 nr_entries = CLOCK_ENTRIES_TEST;
699
700         calibrate_cpu_clock();
701
702         if (debug) {
703 #ifdef FIO_INC_DEBUG
704                 fio_debug &= ~(1U << FD_TIME);
705 #endif
706         }
707
708         cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
709         tentries = nr_entries * nr_cpus;
710         entries = malloc(tentries * sizeof(struct clock_entry));
711
712         if (debug)
713                 log_info("cs: Testing %u CPUs\n", nr_cpus);
714
715         seen_cpus = 0;
716         for (i = 0; i < nr_cpus; i++) {
717                 struct clock_thread *t = &cthreads[i];
718
719                 if (!fio_cpu_isset(&mask, i))
720                         continue;
721                 t->cpu = i;
722                 t->debug = debug;
723                 t->seq = &seq;
724                 t->nr_entries = nr_entries;
725                 t->entries = &entries[seen_cpus * nr_entries];
726                 __fio_sem_init(&t->lock, FIO_SEM_LOCKED);
727                 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
728                         failed++;
729                         nr_cpus = i;
730                         break;
731                 }
732                 seen_cpus++;
733         }
734
735         for (i = 0; i < nr_cpus; i++) {
736                 struct clock_thread *t = &cthreads[i];
737
738                 if (!fio_cpu_isset(&mask, i))
739                         continue;
740                 fio_sem_up(&t->lock);
741         }
742
743         for (i = 0; i < nr_cpus; i++) {
744                 struct clock_thread *t = &cthreads[i];
745                 void *ret;
746
747                 if (!fio_cpu_isset(&mask, i))
748                         continue;
749                 pthread_join(t->thread, &ret);
750                 if (ret)
751                         failed++;
752                 __fio_sem_remove(&t->lock);
753         }
754         free(cthreads);
755
756         if (failed) {
757                 if (debug)
758                         log_err("Clocksource test: %lu threads failed\n", failed);
759                 goto err;
760         }
761
762         tentries = nr_entries * seen_cpus;
763         qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
764
765         /* silence silly gcc */
766         prev = NULL;
767         for (failed = i = 0; i < tentries; i++) {
768                 this = &entries[i];
769
770                 if (!i) {
771                         prev = this;
772                         continue;
773                 }
774
775                 if (prev->tsc > this->tsc) {
776                         uint64_t diff = prev->tsc - this->tsc;
777
778                         if (!debug) {
779                                 failed++;
780                                 break;
781                         }
782
783                         log_info("cs: CPU clock mismatch (diff=%llu):\n",
784                                                 (unsigned long long) diff);
785                         log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
786                         log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
787                         failed++;
788                 }
789
790                 prev = this;
791         }
792
793         if (debug) {
794                 if (failed)
795                         log_info("cs: Failed: %lu\n", failed);
796                 else
797                         log_info("cs: Pass!\n");
798         }
799 err:
800         free(entries);
801         return !!failed;
802 }
803
804 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
805
806 int fio_monotonic_clocktest(int debug)
807 {
808         if (debug)
809                 log_info("cs: current platform does not support CPU clocks\n");
810         return 1;
811 }
812
813 #endif