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;
28 #ifdef CONFIG_TLS_THREAD
29 static __thread struct tv_valid static_tv_valid;
31 static pthread_key_t tv_tls_key;
34 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
35 int fio_clock_source_set = 0;
36 static enum fio_cs fio_clock_source_inited = CS_INVAL;
41 #define HASH_SIZE (1 << HASH_BITS)
43 static struct flist_head hash[HASH_SIZE];
44 static int gtod_inited;
47 struct flist_head list;
52 static struct gtod_log *find_hash(void *caller)
54 unsigned long h = hash_ptr(caller, HASH_BITS);
55 struct flist_head *entry;
57 flist_for_each(entry, &hash[h]) {
58 struct gtod_log *log = flist_entry(entry, struct gtod_log,
61 if (log->caller == caller)
68 static struct gtod_log *find_log(void *caller)
70 struct gtod_log *log = find_hash(caller);
75 log = malloc(sizeof(*log));
76 INIT_FLIST_HEAD(&log->list);
80 h = hash_ptr(caller, HASH_BITS);
81 flist_add_tail(&log->list, &hash[h]);
87 static void gtod_log_caller(void *caller)
90 struct gtod_log *log = find_log(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)
144 #ifdef CONFIG_TLS_THREAD
145 tv = &static_tv_valid;
147 tv = pthread_getspecific(tv_tls_key);
150 switch (fio_clock_source) {
151 #ifdef CONFIG_GETTIMEOFDAY
153 gettimeofday(tp, NULL);
156 #ifdef CONFIG_CLOCK_GETTIME
160 if (fill_clock_gettime(&ts) < 0) {
161 log_err("fio: clock_gettime fails\n");
165 tp->tv_sec = ts.tv_sec;
166 tp->tv_usec = ts.tv_nsec / 1000;
170 #ifdef ARCH_HAVE_CPU_CLOCK
175 if (t < tv->last_cycles && tv->last_tv_valid &&
177 log_err("fio: CPU clock going back in time\n");
182 tv->last_tv_valid = 1;
183 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
184 usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
186 if (t < max_cycles_for_mult)
187 usecs = (t * inv_cycles_per_usec) / 16777216UL;
189 usecs = t / cycles_per_usec;
191 tp->tv_sec = usecs / 1000000;
192 tp->tv_usec = usecs % 1000000;
197 log_err("fio: invalid clock source %d\n", fio_clock_source);
202 #ifdef FIO_DEBUG_TIME
203 void fio_gettime(struct timeval *tp, void *caller)
205 void fio_gettime(struct timeval *tp, void fio_unused *caller)
208 #ifdef FIO_DEBUG_TIME
210 caller = __builtin_return_address(0);
212 gtod_log_caller(caller);
214 if (fio_unlikely(fio_gettime_offload(tp)))
220 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
221 static unsigned long get_cycles_per_usec(void)
225 enum fio_cs old_cs = fio_clock_source;
227 #ifdef CONFIG_CLOCK_GETTIME
228 fio_clock_source = CS_CGETTIME;
230 fio_clock_source = CS_GTOD;
234 c_s = get_cpu_clock();
240 elapsed = utime_since(&s, &e);
241 if (elapsed >= 1280) {
242 c_e = get_cpu_clock();
247 fio_clock_source = old_cs;
248 return (c_e - c_s + 127) >> 7;
251 #define NR_TIME_ITERS 50
253 static int calibrate_cpu_clock(void)
255 double delta, mean, S;
256 uint64_t avg, cycles[NR_TIME_ITERS];
259 cycles[0] = get_cycles_per_usec();
260 S = delta = mean = 0.0;
261 for (i = 0; i < NR_TIME_ITERS; i++) {
262 cycles[i] = get_cycles_per_usec();
263 delta = cycles[i] - mean;
265 mean += delta / (i + 1.0);
266 S += delta * (cycles[i] - mean);
271 * The most common platform clock breakage is returning zero
272 * indefinitely. Check for that and return failure.
274 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
277 S = sqrt(S / (NR_TIME_ITERS - 1.0));
280 for (i = 0; i < NR_TIME_ITERS; i++) {
281 double this = cycles[i];
283 if ((fmax(this, mean) - fmin(this, mean)) > S)
289 S /= (double) NR_TIME_ITERS;
292 for (i = 0; i < NR_TIME_ITERS; i++)
293 dprint(FD_TIME, "cycles[%d]=%llu\n", i,
294 (unsigned long long) cycles[i] / 10);
297 avg = (avg + 5) / 10;
298 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
299 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
301 cycles_per_usec = avg;
302 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
303 max_cycles_for_mult = ~0ULL / inv_cycles_per_usec;
304 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
308 static int calibrate_cpu_clock(void)
310 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
316 #endif // ARCH_HAVE_CPU_CLOCK
318 #ifndef CONFIG_TLS_THREAD
319 void fio_local_clock_init(int is_thread)
323 t = calloc(1, sizeof(*t));
324 if (pthread_setspecific(tv_tls_key, t)) {
325 log_err("fio: can't set TLS key\n");
330 static void kill_tv_tls_key(void *data)
335 void fio_local_clock_init(int is_thread)
340 void fio_clock_init(void)
342 if (fio_clock_source == fio_clock_source_inited)
345 #ifndef CONFIG_TLS_THREAD
346 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
347 log_err("fio: can't create TLS key\n");
350 fio_clock_source_inited = fio_clock_source;
352 if (calibrate_cpu_clock())
356 * If the arch sets tsc_reliable != 0, then it must be good enough
357 * to use as THE clock source. For x86 CPUs, this means the TSC
358 * runs at a constant rate and is synced across CPU cores.
361 if (!fio_clock_source_set)
362 fio_clock_source = CS_CPUCLOCK;
363 } else if (fio_clock_source == CS_CPUCLOCK)
364 log_info("fio: clocksource=cpu may not be reliable\n");
367 uint64_t utime_since(const struct timeval *s, const struct timeval *e)
372 sec = e->tv_sec - s->tv_sec;
373 usec = e->tv_usec - s->tv_usec;
374 if (sec > 0 && usec < 0) {
380 * time warp bug on some kernels?
382 if (sec < 0 || (sec == 0 && usec < 0))
385 ret = sec * 1000000ULL + usec;
390 uint64_t utime_since_now(const struct timeval *s)
394 fio_gettime(&t, NULL);
395 return utime_since(s, &t);
398 uint64_t mtime_since(const struct timeval *s, const struct timeval *e)
402 sec = e->tv_sec - s->tv_sec;
403 usec = e->tv_usec - s->tv_usec;
404 if (sec > 0 && usec < 0) {
409 if (sec < 0 || (sec == 0 && usec < 0))
419 uint64_t mtime_since_now(const struct timeval *s)
422 void *p = __builtin_return_address(0);
425 return mtime_since(s, &t);
428 uint64_t time_since_now(const struct timeval *s)
430 return mtime_since_now(s) / 1000;
433 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
436 #define CLOCK_ENTRIES 100000
444 struct clock_thread {
447 pthread_mutex_t lock;
448 pthread_mutex_t started;
450 struct clock_entry *entries;
453 static inline uint32_t atomic32_inc_return(uint32_t *seq)
455 return 1 + __sync_fetch_and_add(seq, 1);
458 static void *clock_thread_fn(void *data)
460 struct clock_thread *t = data;
461 struct clock_entry *c;
462 os_cpu_mask_t cpu_mask;
466 memset(&cpu_mask, 0, sizeof(cpu_mask));
467 fio_cpu_set(&cpu_mask, t->cpu);
469 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
470 log_err("clock setaffinity failed\n");
474 pthread_mutex_lock(&t->lock);
475 pthread_mutex_unlock(&t->started);
479 for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
485 seq = atomic32_inc_return(t->seq);
488 tsc = get_cpu_clock();
489 } while (seq != *t->seq);
495 log_info("cs: cpu%3d: %llu clocks seen\n", t->cpu,
496 (unsigned long long) t->entries[i - 1].tsc - t->entries[0].tsc);
499 * The most common platform clock breakage is returning zero
500 * indefinitely. Check for that and return failure.
502 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
508 static int clock_cmp(const void *p1, const void *p2)
510 const struct clock_entry *c1 = p1;
511 const struct clock_entry *c2 = p2;
513 if (c1->seq == c2->seq)
514 log_err("cs: bug in atomic sequence!\n");
516 return c1->seq - c2->seq;
519 int fio_monotonic_clocktest(void)
521 struct clock_thread *cthreads;
522 unsigned int nr_cpus = cpus_online();
523 struct clock_entry *entries;
524 unsigned long tentries, failed = 0;
525 struct clock_entry *prev, *this;
529 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
532 fio_debug |= 1U << FD_TIME;
534 calibrate_cpu_clock();
536 fio_debug &= ~(1U << FD_TIME);
539 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
540 tentries = CLOCK_ENTRIES * nr_cpus;
541 entries = malloc(tentries * sizeof(struct clock_entry));
543 log_info("cs: Testing %u CPUs\n", nr_cpus);
545 for (i = 0; i < nr_cpus; i++) {
546 struct clock_thread *t = &cthreads[i];
550 t->entries = &entries[i * CLOCK_ENTRIES];
551 pthread_mutex_init(&t->lock, NULL);
552 pthread_mutex_init(&t->started, NULL);
553 pthread_mutex_lock(&t->lock);
554 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
561 for (i = 0; i < nr_cpus; i++) {
562 struct clock_thread *t = &cthreads[i];
564 pthread_mutex_lock(&t->started);
567 for (i = 0; i < nr_cpus; i++) {
568 struct clock_thread *t = &cthreads[i];
570 pthread_mutex_unlock(&t->lock);
573 for (i = 0; i < nr_cpus; i++) {
574 struct clock_thread *t = &cthreads[i];
577 pthread_join(t->thread, &ret);
584 log_err("Clocksource test: %lu threads failed\n", failed);
588 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
590 for (failed = i = 0; i < tentries; i++) {
598 if (prev->tsc > this->tsc) {
599 uint64_t diff = prev->tsc - this->tsc;
601 log_info("cs: CPU clock mismatch (diff=%llu):\n",
602 (unsigned long long) diff);
603 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
604 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
612 log_info("cs: Failed: %lu\n", failed);
614 log_info("cs: Pass!\n");
621 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
623 int fio_monotonic_clocktest(void)
625 log_info("cs: current platform does not support CPU clocks\n");