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;
242 enum fio_cs old_cs = fio_clock_source;
245 #ifdef CONFIG_CLOCK_GETTIME
246 fio_clock_source = CS_CGETTIME;
248 fio_clock_source = CS_GTOD;
252 c_s = get_cpu_clock();
255 c_e = get_cpu_clock();
257 elapsed = ntime_since(&s, &e);
258 if (elapsed >= 1280000)
262 fio_clock_source = old_cs;
263 return (c_e - c_s) * 1000000 / elapsed;
266 #define NR_TIME_ITERS 50
268 static int calibrate_cpu_clock(void)
270 double delta, mean, S;
271 uint64_t minc, maxc, avg, cycles[NR_TIME_ITERS];
272 int i, samples, sft = 0;
273 unsigned long long tmp, max_ticks, max_mult;
275 cycles[0] = get_cycles_per_msec();
276 S = delta = mean = 0.0;
277 for (i = 0; i < NR_TIME_ITERS; i++) {
278 cycles[i] = get_cycles_per_msec();
279 delta = cycles[i] - mean;
281 mean += delta / (i + 1.0);
282 S += delta * (cycles[i] - mean);
287 * The most common platform clock breakage is returning zero
288 * indefinitely. Check for that and return failure.
290 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
293 S = sqrt(S / (NR_TIME_ITERS - 1.0));
296 maxc = samples = avg = 0;
297 for (i = 0; i < NR_TIME_ITERS; i++) {
298 double this = cycles[i];
300 minc = min(cycles[i], minc);
301 maxc = max(cycles[i], maxc);
303 if ((fmax(this, mean) - fmin(this, mean)) > S)
309 S /= (double) NR_TIME_ITERS;
311 for (i = 0; i < NR_TIME_ITERS; i++)
312 dprint(FD_TIME, "cycles[%d]=%llu\n", i, (unsigned long long) cycles[i]);
315 cycles_per_msec = avg;
316 dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f, N=%d\n",
317 (unsigned long long) minc,
318 (unsigned long long) maxc, mean, S, NR_TIME_ITERS);
319 dprint(FD_TIME, "trimmed mean=%llu, N=%d\n", (unsigned long long) avg, samples);
321 max_ticks = MAX_CLOCK_SEC * cycles_per_msec * 1000ULL;
322 max_mult = ULLONG_MAX / max_ticks;
323 dprint(FD_TIME, "\n\nmax_ticks=%llu, __builtin_clzll=%d, "
324 "max_mult=%llu\n", max_ticks,
325 __builtin_clzll(max_ticks), max_mult);
328 * Find the largest shift count that will produce
329 * a multiplier that does not exceed max_mult
331 tmp = max_mult * cycles_per_msec / 1000000;
335 dprint(FD_TIME, "tmp=%llu, sft=%u\n", tmp, sft);
339 clock_mult = (1ULL << sft) * 1000000 / cycles_per_msec;
340 dprint(FD_TIME, "clock_shift=%u, clock_mult=%llu\n", clock_shift,
344 * Find the greatest power of 2 clock ticks that is less than the
345 * ticks in MAX_CLOCK_SEC_2STAGE
347 max_cycles_shift = max_cycles_mask = 0;
348 tmp = MAX_CLOCK_SEC * 1000ULL * cycles_per_msec;
349 dprint(FD_TIME, "tmp=%llu, max_cycles_shift=%u\n", tmp,
354 dprint(FD_TIME, "tmp=%llu, max_cycles_shift=%u\n", tmp, max_cycles_shift);
357 * if use use (1ULL << max_cycles_shift) * 1000 / cycles_per_msec
358 * here we will have a discontinuity every
359 * (1ULL << max_cycles_shift) cycles
361 nsecs_for_max_cycles = ((1ULL << max_cycles_shift) * clock_mult)
364 /* Use a bitmask to calculate ticks % (1ULL << max_cycles_shift) */
365 for (tmp = 0; tmp < max_cycles_shift; tmp++)
366 max_cycles_mask |= 1ULL << tmp;
368 dprint(FD_TIME, "max_cycles_shift=%u, 2^max_cycles_shift=%llu, "
369 "nsecs_for_max_cycles=%llu, "
370 "max_cycles_mask=%016llx\n",
371 max_cycles_shift, (1ULL << max_cycles_shift),
372 nsecs_for_max_cycles, max_cycles_mask);
374 cycles_start = get_cpu_clock();
375 dprint(FD_TIME, "cycles_start=%llu\n", cycles_start);
379 static int calibrate_cpu_clock(void)
381 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
387 #endif // ARCH_HAVE_CPU_CLOCK
389 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(CONFIG_TLS_THREAD)
390 void fio_local_clock_init(void)
394 t = calloc(1, sizeof(*t));
395 if (pthread_setspecific(tv_tls_key, t)) {
396 log_err("fio: can't set TLS key\n");
401 static void kill_tv_tls_key(void *data)
406 void fio_local_clock_init(void)
411 void fio_clock_init(void)
413 if (fio_clock_source == fio_clock_source_inited)
416 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(CONFIG_TLS_THREAD)
417 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
418 log_err("fio: can't create TLS key\n");
421 fio_clock_source_inited = fio_clock_source;
423 if (calibrate_cpu_clock())
424 tsc_reliable = false;
427 * If the arch sets tsc_reliable != 0, then it must be good enough
428 * to use as THE clock source. For x86 CPUs, this means the TSC
429 * runs at a constant rate and is synced across CPU cores.
432 if (!fio_clock_source_set && !fio_monotonic_clocktest(0))
433 fio_clock_source = CS_CPUCLOCK;
434 } else if (fio_clock_source == CS_CPUCLOCK)
435 log_info("fio: clocksource=cpu may not be reliable\n");
436 dprint(FD_TIME, "gettime: clocksource=%d\n", (int) fio_clock_source);
439 uint64_t ntime_since(const struct timespec *s, const struct timespec *e)
443 sec = e->tv_sec - s->tv_sec;
444 nsec = e->tv_nsec - s->tv_nsec;
445 if (sec > 0 && nsec < 0) {
447 nsec += 1000000000LL;
451 * time warp bug on some kernels?
453 if (sec < 0 || (sec == 0 && nsec < 0))
456 return nsec + (sec * 1000000000LL);
459 uint64_t ntime_since_now(const struct timespec *s)
463 fio_gettime(&now, NULL);
464 return ntime_since(s, &now);
467 uint64_t utime_since(const struct timespec *s, const struct timespec *e)
471 sec = e->tv_sec - s->tv_sec;
472 usec = (e->tv_nsec - s->tv_nsec) / 1000;
473 if (sec > 0 && usec < 0) {
479 * time warp bug on some kernels?
481 if (sec < 0 || (sec == 0 && usec < 0))
484 return usec + (sec * 1000000);
487 uint64_t utime_since_now(const struct timespec *s)
490 #ifdef FIO_DEBUG_TIME
491 void *p = __builtin_return_address(0);
495 fio_gettime(&t, NULL);
498 return utime_since(s, &t);
501 uint64_t mtime_since_tv(const struct timeval *s, const struct timeval *e)
505 sec = e->tv_sec - s->tv_sec;
506 usec = (e->tv_usec - s->tv_usec);
507 if (sec > 0 && usec < 0) {
512 if (sec < 0 || (sec == 0 && usec < 0))
520 uint64_t mtime_since_now(const struct timespec *s)
523 #ifdef FIO_DEBUG_TIME
524 void *p = __builtin_return_address(0);
528 fio_gettime(&t, NULL);
531 return mtime_since(s, &t);
534 uint64_t mtime_since(const struct timespec *s, const struct timespec *e)
538 sec = e->tv_sec - s->tv_sec;
539 usec = (e->tv_nsec - s->tv_nsec) / 1000;
540 if (sec > 0 && usec < 0) {
545 if (sec < 0 || (sec == 0 && usec < 0))
553 uint64_t time_since_now(const struct timespec *s)
555 return mtime_since_now(s) / 1000;
558 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
559 defined(CONFIG_SYNC_SYNC) && defined(CONFIG_CMP_SWAP)
561 #define CLOCK_ENTRIES_DEBUG 100000
562 #define CLOCK_ENTRIES_TEST 1000
570 struct clock_thread {
575 unsigned long nr_entries;
577 struct clock_entry *entries;
580 static inline uint32_t atomic32_compare_and_swap(uint32_t *ptr, uint32_t old,
583 return __sync_val_compare_and_swap(ptr, old, new);
586 static void *clock_thread_fn(void *data)
588 struct clock_thread *t = data;
589 struct clock_entry *c;
590 os_cpu_mask_t cpu_mask;
591 unsigned long long first;
594 if (fio_cpuset_init(&cpu_mask)) {
597 log_err("clock cpuset init failed: %s\n", strerror(__err));
601 fio_cpu_set(&cpu_mask, t->cpu);
603 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
606 log_err("clock setaffinity failed: %s\n", strerror(__err));
610 fio_sem_down(&t->lock);
612 first = get_cpu_clock();
614 for (i = 0; i < t->nr_entries; i++, c++) {
623 __sync_synchronize();
624 tsc = get_cpu_clock();
625 } while (seq != atomic32_compare_and_swap(t->seq, seq, seq + 1));
635 unsigned long long clocks;
637 clocks = t->entries[i - 1].tsc - t->entries[0].tsc;
638 log_info("cs: cpu%3d: %llu clocks seen, first %llu\n", t->cpu,
643 * The most common platform clock breakage is returning zero
644 * indefinitely. Check for that and return failure.
646 if (i > 1 && !t->entries[i - 1].tsc && !t->entries[0].tsc)
649 fio_cpuset_exit(&cpu_mask);
652 fio_cpuset_exit(&cpu_mask);
657 static int clock_cmp(const void *p1, const void *p2)
659 const struct clock_entry *c1 = p1;
660 const struct clock_entry *c2 = p2;
662 if (c1->seq == c2->seq)
663 log_err("cs: bug in atomic sequence!\n");
665 return c1->seq - c2->seq;
668 int fio_monotonic_clocktest(int debug)
670 struct clock_thread *cthreads;
671 unsigned int nr_cpus = cpus_online();
672 struct clock_entry *entries;
673 unsigned long nr_entries, tentries, failed = 0;
674 struct clock_entry *prev, *this;
679 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
682 fio_debug |= 1U << FD_TIME;
684 nr_entries = CLOCK_ENTRIES_DEBUG;
686 nr_entries = CLOCK_ENTRIES_TEST;
688 calibrate_cpu_clock();
692 fio_debug &= ~(1U << FD_TIME);
696 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
697 tentries = nr_entries * nr_cpus;
698 entries = malloc(tentries * sizeof(struct clock_entry));
701 log_info("cs: Testing %u CPUs\n", nr_cpus);
703 for (i = 0; i < nr_cpus; i++) {
704 struct clock_thread *t = &cthreads[i];
709 t->nr_entries = nr_entries;
710 t->entries = &entries[i * nr_entries];
711 __fio_sem_init(&t->lock, FIO_SEM_LOCKED);
712 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
719 for (i = 0; i < nr_cpus; i++) {
720 struct clock_thread *t = &cthreads[i];
722 fio_sem_up(&t->lock);
725 for (i = 0; i < nr_cpus; i++) {
726 struct clock_thread *t = &cthreads[i];
729 pthread_join(t->thread, &ret);
732 __fio_sem_remove(&t->lock);
738 log_err("Clocksource test: %lu threads failed\n", failed);
742 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
744 /* silence silly gcc */
746 for (failed = i = 0; i < tentries; i++) {
754 if (prev->tsc > this->tsc) {
755 uint64_t diff = prev->tsc - this->tsc;
762 log_info("cs: CPU clock mismatch (diff=%llu):\n",
763 (unsigned long long) diff);
764 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
765 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
774 log_info("cs: Failed: %lu\n", failed);
776 log_info("cs: Pass!\n");
783 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
785 int fio_monotonic_clocktest(int debug)
788 log_info("cs: current platform does not support CPU clocks\n");