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;
21 #ifdef ARCH_CPU_CLOCK_WRAPS
22 static unsigned long long cycles_start, cycles_wrap;
31 #ifdef ARCH_HAVE_CPU_CLOCK
32 #ifdef CONFIG_TLS_THREAD
33 static __thread struct tv_valid static_tv_valid;
35 static pthread_key_t tv_tls_key;
39 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
40 int fio_clock_source_set = 0;
41 static enum fio_cs fio_clock_source_inited = CS_INVAL;
46 #define HASH_SIZE (1 << HASH_BITS)
48 static struct flist_head hash[HASH_SIZE];
49 static int gtod_inited;
52 struct flist_head list;
57 static struct gtod_log *find_hash(void *caller)
59 unsigned long h = hash_ptr(caller, HASH_BITS);
60 struct flist_head *entry;
62 flist_for_each(entry, &hash[h]) {
63 struct gtod_log *log = flist_entry(entry, struct gtod_log,
66 if (log->caller == caller)
73 static void inc_caller(void *caller)
75 struct gtod_log *log = find_hash(caller);
80 log = malloc(sizeof(*log));
81 INIT_FLIST_HEAD(&log->list);
85 h = hash_ptr(caller, HASH_BITS);
86 flist_add_tail(&log->list, &hash[h]);
92 static void gtod_log_caller(void *caller)
98 static void fio_exit fio_dump_gtod(void)
100 unsigned long total_calls = 0;
103 for (i = 0; i < HASH_SIZE; i++) {
104 struct flist_head *entry;
105 struct gtod_log *log;
107 flist_for_each(entry, &hash[i]) {
108 log = flist_entry(entry, struct gtod_log, list);
110 printf("function %p, calls %lu\n", log->caller,
112 total_calls += log->calls;
116 printf("Total %lu gettimeofday\n", total_calls);
119 static void fio_init gtod_init(void)
123 for (i = 0; i < HASH_SIZE; i++)
124 INIT_FLIST_HEAD(&hash[i]);
129 #endif /* FIO_DEBUG_TIME */
131 #ifdef CONFIG_CLOCK_GETTIME
132 static int fill_clock_gettime(struct timespec *ts)
134 #ifdef CONFIG_CLOCK_MONOTONIC
135 return clock_gettime(CLOCK_MONOTONIC, ts);
137 return clock_gettime(CLOCK_REALTIME, ts);
142 static void __fio_gettime(struct timeval *tp)
144 switch (fio_clock_source) {
145 #ifdef CONFIG_GETTIMEOFDAY
147 gettimeofday(tp, NULL);
150 #ifdef CONFIG_CLOCK_GETTIME
154 if (fill_clock_gettime(&ts) < 0) {
155 log_err("fio: clock_gettime fails\n");
159 tp->tv_sec = ts.tv_sec;
160 tp->tv_usec = ts.tv_nsec / 1000;
164 #ifdef ARCH_HAVE_CPU_CLOCK
169 #ifdef CONFIG_TLS_THREAD
170 tv = &static_tv_valid;
172 tv = pthread_getspecific(tv_tls_key);
176 #ifdef ARCH_CPU_CLOCK_WRAPS
177 if (t < cycles_start && !cycles_wrap)
179 else if (cycles_wrap && t >= cycles_start && !tv->warned) {
180 log_err("fio: double CPU clock wrap\n");
187 tv->last_tv_valid = 1;
188 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
189 usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
191 if (t < max_cycles_for_mult)
192 usecs = (t * inv_cycles_per_usec) / 16777216UL;
194 usecs = t / cycles_per_usec;
196 tp->tv_sec = usecs / 1000000;
197 tp->tv_usec = usecs % 1000000;
202 log_err("fio: invalid clock source %d\n", fio_clock_source);
207 #ifdef FIO_DEBUG_TIME
208 void fio_gettime(struct timeval *tp, void *caller)
210 void fio_gettime(struct timeval *tp, void fio_unused *caller)
213 #ifdef FIO_DEBUG_TIME
215 caller = __builtin_return_address(0);
217 gtod_log_caller(caller);
219 if (fio_unlikely(fio_gettime_offload(tp)))
225 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
226 static unsigned long get_cycles_per_usec(void)
230 enum fio_cs old_cs = fio_clock_source;
232 #ifdef CONFIG_CLOCK_GETTIME
233 fio_clock_source = CS_CGETTIME;
235 fio_clock_source = CS_GTOD;
239 c_s = get_cpu_clock();
245 elapsed = utime_since(&s, &e);
246 if (elapsed >= 1280) {
247 c_e = get_cpu_clock();
252 fio_clock_source = old_cs;
253 return (c_e - c_s + 127) >> 7;
256 #define NR_TIME_ITERS 50
258 static int calibrate_cpu_clock(void)
260 double delta, mean, S;
261 uint64_t minc, maxc, avg, cycles[NR_TIME_ITERS];
264 cycles[0] = get_cycles_per_usec();
265 S = delta = mean = 0.0;
266 for (i = 0; i < NR_TIME_ITERS; i++) {
267 cycles[i] = get_cycles_per_usec();
268 delta = cycles[i] - mean;
270 mean += delta / (i + 1.0);
271 S += delta * (cycles[i] - mean);
276 * The most common platform clock breakage is returning zero
277 * indefinitely. Check for that and return failure.
279 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
282 S = sqrt(S / (NR_TIME_ITERS - 1.0));
285 maxc = samples = avg = 0;
286 for (i = 0; i < NR_TIME_ITERS; i++) {
287 double this = cycles[i];
289 minc = min(cycles[i], minc);
290 maxc = max(cycles[i], maxc);
292 if ((fmax(this, mean) - fmin(this, mean)) > S)
298 S /= (double) NR_TIME_ITERS;
301 for (i = 0; i < NR_TIME_ITERS; i++)
302 dprint(FD_TIME, "cycles[%d]=%llu\n", i,
303 (unsigned long long) cycles[i] / 10);
306 avg = (avg + 5) / 10;
309 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
310 dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f\n",
311 (unsigned long long) minc,
312 (unsigned long long) maxc, mean, S);
314 cycles_per_usec = avg;
315 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
316 max_cycles_for_mult = ~0ULL / inv_cycles_per_usec;
317 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
318 #ifdef ARCH_CPU_CLOCK_WRAPS
319 cycles_start = get_cpu_clock();
320 dprint(FD_TIME, "cycles_start=%llu\n", cycles_start);
325 static int calibrate_cpu_clock(void)
327 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
333 #endif // ARCH_HAVE_CPU_CLOCK
335 #ifndef CONFIG_TLS_THREAD
336 void fio_local_clock_init(int is_thread)
340 t = calloc(1, sizeof(*t));
341 if (pthread_setspecific(tv_tls_key, t)) {
342 log_err("fio: can't set TLS key\n");
347 static void kill_tv_tls_key(void *data)
352 void fio_local_clock_init(int is_thread)
357 void fio_clock_init(void)
359 if (fio_clock_source == fio_clock_source_inited)
362 #ifndef CONFIG_TLS_THREAD
363 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
364 log_err("fio: can't create TLS key\n");
367 fio_clock_source_inited = fio_clock_source;
369 if (calibrate_cpu_clock())
373 * If the arch sets tsc_reliable != 0, then it must be good enough
374 * to use as THE clock source. For x86 CPUs, this means the TSC
375 * runs at a constant rate and is synced across CPU cores.
378 if (!fio_clock_source_set && !fio_monotonic_clocktest(0))
379 fio_clock_source = CS_CPUCLOCK;
380 } else if (fio_clock_source == CS_CPUCLOCK)
381 log_info("fio: clocksource=cpu may not be reliable\n");
384 uint64_t utime_since(const struct timeval *s, const struct timeval *e)
389 sec = e->tv_sec - s->tv_sec;
390 usec = e->tv_usec - s->tv_usec;
391 if (sec > 0 && usec < 0) {
397 * time warp bug on some kernels?
399 if (sec < 0 || (sec == 0 && usec < 0))
402 ret = sec * 1000000ULL + usec;
407 uint64_t utime_since_now(const struct timeval *s)
411 fio_gettime(&t, NULL);
412 return utime_since(s, &t);
415 uint64_t mtime_since(const struct timeval *s, const struct timeval *e)
419 sec = e->tv_sec - s->tv_sec;
420 usec = e->tv_usec - s->tv_usec;
421 if (sec > 0 && usec < 0) {
426 if (sec < 0 || (sec == 0 && usec < 0))
436 uint64_t mtime_since_now(const struct timeval *s)
439 void *p = __builtin_return_address(0);
442 return mtime_since(s, &t);
445 uint64_t time_since_now(const struct timeval *s)
447 return mtime_since_now(s) / 1000;
450 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
453 #define CLOCK_ENTRIES_DEBUG 100000
454 #define CLOCK_ENTRIES_TEST 10000
462 struct clock_thread {
466 pthread_mutex_t lock;
467 pthread_mutex_t started;
468 unsigned long nr_entries;
470 struct clock_entry *entries;
473 static inline uint32_t atomic32_inc_return(uint32_t *seq)
475 return 1 + __sync_fetch_and_add(seq, 1);
478 static void *clock_thread_fn(void *data)
480 struct clock_thread *t = data;
481 struct clock_entry *c;
482 os_cpu_mask_t cpu_mask;
486 if (fio_cpuset_init(&cpu_mask)) {
489 log_err("clock cpuset init failed: %s\n", strerror(__err));
493 fio_cpu_set(&cpu_mask, t->cpu);
495 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
498 log_err("clock setaffinity failed: %s\n", strerror(__err));
502 pthread_mutex_lock(&t->lock);
503 pthread_mutex_unlock(&t->started);
507 for (i = 0; i < t->nr_entries; i++, c++) {
513 seq = atomic32_inc_return(t->seq);
516 tsc = get_cpu_clock();
517 } while (seq != *t->seq);
524 unsigned long long clocks;
526 clocks = t->entries[i - 1].tsc - t->entries[0].tsc;
527 log_info("cs: cpu%3d: %llu clocks seen\n", t->cpu, clocks);
531 * The most common platform clock breakage is returning zero
532 * indefinitely. Check for that and return failure.
534 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
537 fio_cpuset_exit(&cpu_mask);
540 fio_cpuset_exit(&cpu_mask);
545 static int clock_cmp(const void *p1, const void *p2)
547 const struct clock_entry *c1 = p1;
548 const struct clock_entry *c2 = p2;
550 if (c1->seq == c2->seq)
551 log_err("cs: bug in atomic sequence!\n");
553 return c1->seq - c2->seq;
556 int fio_monotonic_clocktest(int debug)
558 struct clock_thread *cthreads;
559 unsigned int nr_cpus = cpus_online();
560 struct clock_entry *entries;
561 unsigned long nr_entries, tentries, failed = 0;
562 struct clock_entry *prev, *this;
567 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
570 fio_debug |= 1U << FD_TIME;
572 nr_entries = CLOCK_ENTRIES_DEBUG;
574 nr_entries = CLOCK_ENTRIES_TEST;
576 calibrate_cpu_clock();
580 fio_debug &= ~(1U << FD_TIME);
584 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
585 tentries = nr_entries * nr_cpus;
586 entries = malloc(tentries * sizeof(struct clock_entry));
589 log_info("cs: Testing %u CPUs\n", nr_cpus);
591 for (i = 0; i < nr_cpus; i++) {
592 struct clock_thread *t = &cthreads[i];
597 t->nr_entries = nr_entries;
598 t->entries = &entries[i * nr_entries];
599 pthread_mutex_init(&t->lock, NULL);
600 pthread_mutex_init(&t->started, NULL);
601 pthread_mutex_lock(&t->lock);
602 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
609 for (i = 0; i < nr_cpus; i++) {
610 struct clock_thread *t = &cthreads[i];
612 pthread_mutex_lock(&t->started);
615 for (i = 0; i < nr_cpus; i++) {
616 struct clock_thread *t = &cthreads[i];
618 pthread_mutex_unlock(&t->lock);
621 for (i = 0; i < nr_cpus; i++) {
622 struct clock_thread *t = &cthreads[i];
625 pthread_join(t->thread, &ret);
633 log_err("Clocksource test: %lu threads failed\n", failed);
637 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
639 for (failed = i = 0; i < tentries; i++) {
647 if (prev->tsc > this->tsc) {
648 uint64_t diff = prev->tsc - this->tsc;
655 log_info("cs: CPU clock mismatch (diff=%llu):\n",
656 (unsigned long long) diff);
657 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
658 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
667 log_info("cs: Failed: %lu\n", failed);
669 log_info("cs: Pass!\n");
676 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
678 int fio_monotonic_clocktest(int debug)
681 log_info("cs: current platform does not support CPU clocks\n");