optgroup: add check for optgroup bit numbers being within range
[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 "fio_sem.h"
12#include "smalloc.h"
13
14#include "hash.h"
15#include "os/os.h"
16
17#if defined(ARCH_HAVE_CPU_CLOCK)
18#ifndef ARCH_CPU_CLOCK_CYCLES_PER_USEC
19static unsigned long long cycles_per_msec;
20static unsigned long long cycles_start;
21static unsigned long long clock_mult;
22static unsigned long long max_cycles_mask;
23static unsigned long long nsecs_for_max_cycles;
24static unsigned int clock_shift;
25static unsigned int max_cycles_shift;
26#define MAX_CLOCK_SEC 60*60
27#endif
28#ifdef ARCH_CPU_CLOCK_WRAPS
29static unsigned int cycles_wrap;
30#endif
31#endif
32bool tsc_reliable = false;
33
34struct tv_valid {
35 int warned;
36};
37#ifdef ARCH_HAVE_CPU_CLOCK
38#ifdef CONFIG_TLS_THREAD
39static __thread struct tv_valid static_tv_valid;
40#else
41static pthread_key_t tv_tls_key;
42#endif
43#endif
44
45enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
46int fio_clock_source_set = 0;
47static enum fio_cs fio_clock_source_inited = CS_INVAL;
48
49#ifdef FIO_DEBUG_TIME
50
51#define HASH_BITS 8
52#define HASH_SIZE (1 << HASH_BITS)
53
54static struct flist_head hash[HASH_SIZE];
55static int gtod_inited;
56
57struct gtod_log {
58 struct flist_head list;
59 void *caller;
60 unsigned long calls;
61};
62
63static struct gtod_log *find_hash(void *caller)
64{
65 unsigned long h = hash_ptr(caller, HASH_BITS);
66 struct flist_head *entry;
67
68 flist_for_each(entry, &hash[h]) {
69 struct gtod_log *log = flist_entry(entry, struct gtod_log,
70 list);
71
72 if (log->caller == caller)
73 return log;
74 }
75
76 return NULL;
77}
78
79static void inc_caller(void *caller)
80{
81 struct gtod_log *log = find_hash(caller);
82
83 if (!log) {
84 unsigned long h;
85
86 log = malloc(sizeof(*log));
87 INIT_FLIST_HEAD(&log->list);
88 log->caller = caller;
89 log->calls = 0;
90
91 h = hash_ptr(caller, HASH_BITS);
92 flist_add_tail(&log->list, &hash[h]);
93 }
94
95 log->calls++;
96}
97
98static void gtod_log_caller(void *caller)
99{
100 if (gtod_inited)
101 inc_caller(caller);
102}
103
104static void fio_exit fio_dump_gtod(void)
105{
106 unsigned long total_calls = 0;
107 int i;
108
109 for (i = 0; i < HASH_SIZE; i++) {
110 struct flist_head *entry;
111 struct gtod_log *log;
112
113 flist_for_each(entry, &hash[i]) {
114 log = flist_entry(entry, struct gtod_log, list);
115
116 printf("function %p, calls %lu\n", log->caller,
117 log->calls);
118 total_calls += log->calls;
119 }
120 }
121
122 printf("Total %lu gettimeofday\n", total_calls);
123}
124
125static void fio_init gtod_init(void)
126{
127 int i;
128
129 for (i = 0; i < HASH_SIZE; i++)
130 INIT_FLIST_HEAD(&hash[i]);
131
132 gtod_inited = 1;
133}
134
135#endif /* FIO_DEBUG_TIME */
136
137#ifdef CONFIG_CLOCK_GETTIME
138static int fill_clock_gettime(struct timespec *ts)
139{
140#if defined(CONFIG_CLOCK_MONOTONIC_RAW)
141 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
142#elif defined(CONFIG_CLOCK_MONOTONIC)
143 return clock_gettime(CLOCK_MONOTONIC, ts);
144#else
145 return clock_gettime(CLOCK_REALTIME, ts);
146#endif
147}
148#endif
149
150static void __fio_gettime(struct timespec *tp)
151{
152 switch (fio_clock_source) {
153#ifdef CONFIG_GETTIMEOFDAY
154 case CS_GTOD: {
155 struct timeval tv;
156 gettimeofday(&tv, NULL);
157
158 tp->tv_sec = tv.tv_sec;
159 tp->tv_nsec = tv.tv_usec * 1000;
160 break;
161 }
162#endif
163#ifdef CONFIG_CLOCK_GETTIME
164 case CS_CGETTIME: {
165 if (fill_clock_gettime(tp) < 0) {
166 log_err("fio: clock_gettime fails\n");
167 assert(0);
168 }
169 break;
170 }
171#endif
172#ifdef ARCH_HAVE_CPU_CLOCK
173 case CS_CPUCLOCK: {
174 uint64_t nsecs, t, multiples;
175 struct tv_valid *tv;
176
177#ifdef CONFIG_TLS_THREAD
178 tv = &static_tv_valid;
179#else
180 tv = pthread_getspecific(tv_tls_key);
181#endif
182
183 t = get_cpu_clock();
184#ifdef ARCH_CPU_CLOCK_WRAPS
185 if (t < cycles_start && !cycles_wrap)
186 cycles_wrap = 1;
187 else if (cycles_wrap && t >= cycles_start && !tv->warned) {
188 log_err("fio: double CPU clock wrap\n");
189 tv->warned = 1;
190 }
191#endif
192#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
193 nsecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC * 1000;
194#else
195 t -= cycles_start;
196 multiples = t >> max_cycles_shift;
197 nsecs = multiples * nsecs_for_max_cycles;
198 nsecs += ((t & max_cycles_mask) * clock_mult) >> clock_shift;
199#endif
200 tp->tv_sec = nsecs / 1000000000ULL;
201 tp->tv_nsec = nsecs % 1000000000ULL;
202 break;
203 }
204#endif
205 default:
206 log_err("fio: invalid clock source %d\n", fio_clock_source);
207 break;
208 }
209}
210
211#ifdef FIO_DEBUG_TIME
212void fio_gettime(struct timespec *tp, void *caller)
213#else
214void fio_gettime(struct timespec *tp, void fio_unused *caller)
215#endif
216{
217#ifdef FIO_DEBUG_TIME
218 if (!caller)
219 caller = __builtin_return_address(0);
220
221 gtod_log_caller(caller);
222#endif
223 if (fio_unlikely(fio_gettime_offload(tp)))
224 return;
225
226 __fio_gettime(tp);
227}
228
229#if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
230static unsigned long get_cycles_per_msec(void)
231{
232 struct timespec s, e;
233 uint64_t c_s, c_e;
234 enum fio_cs old_cs = fio_clock_source;
235 uint64_t elapsed;
236
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);
243
244 c_s = get_cpu_clock();
245 do {
246 __fio_gettime(&e);
247
248 elapsed = utime_since(&s, &e);
249 if (elapsed >= 1280) {
250 c_e = get_cpu_clock();
251 break;
252 }
253 } while (1);
254
255 fio_clock_source = old_cs;
256 return (c_e - c_s) * 1000 / elapsed;
257}
258
259#define NR_TIME_ITERS 50
260
261static 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, "avg: %llu\n", (unsigned long long) avg);
310 dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f\n",
311 (unsigned long long) minc,
312 (unsigned long long) maxc, mean, S);
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
372static 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#ifndef CONFIG_TLS_THREAD
383void fio_local_clock_init(int is_thread)
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
394static void kill_tv_tls_key(void *data)
395{
396 free(data);
397}
398#else
399void fio_local_clock_init(int is_thread)
400{
401}
402#endif
403
404void fio_clock_init(void)
405{
406 if (fio_clock_source == fio_clock_source_inited)
407 return;
408
409#ifndef 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
432uint64_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
452uint64_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
460uint64_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
480uint64_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
494uint64_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
513uint64_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
527uint64_t mtime_since(const struct timespec *s, const struct timespec *e)
528{
529 int64_t sec, usec;
530
531 sec = e->tv_sec - s->tv_sec;
532 usec = (e->tv_nsec - s->tv_nsec) / 1000;
533 if (sec > 0 && usec < 0) {
534 sec--;
535 usec += 1000000;
536 }
537
538 if (sec < 0 || (sec == 0 && usec < 0))
539 return 0;
540
541 sec *= 1000;
542 usec /= 1000;
543 return sec + usec;
544}
545
546uint64_t time_since_now(const struct timespec *s)
547{
548 return mtime_since_now(s) / 1000;
549}
550
551#if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
552 defined(CONFIG_SYNC_SYNC) && defined(CONFIG_CMP_SWAP)
553
554#define CLOCK_ENTRIES_DEBUG 100000
555#define CLOCK_ENTRIES_TEST 1000
556
557struct clock_entry {
558 uint32_t seq;
559 uint32_t cpu;
560 uint64_t tsc;
561};
562
563struct clock_thread {
564 pthread_t thread;
565 int cpu;
566 int debug;
567 struct fio_sem lock;
568 unsigned long nr_entries;
569 uint32_t *seq;
570 struct clock_entry *entries;
571};
572
573static inline uint32_t atomic32_compare_and_swap(uint32_t *ptr, uint32_t old,
574 uint32_t new)
575{
576 return __sync_val_compare_and_swap(ptr, old, new);
577}
578
579static void *clock_thread_fn(void *data)
580{
581 struct clock_thread *t = data;
582 struct clock_entry *c;
583 os_cpu_mask_t cpu_mask;
584 unsigned long long first;
585 int i;
586
587 if (fio_cpuset_init(&cpu_mask)) {
588 int __err = errno;
589
590 log_err("clock cpuset init failed: %s\n", strerror(__err));
591 goto err_out;
592 }
593
594 fio_cpu_set(&cpu_mask, t->cpu);
595
596 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
597 int __err = errno;
598
599 log_err("clock setaffinity failed: %s\n", strerror(__err));
600 goto err;
601 }
602
603 fio_sem_down(&t->lock);
604
605 first = get_cpu_clock();
606 c = &t->entries[0];
607 for (i = 0; i < t->nr_entries; i++, c++) {
608 uint32_t seq;
609 uint64_t tsc;
610
611 c->cpu = t->cpu;
612 do {
613 seq = *t->seq;
614 if (seq == UINT_MAX)
615 break;
616 __sync_synchronize();
617 tsc = get_cpu_clock();
618 } while (seq != atomic32_compare_and_swap(t->seq, seq, seq + 1));
619
620 if (seq == UINT_MAX)
621 break;
622
623 c->seq = seq;
624 c->tsc = tsc;
625 }
626
627 if (t->debug) {
628 unsigned long long clocks;
629
630 clocks = t->entries[i - 1].tsc - t->entries[0].tsc;
631 log_info("cs: cpu%3d: %llu clocks seen, first %llu\n", t->cpu,
632 clocks, first);
633 }
634
635 /*
636 * The most common platform clock breakage is returning zero
637 * indefinitely. Check for that and return failure.
638 */
639 if (i > 1 && !t->entries[i - 1].tsc && !t->entries[0].tsc)
640 goto err;
641
642 fio_cpuset_exit(&cpu_mask);
643 return NULL;
644err:
645 fio_cpuset_exit(&cpu_mask);
646err_out:
647 return (void *) 1;
648}
649
650static int clock_cmp(const void *p1, const void *p2)
651{
652 const struct clock_entry *c1 = p1;
653 const struct clock_entry *c2 = p2;
654
655 if (c1->seq == c2->seq)
656 log_err("cs: bug in atomic sequence!\n");
657
658 return c1->seq - c2->seq;
659}
660
661int fio_monotonic_clocktest(int debug)
662{
663 struct clock_thread *cthreads;
664 unsigned int nr_cpus = cpus_online();
665 struct clock_entry *entries;
666 unsigned long nr_entries, tentries, failed = 0;
667 struct clock_entry *prev, *this;
668 uint32_t seq = 0;
669 unsigned int i;
670
671 if (debug) {
672 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
673
674#ifdef FIO_INC_DEBUG
675 fio_debug |= 1U << FD_TIME;
676#endif
677 nr_entries = CLOCK_ENTRIES_DEBUG;
678 } else
679 nr_entries = CLOCK_ENTRIES_TEST;
680
681 calibrate_cpu_clock();
682
683 if (debug) {
684#ifdef FIO_INC_DEBUG
685 fio_debug &= ~(1U << FD_TIME);
686#endif
687 }
688
689 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
690 tentries = nr_entries * nr_cpus;
691 entries = malloc(tentries * sizeof(struct clock_entry));
692
693 if (debug)
694 log_info("cs: Testing %u CPUs\n", nr_cpus);
695
696 for (i = 0; i < nr_cpus; i++) {
697 struct clock_thread *t = &cthreads[i];
698
699 t->cpu = i;
700 t->debug = debug;
701 t->seq = &seq;
702 t->nr_entries = nr_entries;
703 t->entries = &entries[i * nr_entries];
704 __fio_sem_init(&t->lock, FIO_SEM_LOCKED);
705 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
706 failed++;
707 nr_cpus = i;
708 break;
709 }
710 }
711
712 for (i = 0; i < nr_cpus; i++) {
713 struct clock_thread *t = &cthreads[i];
714
715 fio_sem_up(&t->lock);
716 }
717
718 for (i = 0; i < nr_cpus; i++) {
719 struct clock_thread *t = &cthreads[i];
720 void *ret;
721
722 pthread_join(t->thread, &ret);
723 if (ret)
724 failed++;
725 __fio_sem_remove(&t->lock);
726 }
727 free(cthreads);
728
729 if (failed) {
730 if (debug)
731 log_err("Clocksource test: %lu threads failed\n", failed);
732 goto err;
733 }
734
735 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
736
737 /* silence silly gcc */
738 prev = NULL;
739 for (failed = i = 0; i < tentries; i++) {
740 this = &entries[i];
741
742 if (!i) {
743 prev = this;
744 continue;
745 }
746
747 if (prev->tsc > this->tsc) {
748 uint64_t diff = prev->tsc - this->tsc;
749
750 if (!debug) {
751 failed++;
752 break;
753 }
754
755 log_info("cs: CPU clock mismatch (diff=%llu):\n",
756 (unsigned long long) diff);
757 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
758 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
759 failed++;
760 }
761
762 prev = this;
763 }
764
765 if (debug) {
766 if (failed)
767 log_info("cs: Failed: %lu\n", failed);
768 else
769 log_info("cs: Pass!\n");
770 }
771err:
772 free(entries);
773 return !!failed;
774}
775
776#else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
777
778int fio_monotonic_clocktest(int debug)
779{
780 if (debug)
781 log_info("cs: current platform does not support CPU clocks\n");
782 return 1;
783}
784
785#endif