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
21 #ifdef ARCH_CPU_CLOCK_WRAPS
22 static unsigned int cycles_wrap;
25 bool tsc_reliable = false;
30 #ifdef ARCH_HAVE_CPU_CLOCK
31 #ifdef CONFIG_TLS_THREAD
32 static __thread struct tv_valid static_tv_valid;
34 static pthread_key_t tv_tls_key;
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;
45 #define HASH_SIZE (1 << HASH_BITS)
47 static struct flist_head hash[HASH_SIZE];
48 static int gtod_inited;
51 struct flist_head list;
56 static struct gtod_log *find_hash(void *caller)
58 unsigned long h = hash_ptr(caller, HASH_BITS);
59 struct flist_head *entry;
61 flist_for_each(entry, &hash[h]) {
62 struct gtod_log *log = flist_entry(entry, struct gtod_log,
65 if (log->caller == caller)
72 static void inc_caller(void *caller)
74 struct gtod_log *log = find_hash(caller);
79 log = malloc(sizeof(*log));
80 INIT_FLIST_HEAD(&log->list);
84 h = hash_ptr(caller, HASH_BITS);
85 flist_add_tail(&log->list, &hash[h]);
91 static void gtod_log_caller(void *caller)
97 static void fio_exit fio_dump_gtod(void)
99 unsigned long total_calls = 0;
102 for (i = 0; i < HASH_SIZE; i++) {
103 struct flist_head *entry;
104 struct gtod_log *log;
106 flist_for_each(entry, &hash[i]) {
107 log = flist_entry(entry, struct gtod_log, list);
109 printf("function %p, calls %lu\n", log->caller,
111 total_calls += log->calls;
115 printf("Total %lu gettimeofday\n", total_calls);
118 static void fio_init gtod_init(void)
122 for (i = 0; i < HASH_SIZE; i++)
123 INIT_FLIST_HEAD(&hash[i]);
128 #endif /* FIO_DEBUG_TIME */
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.
135 int fio_get_mono_time(struct timespec *ts)
139 #ifdef CONFIG_CLOCK_GETTIME
140 #if defined(CONFIG_CLOCK_MONOTONIC)
141 ret = clock_gettime(CLOCK_MONOTONIC, ts);
143 ret = clock_gettime(CLOCK_REALTIME, ts);
148 ret = gettimeofday(&tv, NULL);
150 ts->tv_sec = tv.tv_sec;
151 ts->tv_nsec = tv.tv_usec * 1000;
158 static void __fio_gettime(struct timespec *tp)
160 switch (fio_clock_source) {
161 #ifdef CONFIG_GETTIMEOFDAY
164 gettimeofday(&tv, NULL);
166 tp->tv_sec = tv.tv_sec;
167 tp->tv_nsec = tv.tv_usec * 1000;
171 #ifdef CONFIG_CLOCK_GETTIME
173 if (fio_get_mono_time(tp) < 0) {
174 log_err("fio: fio_get_mono_time() fails\n");
180 #ifdef ARCH_HAVE_CPU_CLOCK
182 uint64_t nsecs, t, multiples;
185 #ifdef CONFIG_TLS_THREAD
186 tv = &static_tv_valid;
188 tv = pthread_getspecific(tv_tls_key);
192 #ifdef ARCH_CPU_CLOCK_WRAPS
193 if (t < cycles_start && !cycles_wrap)
195 else if (cycles_wrap && t >= cycles_start && !tv->warned) {
196 log_err("fio: double CPU clock wrap\n");
200 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
201 nsecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC * 1000;
204 multiples = t >> max_cycles_shift;
205 nsecs = multiples * nsecs_for_max_cycles;
206 nsecs += ((t & max_cycles_mask) * clock_mult) >> clock_shift;
208 tp->tv_sec = nsecs / 1000000000ULL;
209 tp->tv_nsec = nsecs % 1000000000ULL;
214 log_err("fio: invalid clock source %d\n", fio_clock_source);
219 #ifdef FIO_DEBUG_TIME
220 void fio_gettime(struct timespec *tp, void *caller)
222 void fio_gettime(struct timespec *tp, void fio_unused *caller)
225 #ifdef FIO_DEBUG_TIME
227 caller = __builtin_return_address(0);
229 gtod_log_caller(caller);
231 if (fio_unlikely(fio_gettime_offload(tp)))
237 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
238 static unsigned long get_cycles_per_msec(void)
240 struct timespec s, e;
244 fio_get_mono_time(&s);
246 c_s = get_cpu_clock();
248 fio_get_mono_time(&e);
249 c_e = get_cpu_clock();
251 elapsed = ntime_since(&s, &e);
252 if (elapsed >= 1280000)
256 return (c_e - c_s) * 1000000 / 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, "min=%llu, max=%llu, mean=%f, S=%f, N=%d\n",
310 (unsigned long long) minc,
311 (unsigned long long) maxc, mean, S, NR_TIME_ITERS);
312 dprint(FD_TIME, "trimmed mean=%llu, N=%d\n", (unsigned long long) avg, samples);
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 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(CONFIG_TLS_THREAD)
383 void fio_local_clock_init(void)
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(void)
404 void fio_clock_init(void)
406 if (fio_clock_source == fio_clock_source_inited)
409 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(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");