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_nsec;
20 static uint64_t max_cycles_for_mult;
21 #define NSEC_INV_FACTOR 4096
23 #ifdef ARCH_CPU_CLOCK_WRAPS
24 static unsigned long long cycles_start, cycles_wrap;
34 #ifdef ARCH_HAVE_CPU_CLOCK
35 #ifdef CONFIG_TLS_THREAD
36 static __thread struct tv_valid static_tv_valid;
38 static pthread_key_t tv_tls_key;
42 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
43 int fio_clock_source_set = 0;
44 static enum fio_cs fio_clock_source_inited = CS_INVAL;
49 #define HASH_SIZE (1 << HASH_BITS)
51 static struct flist_head hash[HASH_SIZE];
52 static int gtod_inited;
55 struct flist_head list;
60 static struct gtod_log *find_hash(void *caller)
62 unsigned long h = hash_ptr(caller, HASH_BITS);
63 struct flist_head *entry;
65 flist_for_each(entry, &hash[h]) {
66 struct gtod_log *log = flist_entry(entry, struct gtod_log,
69 if (log->caller == caller)
76 static void inc_caller(void *caller)
78 struct gtod_log *log = find_hash(caller);
83 log = malloc(sizeof(*log));
84 INIT_FLIST_HEAD(&log->list);
88 h = hash_ptr(caller, HASH_BITS);
89 flist_add_tail(&log->list, &hash[h]);
95 static void gtod_log_caller(void *caller)
101 static void fio_exit fio_dump_gtod(void)
103 unsigned long total_calls = 0;
106 for (i = 0; i < HASH_SIZE; i++) {
107 struct flist_head *entry;
108 struct gtod_log *log;
110 flist_for_each(entry, &hash[i]) {
111 log = flist_entry(entry, struct gtod_log, list);
113 printf("function %p, calls %lu\n", log->caller,
115 total_calls += log->calls;
119 printf("Total %lu gettimeofday\n", total_calls);
122 static void fio_init gtod_init(void)
126 for (i = 0; i < HASH_SIZE; i++)
127 INIT_FLIST_HEAD(&hash[i]);
132 #endif /* FIO_DEBUG_TIME */
134 #ifdef CONFIG_CLOCK_GETTIME
135 static int fill_clock_gettime(struct timespec *ts)
137 #if defined(CONFIG_CLOCK_MONOTONIC_RAW)
138 return clock_gettime(CLOCK_MONOTONIC_RAW, ts);
139 #elif defined(CONFIG_CLOCK_MONOTONIC)
140 return clock_gettime(CLOCK_MONOTONIC, ts);
142 return clock_gettime(CLOCK_REALTIME, ts);
147 static void __fio_gettime(struct timespec *tp)
149 switch (fio_clock_source) {
150 #ifdef CONFIG_GETTIMEOFDAY
153 gettimeofday(&tv, NULL);
155 tp->tv_sec = tv.tv_sec;
156 tp->tv_nsec = tv.tv_usec * 1000;
160 #ifdef CONFIG_CLOCK_GETTIME
162 if (fill_clock_gettime(tp) < 0) {
163 log_err("fio: clock_gettime fails\n");
169 #ifdef ARCH_HAVE_CPU_CLOCK
174 #ifdef CONFIG_TLS_THREAD
175 tv = &static_tv_valid;
177 tv = pthread_getspecific(tv_tls_key);
181 #ifdef ARCH_CPU_CLOCK_WRAPS
182 if (t < cycles_start && !cycles_wrap)
184 else if (cycles_wrap && t >= cycles_start && !tv->warned) {
185 log_err("fio: double CPU clock wrap\n");
192 tv->last_tv_valid = 1;
193 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
194 nsecs = t * 1000 / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
196 if (t < max_cycles_for_mult)
197 nsecs = (t * inv_cycles_per_nsec) / NSEC_INV_FACTOR;
199 nsecs = (t / NSEC_INV_FACTOR) * inv_cycles_per_nsec;
201 tp->tv_sec = nsecs / 1000000000ULL;
202 tp->tv_nsec = nsecs % 1000000000ULL;
207 log_err("fio: invalid clock source %d\n", fio_clock_source);
212 #ifdef FIO_DEBUG_TIME
213 void fio_gettime(struct timespec *tp, void *caller)
215 void fio_gettime(struct timespec *tp, void fio_unused *caller)
218 #ifdef FIO_DEBUG_TIME
220 caller = __builtin_return_address(0);
222 gtod_log_caller(caller);
224 if (fio_unlikely(fio_gettime_offload(tp)))
230 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
231 static unsigned long get_cycles_per_usec(void)
233 struct timespec s, e;
235 enum fio_cs old_cs = fio_clock_source;
238 #ifdef CONFIG_CLOCK_GETTIME
239 fio_clock_source = CS_CGETTIME;
241 fio_clock_source = CS_GTOD;
245 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) / elapsed;
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;
304 for (i = 0; i < NR_TIME_ITERS; i++)
305 dprint(FD_TIME, "cycles[%d]=%llu\n", i, (unsigned long long) cycles[i]);
308 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
309 dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f\n",
310 (unsigned long long) minc,
311 (unsigned long long) maxc, mean, S);
313 cycles_per_usec = avg;
314 inv_cycles_per_nsec = NSEC_INV_FACTOR * 1000 / cycles_per_usec;
315 max_cycles_for_mult = ~0ULL / inv_cycles_per_nsec;
316 dprint(FD_TIME, "inv_cycles_per_nsec=%lu\n", inv_cycles_per_nsec);
317 #ifdef ARCH_CPU_CLOCK_WRAPS
318 cycles_start = get_cpu_clock();
319 dprint(FD_TIME, "cycles_start=%llu\n", cycles_start);
324 static int calibrate_cpu_clock(void)
326 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
332 #endif // ARCH_HAVE_CPU_CLOCK
334 #ifndef CONFIG_TLS_THREAD
335 void fio_local_clock_init(int is_thread)
339 t = calloc(1, sizeof(*t));
340 if (pthread_setspecific(tv_tls_key, t)) {
341 log_err("fio: can't set TLS key\n");
346 static void kill_tv_tls_key(void *data)
351 void fio_local_clock_init(int is_thread)
356 void fio_clock_init(void)
358 if (fio_clock_source == fio_clock_source_inited)
361 #ifndef CONFIG_TLS_THREAD
362 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
363 log_err("fio: can't create TLS key\n");
366 fio_clock_source_inited = fio_clock_source;
368 if (calibrate_cpu_clock())
372 * If the arch sets tsc_reliable != 0, then it must be good enough
373 * to use as THE clock source. For x86 CPUs, this means the TSC
374 * runs at a constant rate and is synced across CPU cores.
377 if (!fio_clock_source_set && !fio_monotonic_clocktest(0))
378 fio_clock_source = CS_CPUCLOCK;
379 } else if (fio_clock_source == CS_CPUCLOCK)
380 log_info("fio: clocksource=cpu may not be reliable\n");
383 uint64_t utime_since(const struct timespec *s, const struct timespec *e)
387 sec = e->tv_sec - s->tv_sec;
388 usec = (e->tv_nsec - s->tv_nsec) / 1000;
389 if (sec > 0 && usec < 0) {
395 * time warp bug on some kernels?
397 if (sec < 0 || (sec == 0 && usec < 0))
400 return usec + (sec * 1000000);
403 uint64_t utime_since_now(const struct timespec *s)
406 #ifdef FIO_DEBUG_TIME
407 void *p = __builtin_return_address(0);
411 fio_gettime(&t, NULL);
414 return utime_since(s, &t);
417 uint64_t mtime_since_tv(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))
436 uint64_t mtime_since_now(const struct timespec *s)
439 #ifdef FIO_DEBUG_TIME
440 void *p = __builtin_return_address(0);
444 fio_gettime(&t, NULL);
447 return mtime_since(s, &t);
450 uint64_t mtime_since(const struct timespec *s, const struct timespec *e)
454 sec = e->tv_sec - s->tv_sec;
455 usec = (e->tv_nsec - s->tv_nsec) / 1000;
456 if (sec > 0 && usec < 0) {
461 if (sec < 0 || (sec == 0 && usec < 0))
469 uint64_t time_since_now(const struct timespec *s)
471 return mtime_since_now(s) / 1000;
474 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
477 #define CLOCK_ENTRIES_DEBUG 100000
478 #define CLOCK_ENTRIES_TEST 10000
486 struct clock_thread {
490 pthread_mutex_t lock;
491 pthread_mutex_t started;
492 unsigned long nr_entries;
494 struct clock_entry *entries;
497 static inline uint32_t atomic32_inc_return(uint32_t *seq)
499 return 1 + __sync_fetch_and_add(seq, 1);
502 static void *clock_thread_fn(void *data)
504 struct clock_thread *t = data;
505 struct clock_entry *c;
506 os_cpu_mask_t cpu_mask;
508 unsigned long long first;
511 if (fio_cpuset_init(&cpu_mask)) {
514 log_err("clock cpuset init failed: %s\n", strerror(__err));
518 fio_cpu_set(&cpu_mask, t->cpu);
520 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
523 log_err("clock setaffinity failed: %s\n", strerror(__err));
527 pthread_mutex_lock(&t->lock);
528 pthread_mutex_unlock(&t->started);
530 first = get_cpu_clock();
533 for (i = 0; i < t->nr_entries; i++, c++) {
539 seq = atomic32_inc_return(t->seq);
542 tsc = get_cpu_clock();
543 } while (seq != *t->seq);
550 unsigned long long clocks;
552 clocks = t->entries[i - 1].tsc - t->entries[0].tsc;
553 log_info("cs: cpu%3d: %llu clocks seen, first %llu\n", t->cpu,
558 * The most common platform clock breakage is returning zero
559 * indefinitely. Check for that and return failure.
561 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
564 fio_cpuset_exit(&cpu_mask);
567 fio_cpuset_exit(&cpu_mask);
572 static int clock_cmp(const void *p1, const void *p2)
574 const struct clock_entry *c1 = p1;
575 const struct clock_entry *c2 = p2;
577 if (c1->seq == c2->seq)
578 log_err("cs: bug in atomic sequence!\n");
580 return c1->seq - c2->seq;
583 int fio_monotonic_clocktest(int debug)
585 struct clock_thread *cthreads;
586 unsigned int nr_cpus = cpus_online();
587 struct clock_entry *entries;
588 unsigned long nr_entries, tentries, failed = 0;
589 struct clock_entry *prev, *this;
594 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
597 fio_debug |= 1U << FD_TIME;
599 nr_entries = CLOCK_ENTRIES_DEBUG;
601 nr_entries = CLOCK_ENTRIES_TEST;
603 calibrate_cpu_clock();
607 fio_debug &= ~(1U << FD_TIME);
611 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
612 tentries = nr_entries * nr_cpus;
613 entries = malloc(tentries * sizeof(struct clock_entry));
616 log_info("cs: Testing %u CPUs\n", nr_cpus);
618 for (i = 0; i < nr_cpus; i++) {
619 struct clock_thread *t = &cthreads[i];
624 t->nr_entries = nr_entries;
625 t->entries = &entries[i * nr_entries];
626 pthread_mutex_init(&t->lock, NULL);
627 pthread_mutex_init(&t->started, NULL);
628 pthread_mutex_lock(&t->lock);
629 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
636 for (i = 0; i < nr_cpus; i++) {
637 struct clock_thread *t = &cthreads[i];
639 pthread_mutex_lock(&t->started);
642 for (i = 0; i < nr_cpus; i++) {
643 struct clock_thread *t = &cthreads[i];
645 pthread_mutex_unlock(&t->lock);
648 for (i = 0; i < nr_cpus; i++) {
649 struct clock_thread *t = &cthreads[i];
652 pthread_join(t->thread, &ret);
660 log_err("Clocksource test: %lu threads failed\n", failed);
664 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
666 /* silence silly gcc */
668 for (failed = i = 0; i < tentries; i++) {
676 if (prev->tsc > this->tsc) {
677 uint64_t diff = prev->tsc - this->tsc;
684 log_info("cs: CPU clock mismatch (diff=%llu):\n",
685 (unsigned long long) diff);
686 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
687 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
696 log_info("cs: Failed: %lu\n", failed);
698 log_info("cs: Pass!\n");
705 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
707 int fio_monotonic_clocktest(int debug)
710 log_info("cs: current platform does not support CPU clocks\n");