16 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
17 static unsigned long cycles_per_usec;
18 static unsigned long inv_cycles_per_usec;
19 static uint64_t max_cycles_for_mult;
20 static unsigned long long cycles_start, cycles_wrap;
29 #ifdef ARCH_HAVE_CPU_CLOCK
30 #ifdef CONFIG_TLS_THREAD
31 static __thread struct tv_valid static_tv_valid;
33 static pthread_key_t tv_tls_key;
37 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
38 int fio_clock_source_set = 0;
39 static enum fio_cs fio_clock_source_inited = CS_INVAL;
44 #define HASH_SIZE (1 << HASH_BITS)
46 static struct flist_head hash[HASH_SIZE];
47 static int gtod_inited;
50 struct flist_head list;
55 static struct gtod_log *find_hash(void *caller)
57 unsigned long h = hash_ptr(caller, HASH_BITS);
58 struct flist_head *entry;
60 flist_for_each(entry, &hash[h]) {
61 struct gtod_log *log = flist_entry(entry, struct gtod_log,
64 if (log->caller == caller)
71 static void inc_caller(void *caller)
73 struct gtod_log *log = find_hash(caller);
78 log = malloc(sizeof(*log));
79 INIT_FLIST_HEAD(&log->list);
83 h = hash_ptr(caller, HASH_BITS);
84 flist_add_tail(&log->list, &hash[h]);
90 static void gtod_log_caller(void *caller)
96 static void fio_exit fio_dump_gtod(void)
98 unsigned long total_calls = 0;
101 for (i = 0; i < HASH_SIZE; i++) {
102 struct flist_head *entry;
103 struct gtod_log *log;
105 flist_for_each(entry, &hash[i]) {
106 log = flist_entry(entry, struct gtod_log, list);
108 printf("function %p, calls %lu\n", log->caller,
110 total_calls += log->calls;
114 printf("Total %lu gettimeofday\n", total_calls);
117 static void fio_init gtod_init(void)
121 for (i = 0; i < HASH_SIZE; i++)
122 INIT_FLIST_HEAD(&hash[i]);
127 #endif /* FIO_DEBUG_TIME */
129 #ifdef CONFIG_CLOCK_GETTIME
130 static int fill_clock_gettime(struct timespec *ts)
132 #ifdef CONFIG_CLOCK_MONOTONIC
133 return clock_gettime(CLOCK_MONOTONIC, ts);
135 return clock_gettime(CLOCK_REALTIME, ts);
140 static void __fio_gettime(struct timeval *tp)
142 switch (fio_clock_source) {
143 #ifdef CONFIG_GETTIMEOFDAY
145 gettimeofday(tp, NULL);
148 #ifdef CONFIG_CLOCK_GETTIME
152 if (fill_clock_gettime(&ts) < 0) {
153 log_err("fio: clock_gettime fails\n");
157 tp->tv_sec = ts.tv_sec;
158 tp->tv_usec = ts.tv_nsec / 1000;
162 #ifdef ARCH_HAVE_CPU_CLOCK
167 #ifdef CONFIG_TLS_THREAD
168 tv = &static_tv_valid;
170 tv = pthread_getspecific(tv_tls_key);
174 if (t < cycles_start && !cycles_wrap)
176 else if (cycles_wrap && t >= cycles_start && !tv->warned) {
177 log_err("fio: double CPU clock wrap\n");
183 tv->last_tv_valid = 1;
184 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
185 usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
187 if (t < max_cycles_for_mult)
188 usecs = (t * inv_cycles_per_usec) / 16777216UL;
190 usecs = t / cycles_per_usec;
192 tp->tv_sec = usecs / 1000000;
193 tp->tv_usec = usecs % 1000000;
198 log_err("fio: invalid clock source %d\n", fio_clock_source);
203 #ifdef FIO_DEBUG_TIME
204 void fio_gettime(struct timeval *tp, void *caller)
206 void fio_gettime(struct timeval *tp, void fio_unused *caller)
209 #ifdef FIO_DEBUG_TIME
211 caller = __builtin_return_address(0);
213 gtod_log_caller(caller);
215 if (fio_unlikely(fio_gettime_offload(tp)))
221 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
222 static unsigned long get_cycles_per_usec(void)
226 enum fio_cs old_cs = fio_clock_source;
228 #ifdef CONFIG_CLOCK_GETTIME
229 fio_clock_source = CS_CGETTIME;
231 fio_clock_source = CS_GTOD;
235 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 + 127) >> 7;
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];
260 cycles[0] = get_cycles_per_usec();
261 S = delta = mean = 0.0;
262 for (i = 0; i < NR_TIME_ITERS; i++) {
263 cycles[i] = get_cycles_per_usec();
264 delta = cycles[i] - mean;
266 mean += delta / (i + 1.0);
267 S += delta * (cycles[i] - mean);
272 * The most common platform clock breakage is returning zero
273 * indefinitely. Check for that and return failure.
275 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
278 S = sqrt(S / (NR_TIME_ITERS - 1.0));
281 maxc = samples = avg = 0;
282 for (i = 0; i < NR_TIME_ITERS; i++) {
283 double this = cycles[i];
285 minc = min(cycles[i], minc);
286 maxc = max(cycles[i], maxc);
288 if ((fmax(this, mean) - fmin(this, mean)) > S)
294 S /= (double) NR_TIME_ITERS;
297 for (i = 0; i < NR_TIME_ITERS; i++)
298 dprint(FD_TIME, "cycles[%d]=%llu\n", i,
299 (unsigned long long) cycles[i] / 10);
302 avg = (avg + 5) / 10;
305 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
306 dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f\n",
307 (unsigned long long) minc,
308 (unsigned long long) maxc, mean, S);
310 cycles_per_usec = avg;
311 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
312 max_cycles_for_mult = ~0ULL / inv_cycles_per_usec;
313 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
314 cycles_start = get_cpu_clock();
315 dprint(FD_TIME, "cycles_start=%llu\n", cycles_start);
319 static int calibrate_cpu_clock(void)
321 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
327 #endif // ARCH_HAVE_CPU_CLOCK
329 #ifndef CONFIG_TLS_THREAD
330 void fio_local_clock_init(int is_thread)
334 t = calloc(1, sizeof(*t));
335 if (pthread_setspecific(tv_tls_key, t)) {
336 log_err("fio: can't set TLS key\n");
341 static void kill_tv_tls_key(void *data)
346 void fio_local_clock_init(int is_thread)
351 void fio_clock_init(void)
353 if (fio_clock_source == fio_clock_source_inited)
356 #ifndef CONFIG_TLS_THREAD
357 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
358 log_err("fio: can't create TLS key\n");
361 fio_clock_source_inited = fio_clock_source;
363 if (calibrate_cpu_clock())
367 * If the arch sets tsc_reliable != 0, then it must be good enough
368 * to use as THE clock source. For x86 CPUs, this means the TSC
369 * runs at a constant rate and is synced across CPU cores.
372 if (!fio_clock_source_set && !fio_monotonic_clocktest(0))
373 fio_clock_source = CS_CPUCLOCK;
374 } else if (fio_clock_source == CS_CPUCLOCK)
375 log_info("fio: clocksource=cpu may not be reliable\n");
378 uint64_t utime_since(const struct timeval *s, const struct timeval *e)
383 sec = e->tv_sec - s->tv_sec;
384 usec = e->tv_usec - s->tv_usec;
385 if (sec > 0 && usec < 0) {
391 * time warp bug on some kernels?
393 if (sec < 0 || (sec == 0 && usec < 0))
396 ret = sec * 1000000ULL + usec;
401 uint64_t utime_since_now(const struct timeval *s)
405 fio_gettime(&t, NULL);
406 return utime_since(s, &t);
409 uint64_t mtime_since(const struct timeval *s, const struct timeval *e)
413 sec = e->tv_sec - s->tv_sec;
414 usec = e->tv_usec - s->tv_usec;
415 if (sec > 0 && usec < 0) {
420 if (sec < 0 || (sec == 0 && usec < 0))
430 uint64_t mtime_since_now(const struct timeval *s)
433 void *p = __builtin_return_address(0);
436 return mtime_since(s, &t);
439 uint64_t time_since_now(const struct timeval *s)
441 return mtime_since_now(s) / 1000;
444 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
447 #define CLOCK_ENTRIES_DEBUG 100000
448 #define CLOCK_ENTRIES_TEST 10000
456 struct clock_thread {
460 pthread_mutex_t lock;
461 pthread_mutex_t started;
462 unsigned long nr_entries;
464 struct clock_entry *entries;
467 static inline uint32_t atomic32_inc_return(uint32_t *seq)
469 return 1 + __sync_fetch_and_add(seq, 1);
472 static void *clock_thread_fn(void *data)
474 struct clock_thread *t = data;
475 struct clock_entry *c;
476 os_cpu_mask_t cpu_mask;
480 memset(&cpu_mask, 0, sizeof(cpu_mask));
481 fio_cpu_set(&cpu_mask, t->cpu);
483 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
484 log_err("clock setaffinity failed\n");
488 pthread_mutex_lock(&t->lock);
489 pthread_mutex_unlock(&t->started);
493 for (i = 0; i < t->nr_entries; i++, c++) {
499 seq = atomic32_inc_return(t->seq);
502 tsc = get_cpu_clock();
503 } while (seq != *t->seq);
510 unsigned long long clocks;
512 clocks = t->entries[i - 1].tsc - t->entries[0].tsc;
513 log_info("cs: cpu%3d: %llu clocks seen\n", t->cpu, clocks);
517 * The most common platform clock breakage is returning zero
518 * indefinitely. Check for that and return failure.
520 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
526 static int clock_cmp(const void *p1, const void *p2)
528 const struct clock_entry *c1 = p1;
529 const struct clock_entry *c2 = p2;
531 if (c1->seq == c2->seq)
532 log_err("cs: bug in atomic sequence!\n");
534 return c1->seq - c2->seq;
537 int fio_monotonic_clocktest(int debug)
539 struct clock_thread *cthreads;
540 unsigned int nr_cpus = cpus_online();
541 struct clock_entry *entries;
542 unsigned long nr_entries, tentries, failed = 0;
543 struct clock_entry *prev, *this;
548 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
551 fio_debug |= 1U << FD_TIME;
553 nr_entries = CLOCK_ENTRIES_DEBUG;
555 nr_entries = CLOCK_ENTRIES_TEST;
557 calibrate_cpu_clock();
561 fio_debug &= ~(1U << FD_TIME);
565 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
566 tentries = nr_entries * nr_cpus;
567 entries = malloc(tentries * sizeof(struct clock_entry));
570 log_info("cs: Testing %u CPUs\n", nr_cpus);
572 for (i = 0; i < nr_cpus; i++) {
573 struct clock_thread *t = &cthreads[i];
578 t->nr_entries = nr_entries;
579 t->entries = &entries[i * nr_entries];
580 pthread_mutex_init(&t->lock, NULL);
581 pthread_mutex_init(&t->started, NULL);
582 pthread_mutex_lock(&t->lock);
583 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
590 for (i = 0; i < nr_cpus; i++) {
591 struct clock_thread *t = &cthreads[i];
593 pthread_mutex_lock(&t->started);
596 for (i = 0; i < nr_cpus; i++) {
597 struct clock_thread *t = &cthreads[i];
599 pthread_mutex_unlock(&t->lock);
602 for (i = 0; i < nr_cpus; i++) {
603 struct clock_thread *t = &cthreads[i];
606 pthread_join(t->thread, &ret);
614 log_err("Clocksource test: %lu threads failed\n", failed);
618 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
620 for (failed = i = 0; i < tentries; i++) {
628 if (prev->tsc > this->tsc) {
629 uint64_t diff = prev->tsc - this->tsc;
636 log_info("cs: CPU clock mismatch (diff=%llu):\n",
637 (unsigned long long) diff);
638 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
639 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
648 log_info("cs: Failed: %lu\n", failed);
650 log_info("cs: Pass!\n");
657 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
659 int fio_monotonic_clocktest(int debug)
662 log_info("cs: current platform does not support CPU clocks\n");