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