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 #ifdef CONFIG_CLOCK_MONOTONIC
137 return clock_gettime(CLOCK_MONOTONIC, ts);
139 return clock_gettime(CLOCK_REALTIME, ts);
144 static void __fio_gettime(struct timeval *tp)
146 switch (fio_clock_source) {
147 #ifdef CONFIG_GETTIMEOFDAY
149 gettimeofday(tp, NULL);
152 #ifdef CONFIG_CLOCK_GETTIME
156 if (fill_clock_gettime(&ts) < 0) {
157 log_err("fio: clock_gettime fails\n");
161 tp->tv_sec = ts.tv_sec;
162 tp->tv_usec = ts.tv_nsec / 1000;
166 #ifdef ARCH_HAVE_CPU_CLOCK
171 #ifdef CONFIG_TLS_THREAD
172 tv = &static_tv_valid;
174 tv = pthread_getspecific(tv_tls_key);
178 #ifdef ARCH_CPU_CLOCK_WRAPS
179 if (t < cycles_start && !cycles_wrap)
181 else if (cycles_wrap && t >= cycles_start && !tv->warned) {
182 log_err("fio: double CPU clock wrap\n");
189 tv->last_tv_valid = 1;
190 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
191 usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
193 if (t < max_cycles_for_mult)
194 usecs = (t * inv_cycles_per_usec) / 16777216UL;
196 usecs = t / cycles_per_usec;
198 tp->tv_sec = usecs / 1000000;
199 tp->tv_usec = usecs % 1000000;
204 log_err("fio: invalid clock source %d\n", fio_clock_source);
209 #ifdef FIO_DEBUG_TIME
210 void fio_gettime(struct timeval *tp, void *caller)
212 void fio_gettime(struct timeval *tp, void fio_unused *caller)
215 #ifdef FIO_DEBUG_TIME
217 caller = __builtin_return_address(0);
219 gtod_log_caller(caller);
221 if (fio_unlikely(fio_gettime_offload(tp)))
227 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
228 static unsigned long get_cycles_per_usec(void)
232 enum fio_cs old_cs = fio_clock_source;
234 #ifdef CONFIG_CLOCK_GETTIME
235 fio_clock_source = CS_CGETTIME;
237 fio_clock_source = CS_GTOD;
241 c_s = get_cpu_clock();
247 elapsed = utime_since(&s, &e);
248 if (elapsed >= 1280) {
249 c_e = get_cpu_clock();
254 fio_clock_source = old_cs;
255 return (c_e - c_s + 127) >> 7;
258 #define NR_TIME_ITERS 50
260 static int calibrate_cpu_clock(void)
262 double delta, mean, S;
263 uint64_t minc, maxc, avg, cycles[NR_TIME_ITERS];
266 cycles[0] = get_cycles_per_usec();
267 S = delta = mean = 0.0;
268 for (i = 0; i < NR_TIME_ITERS; i++) {
269 cycles[i] = get_cycles_per_usec();
270 delta = cycles[i] - mean;
272 mean += delta / (i + 1.0);
273 S += delta * (cycles[i] - mean);
278 * The most common platform clock breakage is returning zero
279 * indefinitely. Check for that and return failure.
281 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
284 S = sqrt(S / (NR_TIME_ITERS - 1.0));
287 maxc = samples = avg = 0;
288 for (i = 0; i < NR_TIME_ITERS; i++) {
289 double this = cycles[i];
291 minc = min(cycles[i], minc);
292 maxc = max(cycles[i], maxc);
294 if ((fmax(this, mean) - fmin(this, mean)) > S)
300 S /= (double) NR_TIME_ITERS;
303 for (i = 0; i < NR_TIME_ITERS; i++)
304 dprint(FD_TIME, "cycles[%d]=%llu\n", i,
305 (unsigned long long) cycles[i] / 10);
308 avg = (avg + 5) / 10;
311 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
312 dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f\n",
313 (unsigned long long) minc,
314 (unsigned long long) maxc, mean, S);
316 cycles_per_usec = avg;
317 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
318 max_cycles_for_mult = ~0ULL / inv_cycles_per_usec;
319 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
320 #ifdef ARCH_CPU_CLOCK_WRAPS
321 cycles_start = get_cpu_clock();
322 dprint(FD_TIME, "cycles_start=%llu\n", cycles_start);
327 static int calibrate_cpu_clock(void)
329 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
335 #endif // ARCH_HAVE_CPU_CLOCK
337 #ifndef CONFIG_TLS_THREAD
338 void fio_local_clock_init(int is_thread)
342 t = calloc(1, sizeof(*t));
343 if (pthread_setspecific(tv_tls_key, t)) {
344 log_err("fio: can't set TLS key\n");
349 static void kill_tv_tls_key(void *data)
354 void fio_local_clock_init(int is_thread)
359 void fio_clock_init(void)
361 if (fio_clock_source == fio_clock_source_inited)
364 #ifndef CONFIG_TLS_THREAD
365 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
366 log_err("fio: can't create TLS key\n");
369 fio_clock_source_inited = fio_clock_source;
371 if (calibrate_cpu_clock())
375 * If the arch sets tsc_reliable != 0, then it must be good enough
376 * to use as THE clock source. For x86 CPUs, this means the TSC
377 * runs at a constant rate and is synced across CPU cores.
380 if (!fio_clock_source_set && !fio_monotonic_clocktest(0))
381 fio_clock_source = CS_CPUCLOCK;
382 } else if (fio_clock_source == CS_CPUCLOCK)
383 log_info("fio: clocksource=cpu may not be reliable\n");
386 uint64_t utime_since(const struct timeval *s, const struct timeval *e)
391 sec = e->tv_sec - s->tv_sec;
392 usec = e->tv_usec - s->tv_usec;
393 if (sec > 0 && usec < 0) {
399 * time warp bug on some kernels?
401 if (sec < 0 || (sec == 0 && usec < 0))
404 ret = sec * 1000000ULL + usec;
409 uint64_t utime_since_now(const struct timeval *s)
413 fio_gettime(&t, NULL);
414 return utime_since(s, &t);
417 uint64_t mtime_since(const struct timeval *s, const struct timeval *e)
421 sec = e->tv_sec - s->tv_sec;
422 usec = e->tv_usec - s->tv_usec;
423 if (sec > 0 && usec < 0) {
428 if (sec < 0 || (sec == 0 && usec < 0))
438 uint64_t mtime_since_now(const struct timeval *s)
441 void *p = __builtin_return_address(0);
444 return mtime_since(s, &t);
447 uint64_t time_since_now(const struct timeval *s)
449 return mtime_since_now(s) / 1000;
452 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
455 #define CLOCK_ENTRIES_DEBUG 100000
456 #define CLOCK_ENTRIES_TEST 10000
464 struct clock_thread {
468 pthread_mutex_t lock;
469 pthread_mutex_t started;
470 unsigned long nr_entries;
472 struct clock_entry *entries;
475 static inline uint32_t atomic32_inc_return(uint32_t *seq)
477 return 1 + __sync_fetch_and_add(seq, 1);
480 static void *clock_thread_fn(void *data)
482 struct clock_thread *t = data;
483 struct clock_entry *c;
484 os_cpu_mask_t cpu_mask;
488 if (fio_cpuset_init(&cpu_mask)) {
491 log_err("clock cpuset init failed: %s\n", strerror(__err));
495 fio_cpu_set(&cpu_mask, t->cpu);
497 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
500 log_err("clock setaffinity failed: %s\n", strerror(__err));
504 pthread_mutex_lock(&t->lock);
505 pthread_mutex_unlock(&t->started);
509 for (i = 0; i < t->nr_entries; i++, c++) {
515 seq = atomic32_inc_return(t->seq);
518 tsc = get_cpu_clock();
519 } while (seq != *t->seq);
526 unsigned long long clocks;
528 clocks = t->entries[i - 1].tsc - t->entries[0].tsc;
529 log_info("cs: cpu%3d: %llu clocks seen\n", t->cpu, clocks);
533 * The most common platform clock breakage is returning zero
534 * indefinitely. Check for that and return failure.
536 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
539 fio_cpuset_exit(&cpu_mask);
542 fio_cpuset_exit(&cpu_mask);
547 static int clock_cmp(const void *p1, const void *p2)
549 const struct clock_entry *c1 = p1;
550 const struct clock_entry *c2 = p2;
552 if (c1->seq == c2->seq)
553 log_err("cs: bug in atomic sequence!\n");
555 return c1->seq - c2->seq;
558 int fio_monotonic_clocktest(int debug)
560 struct clock_thread *cthreads;
561 unsigned int nr_cpus = cpus_online();
562 struct clock_entry *entries;
563 unsigned long nr_entries, tentries, failed = 0;
564 struct clock_entry *prev, *this;
569 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
572 fio_debug |= 1U << FD_TIME;
574 nr_entries = CLOCK_ENTRIES_DEBUG;
576 nr_entries = CLOCK_ENTRIES_TEST;
578 calibrate_cpu_clock();
582 fio_debug &= ~(1U << FD_TIME);
586 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
587 tentries = nr_entries * nr_cpus;
588 entries = malloc(tentries * sizeof(struct clock_entry));
591 log_info("cs: Testing %u CPUs\n", nr_cpus);
593 for (i = 0; i < nr_cpus; i++) {
594 struct clock_thread *t = &cthreads[i];
599 t->nr_entries = nr_entries;
600 t->entries = &entries[i * nr_entries];
601 pthread_mutex_init(&t->lock, NULL);
602 pthread_mutex_init(&t->started, NULL);
603 pthread_mutex_lock(&t->lock);
604 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
611 for (i = 0; i < nr_cpus; i++) {
612 struct clock_thread *t = &cthreads[i];
614 pthread_mutex_lock(&t->started);
617 for (i = 0; i < nr_cpus; i++) {
618 struct clock_thread *t = &cthreads[i];
620 pthread_mutex_unlock(&t->lock);
623 for (i = 0; i < nr_cpus; i++) {
624 struct clock_thread *t = &cthreads[i];
627 pthread_join(t->thread, &ret);
635 log_err("Clocksource test: %lu threads failed\n", failed);
639 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
641 for (failed = i = 0; i < tentries; i++) {
649 if (prev->tsc > this->tsc) {
650 uint64_t diff = prev->tsc - this->tsc;
657 log_info("cs: CPU clock mismatch (diff=%llu):\n",
658 (unsigned long long) diff);
659 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
660 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
669 log_info("cs: Failed: %lu\n", failed);
671 log_info("cs: Pass!\n");
678 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
680 int fio_monotonic_clocktest(int debug)
683 log_info("cs: current platform does not support CPU clocks\n");