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 */
130 #ifdef CONFIG_CLOCK_GETTIME
131 static int fill_clock_gettime(struct timespec *ts)
133 #if defined(CONFIG_CLOCK_MONOTONIC_RAW)
134 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
135 #elif defined(CONFIG_CLOCK_MONOTONIC)
136 return clock_gettime(CLOCK_MONOTONIC, ts);
138 return clock_gettime(CLOCK_REALTIME, ts);
143 static void __fio_gettime(struct timespec *tp)
145 switch (fio_clock_source) {
146 #ifdef CONFIG_GETTIMEOFDAY
149 gettimeofday(&tv, NULL);
151 tp->tv_sec = tv.tv_sec;
152 tp->tv_nsec = tv.tv_usec * 1000;
156 #ifdef CONFIG_CLOCK_GETTIME
158 if (fill_clock_gettime(tp) < 0) {
159 log_err("fio: clock_gettime fails\n");
165 #ifdef ARCH_HAVE_CPU_CLOCK
167 uint64_t nsecs, t, multiples;
170 #ifdef CONFIG_TLS_THREAD
171 tv = &static_tv_valid;
173 tv = pthread_getspecific(tv_tls_key);
177 #ifdef ARCH_CPU_CLOCK_WRAPS
178 if (t < cycles_start && !cycles_wrap)
180 else if (cycles_wrap && t >= cycles_start && !tv->warned) {
181 log_err("fio: double CPU clock wrap\n");
185 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
186 nsecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC * 1000;
189 multiples = t >> max_cycles_shift;
190 nsecs = multiples * nsecs_for_max_cycles;
191 nsecs += ((t & max_cycles_mask) * clock_mult) >> clock_shift;
193 tp->tv_sec = nsecs / 1000000000ULL;
194 tp->tv_nsec = nsecs % 1000000000ULL;
199 log_err("fio: invalid clock source %d\n", fio_clock_source);
204 #ifdef FIO_DEBUG_TIME
205 void fio_gettime(struct timespec *tp, void *caller)
207 void fio_gettime(struct timespec *tp, void fio_unused *caller)
210 #ifdef FIO_DEBUG_TIME
212 caller = __builtin_return_address(0);
214 gtod_log_caller(caller);
216 if (fio_unlikely(fio_gettime_offload(tp)))
222 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
223 static unsigned long get_cycles_per_msec(void)
225 struct timespec s, e;
227 enum fio_cs old_cs = fio_clock_source;
230 #ifdef CONFIG_CLOCK_GETTIME
231 fio_clock_source = CS_CGETTIME;
233 fio_clock_source = CS_GTOD;
237 c_s = get_cpu_clock();
241 elapsed = utime_since(&s, &e);
242 if (elapsed >= 1280) {
243 c_e = get_cpu_clock();
248 fio_clock_source = old_cs;
249 return (c_e - c_s) * 1000 / elapsed;
252 #define NR_TIME_ITERS 50
254 static int calibrate_cpu_clock(void)
256 double delta, mean, S;
257 uint64_t minc, maxc, avg, cycles[NR_TIME_ITERS];
258 int i, samples, sft = 0;
259 unsigned long long tmp, max_ticks, max_mult;
261 cycles[0] = get_cycles_per_msec();
262 S = delta = mean = 0.0;
263 for (i = 0; i < NR_TIME_ITERS; i++) {
264 cycles[i] = get_cycles_per_msec();
265 delta = cycles[i] - mean;
267 mean += delta / (i + 1.0);
268 S += delta * (cycles[i] - mean);
273 * The most common platform clock breakage is returning zero
274 * indefinitely. Check for that and return failure.
276 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
279 S = sqrt(S / (NR_TIME_ITERS - 1.0));
282 maxc = samples = avg = 0;
283 for (i = 0; i < NR_TIME_ITERS; i++) {
284 double this = cycles[i];
286 minc = min(cycles[i], minc);
287 maxc = max(cycles[i], maxc);
289 if ((fmax(this, mean) - fmin(this, mean)) > S)
295 S /= (double) NR_TIME_ITERS;
297 for (i = 0; i < NR_TIME_ITERS; i++)
298 dprint(FD_TIME, "cycles[%d]=%llu\n", i, (unsigned long long) cycles[i]);
301 cycles_per_msec = avg;
302 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
303 dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f\n",
304 (unsigned long long) minc,
305 (unsigned long long) maxc, mean, S);
307 max_ticks = MAX_CLOCK_SEC * cycles_per_msec * 1000ULL;
308 max_mult = ULLONG_MAX / max_ticks;
309 dprint(FD_TIME, "\n\nmax_ticks=%llu, __builtin_clzll=%d, "
310 "max_mult=%llu\n", max_ticks,
311 __builtin_clzll(max_ticks), max_mult);
314 * Find the largest shift count that will produce
315 * a multiplier that does not exceed max_mult
317 tmp = max_mult * cycles_per_msec / 1000000;
321 dprint(FD_TIME, "tmp=%llu, sft=%u\n", tmp, sft);
325 clock_mult = (1ULL << sft) * 1000000 / cycles_per_msec;
326 dprint(FD_TIME, "clock_shift=%u, clock_mult=%llu\n", clock_shift,
330 * Find the greatest power of 2 clock ticks that is less than the
331 * ticks in MAX_CLOCK_SEC_2STAGE
333 max_cycles_shift = max_cycles_mask = 0;
334 tmp = MAX_CLOCK_SEC * 1000ULL * cycles_per_msec;
335 dprint(FD_TIME, "tmp=%llu, max_cycles_shift=%u\n", tmp,
340 dprint(FD_TIME, "tmp=%llu, max_cycles_shift=%u\n", tmp, max_cycles_shift);
343 * if use use (1ULL << max_cycles_shift) * 1000 / cycles_per_msec
344 * here we will have a discontinuity every
345 * (1ULL << max_cycles_shift) cycles
347 nsecs_for_max_cycles = ((1ULL << max_cycles_shift) * clock_mult)
350 /* Use a bitmask to calculate ticks % (1ULL << max_cycles_shift) */
351 for (tmp = 0; tmp < max_cycles_shift; tmp++)
352 max_cycles_mask |= 1ULL << tmp;
354 dprint(FD_TIME, "max_cycles_shift=%u, 2^max_cycles_shift=%llu, "
355 "nsecs_for_max_cycles=%llu, "
356 "max_cycles_mask=%016llx\n",
357 max_cycles_shift, (1ULL << max_cycles_shift),
358 nsecs_for_max_cycles, max_cycles_mask);
360 cycles_start = get_cpu_clock();
361 dprint(FD_TIME, "cycles_start=%llu\n", cycles_start);
365 static int calibrate_cpu_clock(void)
367 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
373 #endif // ARCH_HAVE_CPU_CLOCK
375 #ifndef CONFIG_TLS_THREAD
376 void fio_local_clock_init(int is_thread)
380 t = calloc(1, sizeof(*t));
381 if (pthread_setspecific(tv_tls_key, t)) {
382 log_err("fio: can't set TLS key\n");
387 static void kill_tv_tls_key(void *data)
392 void fio_local_clock_init(int is_thread)
397 void fio_clock_init(void)
399 if (fio_clock_source == fio_clock_source_inited)
402 #ifndef CONFIG_TLS_THREAD
403 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
404 log_err("fio: can't create TLS key\n");
407 fio_clock_source_inited = fio_clock_source;
409 if (calibrate_cpu_clock())
410 tsc_reliable = false;
413 * If the arch sets tsc_reliable != 0, then it must be good enough
414 * to use as THE clock source. For x86 CPUs, this means the TSC
415 * runs at a constant rate and is synced across CPU cores.
418 if (!fio_clock_source_set && !fio_monotonic_clocktest(0))
419 fio_clock_source = CS_CPUCLOCK;
420 } else if (fio_clock_source == CS_CPUCLOCK)
421 log_info("fio: clocksource=cpu may not be reliable\n");
422 dprint(FD_TIME, "gettime: clocksource=%d\n", (int) fio_clock_source);
425 uint64_t ntime_since(const struct timespec *s, const struct timespec *e)
429 sec = e->tv_sec - s->tv_sec;
430 nsec = e->tv_nsec - s->tv_nsec;
431 if (sec > 0 && nsec < 0) {
433 nsec += 1000000000LL;
437 * time warp bug on some kernels?
439 if (sec < 0 || (sec == 0 && nsec < 0))
442 return nsec + (sec * 1000000000LL);
445 uint64_t ntime_since_now(const struct timespec *s)
449 fio_gettime(&now, NULL);
450 return ntime_since(s, &now);
453 uint64_t utime_since(const struct timespec *s, const struct timespec *e)
457 sec = e->tv_sec - s->tv_sec;
458 usec = (e->tv_nsec - s->tv_nsec) / 1000;
459 if (sec > 0 && usec < 0) {
465 * time warp bug on some kernels?
467 if (sec < 0 || (sec == 0 && usec < 0))
470 return usec + (sec * 1000000);
473 uint64_t utime_since_now(const struct timespec *s)
476 #ifdef FIO_DEBUG_TIME
477 void *p = __builtin_return_address(0);
481 fio_gettime(&t, NULL);
484 return utime_since(s, &t);
487 uint64_t mtime_since_tv(const struct timeval *s, const struct timeval *e)
491 sec = e->tv_sec - s->tv_sec;
492 usec = (e->tv_usec - s->tv_usec);
493 if (sec > 0 && usec < 0) {
498 if (sec < 0 || (sec == 0 && usec < 0))
506 uint64_t mtime_since_now(const struct timespec *s)
509 #ifdef FIO_DEBUG_TIME
510 void *p = __builtin_return_address(0);
514 fio_gettime(&t, NULL);
517 return mtime_since(s, &t);
520 uint64_t mtime_since(const struct timespec *s, const struct timespec *e)
524 sec = e->tv_sec - s->tv_sec;
525 usec = (e->tv_nsec - s->tv_nsec) / 1000;
526 if (sec > 0 && usec < 0) {
531 if (sec < 0 || (sec == 0 && usec < 0))
539 uint64_t time_since_now(const struct timespec *s)
541 return mtime_since_now(s) / 1000;
544 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
545 defined(CONFIG_SYNC_SYNC) && defined(CONFIG_CMP_SWAP)
547 #define CLOCK_ENTRIES_DEBUG 100000
548 #define CLOCK_ENTRIES_TEST 1000
556 struct clock_thread {
561 unsigned long nr_entries;
563 struct clock_entry *entries;
566 static inline uint32_t atomic32_compare_and_swap(uint32_t *ptr, uint32_t old,
569 return __sync_val_compare_and_swap(ptr, old, new);
572 static void *clock_thread_fn(void *data)
574 struct clock_thread *t = data;
575 struct clock_entry *c;
576 os_cpu_mask_t cpu_mask;
577 unsigned long long first;
580 if (fio_cpuset_init(&cpu_mask)) {
583 log_err("clock cpuset init failed: %s\n", strerror(__err));
587 fio_cpu_set(&cpu_mask, t->cpu);
589 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
592 log_err("clock setaffinity failed: %s\n", strerror(__err));
596 fio_sem_down(&t->lock);
598 first = get_cpu_clock();
600 for (i = 0; i < t->nr_entries; i++, c++) {
609 __sync_synchronize();
610 tsc = get_cpu_clock();
611 } while (seq != atomic32_compare_and_swap(t->seq, seq, seq + 1));
621 unsigned long long clocks;
623 clocks = t->entries[i - 1].tsc - t->entries[0].tsc;
624 log_info("cs: cpu%3d: %llu clocks seen, first %llu\n", t->cpu,
629 * The most common platform clock breakage is returning zero
630 * indefinitely. Check for that and return failure.
632 if (i > 1 && !t->entries[i - 1].tsc && !t->entries[0].tsc)
635 fio_cpuset_exit(&cpu_mask);
638 fio_cpuset_exit(&cpu_mask);
643 static int clock_cmp(const void *p1, const void *p2)
645 const struct clock_entry *c1 = p1;
646 const struct clock_entry *c2 = p2;
648 if (c1->seq == c2->seq)
649 log_err("cs: bug in atomic sequence!\n");
651 return c1->seq - c2->seq;
654 int fio_monotonic_clocktest(int debug)
656 struct clock_thread *cthreads;
657 unsigned int nr_cpus = cpus_online();
658 struct clock_entry *entries;
659 unsigned long nr_entries, tentries, failed = 0;
660 struct clock_entry *prev, *this;
665 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
668 fio_debug |= 1U << FD_TIME;
670 nr_entries = CLOCK_ENTRIES_DEBUG;
672 nr_entries = CLOCK_ENTRIES_TEST;
674 calibrate_cpu_clock();
678 fio_debug &= ~(1U << FD_TIME);
682 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
683 tentries = nr_entries * nr_cpus;
684 entries = malloc(tentries * sizeof(struct clock_entry));
687 log_info("cs: Testing %u CPUs\n", nr_cpus);
689 for (i = 0; i < nr_cpus; i++) {
690 struct clock_thread *t = &cthreads[i];
695 t->nr_entries = nr_entries;
696 t->entries = &entries[i * nr_entries];
697 __fio_sem_init(&t->lock, FIO_SEM_LOCKED);
698 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
705 for (i = 0; i < nr_cpus; i++) {
706 struct clock_thread *t = &cthreads[i];
708 fio_sem_up(&t->lock);
711 for (i = 0; i < nr_cpus; i++) {
712 struct clock_thread *t = &cthreads[i];
715 pthread_join(t->thread, &ret);
718 __fio_sem_remove(&t->lock);
724 log_err("Clocksource test: %lu threads failed\n", failed);
728 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
730 /* silence silly gcc */
732 for (failed = i = 0; i < tentries; i++) {
740 if (prev->tsc > this->tsc) {
741 uint64_t diff = prev->tsc - this->tsc;
748 log_info("cs: CPU clock mismatch (diff=%llu):\n",
749 (unsigned long long) diff);
750 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
751 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
760 log_info("cs: Failed: %lu\n", failed);
762 log_info("cs: Pass!\n");
769 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
771 int fio_monotonic_clocktest(int debug)
774 log_info("cs: current platform does not support CPU clocks\n");