6b202e8f5e29239b381d0c99391bb080f2eac948
[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         enum fio_cs old_cs = fio_clock_source;
243         uint64_t elapsed;
244
245 #ifdef CONFIG_CLOCK_GETTIME
246         fio_clock_source = CS_CGETTIME;
247 #else
248         fio_clock_source = CS_GTOD;
249 #endif
250         __fio_gettime(&s);
251
252         c_s = get_cpu_clock();
253         do {
254                 __fio_gettime(&e);
255                 c_e = get_cpu_clock();
256
257                 elapsed = ntime_since(&s, &e);
258                 if (elapsed >= 1280000)
259                         break;
260         } while (1);
261
262         fio_clock_source = old_cs;
263         return (c_e - c_s) * 1000000 / elapsed;
264 }
265
266 #define NR_TIME_ITERS   50
267
268 static int calibrate_cpu_clock(void)
269 {
270         double delta, mean, S;
271         uint64_t minc, maxc, avg, cycles[NR_TIME_ITERS];
272         int i, samples, sft = 0;
273         unsigned long long tmp, max_ticks, max_mult;
274
275         cycles[0] = get_cycles_per_msec();
276         S = delta = mean = 0.0;
277         for (i = 0; i < NR_TIME_ITERS; i++) {
278                 cycles[i] = get_cycles_per_msec();
279                 delta = cycles[i] - mean;
280                 if (delta) {
281                         mean += delta / (i + 1.0);
282                         S += delta * (cycles[i] - mean);
283                 }
284         }
285
286         /*
287          * The most common platform clock breakage is returning zero
288          * indefinitely. Check for that and return failure.
289          */
290         if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
291                 return 1;
292
293         S = sqrt(S / (NR_TIME_ITERS - 1.0));
294
295         minc = -1ULL;
296         maxc = samples = avg = 0;
297         for (i = 0; i < NR_TIME_ITERS; i++) {
298                 double this = cycles[i];
299
300                 minc = min(cycles[i], minc);
301                 maxc = max(cycles[i], maxc);
302
303                 if ((fmax(this, mean) - fmin(this, mean)) > S)
304                         continue;
305                 samples++;
306                 avg += this;
307         }
308
309         S /= (double) NR_TIME_ITERS;
310
311         for (i = 0; i < NR_TIME_ITERS; i++)
312                 dprint(FD_TIME, "cycles[%d]=%llu\n", i, (unsigned long long) cycles[i]);
313
314         avg /= samples;
315         cycles_per_msec = avg;
316         dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f, N=%d\n",
317                         (unsigned long long) minc,
318                         (unsigned long long) maxc, mean, S, NR_TIME_ITERS);
319         dprint(FD_TIME, "trimmed mean=%llu, N=%d\n", (unsigned long long) avg, samples);
320
321         max_ticks = MAX_CLOCK_SEC * cycles_per_msec * 1000ULL;
322         max_mult = ULLONG_MAX / max_ticks;
323         dprint(FD_TIME, "\n\nmax_ticks=%llu, __builtin_clzll=%d, "
324                         "max_mult=%llu\n", max_ticks,
325                         __builtin_clzll(max_ticks), max_mult);
326
327         /*
328          * Find the largest shift count that will produce
329          * a multiplier that does not exceed max_mult
330          */
331         tmp = max_mult * cycles_per_msec / 1000000;
332         while (tmp > 1) {
333                 tmp >>= 1;
334                 sft++;
335                 dprint(FD_TIME, "tmp=%llu, sft=%u\n", tmp, sft);
336         }
337
338         clock_shift = sft;
339         clock_mult = (1ULL << sft) * 1000000 / cycles_per_msec;
340         dprint(FD_TIME, "clock_shift=%u, clock_mult=%llu\n", clock_shift,
341                                                         clock_mult);
342
343         /*
344          * Find the greatest power of 2 clock ticks that is less than the
345          * ticks in MAX_CLOCK_SEC_2STAGE
346          */
347         max_cycles_shift = max_cycles_mask = 0;
348         tmp = MAX_CLOCK_SEC * 1000ULL * cycles_per_msec;
349         dprint(FD_TIME, "tmp=%llu, max_cycles_shift=%u\n", tmp,
350                                                         max_cycles_shift);
351         while (tmp > 1) {
352                 tmp >>= 1;
353                 max_cycles_shift++;
354                 dprint(FD_TIME, "tmp=%llu, max_cycles_shift=%u\n", tmp, max_cycles_shift);
355         }
356         /*
357          * if use use (1ULL << max_cycles_shift) * 1000 / cycles_per_msec
358          * here we will have a discontinuity every
359          * (1ULL << max_cycles_shift) cycles
360          */
361         nsecs_for_max_cycles = ((1ULL << max_cycles_shift) * clock_mult)
362                                         >> clock_shift;
363
364         /* Use a bitmask to calculate ticks % (1ULL << max_cycles_shift) */
365         for (tmp = 0; tmp < max_cycles_shift; tmp++)
366                 max_cycles_mask |= 1ULL << tmp;
367
368         dprint(FD_TIME, "max_cycles_shift=%u, 2^max_cycles_shift=%llu, "
369                         "nsecs_for_max_cycles=%llu, "
370                         "max_cycles_mask=%016llx\n",
371                         max_cycles_shift, (1ULL << max_cycles_shift),
372                         nsecs_for_max_cycles, max_cycles_mask);
373
374         cycles_start = get_cpu_clock();
375         dprint(FD_TIME, "cycles_start=%llu\n", cycles_start);
376         return 0;
377 }
378 #else
379 static int calibrate_cpu_clock(void)
380 {
381 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
382         return 0;
383 #else
384         return 1;
385 #endif
386 }
387 #endif // ARCH_HAVE_CPU_CLOCK
388
389 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(CONFIG_TLS_THREAD)
390 void fio_local_clock_init(void)
391 {
392         struct tv_valid *t;
393
394         t = calloc(1, sizeof(*t));
395         if (pthread_setspecific(tv_tls_key, t)) {
396                 log_err("fio: can't set TLS key\n");
397                 assert(0);
398         }
399 }
400
401 static void kill_tv_tls_key(void *data)
402 {
403         free(data);
404 }
405 #else
406 void fio_local_clock_init(void)
407 {
408 }
409 #endif
410
411 void fio_clock_init(void)
412 {
413         if (fio_clock_source == fio_clock_source_inited)
414                 return;
415
416 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(CONFIG_TLS_THREAD)
417         if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
418                 log_err("fio: can't create TLS key\n");
419 #endif
420
421         fio_clock_source_inited = fio_clock_source;
422
423         if (calibrate_cpu_clock())
424                 tsc_reliable = false;
425
426         /*
427          * If the arch sets tsc_reliable != 0, then it must be good enough
428          * to use as THE clock source. For x86 CPUs, this means the TSC
429          * runs at a constant rate and is synced across CPU cores.
430          */
431         if (tsc_reliable) {
432                 if (!fio_clock_source_set && !fio_monotonic_clocktest(0))
433                         fio_clock_source = CS_CPUCLOCK;
434         } else if (fio_clock_source == CS_CPUCLOCK)
435                 log_info("fio: clocksource=cpu may not be reliable\n");
436         dprint(FD_TIME, "gettime: clocksource=%d\n", (int) fio_clock_source);
437 }
438
439 uint64_t ntime_since(const struct timespec *s, const struct timespec *e)
440 {
441        int64_t sec, nsec;
442
443        sec = e->tv_sec - s->tv_sec;
444        nsec = e->tv_nsec - s->tv_nsec;
445        if (sec > 0 && nsec < 0) {
446                sec--;
447                nsec += 1000000000LL;
448        }
449
450        /*
451         * time warp bug on some kernels?
452         */
453        if (sec < 0 || (sec == 0 && nsec < 0))
454                return 0;
455
456        return nsec + (sec * 1000000000LL);
457 }
458
459 uint64_t ntime_since_now(const struct timespec *s)
460 {
461         struct timespec now;
462
463         fio_gettime(&now, NULL);
464         return ntime_since(s, &now);
465 }
466
467 uint64_t utime_since(const struct timespec *s, const struct timespec *e)
468 {
469         int64_t sec, usec;
470
471         sec = e->tv_sec - s->tv_sec;
472         usec = (e->tv_nsec - s->tv_nsec) / 1000;
473         if (sec > 0 && usec < 0) {
474                 sec--;
475                 usec += 1000000;
476         }
477
478         /*
479          * time warp bug on some kernels?
480          */
481         if (sec < 0 || (sec == 0 && usec < 0))
482                 return 0;
483
484         return usec + (sec * 1000000);
485 }
486
487 uint64_t utime_since_now(const struct timespec *s)
488 {
489         struct timespec t;
490 #ifdef FIO_DEBUG_TIME
491         void *p = __builtin_return_address(0);
492
493         fio_gettime(&t, p);
494 #else
495         fio_gettime(&t, NULL);
496 #endif
497
498         return utime_since(s, &t);
499 }
500
501 uint64_t mtime_since_tv(const struct timeval *s, const struct timeval *e)
502 {
503         int64_t sec, usec;
504
505         sec = e->tv_sec - s->tv_sec;
506         usec = (e->tv_usec - s->tv_usec);
507         if (sec > 0 && usec < 0) {
508                 sec--;
509                 usec += 1000000;
510         }
511
512         if (sec < 0 || (sec == 0 && usec < 0))
513                 return 0;
514
515         sec *= 1000;
516         usec /= 1000;
517         return sec + usec;
518 }
519
520 uint64_t mtime_since_now(const struct timespec *s)
521 {
522         struct timespec t;
523 #ifdef FIO_DEBUG_TIME
524         void *p = __builtin_return_address(0);
525
526         fio_gettime(&t, p);
527 #else
528         fio_gettime(&t, NULL);
529 #endif
530
531         return mtime_since(s, &t);
532 }
533
534 uint64_t mtime_since(const struct timespec *s, const struct timespec *e)
535 {
536         int64_t sec, usec;
537
538         sec = e->tv_sec - s->tv_sec;
539         usec = (e->tv_nsec - s->tv_nsec) / 1000;
540         if (sec > 0 && usec < 0) {
541                 sec--;
542                 usec += 1000000;
543         }
544
545         if (sec < 0 || (sec == 0 && usec < 0))
546                 return 0;
547
548         sec *= 1000;
549         usec /= 1000;
550         return sec + usec;
551 }
552
553 uint64_t time_since_now(const struct timespec *s)
554 {
555         return mtime_since_now(s) / 1000;
556 }
557
558 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK)  && \
559     defined(CONFIG_SYNC_SYNC) && defined(CONFIG_CMP_SWAP)
560
561 #define CLOCK_ENTRIES_DEBUG     100000
562 #define CLOCK_ENTRIES_TEST      1000
563
564 struct clock_entry {
565         uint32_t seq;
566         uint32_t cpu;
567         uint64_t tsc;
568 };
569
570 struct clock_thread {
571         pthread_t thread;
572         int cpu;
573         int debug;
574         struct fio_sem lock;
575         unsigned long nr_entries;
576         uint32_t *seq;
577         struct clock_entry *entries;
578 };
579
580 static inline uint32_t atomic32_compare_and_swap(uint32_t *ptr, uint32_t old,
581                                                  uint32_t new)
582 {
583         return __sync_val_compare_and_swap(ptr, old, new);
584 }
585
586 static void *clock_thread_fn(void *data)
587 {
588         struct clock_thread *t = data;
589         struct clock_entry *c;
590         os_cpu_mask_t cpu_mask;
591         unsigned long long first;
592         int i;
593
594         if (fio_cpuset_init(&cpu_mask)) {
595                 int __err = errno;
596
597                 log_err("clock cpuset init failed: %s\n", strerror(__err));
598                 goto err_out;
599         }
600
601         fio_cpu_set(&cpu_mask, t->cpu);
602
603         if (fio_setaffinity(gettid(), cpu_mask) == -1) {
604                 int __err = errno;
605
606                 log_err("clock setaffinity failed: %s\n", strerror(__err));
607                 goto err;
608         }
609
610         fio_sem_down(&t->lock);
611
612         first = get_cpu_clock();
613         c = &t->entries[0];
614         for (i = 0; i < t->nr_entries; i++, c++) {
615                 uint32_t seq;
616                 uint64_t tsc;
617
618                 c->cpu = t->cpu;
619                 do {
620                         seq = *t->seq;
621                         if (seq == UINT_MAX)
622                                 break;
623                         __sync_synchronize();
624                         tsc = get_cpu_clock();
625                 } while (seq != atomic32_compare_and_swap(t->seq, seq, seq + 1));
626
627                 if (seq == UINT_MAX)
628                         break;
629
630                 c->seq = seq;
631                 c->tsc = tsc;
632         }
633
634         if (t->debug) {
635                 unsigned long long clocks;
636
637                 clocks = t->entries[i - 1].tsc - t->entries[0].tsc;
638                 log_info("cs: cpu%3d: %llu clocks seen, first %llu\n", t->cpu,
639                                                         clocks, first);
640         }
641
642         /*
643          * The most common platform clock breakage is returning zero
644          * indefinitely. Check for that and return failure.
645          */
646         if (i > 1 && !t->entries[i - 1].tsc && !t->entries[0].tsc)
647                 goto err;
648
649         fio_cpuset_exit(&cpu_mask);
650         return NULL;
651 err:
652         fio_cpuset_exit(&cpu_mask);
653 err_out:
654         return (void *) 1;
655 }
656
657 static int clock_cmp(const void *p1, const void *p2)
658 {
659         const struct clock_entry *c1 = p1;
660         const struct clock_entry *c2 = p2;
661
662         if (c1->seq == c2->seq)
663                 log_err("cs: bug in atomic sequence!\n");
664
665         return c1->seq - c2->seq;
666 }
667
668 int fio_monotonic_clocktest(int debug)
669 {
670         struct clock_thread *cthreads;
671         unsigned int nr_cpus = cpus_online();
672         struct clock_entry *entries;
673         unsigned long nr_entries, tentries, failed = 0;
674         struct clock_entry *prev, *this;
675         uint32_t seq = 0;
676         unsigned int i;
677
678         if (debug) {
679                 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
680
681 #ifdef FIO_INC_DEBUG
682                 fio_debug |= 1U << FD_TIME;
683 #endif
684                 nr_entries = CLOCK_ENTRIES_DEBUG;
685         } else
686                 nr_entries = CLOCK_ENTRIES_TEST;
687
688         calibrate_cpu_clock();
689
690         if (debug) {
691 #ifdef FIO_INC_DEBUG
692                 fio_debug &= ~(1U << FD_TIME);
693 #endif
694         }
695
696         cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
697         tentries = nr_entries * nr_cpus;
698         entries = malloc(tentries * sizeof(struct clock_entry));
699
700         if (debug)
701                 log_info("cs: Testing %u CPUs\n", nr_cpus);
702
703         for (i = 0; i < nr_cpus; i++) {
704                 struct clock_thread *t = &cthreads[i];
705
706                 t->cpu = i;
707                 t->debug = debug;
708                 t->seq = &seq;
709                 t->nr_entries = nr_entries;
710                 t->entries = &entries[i * nr_entries];
711                 __fio_sem_init(&t->lock, FIO_SEM_LOCKED);
712                 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
713                         failed++;
714                         nr_cpus = i;
715                         break;
716                 }
717         }
718
719         for (i = 0; i < nr_cpus; i++) {
720                 struct clock_thread *t = &cthreads[i];
721
722                 fio_sem_up(&t->lock);
723         }
724
725         for (i = 0; i < nr_cpus; i++) {
726                 struct clock_thread *t = &cthreads[i];
727                 void *ret;
728
729                 pthread_join(t->thread, &ret);
730                 if (ret)
731                         failed++;
732                 __fio_sem_remove(&t->lock);
733         }
734         free(cthreads);
735
736         if (failed) {
737                 if (debug)
738                         log_err("Clocksource test: %lu threads failed\n", failed);
739                 goto err;
740         }
741
742         qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
743
744         /* silence silly gcc */
745         prev = NULL;
746         for (failed = i = 0; i < tentries; i++) {
747                 this = &entries[i];
748
749                 if (!i) {
750                         prev = this;
751                         continue;
752                 }
753
754                 if (prev->tsc > this->tsc) {
755                         uint64_t diff = prev->tsc - this->tsc;
756
757                         if (!debug) {
758                                 failed++;
759                                 break;
760                         }
761
762                         log_info("cs: CPU clock mismatch (diff=%llu):\n",
763                                                 (unsigned long long) diff);
764                         log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
765                         log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
766                         failed++;
767                 }
768
769                 prev = this;
770         }
771
772         if (debug) {
773                 if (failed)
774                         log_info("cs: Failed: %lu\n", failed);
775                 else
776                         log_info("cs: Pass!\n");
777         }
778 err:
779         free(entries);
780         return !!failed;
781 }
782
783 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
784
785 int fio_monotonic_clocktest(int debug)
786 {
787         if (debug)
788                 log_info("cs: current platform does not support CPU clocks\n");
789         return 1;
790 }
791
792 #endif