16 #ifdef ARCH_HAVE_CPU_CLOCK
17 static unsigned long cycles_per_usec;
18 static unsigned long last_cycles;
23 struct timeval last_tv;
26 static pthread_key_t tv_tls_key;
28 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
29 int fio_clock_source_set = 0;
30 enum fio_cs fio_clock_source_inited = CS_INVAL;
35 #define HASH_SIZE (1 << HASH_BITS)
37 static struct flist_head hash[HASH_SIZE];
38 static int gtod_inited;
41 struct flist_head list;
46 static struct gtod_log *find_hash(void *caller)
48 unsigned long h = hash_ptr(caller, HASH_BITS);
49 struct flist_head *entry;
51 flist_for_each(entry, &hash[h]) {
52 struct gtod_log *log = flist_entry(entry, struct gtod_log,
55 if (log->caller == caller)
62 static struct gtod_log *find_log(void *caller)
64 struct gtod_log *log = find_hash(caller);
69 log = malloc(sizeof(*log));
70 INIT_FLIST_HEAD(&log->list);
74 h = hash_ptr(caller, HASH_BITS);
75 flist_add_tail(&log->list, &hash[h]);
81 static void gtod_log_caller(void *caller)
84 struct gtod_log *log = find_log(caller);
90 static void fio_exit fio_dump_gtod(void)
92 unsigned long total_calls = 0;
95 for (i = 0; i < HASH_SIZE; i++) {
96 struct flist_head *entry;
99 flist_for_each(entry, &hash[i]) {
100 log = flist_entry(entry, struct gtod_log, list);
102 printf("function %p, calls %lu\n", log->caller,
104 total_calls += log->calls;
108 printf("Total %lu gettimeofday\n", total_calls);
111 static void fio_init gtod_init(void)
115 for (i = 0; i < HASH_SIZE; i++)
116 INIT_FLIST_HEAD(&hash[i]);
121 #endif /* FIO_DEBUG_TIME */
123 #ifdef FIO_DEBUG_TIME
124 void fio_gettime(struct timeval *tp, void *caller)
126 void fio_gettime(struct timeval *tp, void fio_unused *caller)
131 #ifdef FIO_DEBUG_TIME
133 caller = __builtin_return_address(0);
135 gtod_log_caller(caller);
138 memcpy(tp, fio_tv, sizeof(*tp));
142 switch (fio_clock_source) {
144 gettimeofday(tp, NULL);
149 #ifdef FIO_HAVE_CLOCK_MONOTONIC
150 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
152 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
154 log_err("fio: clock_gettime fails\n");
158 tp->tv_sec = ts.tv_sec;
159 tp->tv_usec = ts.tv_nsec / 1000;
162 #ifdef ARCH_HAVE_CPU_CLOCK
164 unsigned long long usecs, t;
167 if (t < last_cycles) {
168 dprint(FD_TIME, "CPU clock going back in time\n");
172 usecs = t / cycles_per_usec;
173 tp->tv_sec = usecs / 1000000;
174 tp->tv_usec = usecs % 1000000;
180 log_err("fio: invalid clock source %d\n", fio_clock_source);
185 * If Linux is using the tsc clock on non-synced processors,
186 * sometimes time can appear to drift backwards. Fix that up.
188 t = pthread_getspecific(tv_tls_key);
190 if (t->last_tv_valid) {
191 if (tp->tv_sec < t->last_tv.tv_sec)
192 tp->tv_sec = t->last_tv.tv_sec;
193 else if (t->last_tv.tv_sec == tp->tv_sec &&
194 tp->tv_usec < t->last_tv.tv_usec)
195 tp->tv_usec = t->last_tv.tv_usec;
197 t->last_tv_valid = 1;
198 memcpy(&t->last_tv, tp, sizeof(*tp));
202 #ifdef ARCH_HAVE_CPU_CLOCK
203 static unsigned long get_cycles_per_usec(void)
206 unsigned long long c_s, c_e;
208 gettimeofday(&s, NULL);
209 c_s = get_cpu_clock();
211 unsigned long long elapsed;
213 gettimeofday(&e, NULL);
214 elapsed = utime_since(&s, &e);
215 if (elapsed >= 1280) {
216 c_e = get_cpu_clock();
221 return (c_e - c_s + 127) >> 7;
224 #define NR_TIME_ITERS 50
226 static void calibrate_cpu_clock(void)
228 double delta, mean, S;
229 unsigned long avg, cycles[NR_TIME_ITERS];
232 cycles[0] = get_cycles_per_usec();
233 S = delta = mean = 0.0;
234 for (i = 0; i < NR_TIME_ITERS; i++) {
235 cycles[i] = get_cycles_per_usec();
236 delta = cycles[i] - mean;
238 mean += delta / (i + 1.0);
239 S += delta * (cycles[i] - mean);
243 S = sqrt(S / (NR_TIME_ITERS - 1.0));
246 for (i = 0; i < NR_TIME_ITERS; i++) {
247 double this = cycles[i];
249 if ((fmax(this, mean) - fmin(this, mean)) > S)
255 S /= (double) NR_TIME_ITERS;
258 for (i = 0; i < NR_TIME_ITERS; i++)
259 dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
262 avg = (avg + 9) / 10;
263 dprint(FD_TIME, "avg: %lu\n", avg);
264 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
266 cycles_per_usec = avg;
269 static void calibrate_cpu_clock(void)
274 void fio_local_clock_init(int is_thread)
278 t = calloc(sizeof(*t), 1);
279 if (pthread_setspecific(tv_tls_key, t))
280 log_err("fio: can't set TLS key\n");
283 static void kill_tv_tls_key(void *data)
288 void fio_clock_init(void)
290 if (fio_clock_source == fio_clock_source_inited)
293 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
294 log_err("fio: can't create TLS key\n");
296 fio_clock_source_inited = fio_clock_source;
297 calibrate_cpu_clock();
300 * If the arch sets tsc_reliable != 0, then it must be good enough
301 * to use as THE clock source. For x86 CPUs, this means the TSC
302 * runs at a constant rate and is synced across CPU cores.
305 if (!fio_clock_source_set)
306 fio_clock_source = CS_CPUCLOCK;
307 } else if (fio_clock_source == CS_CPUCLOCK)
308 log_info("fio: clocksource=cpu may not be reliable\n");
311 unsigned long long utime_since(struct timeval *s, struct timeval *e)
314 unsigned long long ret;
316 sec = e->tv_sec - s->tv_sec;
317 usec = e->tv_usec - s->tv_usec;
318 if (sec > 0 && usec < 0) {
324 * time warp bug on some kernels?
326 if (sec < 0 || (sec == 0 && usec < 0))
329 ret = sec * 1000000ULL + usec;
334 unsigned long long utime_since_now(struct timeval *s)
338 fio_gettime(&t, NULL);
339 return utime_since(s, &t);
342 unsigned long mtime_since(struct timeval *s, struct timeval *e)
346 sec = e->tv_sec - s->tv_sec;
347 usec = e->tv_usec - s->tv_usec;
348 if (sec > 0 && usec < 0) {
353 if (sec < 0 || (sec == 0 && usec < 0))
363 unsigned long mtime_since_now(struct timeval *s)
366 void *p = __builtin_return_address(0);
369 return mtime_since(s, &t);
372 unsigned long time_since_now(struct timeval *s)
374 return mtime_since_now(s) / 1000;
377 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK)
379 #define CLOCK_ENTRIES 100000
387 struct clock_thread {
390 pthread_mutex_t lock;
391 pthread_mutex_t started;
393 struct clock_entry *entries;
396 static inline uint64_t atomic64_inc_return(uint64_t *seq)
398 return 1 + __sync_fetch_and_add(seq, 1);
401 static void *clock_thread_fn(void *data)
403 struct clock_thread *t = data;
404 struct clock_entry *c;
405 os_cpu_mask_t cpu_mask;
408 memset(&cpu_mask, 0, sizeof(cpu_mask));
409 fio_cpu_set(&cpu_mask, t->cpu);
411 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
412 log_err("clock setaffinity failed\n");
416 pthread_mutex_lock(&t->lock);
417 pthread_mutex_unlock(&t->started);
420 for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
425 seq = atomic64_inc_return(t->seq);
426 tsc = get_cpu_clock();
427 } while (seq != *t->seq);
433 log_info("cs: cpu%3d: %lu clocks seen\n", t->cpu, t->entries[CLOCK_ENTRIES - 1].tsc - t->entries[0].tsc);
437 static int clock_cmp(const void *p1, const void *p2)
439 const struct clock_entry *c1 = p1;
440 const struct clock_entry *c2 = p2;
442 if (c1->seq == c2->seq)
443 log_err("cs: bug in atomic sequence!\n");
445 return c1->seq - c2->seq;
448 int fio_monotonic_clocktest(void)
450 struct clock_thread *threads;
451 unsigned int nr_cpus = cpus_online();
452 struct clock_entry *entries;
453 unsigned long tentries, failed;
457 fio_debug |= 1U << FD_TIME;
458 calibrate_cpu_clock();
459 fio_debug &= ~(1U << FD_TIME);
461 threads = malloc(nr_cpus * sizeof(struct clock_thread));
462 tentries = CLOCK_ENTRIES * nr_cpus;
463 entries = malloc(tentries * sizeof(struct clock_entry));
465 log_info("cs: Testing %u CPUs\n", nr_cpus);
467 for (i = 0; i < nr_cpus; i++) {
468 struct clock_thread *t = &threads[i];
472 t->entries = &entries[i * CLOCK_ENTRIES];
473 pthread_mutex_init(&t->lock, NULL);
474 pthread_mutex_init(&t->started, NULL);
475 pthread_mutex_lock(&t->lock);
476 pthread_create(&t->thread, NULL, clock_thread_fn, t);
479 for (i = 0; i < nr_cpus; i++) {
480 struct clock_thread *t = &threads[i];
482 pthread_mutex_lock(&t->started);
485 for (i = 0; i < nr_cpus; i++) {
486 struct clock_thread *t = &threads[i];
488 pthread_mutex_unlock(&t->lock);
491 for (failed = i = 0; i < nr_cpus; i++) {
492 struct clock_thread *t = &threads[i];
495 pthread_join(t->thread, &ret);
502 log_err("Clocksource test: %u threads failed\n", failed);
506 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
508 for (failed = i = 0; i < tentries; i++) {
509 struct clock_entry *prev, *this = &entries[i];
516 if (prev->tsc > this->tsc) {
517 uint64_t diff = prev->tsc - this->tsc;
519 log_info("cs: CPU clock mismatch (diff=%lu):\n", diff);
520 log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", prev->cpu, prev->tsc, prev->seq);
521 log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", this->cpu, this->tsc, this->seq);
529 log_info("cs: Failed: %lu\n", failed);
531 log_info("cs: Pass!\n");
538 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
540 int fio_monotonic_clocktest(void)
542 log_info("cs: current platform does not support CPU clocks\n");