16 #ifdef ARCH_HAVE_CPU_CLOCK
17 static unsigned long cycles_per_usec;
22 struct timeval last_tv;
24 unsigned long last_cycles;
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 tv = pthread_getspecific(tv_tls_key);
144 switch (fio_clock_source) {
146 gettimeofday(tp, NULL);
151 #ifdef FIO_HAVE_CLOCK_MONOTONIC
152 if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
154 if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
156 log_err("fio: clock_gettime fails\n");
160 tp->tv_sec = ts.tv_sec;
161 tp->tv_usec = ts.tv_nsec / 1000;
164 #ifdef ARCH_HAVE_CPU_CLOCK
166 unsigned long long usecs, t;
169 if (tv && t < tv->last_cycles) {
170 dprint(FD_TIME, "CPU clock going back in time\n");
175 usecs = t / cycles_per_usec;
176 tp->tv_sec = usecs / 1000000;
177 tp->tv_usec = usecs % 1000000;
182 log_err("fio: invalid clock source %d\n", fio_clock_source);
187 * If Linux is using the tsc clock on non-synced processors,
188 * sometimes time can appear to drift backwards. Fix that up.
191 if (tv->last_tv_valid) {
192 if (tp->tv_sec < tv->last_tv.tv_sec)
193 tp->tv_sec = tv->last_tv.tv_sec;
194 else if (tv->last_tv.tv_sec == tp->tv_sec &&
195 tp->tv_usec < tv->last_tv.tv_usec)
196 tp->tv_usec = tv->last_tv.tv_usec;
198 tv->last_tv_valid = 1;
199 memcpy(&tv->last_tv, tp, sizeof(*tp));
203 #ifdef ARCH_HAVE_CPU_CLOCK
204 static unsigned long get_cycles_per_usec(void)
207 unsigned long long c_s, c_e;
209 gettimeofday(&s, NULL);
210 c_s = get_cpu_clock();
212 unsigned long long elapsed;
214 gettimeofday(&e, NULL);
215 elapsed = utime_since(&s, &e);
216 if (elapsed >= 1280) {
217 c_e = get_cpu_clock();
222 return (c_e - c_s + 127) >> 7;
225 #define NR_TIME_ITERS 50
227 static void calibrate_cpu_clock(void)
229 double delta, mean, S;
230 unsigned long avg, cycles[NR_TIME_ITERS];
233 cycles[0] = get_cycles_per_usec();
234 S = delta = mean = 0.0;
235 for (i = 0; i < NR_TIME_ITERS; i++) {
236 cycles[i] = get_cycles_per_usec();
237 delta = cycles[i] - mean;
239 mean += delta / (i + 1.0);
240 S += delta * (cycles[i] - mean);
244 S = sqrt(S / (NR_TIME_ITERS - 1.0));
247 for (i = 0; i < NR_TIME_ITERS; i++) {
248 double this = cycles[i];
250 if ((fmax(this, mean) - fmin(this, mean)) > S)
256 S /= (double) NR_TIME_ITERS;
259 for (i = 0; i < NR_TIME_ITERS; i++)
260 dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
263 avg = (avg + 9) / 10;
264 dprint(FD_TIME, "avg: %lu\n", avg);
265 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
267 cycles_per_usec = avg;
270 static void calibrate_cpu_clock(void)
275 void fio_local_clock_init(int is_thread)
279 t = calloc(sizeof(*t), 1);
280 if (pthread_setspecific(tv_tls_key, t))
281 log_err("fio: can't set TLS key\n");
284 static void kill_tv_tls_key(void *data)
289 void fio_clock_init(void)
291 if (fio_clock_source == fio_clock_source_inited)
294 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
295 log_err("fio: can't create TLS key\n");
297 fio_clock_source_inited = fio_clock_source;
298 calibrate_cpu_clock();
301 * If the arch sets tsc_reliable != 0, then it must be good enough
302 * to use as THE clock source. For x86 CPUs, this means the TSC
303 * runs at a constant rate and is synced across CPU cores.
306 if (!fio_clock_source_set)
307 fio_clock_source = CS_CPUCLOCK;
308 } else if (fio_clock_source == CS_CPUCLOCK)
309 log_info("fio: clocksource=cpu may not be reliable\n");
312 unsigned long long utime_since(struct timeval *s, struct timeval *e)
315 unsigned long long ret;
317 sec = e->tv_sec - s->tv_sec;
318 usec = e->tv_usec - s->tv_usec;
319 if (sec > 0 && usec < 0) {
325 * time warp bug on some kernels?
327 if (sec < 0 || (sec == 0 && usec < 0))
330 ret = sec * 1000000ULL + usec;
335 unsigned long long utime_since_now(struct timeval *s)
339 fio_gettime(&t, NULL);
340 return utime_since(s, &t);
343 unsigned long mtime_since(struct timeval *s, struct timeval *e)
347 sec = e->tv_sec - s->tv_sec;
348 usec = e->tv_usec - s->tv_usec;
349 if (sec > 0 && usec < 0) {
354 if (sec < 0 || (sec == 0 && usec < 0))
364 unsigned long mtime_since_now(struct timeval *s)
367 void *p = __builtin_return_address(0);
370 return mtime_since(s, &t);
373 unsigned long time_since_now(struct timeval *s)
375 return mtime_since_now(s) / 1000;
378 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK)
380 #define CLOCK_ENTRIES 100000
388 struct clock_thread {
391 pthread_mutex_t lock;
392 pthread_mutex_t started;
394 struct clock_entry *entries;
397 static inline uint64_t atomic64_inc_return(uint64_t *seq)
399 return 1 + __sync_fetch_and_add(seq, 1);
402 static void *clock_thread_fn(void *data)
404 struct clock_thread *t = data;
405 struct clock_entry *c;
406 os_cpu_mask_t cpu_mask;
409 memset(&cpu_mask, 0, sizeof(cpu_mask));
410 fio_cpu_set(&cpu_mask, t->cpu);
412 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
413 log_err("clock setaffinity failed\n");
417 pthread_mutex_lock(&t->lock);
418 pthread_mutex_unlock(&t->started);
421 for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
426 seq = atomic64_inc_return(t->seq);
427 tsc = get_cpu_clock();
428 } while (seq != *t->seq);
434 log_info("cs: cpu%3d: %lu clocks seen\n", t->cpu, t->entries[CLOCK_ENTRIES - 1].tsc - t->entries[0].tsc);
438 static int clock_cmp(const void *p1, const void *p2)
440 const struct clock_entry *c1 = p1;
441 const struct clock_entry *c2 = p2;
443 if (c1->seq == c2->seq)
444 log_err("cs: bug in atomic sequence!\n");
446 return c1->seq - c2->seq;
449 int fio_monotonic_clocktest(void)
451 struct clock_thread *threads;
452 unsigned int nr_cpus = cpus_online();
453 struct clock_entry *entries;
454 unsigned long tentries, failed;
458 fio_debug |= 1U << FD_TIME;
459 calibrate_cpu_clock();
460 fio_debug &= ~(1U << FD_TIME);
462 threads = malloc(nr_cpus * sizeof(struct clock_thread));
463 tentries = CLOCK_ENTRIES * nr_cpus;
464 entries = malloc(tentries * sizeof(struct clock_entry));
466 log_info("cs: Testing %u CPUs\n", nr_cpus);
468 for (i = 0; i < nr_cpus; i++) {
469 struct clock_thread *t = &threads[i];
473 t->entries = &entries[i * CLOCK_ENTRIES];
474 pthread_mutex_init(&t->lock, NULL);
475 pthread_mutex_init(&t->started, NULL);
476 pthread_mutex_lock(&t->lock);
477 pthread_create(&t->thread, NULL, clock_thread_fn, t);
480 for (i = 0; i < nr_cpus; i++) {
481 struct clock_thread *t = &threads[i];
483 pthread_mutex_lock(&t->started);
486 for (i = 0; i < nr_cpus; i++) {
487 struct clock_thread *t = &threads[i];
489 pthread_mutex_unlock(&t->lock);
492 for (failed = i = 0; i < nr_cpus; i++) {
493 struct clock_thread *t = &threads[i];
496 pthread_join(t->thread, &ret);
503 log_err("Clocksource test: %u threads failed\n", failed);
507 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
509 for (failed = i = 0; i < tentries; i++) {
510 struct clock_entry *prev, *this = &entries[i];
517 if (prev->tsc > this->tsc) {
518 uint64_t diff = prev->tsc - this->tsc;
520 log_info("cs: CPU clock mismatch (diff=%lu):\n", diff);
521 log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", prev->cpu, prev->tsc, prev->seq);
522 log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", this->cpu, this->tsc, this->seq);
530 log_info("cs: Failed: %lu\n", failed);
532 log_info("cs: Pass!\n");
539 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
541 int fio_monotonic_clocktest(void)
543 log_info("cs: current platform does not support CPU clocks\n");