blob: fa750ec85584716f7b6e37c5241130f8688dd6cb [file] [log] [blame]
Jens Axboe02bcaa82006-11-24 10:42:00 +01001/*
Jens Axboef5cc0242006-11-24 10:47:40 +01002 * Clock functions
Jens Axboe02bcaa82006-11-24 10:42:00 +01003 */
Jens Axboef5cc0242006-11-24 10:47:40 +01004
Jens Axboe02bcaa82006-11-24 10:42:00 +01005#include <unistd.h>
Jens Axboec223da82010-03-24 13:23:53 +01006#include <math.h>
Jens Axboe02bcaa82006-11-24 10:42:00 +01007#include <sys/time.h>
Bruce Cran03e20d62011-01-02 20:14:54 +01008#include <time.h>
Jens Axboe02bcaa82006-11-24 10:42:00 +01009
10#include "fio.h"
Jens Axboebe4ecfd2008-12-08 14:10:52 +010011#include "smalloc.h"
Jens Axboe02bcaa82006-11-24 10:42:00 +010012
13#include "hash.h"
Jens Axboe7d11f872012-12-17 12:03:29 +010014#include "os/os.h"
Jens Axboe02bcaa82006-11-24 10:42:00 +010015
Christian Ehrhardt919e7892014-04-08 17:52:00 +020016#if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
Jens Axboec223da82010-03-24 13:23:53 +010017static unsigned long cycles_per_usec;
Jens Axboe71339112012-12-21 22:54:56 +010018static unsigned long inv_cycles_per_usec;
Jens Axboe09a32402010-08-15 15:01:51 -040019#endif
Jens Axboe4de98eb2013-01-01 10:59:04 +010020int tsc_reliable = 0;
Jens Axboe5d879392012-12-18 09:12:27 +010021
22struct tv_valid {
23 struct timeval last_tv;
Jens Axboeba458c22013-01-21 05:38:22 -070024 uint64_t last_cycles;
Jens Axboe5d879392012-12-18 09:12:27 +010025 int last_tv_valid;
26};
Jens Axboe67bf9822013-01-10 11:23:19 +010027#ifdef CONFIG_TLS_THREAD
Jens Axboeb4ea84d2013-04-11 14:20:33 +020028static __thread struct tv_valid static_tv_valid;
Jens Axboe67bf9822013-01-10 11:23:19 +010029#else
Jens Axboe5d879392012-12-18 09:12:27 +010030static pthread_key_t tv_tls_key;
Jens Axboe67bf9822013-01-10 11:23:19 +010031#endif
Jens Axboe02bcaa82006-11-24 10:42:00 +010032
Bruce Cran16de1bf2012-02-20 17:07:32 +000033enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
Jens Axboefa80fea2012-12-09 20:29:00 +010034int fio_clock_source_set = 0;
Jens Axboe10aa1362014-04-01 21:10:36 -060035static enum fio_cs fio_clock_source_inited = CS_INVAL;
Jens Axboec223da82010-03-24 13:23:53 +010036
Jens Axboe02bcaa82006-11-24 10:42:00 +010037#ifdef FIO_DEBUG_TIME
38
39#define HASH_BITS 8
40#define HASH_SIZE (1 << HASH_BITS)
41
Jens Axboe01743ee2008-06-02 12:19:19 +020042static struct flist_head hash[HASH_SIZE];
Jens Axboe02bcaa82006-11-24 10:42:00 +010043static int gtod_inited;
44
45struct gtod_log {
Jens Axboe01743ee2008-06-02 12:19:19 +020046 struct flist_head list;
Jens Axboe02bcaa82006-11-24 10:42:00 +010047 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);
Jens Axboe01743ee2008-06-02 12:19:19 +020054 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +010055
Jens Axboe01743ee2008-06-02 12:19:19 +020056 flist_for_each(entry, &hash[h]) {
57 struct gtod_log *log = flist_entry(entry, struct gtod_log,
58 list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010059
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));
Jens Axboe01743ee2008-06-02 12:19:19 +020075 INIT_FLIST_HEAD(&log->list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010076 log->caller = caller;
77 log->calls = 0;
78
79 h = hash_ptr(caller, HASH_BITS);
Jens Axboe01743ee2008-06-02 12:19:19 +020080 flist_add_tail(&log->list, &hash[h]);
Jens Axboe02bcaa82006-11-24 10:42:00 +010081 }
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++) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200101 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100102 struct gtod_log *log;
103
Jens Axboe01743ee2008-06-02 12:19:19 +0200104 flist_for_each(entry, &hash[i]) {
105 log = flist_entry(entry, struct gtod_log, list);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100106
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100107 printf("function %p, calls %lu\n", log->caller,
108 log->calls);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100109 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++)
Jens Axboe01743ee2008-06-02 12:19:19 +0200121 INIT_FLIST_HEAD(&hash[i]);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100122
123 gtod_inited = 1;
124}
125
126#endif /* FIO_DEBUG_TIME */
127
Jens Axboe67bf9822013-01-10 11:23:19 +0100128#ifdef CONFIG_CLOCK_GETTIME
Jens Axboe9ff1c072012-12-20 15:17:57 +0100129static int fill_clock_gettime(struct timespec *ts)
130{
Jens Axboe67bf9822013-01-10 11:23:19 +0100131#ifdef CONFIG_CLOCK_MONOTONIC
Jens Axboe9ff1c072012-12-20 15:17:57 +0100132 return clock_gettime(CLOCK_MONOTONIC, ts);
133#else
134 return clock_gettime(CLOCK_REALTIME, ts);
135#endif
136}
Jens Axboe1e97cce2006-12-05 11:44:16 +0100137#endif
Jens Axboe67bf9822013-01-10 11:23:19 +0100138
139static void *__fio_gettime(struct timeval *tp)
Jens Axboe02bcaa82006-11-24 10:42:00 +0100140{
Jens Axboe93f0b092012-12-18 09:41:59 +0100141 struct tv_valid *tv;
Jens Axboe5d879392012-12-18 09:12:27 +0100142
Jens Axboe67bf9822013-01-10 11:23:19 +0100143#ifdef CONFIG_TLS_THREAD
144 tv = &static_tv_valid;
145#else
Jens Axboe93f0b092012-12-18 09:41:59 +0100146 tv = pthread_getspecific(tv_tls_key);
Jens Axboe67bf9822013-01-10 11:23:19 +0100147#endif
Jens Axboe93f0b092012-12-18 09:41:59 +0100148
Jens Axboec223da82010-03-24 13:23:53 +0100149 switch (fio_clock_source) {
Jens Axboe67bf9822013-01-10 11:23:19 +0100150#ifdef CONFIG_GETTIMEOFDAY
Jens Axboec223da82010-03-24 13:23:53 +0100151 case CS_GTOD:
Jens Axboe02bcaa82006-11-24 10:42:00 +0100152 gettimeofday(tp, NULL);
Jens Axboec223da82010-03-24 13:23:53 +0100153 break;
Jens Axboe67bf9822013-01-10 11:23:19 +0100154#endif
155#ifdef CONFIG_CLOCK_GETTIME
Jens Axboec223da82010-03-24 13:23:53 +0100156 case CS_CGETTIME: {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100157 struct timespec ts;
158
Jens Axboe9ff1c072012-12-20 15:17:57 +0100159 if (fill_clock_gettime(&ts) < 0) {
Jens Axboec223da82010-03-24 13:23:53 +0100160 log_err("fio: clock_gettime fails\n");
161 assert(0);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100162 }
163
164 tp->tv_sec = ts.tv_sec;
165 tp->tv_usec = ts.tv_nsec / 1000;
Jens Axboec223da82010-03-24 13:23:53 +0100166 break;
167 }
Jens Axboe67bf9822013-01-10 11:23:19 +0100168#endif
Jens Axboec223da82010-03-24 13:23:53 +0100169#ifdef ARCH_HAVE_CPU_CLOCK
170 case CS_CPUCLOCK: {
Jens Axboeba458c22013-01-21 05:38:22 -0700171 uint64_t usecs, t;
Jens Axboec223da82010-03-24 13:23:53 +0100172
173 t = get_cpu_clock();
Jens Axboe93f0b092012-12-18 09:41:59 +0100174 if (tv && t < tv->last_cycles) {
Jens Axboec223da82010-03-24 13:23:53 +0100175 dprint(FD_TIME, "CPU clock going back in time\n");
Jens Axboe93f0b092012-12-18 09:41:59 +0100176 t = tv->last_cycles;
177 } else if (tv)
178 tv->last_cycles = t;
Jens Axboec223da82010-03-24 13:23:53 +0100179
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200180#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
181 usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
182#else
Jens Axboe71339112012-12-21 22:54:56 +0100183 usecs = (t * inv_cycles_per_usec) / 16777216UL;
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200184#endif
Jens Axboec223da82010-03-24 13:23:53 +0100185 tp->tv_sec = usecs / 1000000;
186 tp->tv_usec = usecs % 1000000;
Jens Axboec223da82010-03-24 13:23:53 +0100187 break;
188 }
189#endif
190 default:
191 log_err("fio: invalid clock source %d\n", fio_clock_source);
192 break;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100193 }
Jens Axboe3e488922008-11-13 12:07:20 +0100194
Jens Axboe67bf9822013-01-10 11:23:19 +0100195 return tv;
196}
197
198#ifdef FIO_DEBUG_TIME
199void fio_gettime(struct timeval *tp, void *caller)
200#else
201void fio_gettime(struct timeval *tp, void fio_unused *caller)
202#endif
203{
204 struct tv_valid *tv;
205
206#ifdef FIO_DEBUG_TIME
207 if (!caller)
208 caller = __builtin_return_address(0);
209
210 gtod_log_caller(caller);
211#endif
Jens Axboe225ba9e2014-02-26 14:31:15 -0800212 if (fio_unlikely(fio_tv)) {
Jens Axboe67bf9822013-01-10 11:23:19 +0100213 memcpy(tp, fio_tv, sizeof(*tp));
214 return;
215 }
216
217 tv = __fio_gettime(tp);
218
Jens Axboe3e488922008-11-13 12:07:20 +0100219 /*
220 * If Linux is using the tsc clock on non-synced processors,
221 * sometimes time can appear to drift backwards. Fix that up.
222 */
Jens Axboe93f0b092012-12-18 09:41:59 +0100223 if (tv) {
224 if (tv->last_tv_valid) {
225 if (tp->tv_sec < tv->last_tv.tv_sec)
226 tp->tv_sec = tv->last_tv.tv_sec;
227 else if (tv->last_tv.tv_sec == tp->tv_sec &&
228 tp->tv_usec < tv->last_tv.tv_usec)
229 tp->tv_usec = tv->last_tv.tv_usec;
Jens Axboe5d879392012-12-18 09:12:27 +0100230 }
Jens Axboe93f0b092012-12-18 09:41:59 +0100231 tv->last_tv_valid = 1;
232 memcpy(&tv->last_tv, tp, sizeof(*tp));
Jens Axboe3e488922008-11-13 12:07:20 +0100233 }
Jens Axboe02bcaa82006-11-24 10:42:00 +0100234}
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100235
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200236#if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
Jens Axboec223da82010-03-24 13:23:53 +0100237static unsigned long get_cycles_per_usec(void)
238{
239 struct timeval s, e;
Jens Axboeba458c22013-01-21 05:38:22 -0700240 uint64_t c_s, c_e;
Jens Axboe67bf9822013-01-10 11:23:19 +0100241 enum fio_cs old_cs = fio_clock_source;
Jens Axboec223da82010-03-24 13:23:53 +0100242
Jens Axboe67bf9822013-01-10 11:23:19 +0100243#ifdef CONFIG_CLOCK_GETTIME
244 fio_clock_source = CS_CGETTIME;
245#else
246 fio_clock_source = CS_GTOD;
247#endif
248 __fio_gettime(&s);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100249
Jens Axboec223da82010-03-24 13:23:53 +0100250 c_s = get_cpu_clock();
251 do {
Jens Axboeba458c22013-01-21 05:38:22 -0700252 uint64_t elapsed;
Jens Axboec223da82010-03-24 13:23:53 +0100253
Jens Axboe67bf9822013-01-10 11:23:19 +0100254 __fio_gettime(&e);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100255
Jens Axboec223da82010-03-24 13:23:53 +0100256 elapsed = utime_since(&s, &e);
Jens Axboe486332e2012-12-10 08:07:14 +0100257 if (elapsed >= 1280) {
Jens Axboec223da82010-03-24 13:23:53 +0100258 c_e = get_cpu_clock();
259 break;
260 }
261 } while (1);
262
Jens Axboe67bf9822013-01-10 11:23:19 +0100263 fio_clock_source = old_cs;
Jens Axboec0110002012-12-10 08:29:03 +0100264 return (c_e - c_s + 127) >> 7;
Jens Axboec223da82010-03-24 13:23:53 +0100265}
266
Jens Axboefa80fea2012-12-09 20:29:00 +0100267#define NR_TIME_ITERS 50
268
Jens Axboee2598792013-02-24 21:29:35 +0100269static int calibrate_cpu_clock(void)
Jens Axboec223da82010-03-24 13:23:53 +0100270{
271 double delta, mean, S;
Jens Axboeba458c22013-01-21 05:38:22 -0700272 uint64_t avg, cycles[NR_TIME_ITERS];
Jens Axboec223da82010-03-24 13:23:53 +0100273 int i, samples;
274
Jens Axboec223da82010-03-24 13:23:53 +0100275 cycles[0] = get_cycles_per_usec();
276 S = delta = mean = 0.0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100277 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100278 cycles[i] = get_cycles_per_usec();
279 delta = cycles[i] - mean;
280 if (delta) {
281 mean += delta / (i + 1.0);
282 S += delta * (cycles[i] - mean);
283 }
284 }
285
Jens Axboee2598792013-02-24 21:29:35 +0100286 /*
287 * The most common platform clock breakage is returning zero
288 * indefinitely. Check for that and return failure.
289 */
290 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
291 return 1;
292
Jens Axboefa80fea2012-12-09 20:29:00 +0100293 S = sqrt(S / (NR_TIME_ITERS - 1.0));
Jens Axboec223da82010-03-24 13:23:53 +0100294
295 samples = avg = 0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100296 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100297 double this = cycles[i];
298
Bruce Cran03e20d62011-01-02 20:14:54 +0100299 if ((fmax(this, mean) - fmin(this, mean)) > S)
Jens Axboec223da82010-03-24 13:23:53 +0100300 continue;
301 samples++;
302 avg += this;
303 }
304
Jens Axboefa80fea2012-12-09 20:29:00 +0100305 S /= (double) NR_TIME_ITERS;
Jens Axboe89db7272012-12-10 08:29:56 +0100306 mean /= 10.0;
Jens Axboec223da82010-03-24 13:23:53 +0100307
Jens Axboefa80fea2012-12-09 20:29:00 +0100308 for (i = 0; i < NR_TIME_ITERS; i++)
Jens Axboe4b91ee82013-02-25 10:18:33 +0100309 dprint(FD_TIME, "cycles[%d]=%llu\n", i,
310 (unsigned long long) cycles[i] / 10);
Jens Axboec223da82010-03-24 13:23:53 +0100311
Jens Axboed7abad32012-12-10 10:15:59 +0100312 avg /= samples;
Jens Axboeb0ff22d2013-01-01 13:38:18 +0100313 avg = (avg + 5) / 10;
Jens Axboe4b91ee82013-02-25 10:18:33 +0100314 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
Jens Axboec223da82010-03-24 13:23:53 +0100315 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
316
317 cycles_per_usec = avg;
Jens Axboe71339112012-12-21 22:54:56 +0100318 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
319 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
Jens Axboee2598792013-02-24 21:29:35 +0100320 return 0;
Jens Axboe09a32402010-08-15 15:01:51 -0400321}
322#else
Jens Axboee2598792013-02-24 21:29:35 +0100323static int calibrate_cpu_clock(void)
Jens Axboe09a32402010-08-15 15:01:51 -0400324{
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200325#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
326 return 0;
327#else
Jens Axboee2598792013-02-24 21:29:35 +0100328 return 1;
Jens Axboe09a32402010-08-15 15:01:51 -0400329#endif
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200330}
331#endif // ARCH_HAVE_CPU_CLOCK
Jens Axboe09a32402010-08-15 15:01:51 -0400332
Jens Axboe67bf9822013-01-10 11:23:19 +0100333#ifndef CONFIG_TLS_THREAD
Jens Axboe5d879392012-12-18 09:12:27 +0100334void fio_local_clock_init(int is_thread)
335{
336 struct tv_valid *t;
337
Jens Axboe572cfb32013-12-06 12:02:10 -0700338 t = calloc(1, sizeof(*t));
Jens Axboe5d879392012-12-18 09:12:27 +0100339 if (pthread_setspecific(tv_tls_key, t))
340 log_err("fio: can't set TLS key\n");
341}
342
343static void kill_tv_tls_key(void *data)
344{
345 free(data);
346}
Jens Axboe67bf9822013-01-10 11:23:19 +0100347#else
348void fio_local_clock_init(int is_thread)
349{
350}
351#endif
Jens Axboe5d879392012-12-18 09:12:27 +0100352
Jens Axboe09a32402010-08-15 15:01:51 -0400353void fio_clock_init(void)
354{
Jens Axboe01423ea2012-12-14 20:37:06 +0100355 if (fio_clock_source == fio_clock_source_inited)
356 return;
357
Jens Axboe67bf9822013-01-10 11:23:19 +0100358#ifndef CONFIG_TLS_THREAD
Jens Axboe5d879392012-12-18 09:12:27 +0100359 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
360 log_err("fio: can't create TLS key\n");
Jens Axboe67bf9822013-01-10 11:23:19 +0100361#endif
Jens Axboe5d879392012-12-18 09:12:27 +0100362
Jens Axboe01423ea2012-12-14 20:37:06 +0100363 fio_clock_source_inited = fio_clock_source;
Jens Axboee2598792013-02-24 21:29:35 +0100364
365 if (calibrate_cpu_clock())
366 tsc_reliable = 0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100367
368 /*
369 * If the arch sets tsc_reliable != 0, then it must be good enough
370 * to use as THE clock source. For x86 CPUs, this means the TSC
371 * runs at a constant rate and is synced across CPU cores.
372 */
373 if (tsc_reliable) {
374 if (!fio_clock_source_set)
375 fio_clock_source = CS_CPUCLOCK;
376 } else if (fio_clock_source == CS_CPUCLOCK)
377 log_info("fio: clocksource=cpu may not be reliable\n");
Jens Axboec223da82010-03-24 13:23:53 +0100378}
379
Jens Axboeaa60bc52013-01-04 13:24:52 +0100380uint64_t utime_since(struct timeval *s, struct timeval *e)
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100381{
Jens Axboe39ab7da2012-11-06 22:10:43 +0100382 long sec, usec;
Jens Axboeaa60bc52013-01-04 13:24:52 +0100383 uint64_t ret;
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100384
Jens Axboe39ab7da2012-11-06 22:10:43 +0100385 sec = e->tv_sec - s->tv_sec;
386 usec = e->tv_usec - s->tv_usec;
387 if (sec > 0 && usec < 0) {
388 sec--;
389 usec += 1000000;
390 }
Jens Axboe783a3eb2012-02-09 10:27:10 +0100391
392 /*
Jens Axboe39ab7da2012-11-06 22:10:43 +0100393 * time warp bug on some kernels?
Jens Axboe783a3eb2012-02-09 10:27:10 +0100394 */
Jens Axboe39ab7da2012-11-06 22:10:43 +0100395 if (sec < 0 || (sec == 0 && usec < 0))
396 return 0;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100397
Jens Axboe39ab7da2012-11-06 22:10:43 +0100398 ret = sec * 1000000ULL + usec;
399
400 return ret;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100401}
402
Jens Axboeaa60bc52013-01-04 13:24:52 +0100403uint64_t utime_since_now(struct timeval *s)
Jens Axboe783a3eb2012-02-09 10:27:10 +0100404{
Jens Axboe39ab7da2012-11-06 22:10:43 +0100405 struct timeval t;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100406
Jens Axboe39ab7da2012-11-06 22:10:43 +0100407 fio_gettime(&t, NULL);
408 return utime_since(s, &t);
409}
Jens Axboe783a3eb2012-02-09 10:27:10 +0100410
Jens Axboeaa60bc52013-01-04 13:24:52 +0100411uint64_t mtime_since(struct timeval *s, struct timeval *e)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100412{
413 long sec, usec, ret;
414
415 sec = e->tv_sec - s->tv_sec;
416 usec = e->tv_usec - s->tv_usec;
417 if (sec > 0 && usec < 0) {
418 sec--;
419 usec += 1000000;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100420 }
421
Jens Axboe39ab7da2012-11-06 22:10:43 +0100422 if (sec < 0 || (sec == 0 && usec < 0))
423 return 0;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100424
Jens Axboe39ab7da2012-11-06 22:10:43 +0100425 sec *= 1000UL;
426 usec /= 1000UL;
427 ret = sec + usec;
428
Jens Axboe783a3eb2012-02-09 10:27:10 +0100429 return ret;
430}
Jens Axboe39ab7da2012-11-06 22:10:43 +0100431
Jens Axboeaa60bc52013-01-04 13:24:52 +0100432uint64_t mtime_since_now(struct timeval *s)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100433{
434 struct timeval t;
435 void *p = __builtin_return_address(0);
436
437 fio_gettime(&t, p);
438 return mtime_since(s, &t);
439}
440
Jens Axboeaa60bc52013-01-04 13:24:52 +0100441uint64_t time_since_now(struct timeval *s)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100442{
443 return mtime_since_now(s) / 1000;
444}
Jens Axboe7d11f872012-12-17 12:03:29 +0100445
Jens Axboe67bf9822013-01-10 11:23:19 +0100446#if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
447 defined(CONFIG_SFAA)
Jens Axboe7d11f872012-12-17 12:03:29 +0100448
449#define CLOCK_ENTRIES 100000
450
451struct clock_entry {
Jens Axboe58002f92013-02-25 10:23:58 +0100452 uint32_t seq;
453 uint32_t cpu;
Jens Axboeba458c22013-01-21 05:38:22 -0700454 uint64_t tsc;
Jens Axboe7d11f872012-12-17 12:03:29 +0100455};
456
457struct clock_thread {
458 pthread_t thread;
459 int cpu;
460 pthread_mutex_t lock;
461 pthread_mutex_t started;
Jens Axboe58002f92013-02-25 10:23:58 +0100462 uint32_t *seq;
Jens Axboe7d11f872012-12-17 12:03:29 +0100463 struct clock_entry *entries;
464};
465
Jens Axboe58002f92013-02-25 10:23:58 +0100466static inline uint32_t atomic32_inc_return(uint32_t *seq)
Jens Axboe7d11f872012-12-17 12:03:29 +0100467{
468 return 1 + __sync_fetch_and_add(seq, 1);
469}
470
471static void *clock_thread_fn(void *data)
472{
473 struct clock_thread *t = data;
474 struct clock_entry *c;
475 os_cpu_mask_t cpu_mask;
Jens Axboe58002f92013-02-25 10:23:58 +0100476 uint32_t last_seq;
Jens Axboe7d11f872012-12-17 12:03:29 +0100477 int i;
478
479 memset(&cpu_mask, 0, sizeof(cpu_mask));
480 fio_cpu_set(&cpu_mask, t->cpu);
481
482 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
483 log_err("clock setaffinity failed\n");
484 return (void *) 1;
485 }
486
Jens Axboe7d11f872012-12-17 12:03:29 +0100487 pthread_mutex_lock(&t->lock);
Jens Axboeb9b34982012-12-17 14:23:47 +0100488 pthread_mutex_unlock(&t->started);
Jens Axboe7d11f872012-12-17 12:03:29 +0100489
Jens Axboe58002f92013-02-25 10:23:58 +0100490 last_seq = 0;
Jens Axboe7d11f872012-12-17 12:03:29 +0100491 c = &t->entries[0];
492 for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
Jens Axboe58002f92013-02-25 10:23:58 +0100493 uint32_t seq;
494 uint64_t tsc;
Jens Axboe7d11f872012-12-17 12:03:29 +0100495
496 c->cpu = t->cpu;
497 do {
Jens Axboe58002f92013-02-25 10:23:58 +0100498 seq = atomic32_inc_return(t->seq);
499 if (seq < last_seq)
500 break;
Jens Axboe7d11f872012-12-17 12:03:29 +0100501 tsc = get_cpu_clock();
502 } while (seq != *t->seq);
503
504 c->seq = seq;
505 c->tsc = tsc;
506 }
507
Jens Axboe0f78b222013-02-26 13:54:20 +0100508 log_info("cs: cpu%3d: %llu clocks seen\n", t->cpu,
509 (unsigned long long) t->entries[i - 1].tsc - t->entries[0].tsc);
Jens Axboe58002f92013-02-25 10:23:58 +0100510
Jens Axboee2598792013-02-24 21:29:35 +0100511 /*
512 * The most common platform clock breakage is returning zero
513 * indefinitely. Check for that and return failure.
514 */
Jens Axboe58002f92013-02-25 10:23:58 +0100515 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
Jens Axboee2598792013-02-24 21:29:35 +0100516 return (void *) 1;
517
Jens Axboe7d11f872012-12-17 12:03:29 +0100518 return NULL;
519}
520
521static int clock_cmp(const void *p1, const void *p2)
522{
523 const struct clock_entry *c1 = p1;
524 const struct clock_entry *c2 = p2;
525
Jens Axboeb9b34982012-12-17 14:23:47 +0100526 if (c1->seq == c2->seq)
527 log_err("cs: bug in atomic sequence!\n");
528
Jens Axboe7d11f872012-12-17 12:03:29 +0100529 return c1->seq - c2->seq;
530}
531
532int fio_monotonic_clocktest(void)
533{
534 struct clock_thread *threads;
535 unsigned int nr_cpus = cpus_online();
536 struct clock_entry *entries;
Jens Axboe814917b2014-04-14 12:20:04 -0600537 unsigned long tentries, failed = 0;
Bruce Cran80da8a82013-02-21 14:16:17 +0100538 struct clock_entry *prev, *this;
Jens Axboe58002f92013-02-25 10:23:58 +0100539 uint32_t seq = 0;
Jens Axboecaa3eb12014-04-14 09:05:46 -0600540 unsigned int i;
Jens Axboe7d11f872012-12-17 12:03:29 +0100541
Jens Axboed5e3f5d2013-01-18 20:13:45 +0100542 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
543
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100544 fio_debug |= 1U << FD_TIME;
545 calibrate_cpu_clock();
546 fio_debug &= ~(1U << FD_TIME);
547
Jens Axboe7d11f872012-12-17 12:03:29 +0100548 threads = malloc(nr_cpus * sizeof(struct clock_thread));
549 tentries = CLOCK_ENTRIES * nr_cpus;
550 entries = malloc(tentries * sizeof(struct clock_entry));
551
552 log_info("cs: Testing %u CPUs\n", nr_cpus);
553
554 for (i = 0; i < nr_cpus; i++) {
555 struct clock_thread *t = &threads[i];
556
557 t->cpu = i;
558 t->seq = &seq;
559 t->entries = &entries[i * CLOCK_ENTRIES];
560 pthread_mutex_init(&t->lock, NULL);
561 pthread_mutex_init(&t->started, NULL);
562 pthread_mutex_lock(&t->lock);
Jens Axboe6b0110c2014-04-14 12:14:17 -0600563 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
564 failed++;
565 nr_cpus = i;
566 break;
567 }
Jens Axboe7d11f872012-12-17 12:03:29 +0100568 }
569
570 for (i = 0; i < nr_cpus; i++) {
571 struct clock_thread *t = &threads[i];
572
573 pthread_mutex_lock(&t->started);
574 }
575
576 for (i = 0; i < nr_cpus; i++) {
577 struct clock_thread *t = &threads[i];
578
579 pthread_mutex_unlock(&t->lock);
580 }
581
Jens Axboe814917b2014-04-14 12:20:04 -0600582 for (i = 0; i < nr_cpus; i++) {
Jens Axboe7d11f872012-12-17 12:03:29 +0100583 struct clock_thread *t = &threads[i];
584 void *ret;
585
586 pthread_join(t->thread, &ret);
587 if (ret)
588 failed++;
589 }
590 free(threads);
591
592 if (failed) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200593 log_err("Clocksource test: %lu threads failed\n", failed);
Jens Axboe7d11f872012-12-17 12:03:29 +0100594 goto err;
595 }
596
597 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
598
599 for (failed = i = 0; i < tentries; i++) {
Bruce Cran80da8a82013-02-21 14:16:17 +0100600 this = &entries[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100601
602 if (!i) {
603 prev = this;
604 continue;
605 }
606
607 if (prev->tsc > this->tsc) {
608 uint64_t diff = prev->tsc - this->tsc;
609
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200610 log_info("cs: CPU clock mismatch (diff=%llu):\n",
611 (unsigned long long) diff);
612 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
613 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
Jens Axboe7d11f872012-12-17 12:03:29 +0100614 failed++;
615 }
616
617 prev = this;
618 }
619
620 if (failed)
621 log_info("cs: Failed: %lu\n", failed);
622 else
623 log_info("cs: Pass!\n");
624
625err:
626 free(entries);
627 return !!failed;
628}
629
630#else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
631
632int fio_monotonic_clocktest(void)
633{
634 log_info("cs: current platform does not support CPU clocks\n");
635 return 0;
636}
637
638#endif