16 #if defined(ARCH_HAVE_CPU_CLOCK)
17 #ifndef ARCH_CPU_CLOCK_CYCLES_PER_USEC
18 static unsigned long cycles_per_usec;
19 static unsigned long inv_cycles_per_usec;
20 static uint64_t max_cycles_for_mult;
22 #ifdef ARCH_CPU_CLOCK_WRAPS
23 static unsigned long long cycles_start, cycles_wrap;
33 #ifdef ARCH_HAVE_CPU_CLOCK
34 #ifdef CONFIG_TLS_THREAD
35 static __thread struct tv_valid static_tv_valid;
37 static pthread_key_t tv_tls_key;
41 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
42 int fio_clock_source_set = 0;
43 static enum fio_cs fio_clock_source_inited = CS_INVAL;
48 #define HASH_SIZE (1 << HASH_BITS)
50 static struct flist_head hash[HASH_SIZE];
51 static int gtod_inited;
54 struct flist_head list;
59 static struct gtod_log *find_hash(void *caller)
61 unsigned long h = hash_ptr(caller, HASH_BITS);
62 struct flist_head *entry;
64 flist_for_each(entry, &hash[h]) {
65 struct gtod_log *log = flist_entry(entry, struct gtod_log,
68 if (log->caller == caller)
75 static void inc_caller(void *caller)
77 struct gtod_log *log = find_hash(caller);
82 log = malloc(sizeof(*log));
83 INIT_FLIST_HEAD(&log->list);
87 h = hash_ptr(caller, HASH_BITS);
88 flist_add_tail(&log->list, &hash[h]);
94 static void gtod_log_caller(void *caller)
100 static void fio_exit fio_dump_gtod(void)
102 unsigned long total_calls = 0;
105 for (i = 0; i < HASH_SIZE; i++) {
106 struct flist_head *entry;
107 struct gtod_log *log;
109 flist_for_each(entry, &hash[i]) {
110 log = flist_entry(entry, struct gtod_log, list);
112 printf("function %p, calls %lu\n", log->caller,
114 total_calls += log->calls;
118 printf("Total %lu gettimeofday\n", total_calls);
121 static void fio_init gtod_init(void)
125 for (i = 0; i < HASH_SIZE; i++)
126 INIT_FLIST_HEAD(&hash[i]);
131 #endif /* FIO_DEBUG_TIME */
133 #ifdef CONFIG_CLOCK_GETTIME
134 static int fill_clock_gettime(struct timespec *ts)
136 #if defined(CONFIG_CLOCK_MONOTONIC_RAW)
137 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
138 #elif defined(CONFIG_CLOCK_MONOTONIC)
139 return clock_gettime(CLOCK_MONOTONIC, ts);
141 return clock_gettime(CLOCK_REALTIME, ts);
146 static void __fio_gettime(struct timeval *tp)
148 switch (fio_clock_source) {
149 #ifdef CONFIG_GETTIMEOFDAY
151 gettimeofday(tp, NULL);
154 #ifdef CONFIG_CLOCK_GETTIME
158 if (fill_clock_gettime(&ts) < 0) {
159 log_err("fio: clock_gettime fails\n");
163 tp->tv_sec = ts.tv_sec;
164 tp->tv_usec = ts.tv_nsec / 1000;
168 #ifdef ARCH_HAVE_CPU_CLOCK
173 #ifdef CONFIG_TLS_THREAD
174 tv = &static_tv_valid;
176 tv = pthread_getspecific(tv_tls_key);
180 #ifdef ARCH_CPU_CLOCK_WRAPS
181 if (t < cycles_start && !cycles_wrap)
183 else if (cycles_wrap && t >= cycles_start && !tv->warned) {
184 log_err("fio: double CPU clock wrap\n");
191 tv->last_tv_valid = 1;
192 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
193 usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
195 if (t < max_cycles_for_mult)
196 usecs = (t * inv_cycles_per_usec) / 16777216UL;
198 usecs = t / cycles_per_usec;
200 tp->tv_sec = usecs / 1000000;
201 tp->tv_usec = usecs % 1000000;
206 log_err("fio: invalid clock source %d\n", fio_clock_source);
211 #ifdef FIO_DEBUG_TIME
212 void fio_gettime(struct timeval *tp, void *caller)
214 void fio_gettime(struct timeval *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_usec(void)
234 enum fio_cs old_cs = fio_clock_source;
236 #ifdef CONFIG_CLOCK_GETTIME
237 fio_clock_source = CS_CGETTIME;
239 fio_clock_source = CS_GTOD;
243 c_s = get_cpu_clock();
249 elapsed = utime_since(&s, &e);
250 if (elapsed >= 1280) {
251 c_e = get_cpu_clock();
256 fio_clock_source = old_cs;
257 return (c_e - c_s + 127) >> 7;
260 #define NR_TIME_ITERS 50
262 static int calibrate_cpu_clock(void)
264 double delta, mean, S;
265 uint64_t minc, maxc, avg, cycles[NR_TIME_ITERS];
268 cycles[0] = get_cycles_per_usec();
269 S = delta = mean = 0.0;
270 for (i = 0; i < NR_TIME_ITERS; i++) {
271 cycles[i] = get_cycles_per_usec();
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;
305 for (i = 0; i < NR_TIME_ITERS; i++)
306 dprint(FD_TIME, "cycles[%d]=%llu\n", i,
307 (unsigned long long) cycles[i] / 10);
310 avg = (avg + 5) / 10;
313 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
314 dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f\n",
315 (unsigned long long) minc,
316 (unsigned long long) maxc, mean, S);
318 cycles_per_usec = avg;
319 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
320 max_cycles_for_mult = ~0ULL / inv_cycles_per_usec;
321 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
322 #ifdef ARCH_CPU_CLOCK_WRAPS
323 cycles_start = get_cpu_clock();
324 dprint(FD_TIME, "cycles_start=%llu\n", cycles_start);
329 static int calibrate_cpu_clock(void)
331 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
337 #endif // ARCH_HAVE_CPU_CLOCK
339 #ifndef CONFIG_TLS_THREAD
340 void fio_local_clock_init(int is_thread)
344 t = calloc(1, sizeof(*t));
345 if (pthread_setspecific(tv_tls_key, t)) {
346 log_err("fio: can't set TLS key\n");
351 static void kill_tv_tls_key(void *data)
356 void fio_local_clock_init(int is_thread)
361 void fio_clock_init(void)
363 if (fio_clock_source == fio_clock_source_inited)
366 #ifndef CONFIG_TLS_THREAD
367 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
368 log_err("fio: can't create TLS key\n");
371 fio_clock_source_inited = fio_clock_source;
373 if (calibrate_cpu_clock())
377 * If the arch sets tsc_reliable != 0, then it must be good enough
378 * to use as THE clock source. For x86 CPUs, this means the TSC
379 * runs at a constant rate and is synced across CPU cores.
382 if (!fio_clock_source_set && !fio_monotonic_clocktest(0))
383 fio_clock_source = CS_CPUCLOCK;
384 } else if (fio_clock_source == CS_CPUCLOCK)
385 log_info("fio: clocksource=cpu may not be reliable\n");
388 uint64_t utime_since(const struct timeval *s, const struct timeval *e)
393 sec = e->tv_sec - s->tv_sec;
394 usec = e->tv_usec - s->tv_usec;
395 if (sec > 0 && usec < 0) {
401 * time warp bug on some kernels?
403 if (sec < 0 || (sec == 0 && usec < 0))
406 ret = sec * 1000000ULL + usec;
411 uint64_t utime_since_now(const struct timeval *s)
415 fio_gettime(&t, NULL);
416 return utime_since(s, &t);
419 uint64_t mtime_since(const struct timeval *s, const struct timeval *e)
423 sec = e->tv_sec - s->tv_sec;
424 usec = e->tv_usec - s->tv_usec;
425 if (sec > 0 && usec < 0) {
430 if (sec < 0 || (sec == 0 && usec < 0))
440 uint64_t mtime_since_now(const struct timeval *s)
443 void *p = __builtin_return_address(0);
446 return mtime_since(s, &t);
449 uint64_t time_since_now(const struct timeval *s)
451 return mtime_since_now(s) / 1000;
454 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
457 #define CLOCK_ENTRIES_DEBUG 100000
458 #define CLOCK_ENTRIES_TEST 10000
466 struct clock_thread {
470 pthread_mutex_t lock;
471 pthread_mutex_t started;
472 unsigned long nr_entries;
474 struct clock_entry *entries;
477 static inline uint32_t atomic32_inc_return(uint32_t *seq)
479 return 1 + __sync_fetch_and_add(seq, 1);
482 static void *clock_thread_fn(void *data)
484 struct clock_thread *t = data;
485 struct clock_entry *c;
486 os_cpu_mask_t cpu_mask;
488 unsigned long long first;
491 if (fio_cpuset_init(&cpu_mask)) {
494 log_err("clock cpuset init failed: %s\n", strerror(__err));
498 fio_cpu_set(&cpu_mask, t->cpu);
500 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
503 log_err("clock setaffinity failed: %s\n", strerror(__err));
507 pthread_mutex_lock(&t->lock);
508 pthread_mutex_unlock(&t->started);
510 first = get_cpu_clock();
513 for (i = 0; i < t->nr_entries; i++, c++) {
519 seq = atomic32_inc_return(t->seq);
522 tsc = get_cpu_clock();
523 } while (seq != *t->seq);
530 unsigned long long clocks;
532 clocks = t->entries[i - 1].tsc - t->entries[0].tsc;
533 log_info("cs: cpu%3d: %llu clocks seen, first %llu\n", t->cpu,
538 * The most common platform clock breakage is returning zero
539 * indefinitely. Check for that and return failure.
541 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
544 fio_cpuset_exit(&cpu_mask);
547 fio_cpuset_exit(&cpu_mask);
552 static int clock_cmp(const void *p1, const void *p2)
554 const struct clock_entry *c1 = p1;
555 const struct clock_entry *c2 = p2;
557 if (c1->seq == c2->seq)
558 log_err("cs: bug in atomic sequence!\n");
560 return c1->seq - c2->seq;
563 int fio_monotonic_clocktest(int debug)
565 struct clock_thread *cthreads;
566 unsigned int nr_cpus = cpus_online();
567 struct clock_entry *entries;
568 unsigned long nr_entries, tentries, failed = 0;
569 struct clock_entry *prev, *this;
574 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
577 fio_debug |= 1U << FD_TIME;
579 nr_entries = CLOCK_ENTRIES_DEBUG;
581 nr_entries = CLOCK_ENTRIES_TEST;
583 calibrate_cpu_clock();
587 fio_debug &= ~(1U << FD_TIME);
591 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
592 tentries = nr_entries * nr_cpus;
593 entries = malloc(tentries * sizeof(struct clock_entry));
596 log_info("cs: Testing %u CPUs\n", nr_cpus);
598 for (i = 0; i < nr_cpus; i++) {
599 struct clock_thread *t = &cthreads[i];
604 t->nr_entries = nr_entries;
605 t->entries = &entries[i * nr_entries];
606 pthread_mutex_init(&t->lock, NULL);
607 pthread_mutex_init(&t->started, NULL);
608 pthread_mutex_lock(&t->lock);
609 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
616 for (i = 0; i < nr_cpus; i++) {
617 struct clock_thread *t = &cthreads[i];
619 pthread_mutex_lock(&t->started);
622 for (i = 0; i < nr_cpus; i++) {
623 struct clock_thread *t = &cthreads[i];
625 pthread_mutex_unlock(&t->lock);
628 for (i = 0; i < nr_cpus; i++) {
629 struct clock_thread *t = &cthreads[i];
632 pthread_join(t->thread, &ret);
640 log_err("Clocksource test: %lu threads failed\n", failed);
644 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
646 /* silence silly gcc */
648 for (failed = i = 0; i < tentries; i++) {
656 if (prev->tsc > this->tsc) {
657 uint64_t diff = prev->tsc - this->tsc;
664 log_info("cs: CPU clock mismatch (diff=%llu):\n",
665 (unsigned long long) diff);
666 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
667 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
676 log_info("cs: Failed: %lu\n", failed);
678 log_info("cs: Pass!\n");
685 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
687 int fio_monotonic_clocktest(int debug)
690 log_info("cs: current platform does not support CPU clocks\n");