17 #if defined(ARCH_HAVE_CPU_CLOCK)
18 #ifndef ARCH_CPU_CLOCK_CYCLES_PER_USEC
19 static unsigned long long cycles_per_msec;
20 static unsigned long long cycles_start;
21 static unsigned long long clock_mult;
22 static unsigned long long max_cycles_mask;
23 static unsigned long long nsecs_for_max_cycles;
24 static unsigned int clock_shift;
25 static unsigned int max_cycles_shift;
26 #define MAX_CLOCK_SEC 60*60
28 #ifdef ARCH_CPU_CLOCK_WRAPS
29 static unsigned int cycles_wrap;
32 bool tsc_reliable = false;
37 #ifdef ARCH_HAVE_CPU_CLOCK
38 #ifdef CONFIG_TLS_THREAD
39 static __thread struct tv_valid static_tv_valid;
41 static pthread_key_t tv_tls_key;
45 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
46 int fio_clock_source_set = 0;
47 static enum fio_cs fio_clock_source_inited = CS_INVAL;
52 #define HASH_SIZE (1 << HASH_BITS)
54 static struct flist_head hash[HASH_SIZE];
55 static int gtod_inited;
58 struct flist_head list;
63 static struct gtod_log *find_hash(void *caller)
65 unsigned long h = hash_ptr(caller, HASH_BITS);
66 struct flist_head *entry;
68 flist_for_each(entry, &hash[h]) {
69 struct gtod_log *log = flist_entry(entry, struct gtod_log,
72 if (log->caller == caller)
79 static void inc_caller(void *caller)
81 struct gtod_log *log = find_hash(caller);
86 log = malloc(sizeof(*log));
87 INIT_FLIST_HEAD(&log->list);
91 h = hash_ptr(caller, HASH_BITS);
92 flist_add_tail(&log->list, &hash[h]);
98 static void gtod_log_caller(void *caller)
104 static void fio_exit fio_dump_gtod(void)
106 unsigned long total_calls = 0;
109 for (i = 0; i < HASH_SIZE; i++) {
110 struct flist_head *entry;
111 struct gtod_log *log;
113 flist_for_each(entry, &hash[i]) {
114 log = flist_entry(entry, struct gtod_log, list);
116 printf("function %p, calls %lu\n", log->caller,
118 total_calls += log->calls;
122 printf("Total %lu gettimeofday\n", total_calls);
125 static void fio_init gtod_init(void)
129 for (i = 0; i < HASH_SIZE; i++)
130 INIT_FLIST_HEAD(&hash[i]);
135 #endif /* FIO_DEBUG_TIME */
137 #ifdef CONFIG_CLOCK_GETTIME
138 static int fill_clock_gettime(struct timespec *ts)
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);
145 return clock_gettime(CLOCK_REALTIME, ts);
150 static void __fio_gettime(struct timespec *tp)
152 switch (fio_clock_source) {
153 #ifdef CONFIG_GETTIMEOFDAY
156 gettimeofday(&tv, NULL);
158 tp->tv_sec = tv.tv_sec;
159 tp->tv_nsec = tv.tv_usec * 1000;
163 #ifdef CONFIG_CLOCK_GETTIME
165 if (fill_clock_gettime(tp) < 0) {
166 log_err("fio: clock_gettime fails\n");
172 #ifdef ARCH_HAVE_CPU_CLOCK
174 uint64_t nsecs, t, multiples;
177 #ifdef CONFIG_TLS_THREAD
178 tv = &static_tv_valid;
180 tv = pthread_getspecific(tv_tls_key);
184 #ifdef ARCH_CPU_CLOCK_WRAPS
185 if (t < cycles_start && !cycles_wrap)
187 else if (cycles_wrap && t >= cycles_start && !tv->warned) {
188 log_err("fio: double CPU clock wrap\n");
192 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
193 nsecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC * 1000;
196 multiples = t >> max_cycles_shift;
197 nsecs = multiples * nsecs_for_max_cycles;
198 nsecs += ((t & max_cycles_mask) * clock_mult) >> clock_shift;
200 tp->tv_sec = nsecs / 1000000000ULL;
201 tp->tv_nsec = nsecs % 1000000000ULL;
206 log_err("fio: invalid clock source %d\n", fio_clock_source);
211 #ifdef FIO_DEBUG_TIME
212 void fio_gettime(struct timespec *tp, void *caller)
214 void fio_gettime(struct timespec *tp, void fio_unused *caller)
217 #ifdef FIO_DEBUG_TIME
219 caller = __builtin_return_address(0);
221 gtod_log_caller(caller);
223 if (fio_unlikely(fio_gettime_offload(tp)))
229 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
230 static unsigned long get_cycles_per_msec(void)
232 struct timespec s, e;
234 enum fio_cs old_cs = fio_clock_source;
237 #ifdef CONFIG_CLOCK_GETTIME
238 fio_clock_source = CS_CGETTIME;
240 fio_clock_source = CS_GTOD;
244 c_s = get_cpu_clock();
248 elapsed = utime_since(&s, &e);
249 if (elapsed >= 1280) {
250 c_e = get_cpu_clock();
255 fio_clock_source = old_cs;
256 return (c_e - c_s) * 1000 / elapsed;
259 #define NR_TIME_ITERS 50
261 static int calibrate_cpu_clock(void)
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;
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;
274 mean += delta / (i + 1.0);
275 S += delta * (cycles[i] - mean);
280 * The most common platform clock breakage is returning zero
281 * indefinitely. Check for that and return failure.
283 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
286 S = sqrt(S / (NR_TIME_ITERS - 1.0));
289 maxc = samples = avg = 0;
290 for (i = 0; i < NR_TIME_ITERS; i++) {
291 double this = cycles[i];
293 minc = min(cycles[i], minc);
294 maxc = max(cycles[i], maxc);
296 if ((fmax(this, mean) - fmin(this, mean)) > S)
302 S /= (double) NR_TIME_ITERS;
304 for (i = 0; i < NR_TIME_ITERS; i++)
305 dprint(FD_TIME, "cycles[%d]=%llu\n", i, (unsigned long long) cycles[i]);
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);
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);
321 * Find the largest shift count that will produce
322 * a multiplier that does not exceed max_mult
324 tmp = max_mult * cycles_per_msec / 1000000;
328 dprint(FD_TIME, "tmp=%llu, sft=%u\n", tmp, sft);
332 clock_mult = (1ULL << sft) * 1000000 / cycles_per_msec;
333 dprint(FD_TIME, "clock_shift=%u, clock_mult=%llu\n", clock_shift,
337 * Find the greatest power of 2 clock ticks that is less than the
338 * ticks in MAX_CLOCK_SEC_2STAGE
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,
347 dprint(FD_TIME, "tmp=%llu, max_cycles_shift=%u\n", tmp, max_cycles_shift);
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
354 nsecs_for_max_cycles = ((1ULL << max_cycles_shift) * clock_mult)
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;
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);
367 cycles_start = get_cpu_clock();
368 dprint(FD_TIME, "cycles_start=%llu\n", cycles_start);
372 static int calibrate_cpu_clock(void)
374 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
380 #endif // ARCH_HAVE_CPU_CLOCK
382 #ifndef CONFIG_TLS_THREAD
383 void fio_local_clock_init(int is_thread)
387 t = calloc(1, sizeof(*t));
388 if (pthread_setspecific(tv_tls_key, t)) {
389 log_err("fio: can't set TLS key\n");
394 static void kill_tv_tls_key(void *data)
399 void fio_local_clock_init(int is_thread)
404 void fio_clock_init(void)
406 if (fio_clock_source == fio_clock_source_inited)
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");
414 fio_clock_source_inited = fio_clock_source;
416 if (calibrate_cpu_clock())
417 tsc_reliable = false;
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.
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);
432 uint64_t ntime_since(const struct timespec *s, const struct timespec *e)
436 sec = e->tv_sec - s->tv_sec;
437 nsec = e->tv_nsec - s->tv_nsec;
438 if (sec > 0 && nsec < 0) {
440 nsec += 1000000000LL;
444 * time warp bug on some kernels?
446 if (sec < 0 || (sec == 0 && nsec < 0))
449 return nsec + (sec * 1000000000LL);
452 uint64_t ntime_since_now(const struct timespec *s)
456 fio_gettime(&now, NULL);
457 return ntime_since(s, &now);
460 uint64_t utime_since(const struct timespec *s, const struct timespec *e)
464 sec = e->tv_sec - s->tv_sec;
465 usec = (e->tv_nsec - s->tv_nsec) / 1000;
466 if (sec > 0 && usec < 0) {
472 * time warp bug on some kernels?
474 if (sec < 0 || (sec == 0 && usec < 0))
477 return usec + (sec * 1000000);
480 uint64_t utime_since_now(const struct timespec *s)
483 #ifdef FIO_DEBUG_TIME
484 void *p = __builtin_return_address(0);
488 fio_gettime(&t, NULL);
491 return utime_since(s, &t);
494 uint64_t mtime_since_tv(const struct timeval *s, const struct timeval *e)
498 sec = e->tv_sec - s->tv_sec;
499 usec = (e->tv_usec - s->tv_usec);
500 if (sec > 0 && usec < 0) {
505 if (sec < 0 || (sec == 0 && usec < 0))
513 uint64_t mtime_since_now(const struct timespec *s)
516 #ifdef FIO_DEBUG_TIME
517 void *p = __builtin_return_address(0);
521 fio_gettime(&t, NULL);
524 return mtime_since(s, &t);
527 uint64_t mtime_since(const struct timespec *s, const struct timespec *e)
531 sec = e->tv_sec - s->tv_sec;
532 usec = (e->tv_nsec - s->tv_nsec) / 1000;
533 if (sec > 0 && usec < 0) {
538 if (sec < 0 || (sec == 0 && usec < 0))
546 uint64_t time_since_now(const struct timespec *s)
548 return mtime_since_now(s) / 1000;
551 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
552 defined(CONFIG_SYNC_SYNC) && defined(CONFIG_CMP_SWAP)
554 #define CLOCK_ENTRIES_DEBUG 100000
555 #define CLOCK_ENTRIES_TEST 1000
563 struct clock_thread {
568 unsigned long nr_entries;
570 struct clock_entry *entries;
573 static inline uint32_t atomic32_compare_and_swap(uint32_t *ptr, uint32_t old,
576 return __sync_val_compare_and_swap(ptr, old, new);
579 static void *clock_thread_fn(void *data)
581 struct clock_thread *t = data;
582 struct clock_entry *c;
583 os_cpu_mask_t cpu_mask;
584 unsigned long long first;
587 if (fio_cpuset_init(&cpu_mask)) {
590 log_err("clock cpuset init failed: %s\n", strerror(__err));
594 fio_cpu_set(&cpu_mask, t->cpu);
596 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
599 log_err("clock setaffinity failed: %s\n", strerror(__err));
603 fio_sem_down(&t->lock);
605 first = get_cpu_clock();
607 for (i = 0; i < t->nr_entries; i++, c++) {
616 __sync_synchronize();
617 tsc = get_cpu_clock();
618 } while (seq != atomic32_compare_and_swap(t->seq, seq, seq + 1));
628 unsigned long long clocks;
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,
636 * The most common platform clock breakage is returning zero
637 * indefinitely. Check for that and return failure.
639 if (i > 1 && !t->entries[i - 1].tsc && !t->entries[0].tsc)
642 fio_cpuset_exit(&cpu_mask);
645 fio_cpuset_exit(&cpu_mask);
650 static int clock_cmp(const void *p1, const void *p2)
652 const struct clock_entry *c1 = p1;
653 const struct clock_entry *c2 = p2;
655 if (c1->seq == c2->seq)
656 log_err("cs: bug in atomic sequence!\n");
658 return c1->seq - c2->seq;
661 int fio_monotonic_clocktest(int debug)
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;
672 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
675 fio_debug |= 1U << FD_TIME;
677 nr_entries = CLOCK_ENTRIES_DEBUG;
679 nr_entries = CLOCK_ENTRIES_TEST;
681 calibrate_cpu_clock();
685 fio_debug &= ~(1U << FD_TIME);
689 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
690 tentries = nr_entries * nr_cpus;
691 entries = malloc(tentries * sizeof(struct clock_entry));
694 log_info("cs: Testing %u CPUs\n", nr_cpus);
696 for (i = 0; i < nr_cpus; i++) {
697 struct clock_thread *t = &cthreads[i];
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)) {
712 for (i = 0; i < nr_cpus; i++) {
713 struct clock_thread *t = &cthreads[i];
715 fio_sem_up(&t->lock);
718 for (i = 0; i < nr_cpus; i++) {
719 struct clock_thread *t = &cthreads[i];
722 pthread_join(t->thread, &ret);
725 __fio_sem_remove(&t->lock);
731 log_err("Clocksource test: %lu threads failed\n", failed);
735 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
737 /* silence silly gcc */
739 for (failed = i = 0; i < tentries; i++) {
747 if (prev->tsc > this->tsc) {
748 uint64_t diff = prev->tsc - this->tsc;
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);
767 log_info("cs: Failed: %lu\n", failed);
769 log_info("cs: Pass!\n");
776 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
778 int fio_monotonic_clocktest(int debug)
781 log_info("cs: current platform does not support CPU clocks\n");