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