16 #ifdef ARCH_HAVE_CPU_CLOCK
17 static unsigned long cycles_per_usec;
18 static unsigned long inv_cycles_per_usec;
23 struct timeval last_tv;
25 unsigned long last_cycles;
27 static pthread_key_t tv_tls_key;
29 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
30 int fio_clock_source_set = 0;
31 enum fio_cs fio_clock_source_inited = CS_INVAL;
36 #define HASH_SIZE (1 << HASH_BITS)
38 static struct flist_head hash[HASH_SIZE];
39 static int gtod_inited;
42 struct flist_head list;
47 static struct gtod_log *find_hash(void *caller)
49 unsigned long h = hash_ptr(caller, HASH_BITS);
50 struct flist_head *entry;
52 flist_for_each(entry, &hash[h]) {
53 struct gtod_log *log = flist_entry(entry, struct gtod_log,
56 if (log->caller == caller)
63 static struct gtod_log *find_log(void *caller)
65 struct gtod_log *log = find_hash(caller);
70 log = malloc(sizeof(*log));
71 INIT_FLIST_HEAD(&log->list);
75 h = hash_ptr(caller, HASH_BITS);
76 flist_add_tail(&log->list, &hash[h]);
82 static void gtod_log_caller(void *caller)
85 struct gtod_log *log = find_log(caller);
91 static void fio_exit fio_dump_gtod(void)
93 unsigned long total_calls = 0;
96 for (i = 0; i < HASH_SIZE; i++) {
97 struct flist_head *entry;
100 flist_for_each(entry, &hash[i]) {
101 log = flist_entry(entry, struct gtod_log, list);
103 printf("function %p, calls %lu\n", log->caller,
105 total_calls += log->calls;
109 printf("Total %lu gettimeofday\n", total_calls);
112 static void fio_init gtod_init(void)
116 for (i = 0; i < HASH_SIZE; i++)
117 INIT_FLIST_HEAD(&hash[i]);
122 #endif /* FIO_DEBUG_TIME */
124 static int fill_clock_gettime(struct timespec *ts)
126 #ifdef FIO_HAVE_CLOCK_MONOTONIC
127 return clock_gettime(CLOCK_MONOTONIC, ts);
129 return clock_gettime(CLOCK_REALTIME, ts);
133 #ifdef FIO_DEBUG_TIME
134 void fio_gettime(struct timeval *tp, void *caller)
136 void fio_gettime(struct timeval *tp, void fio_unused *caller)
141 #ifdef FIO_DEBUG_TIME
143 caller = __builtin_return_address(0);
145 gtod_log_caller(caller);
148 memcpy(tp, fio_tv, sizeof(*tp));
152 tv = pthread_getspecific(tv_tls_key);
154 switch (fio_clock_source) {
156 gettimeofday(tp, NULL);
161 if (fill_clock_gettime(&ts) < 0) {
162 log_err("fio: clock_gettime fails\n");
166 tp->tv_sec = ts.tv_sec;
167 tp->tv_usec = ts.tv_nsec / 1000;
170 #ifdef ARCH_HAVE_CPU_CLOCK
172 unsigned long long usecs, t;
175 if (tv && t < tv->last_cycles) {
176 dprint(FD_TIME, "CPU clock going back in time\n");
181 usecs = (t * inv_cycles_per_usec) / 16777216UL;
182 tp->tv_sec = usecs / 1000000;
183 tp->tv_usec = usecs % 1000000;
188 log_err("fio: invalid clock source %d\n", fio_clock_source);
193 * If Linux is using the tsc clock on non-synced processors,
194 * sometimes time can appear to drift backwards. Fix that up.
197 if (tv->last_tv_valid) {
198 if (tp->tv_sec < tv->last_tv.tv_sec)
199 tp->tv_sec = tv->last_tv.tv_sec;
200 else if (tv->last_tv.tv_sec == tp->tv_sec &&
201 tp->tv_usec < tv->last_tv.tv_usec)
202 tp->tv_usec = tv->last_tv.tv_usec;
204 tv->last_tv_valid = 1;
205 memcpy(&tv->last_tv, tp, sizeof(*tp));
209 #ifdef ARCH_HAVE_CPU_CLOCK
210 static unsigned long get_cycles_per_usec(void)
214 unsigned long long c_s, c_e;
216 fill_clock_gettime(&ts);
217 s.tv_sec = ts.tv_sec;
218 s.tv_usec = ts.tv_nsec / 1000;
220 c_s = get_cpu_clock();
222 unsigned long long elapsed;
224 fill_clock_gettime(&ts);
225 e.tv_sec = ts.tv_sec;
226 e.tv_usec = ts.tv_nsec / 1000;
228 elapsed = utime_since(&s, &e);
229 if (elapsed >= 1280) {
230 c_e = get_cpu_clock();
235 return (c_e - c_s + 127) >> 7;
238 #define NR_TIME_ITERS 50
240 static void calibrate_cpu_clock(void)
242 double delta, mean, S;
243 unsigned long avg, cycles[NR_TIME_ITERS];
246 cycles[0] = get_cycles_per_usec();
247 S = delta = mean = 0.0;
248 for (i = 0; i < NR_TIME_ITERS; i++) {
249 cycles[i] = get_cycles_per_usec();
250 delta = cycles[i] - mean;
252 mean += delta / (i + 1.0);
253 S += delta * (cycles[i] - mean);
257 S = sqrt(S / (NR_TIME_ITERS - 1.0));
260 for (i = 0; i < NR_TIME_ITERS; i++) {
261 double this = cycles[i];
263 if ((fmax(this, mean) - fmin(this, mean)) > S)
269 S /= (double) NR_TIME_ITERS;
272 for (i = 0; i < NR_TIME_ITERS; i++)
273 dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
276 avg = (avg + 9) / 10;
277 dprint(FD_TIME, "avg: %lu\n", avg);
278 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
280 cycles_per_usec = avg;
281 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
282 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
285 static void calibrate_cpu_clock(void)
290 void fio_local_clock_init(int is_thread)
294 t = calloc(sizeof(*t), 1);
295 if (pthread_setspecific(tv_tls_key, t))
296 log_err("fio: can't set TLS key\n");
299 static void kill_tv_tls_key(void *data)
304 void fio_clock_init(void)
306 if (fio_clock_source == fio_clock_source_inited)
309 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
310 log_err("fio: can't create TLS key\n");
312 fio_clock_source_inited = fio_clock_source;
313 calibrate_cpu_clock();
316 * If the arch sets tsc_reliable != 0, then it must be good enough
317 * to use as THE clock source. For x86 CPUs, this means the TSC
318 * runs at a constant rate and is synced across CPU cores.
321 if (!fio_clock_source_set)
322 fio_clock_source = CS_CPUCLOCK;
323 } else if (fio_clock_source == CS_CPUCLOCK)
324 log_info("fio: clocksource=cpu may not be reliable\n");
327 unsigned long long utime_since(struct timeval *s, struct timeval *e)
330 unsigned long long ret;
332 sec = e->tv_sec - s->tv_sec;
333 usec = e->tv_usec - s->tv_usec;
334 if (sec > 0 && usec < 0) {
340 * time warp bug on some kernels?
342 if (sec < 0 || (sec == 0 && usec < 0))
345 ret = sec * 1000000ULL + usec;
350 unsigned long long utime_since_now(struct timeval *s)
354 fio_gettime(&t, NULL);
355 return utime_since(s, &t);
358 unsigned long mtime_since(struct timeval *s, struct timeval *e)
362 sec = e->tv_sec - s->tv_sec;
363 usec = e->tv_usec - s->tv_usec;
364 if (sec > 0 && usec < 0) {
369 if (sec < 0 || (sec == 0 && usec < 0))
379 unsigned long mtime_since_now(struct timeval *s)
382 void *p = __builtin_return_address(0);
385 return mtime_since(s, &t);
388 unsigned long time_since_now(struct timeval *s)
390 return mtime_since_now(s) / 1000;
393 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK)
395 #define CLOCK_ENTRIES 100000
403 struct clock_thread {
406 pthread_mutex_t lock;
407 pthread_mutex_t started;
409 struct clock_entry *entries;
412 static inline uint64_t atomic64_inc_return(uint64_t *seq)
414 return 1 + __sync_fetch_and_add(seq, 1);
417 static void *clock_thread_fn(void *data)
419 struct clock_thread *t = data;
420 struct clock_entry *c;
421 os_cpu_mask_t cpu_mask;
424 memset(&cpu_mask, 0, sizeof(cpu_mask));
425 fio_cpu_set(&cpu_mask, t->cpu);
427 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
428 log_err("clock setaffinity failed\n");
432 pthread_mutex_lock(&t->lock);
433 pthread_mutex_unlock(&t->started);
436 for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
441 seq = atomic64_inc_return(t->seq);
442 tsc = get_cpu_clock();
443 } while (seq != *t->seq);
449 log_info("cs: cpu%3d: %lu clocks seen\n", t->cpu, t->entries[CLOCK_ENTRIES - 1].tsc - t->entries[0].tsc);
453 static int clock_cmp(const void *p1, const void *p2)
455 const struct clock_entry *c1 = p1;
456 const struct clock_entry *c2 = p2;
458 if (c1->seq == c2->seq)
459 log_err("cs: bug in atomic sequence!\n");
461 return c1->seq - c2->seq;
464 int fio_monotonic_clocktest(void)
466 struct clock_thread *threads;
467 unsigned int nr_cpus = cpus_online();
468 struct clock_entry *entries;
469 unsigned long tentries, failed;
473 fio_debug |= 1U << FD_TIME;
474 calibrate_cpu_clock();
475 fio_debug &= ~(1U << FD_TIME);
477 threads = malloc(nr_cpus * sizeof(struct clock_thread));
478 tentries = CLOCK_ENTRIES * nr_cpus;
479 entries = malloc(tentries * sizeof(struct clock_entry));
481 log_info("cs: Testing %u CPUs\n", nr_cpus);
483 for (i = 0; i < nr_cpus; i++) {
484 struct clock_thread *t = &threads[i];
488 t->entries = &entries[i * CLOCK_ENTRIES];
489 pthread_mutex_init(&t->lock, NULL);
490 pthread_mutex_init(&t->started, NULL);
491 pthread_mutex_lock(&t->lock);
492 pthread_create(&t->thread, NULL, clock_thread_fn, t);
495 for (i = 0; i < nr_cpus; i++) {
496 struct clock_thread *t = &threads[i];
498 pthread_mutex_lock(&t->started);
501 for (i = 0; i < nr_cpus; i++) {
502 struct clock_thread *t = &threads[i];
504 pthread_mutex_unlock(&t->lock);
507 for (failed = i = 0; i < nr_cpus; i++) {
508 struct clock_thread *t = &threads[i];
511 pthread_join(t->thread, &ret);
518 log_err("Clocksource test: %u threads failed\n", failed);
522 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
524 for (failed = i = 0; i < tentries; i++) {
525 struct clock_entry *prev, *this = &entries[i];
532 if (prev->tsc > this->tsc) {
533 uint64_t diff = prev->tsc - this->tsc;
535 log_info("cs: CPU clock mismatch (diff=%lu):\n", diff);
536 log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", prev->cpu, prev->tsc, prev->seq);
537 log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", this->cpu, this->tsc, this->seq);
545 log_info("cs: Failed: %lu\n", failed);
547 log_info("cs: Pass!\n");
554 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
556 int fio_monotonic_clocktest(void)
558 log_info("cs: current platform does not support CPU clocks\n");