Fixup wrong types for dprint()
[fio.git] / gettime.c
... / ...
CommitLineData
1/*
2 * Clock functions
3 */
4
5#include <unistd.h>
6#include <math.h>
7#include <sys/time.h>
8#include <time.h>
9
10#include "fio.h"
11#include "smalloc.h"
12
13#include "hash.h"
14#include "os/os.h"
15
16#ifdef ARCH_HAVE_CPU_CLOCK
17static unsigned long cycles_per_usec;
18static unsigned long inv_cycles_per_usec;
19#endif
20int tsc_reliable = 0;
21
22struct tv_valid {
23 struct timeval last_tv;
24 uint64_t last_cycles;
25 int last_tv_valid;
26};
27#ifdef CONFIG_TLS_THREAD
28static struct tv_valid __thread static_tv_valid;
29#else
30static pthread_key_t tv_tls_key;
31#endif
32
33enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
34int fio_clock_source_set = 0;
35enum fio_cs fio_clock_source_inited = CS_INVAL;
36
37#ifdef FIO_DEBUG_TIME
38
39#define HASH_BITS 8
40#define HASH_SIZE (1 << HASH_BITS)
41
42static struct flist_head hash[HASH_SIZE];
43static int gtod_inited;
44
45struct gtod_log {
46 struct flist_head list;
47 void *caller;
48 unsigned long calls;
49};
50
51static struct gtod_log *find_hash(void *caller)
52{
53 unsigned long h = hash_ptr(caller, HASH_BITS);
54 struct flist_head *entry;
55
56 flist_for_each(entry, &hash[h]) {
57 struct gtod_log *log = flist_entry(entry, struct gtod_log,
58 list);
59
60 if (log->caller == caller)
61 return log;
62 }
63
64 return NULL;
65}
66
67static struct gtod_log *find_log(void *caller)
68{
69 struct gtod_log *log = find_hash(caller);
70
71 if (!log) {
72 unsigned long h;
73
74 log = malloc(sizeof(*log));
75 INIT_FLIST_HEAD(&log->list);
76 log->caller = caller;
77 log->calls = 0;
78
79 h = hash_ptr(caller, HASH_BITS);
80 flist_add_tail(&log->list, &hash[h]);
81 }
82
83 return log;
84}
85
86static void gtod_log_caller(void *caller)
87{
88 if (gtod_inited) {
89 struct gtod_log *log = find_log(caller);
90
91 log->calls++;
92 }
93}
94
95static void fio_exit fio_dump_gtod(void)
96{
97 unsigned long total_calls = 0;
98 int i;
99
100 for (i = 0; i < HASH_SIZE; i++) {
101 struct flist_head *entry;
102 struct gtod_log *log;
103
104 flist_for_each(entry, &hash[i]) {
105 log = flist_entry(entry, struct gtod_log, list);
106
107 printf("function %p, calls %lu\n", log->caller,
108 log->calls);
109 total_calls += log->calls;
110 }
111 }
112
113 printf("Total %lu gettimeofday\n", total_calls);
114}
115
116static void fio_init gtod_init(void)
117{
118 int i;
119
120 for (i = 0; i < HASH_SIZE; i++)
121 INIT_FLIST_HEAD(&hash[i]);
122
123 gtod_inited = 1;
124}
125
126#endif /* FIO_DEBUG_TIME */
127
128#ifdef CONFIG_CLOCK_GETTIME
129static int fill_clock_gettime(struct timespec *ts)
130{
131#ifdef CONFIG_CLOCK_MONOTONIC
132 return clock_gettime(CLOCK_MONOTONIC, ts);
133#else
134 return clock_gettime(CLOCK_REALTIME, ts);
135#endif
136}
137#endif
138
139static void *__fio_gettime(struct timeval *tp)
140{
141 struct tv_valid *tv;
142
143#ifdef CONFIG_TLS_THREAD
144 tv = &static_tv_valid;
145#else
146 tv = pthread_getspecific(tv_tls_key);
147#endif
148
149 switch (fio_clock_source) {
150#ifdef CONFIG_GETTIMEOFDAY
151 case CS_GTOD:
152 gettimeofday(tp, NULL);
153 break;
154#endif
155#ifdef CONFIG_CLOCK_GETTIME
156 case CS_CGETTIME: {
157 struct timespec ts;
158
159 if (fill_clock_gettime(&ts) < 0) {
160 log_err("fio: clock_gettime fails\n");
161 assert(0);
162 }
163
164 tp->tv_sec = ts.tv_sec;
165 tp->tv_usec = ts.tv_nsec / 1000;
166 break;
167 }
168#endif
169#ifdef ARCH_HAVE_CPU_CLOCK
170 case CS_CPUCLOCK: {
171 uint64_t usecs, t;
172
173 t = get_cpu_clock();
174 if (tv && t < tv->last_cycles) {
175 dprint(FD_TIME, "CPU clock going back in time\n");
176 t = tv->last_cycles;
177 } else if (tv)
178 tv->last_cycles = t;
179
180 usecs = (t * inv_cycles_per_usec) / 16777216UL;
181 tp->tv_sec = usecs / 1000000;
182 tp->tv_usec = usecs % 1000000;
183 break;
184 }
185#endif
186 default:
187 log_err("fio: invalid clock source %d\n", fio_clock_source);
188 break;
189 }
190
191 return tv;
192}
193
194#ifdef FIO_DEBUG_TIME
195void fio_gettime(struct timeval *tp, void *caller)
196#else
197void fio_gettime(struct timeval *tp, void fio_unused *caller)
198#endif
199{
200 struct tv_valid *tv;
201
202#ifdef FIO_DEBUG_TIME
203 if (!caller)
204 caller = __builtin_return_address(0);
205
206 gtod_log_caller(caller);
207#endif
208 if (fio_tv) {
209 memcpy(tp, fio_tv, sizeof(*tp));
210 return;
211 }
212
213 tv = __fio_gettime(tp);
214
215 /*
216 * If Linux is using the tsc clock on non-synced processors,
217 * sometimes time can appear to drift backwards. Fix that up.
218 */
219 if (tv) {
220 if (tv->last_tv_valid) {
221 if (tp->tv_sec < tv->last_tv.tv_sec)
222 tp->tv_sec = tv->last_tv.tv_sec;
223 else if (tv->last_tv.tv_sec == tp->tv_sec &&
224 tp->tv_usec < tv->last_tv.tv_usec)
225 tp->tv_usec = tv->last_tv.tv_usec;
226 }
227 tv->last_tv_valid = 1;
228 memcpy(&tv->last_tv, tp, sizeof(*tp));
229 }
230}
231
232#ifdef ARCH_HAVE_CPU_CLOCK
233static unsigned long get_cycles_per_usec(void)
234{
235 struct timeval s, e;
236 uint64_t c_s, c_e;
237 enum fio_cs old_cs = fio_clock_source;
238
239#ifdef CONFIG_CLOCK_GETTIME
240 fio_clock_source = CS_CGETTIME;
241#else
242 fio_clock_source = CS_GTOD;
243#endif
244 __fio_gettime(&s);
245
246 c_s = get_cpu_clock();
247 do {
248 uint64_t elapsed;
249
250 __fio_gettime(&e);
251
252 elapsed = utime_since(&s, &e);
253 if (elapsed >= 1280) {
254 c_e = get_cpu_clock();
255 break;
256 }
257 } while (1);
258
259 fio_clock_source = old_cs;
260 return (c_e - c_s + 127) >> 7;
261}
262
263#define NR_TIME_ITERS 50
264
265static int calibrate_cpu_clock(void)
266{
267 double delta, mean, S;
268 uint64_t avg, cycles[NR_TIME_ITERS];
269 int i, samples;
270
271 cycles[0] = get_cycles_per_usec();
272 S = delta = mean = 0.0;
273 for (i = 0; i < NR_TIME_ITERS; i++) {
274 cycles[i] = get_cycles_per_usec();
275 delta = cycles[i] - mean;
276 if (delta) {
277 mean += delta / (i + 1.0);
278 S += delta * (cycles[i] - mean);
279 }
280 }
281
282 /*
283 * The most common platform clock breakage is returning zero
284 * indefinitely. Check for that and return failure.
285 */
286 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
287 return 1;
288
289 S = sqrt(S / (NR_TIME_ITERS - 1.0));
290
291 samples = avg = 0;
292 for (i = 0; i < NR_TIME_ITERS; i++) {
293 double this = cycles[i];
294
295 if ((fmax(this, mean) - fmin(this, mean)) > S)
296 continue;
297 samples++;
298 avg += this;
299 }
300
301 S /= (double) NR_TIME_ITERS;
302 mean /= 10.0;
303
304 for (i = 0; i < NR_TIME_ITERS; i++)
305 dprint(FD_TIME, "cycles[%d]=%llu\n", i,
306 (unsigned long long) cycles[i] / 10);
307
308 avg /= samples;
309 avg = (avg + 5) / 10;
310 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
311 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
312
313 cycles_per_usec = avg;
314 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
315 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
316 return 0;
317}
318#else
319static int calibrate_cpu_clock(void)
320{
321 return 1;
322}
323#endif
324
325#ifndef CONFIG_TLS_THREAD
326void fio_local_clock_init(int is_thread)
327{
328 struct tv_valid *t;
329
330 t = calloc(sizeof(*t), 1);
331 if (pthread_setspecific(tv_tls_key, t))
332 log_err("fio: can't set TLS key\n");
333}
334
335static void kill_tv_tls_key(void *data)
336{
337 free(data);
338}
339#else
340void fio_local_clock_init(int is_thread)
341{
342}
343#endif
344
345void fio_clock_init(void)
346{
347 if (fio_clock_source == fio_clock_source_inited)
348 return;
349
350#ifndef CONFIG_TLS_THREAD
351 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
352 log_err("fio: can't create TLS key\n");
353#endif
354
355 fio_clock_source_inited = fio_clock_source;
356
357 if (calibrate_cpu_clock())
358 tsc_reliable = 0;
359
360 /*
361 * If the arch sets tsc_reliable != 0, then it must be good enough
362 * to use as THE clock source. For x86 CPUs, this means the TSC
363 * runs at a constant rate and is synced across CPU cores.
364 */
365 if (tsc_reliable) {
366 if (!fio_clock_source_set)
367 fio_clock_source = CS_CPUCLOCK;
368 } else if (fio_clock_source == CS_CPUCLOCK)
369 log_info("fio: clocksource=cpu may not be reliable\n");
370}
371
372uint64_t utime_since(struct timeval *s, struct timeval *e)
373{
374 long sec, usec;
375 uint64_t ret;
376
377 sec = e->tv_sec - s->tv_sec;
378 usec = e->tv_usec - s->tv_usec;
379 if (sec > 0 && usec < 0) {
380 sec--;
381 usec += 1000000;
382 }
383
384 /*
385 * time warp bug on some kernels?
386 */
387 if (sec < 0 || (sec == 0 && usec < 0))
388 return 0;
389
390 ret = sec * 1000000ULL + usec;
391
392 return ret;
393}
394
395uint64_t utime_since_now(struct timeval *s)
396{
397 struct timeval t;
398
399 fio_gettime(&t, NULL);
400 return utime_since(s, &t);
401}
402
403uint64_t mtime_since(struct timeval *s, struct timeval *e)
404{
405 long sec, usec, ret;
406
407 sec = e->tv_sec - s->tv_sec;
408 usec = e->tv_usec - s->tv_usec;
409 if (sec > 0 && usec < 0) {
410 sec--;
411 usec += 1000000;
412 }
413
414 if (sec < 0 || (sec == 0 && usec < 0))
415 return 0;
416
417 sec *= 1000UL;
418 usec /= 1000UL;
419 ret = sec + usec;
420
421 return ret;
422}
423
424uint64_t mtime_since_now(struct timeval *s)
425{
426 struct timeval t;
427 void *p = __builtin_return_address(0);
428
429 fio_gettime(&t, p);
430 return mtime_since(s, &t);
431}
432
433uint64_t time_since_now(struct timeval *s)
434{
435 return mtime_since_now(s) / 1000;
436}
437
438#if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
439 defined(CONFIG_SFAA)
440
441#define CLOCK_ENTRIES 100000
442
443struct clock_entry {
444 uint64_t seq;
445 uint64_t tsc;
446 uint64_t cpu;
447};
448
449struct clock_thread {
450 pthread_t thread;
451 int cpu;
452 pthread_mutex_t lock;
453 pthread_mutex_t started;
454 uint64_t *seq;
455 struct clock_entry *entries;
456};
457
458static inline uint64_t atomic64_inc_return(uint64_t *seq)
459{
460 return 1 + __sync_fetch_and_add(seq, 1);
461}
462
463static void *clock_thread_fn(void *data)
464{
465 struct clock_thread *t = data;
466 struct clock_entry *c;
467 os_cpu_mask_t cpu_mask;
468 int i;
469
470 memset(&cpu_mask, 0, sizeof(cpu_mask));
471 fio_cpu_set(&cpu_mask, t->cpu);
472
473 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
474 log_err("clock setaffinity failed\n");
475 return (void *) 1;
476 }
477
478 pthread_mutex_lock(&t->lock);
479 pthread_mutex_unlock(&t->started);
480
481 c = &t->entries[0];
482 for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
483 uint64_t seq, tsc;
484
485 c->cpu = t->cpu;
486 do {
487 seq = atomic64_inc_return(t->seq);
488 tsc = get_cpu_clock();
489 } while (seq != *t->seq);
490
491 c->seq = seq;
492 c->tsc = tsc;
493 }
494
495 log_info("cs: cpu%3d: %lu clocks seen\n", t->cpu, t->entries[CLOCK_ENTRIES - 1].tsc - t->entries[0].tsc);
496 /*
497 * The most common platform clock breakage is returning zero
498 * indefinitely. Check for that and return failure.
499 */
500 if (!t->entries[CLOCK_ENTRIES - 1].tsc && !t->entries[0].tsc)
501 return (void *) 1;
502
503 return NULL;
504}
505
506static int clock_cmp(const void *p1, const void *p2)
507{
508 const struct clock_entry *c1 = p1;
509 const struct clock_entry *c2 = p2;
510
511 if (c1->seq == c2->seq)
512 log_err("cs: bug in atomic sequence!\n");
513
514 return c1->seq - c2->seq;
515}
516
517int fio_monotonic_clocktest(void)
518{
519 struct clock_thread *threads;
520 unsigned int nr_cpus = cpus_online();
521 struct clock_entry *entries;
522 unsigned long tentries, failed;
523 struct clock_entry *prev, *this;
524 uint64_t seq = 0;
525 int i;
526
527 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
528
529 fio_debug |= 1U << FD_TIME;
530 calibrate_cpu_clock();
531 fio_debug &= ~(1U << FD_TIME);
532
533 threads = malloc(nr_cpus * sizeof(struct clock_thread));
534 tentries = CLOCK_ENTRIES * nr_cpus;
535 entries = malloc(tentries * sizeof(struct clock_entry));
536
537 log_info("cs: Testing %u CPUs\n", nr_cpus);
538
539 for (i = 0; i < nr_cpus; i++) {
540 struct clock_thread *t = &threads[i];
541
542 t->cpu = i;
543 t->seq = &seq;
544 t->entries = &entries[i * CLOCK_ENTRIES];
545 pthread_mutex_init(&t->lock, NULL);
546 pthread_mutex_init(&t->started, NULL);
547 pthread_mutex_lock(&t->lock);
548 pthread_create(&t->thread, NULL, clock_thread_fn, t);
549 }
550
551 for (i = 0; i < nr_cpus; i++) {
552 struct clock_thread *t = &threads[i];
553
554 pthread_mutex_lock(&t->started);
555 }
556
557 for (i = 0; i < nr_cpus; i++) {
558 struct clock_thread *t = &threads[i];
559
560 pthread_mutex_unlock(&t->lock);
561 }
562
563 for (failed = i = 0; i < nr_cpus; i++) {
564 struct clock_thread *t = &threads[i];
565 void *ret;
566
567 pthread_join(t->thread, &ret);
568 if (ret)
569 failed++;
570 }
571 free(threads);
572
573 if (failed) {
574 log_err("Clocksource test: %u threads failed\n", failed);
575 goto err;
576 }
577
578 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
579
580 for (failed = i = 0; i < tentries; i++) {
581 this = &entries[i];
582
583 if (!i) {
584 prev = this;
585 continue;
586 }
587
588 if (prev->tsc > this->tsc) {
589 uint64_t diff = prev->tsc - this->tsc;
590
591 log_info("cs: CPU clock mismatch (diff=%lu):\n", diff);
592 log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", prev->cpu, prev->tsc, prev->seq);
593 log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", this->cpu, this->tsc, this->seq);
594 failed++;
595 }
596
597 prev = this;
598 }
599
600 if (failed)
601 log_info("cs: Failed: %lu\n", failed);
602 else
603 log_info("cs: Pass!\n");
604
605err:
606 free(entries);
607 return !!failed;
608}
609
610#else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
611
612int fio_monotonic_clocktest(void)
613{
614 log_info("cs: current platform does not support CPU clocks\n");
615 return 0;
616}
617
618#endif