blob: cc9dcb7bea7ab28215e09304b3c32688be8d8f25 [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
Jens Axboe09a32402010-08-15 15:01:51 -040016#ifdef ARCH_HAVE_CPU_CLOCK
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
28static struct tv_valid __thread static_tv_valid;
29#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 Axboe01423ea2012-12-14 20:37:06 +010035enum 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
Jens Axboe71339112012-12-21 22:54:56 +0100180 usecs = (t * inv_cycles_per_usec) / 16777216UL;
Jens Axboec223da82010-03-24 13:23:53 +0100181 tp->tv_sec = usecs / 1000000;
182 tp->tv_usec = usecs % 1000000;
Jens Axboec223da82010-03-24 13:23:53 +0100183 break;
184 }
185#endif
186 default:
187 log_err("fio: invalid clock source %d\n", fio_clock_source);
188 break;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100189 }
Jens Axboe3e488922008-11-13 12:07:20 +0100190
Jens Axboe67bf9822013-01-10 11:23:19 +0100191 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
Jens Axboe3e488922008-11-13 12:07:20 +0100215 /*
216 * If Linux is using the tsc clock on non-synced processors,
217 * sometimes time can appear to drift backwards. Fix that up.
218 */
Jens Axboe93f0b092012-12-18 09:41:59 +0100219 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;
Jens Axboe5d879392012-12-18 09:12:27 +0100226 }
Jens Axboe93f0b092012-12-18 09:41:59 +0100227 tv->last_tv_valid = 1;
228 memcpy(&tv->last_tv, tp, sizeof(*tp));
Jens Axboe3e488922008-11-13 12:07:20 +0100229 }
Jens Axboe02bcaa82006-11-24 10:42:00 +0100230}
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100231
Jens Axboe09a32402010-08-15 15:01:51 -0400232#ifdef ARCH_HAVE_CPU_CLOCK
Jens Axboec223da82010-03-24 13:23:53 +0100233static unsigned long get_cycles_per_usec(void)
234{
235 struct timeval s, e;
Jens Axboeba458c22013-01-21 05:38:22 -0700236 uint64_t c_s, c_e;
Jens Axboe67bf9822013-01-10 11:23:19 +0100237 enum fio_cs old_cs = fio_clock_source;
Jens Axboec223da82010-03-24 13:23:53 +0100238
Jens Axboe67bf9822013-01-10 11:23:19 +0100239#ifdef CONFIG_CLOCK_GETTIME
240 fio_clock_source = CS_CGETTIME;
241#else
242 fio_clock_source = CS_GTOD;
243#endif
244 __fio_gettime(&s);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100245
Jens Axboec223da82010-03-24 13:23:53 +0100246 c_s = get_cpu_clock();
247 do {
Jens Axboeba458c22013-01-21 05:38:22 -0700248 uint64_t elapsed;
Jens Axboec223da82010-03-24 13:23:53 +0100249
Jens Axboe67bf9822013-01-10 11:23:19 +0100250 __fio_gettime(&e);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100251
Jens Axboec223da82010-03-24 13:23:53 +0100252 elapsed = utime_since(&s, &e);
Jens Axboe486332e2012-12-10 08:07:14 +0100253 if (elapsed >= 1280) {
Jens Axboec223da82010-03-24 13:23:53 +0100254 c_e = get_cpu_clock();
255 break;
256 }
257 } while (1);
258
Jens Axboe67bf9822013-01-10 11:23:19 +0100259 fio_clock_source = old_cs;
Jens Axboec0110002012-12-10 08:29:03 +0100260 return (c_e - c_s + 127) >> 7;
Jens Axboec223da82010-03-24 13:23:53 +0100261}
262
Jens Axboefa80fea2012-12-09 20:29:00 +0100263#define NR_TIME_ITERS 50
264
Jens Axboe09a32402010-08-15 15:01:51 -0400265static void calibrate_cpu_clock(void)
Jens Axboec223da82010-03-24 13:23:53 +0100266{
267 double delta, mean, S;
Jens Axboeba458c22013-01-21 05:38:22 -0700268 uint64_t avg, cycles[NR_TIME_ITERS];
Jens Axboec223da82010-03-24 13:23:53 +0100269 int i, samples;
270
Jens Axboec223da82010-03-24 13:23:53 +0100271 cycles[0] = get_cycles_per_usec();
272 S = delta = mean = 0.0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100273 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100274 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
Jens Axboefa80fea2012-12-09 20:29:00 +0100282 S = sqrt(S / (NR_TIME_ITERS - 1.0));
Jens Axboec223da82010-03-24 13:23:53 +0100283
284 samples = avg = 0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100285 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100286 double this = cycles[i];
287
Bruce Cran03e20d62011-01-02 20:14:54 +0100288 if ((fmax(this, mean) - fmin(this, mean)) > S)
Jens Axboec223da82010-03-24 13:23:53 +0100289 continue;
290 samples++;
291 avg += this;
292 }
293
Jens Axboefa80fea2012-12-09 20:29:00 +0100294 S /= (double) NR_TIME_ITERS;
Jens Axboe89db7272012-12-10 08:29:56 +0100295 mean /= 10.0;
Jens Axboec223da82010-03-24 13:23:53 +0100296
Jens Axboefa80fea2012-12-09 20:29:00 +0100297 for (i = 0; i < NR_TIME_ITERS; i++)
Jens Axboec223da82010-03-24 13:23:53 +0100298 dprint(FD_TIME, "cycles[%d]=%lu\n", i, cycles[i] / 10);
299
Jens Axboed7abad32012-12-10 10:15:59 +0100300 avg /= samples;
Jens Axboeb0ff22d2013-01-01 13:38:18 +0100301 avg = (avg + 5) / 10;
Jens Axboec223da82010-03-24 13:23:53 +0100302 dprint(FD_TIME, "avg: %lu\n", avg);
303 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
304
305 cycles_per_usec = avg;
Jens Axboe71339112012-12-21 22:54:56 +0100306 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
307 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
Jens Axboe09a32402010-08-15 15:01:51 -0400308}
309#else
310static void calibrate_cpu_clock(void)
311{
312}
313#endif
314
Jens Axboe67bf9822013-01-10 11:23:19 +0100315#ifndef CONFIG_TLS_THREAD
Jens Axboe5d879392012-12-18 09:12:27 +0100316void fio_local_clock_init(int is_thread)
317{
318 struct tv_valid *t;
319
320 t = calloc(sizeof(*t), 1);
321 if (pthread_setspecific(tv_tls_key, t))
322 log_err("fio: can't set TLS key\n");
323}
324
325static void kill_tv_tls_key(void *data)
326{
327 free(data);
328}
Jens Axboe67bf9822013-01-10 11:23:19 +0100329#else
330void fio_local_clock_init(int is_thread)
331{
332}
333#endif
Jens Axboe5d879392012-12-18 09:12:27 +0100334
Jens Axboe09a32402010-08-15 15:01:51 -0400335void fio_clock_init(void)
336{
Jens Axboe01423ea2012-12-14 20:37:06 +0100337 if (fio_clock_source == fio_clock_source_inited)
338 return;
339
Jens Axboe67bf9822013-01-10 11:23:19 +0100340#ifndef CONFIG_TLS_THREAD
Jens Axboe5d879392012-12-18 09:12:27 +0100341 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
342 log_err("fio: can't create TLS key\n");
Jens Axboe67bf9822013-01-10 11:23:19 +0100343#endif
Jens Axboe5d879392012-12-18 09:12:27 +0100344
Jens Axboe01423ea2012-12-14 20:37:06 +0100345 fio_clock_source_inited = fio_clock_source;
Jens Axboe09a32402010-08-15 15:01:51 -0400346 calibrate_cpu_clock();
Jens Axboefa80fea2012-12-09 20:29:00 +0100347
348 /*
349 * If the arch sets tsc_reliable != 0, then it must be good enough
350 * to use as THE clock source. For x86 CPUs, this means the TSC
351 * runs at a constant rate and is synced across CPU cores.
352 */
353 if (tsc_reliable) {
354 if (!fio_clock_source_set)
355 fio_clock_source = CS_CPUCLOCK;
356 } else if (fio_clock_source == CS_CPUCLOCK)
357 log_info("fio: clocksource=cpu may not be reliable\n");
Jens Axboec223da82010-03-24 13:23:53 +0100358}
359
Jens Axboeaa60bc52013-01-04 13:24:52 +0100360uint64_t utime_since(struct timeval *s, struct timeval *e)
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100361{
Jens Axboe39ab7da2012-11-06 22:10:43 +0100362 long sec, usec;
Jens Axboeaa60bc52013-01-04 13:24:52 +0100363 uint64_t ret;
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100364
Jens Axboe39ab7da2012-11-06 22:10:43 +0100365 sec = e->tv_sec - s->tv_sec;
366 usec = e->tv_usec - s->tv_usec;
367 if (sec > 0 && usec < 0) {
368 sec--;
369 usec += 1000000;
370 }
Jens Axboe783a3eb2012-02-09 10:27:10 +0100371
372 /*
Jens Axboe39ab7da2012-11-06 22:10:43 +0100373 * time warp bug on some kernels?
Jens Axboe783a3eb2012-02-09 10:27:10 +0100374 */
Jens Axboe39ab7da2012-11-06 22:10:43 +0100375 if (sec < 0 || (sec == 0 && usec < 0))
376 return 0;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100377
Jens Axboe39ab7da2012-11-06 22:10:43 +0100378 ret = sec * 1000000ULL + usec;
379
380 return ret;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100381}
382
Jens Axboeaa60bc52013-01-04 13:24:52 +0100383uint64_t utime_since_now(struct timeval *s)
Jens Axboe783a3eb2012-02-09 10:27:10 +0100384{
Jens Axboe39ab7da2012-11-06 22:10:43 +0100385 struct timeval t;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100386
Jens Axboe39ab7da2012-11-06 22:10:43 +0100387 fio_gettime(&t, NULL);
388 return utime_since(s, &t);
389}
Jens Axboe783a3eb2012-02-09 10:27:10 +0100390
Jens Axboeaa60bc52013-01-04 13:24:52 +0100391uint64_t mtime_since(struct timeval *s, struct timeval *e)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100392{
393 long sec, usec, ret;
394
395 sec = e->tv_sec - s->tv_sec;
396 usec = e->tv_usec - s->tv_usec;
397 if (sec > 0 && usec < 0) {
398 sec--;
399 usec += 1000000;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100400 }
401
Jens Axboe39ab7da2012-11-06 22:10:43 +0100402 if (sec < 0 || (sec == 0 && usec < 0))
403 return 0;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100404
Jens Axboe39ab7da2012-11-06 22:10:43 +0100405 sec *= 1000UL;
406 usec /= 1000UL;
407 ret = sec + usec;
408
Jens Axboe783a3eb2012-02-09 10:27:10 +0100409 return ret;
410}
Jens Axboe39ab7da2012-11-06 22:10:43 +0100411
Jens Axboeaa60bc52013-01-04 13:24:52 +0100412uint64_t mtime_since_now(struct timeval *s)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100413{
414 struct timeval t;
415 void *p = __builtin_return_address(0);
416
417 fio_gettime(&t, p);
418 return mtime_since(s, &t);
419}
420
Jens Axboeaa60bc52013-01-04 13:24:52 +0100421uint64_t time_since_now(struct timeval *s)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100422{
423 return mtime_since_now(s) / 1000;
424}
Jens Axboe7d11f872012-12-17 12:03:29 +0100425
Jens Axboe67bf9822013-01-10 11:23:19 +0100426#if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
427 defined(CONFIG_SFAA)
Jens Axboe7d11f872012-12-17 12:03:29 +0100428
429#define CLOCK_ENTRIES 100000
430
431struct clock_entry {
Jens Axboeba458c22013-01-21 05:38:22 -0700432 uint64_t seq;
433 uint64_t tsc;
434 uint64_t cpu;
Jens Axboe7d11f872012-12-17 12:03:29 +0100435};
436
437struct clock_thread {
438 pthread_t thread;
439 int cpu;
440 pthread_mutex_t lock;
441 pthread_mutex_t started;
442 uint64_t *seq;
443 struct clock_entry *entries;
444};
445
446static inline uint64_t atomic64_inc_return(uint64_t *seq)
447{
448 return 1 + __sync_fetch_and_add(seq, 1);
449}
450
451static void *clock_thread_fn(void *data)
452{
453 struct clock_thread *t = data;
454 struct clock_entry *c;
455 os_cpu_mask_t cpu_mask;
456 int i;
457
458 memset(&cpu_mask, 0, sizeof(cpu_mask));
459 fio_cpu_set(&cpu_mask, t->cpu);
460
461 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
462 log_err("clock setaffinity failed\n");
463 return (void *) 1;
464 }
465
Jens Axboe7d11f872012-12-17 12:03:29 +0100466 pthread_mutex_lock(&t->lock);
Jens Axboeb9b34982012-12-17 14:23:47 +0100467 pthread_mutex_unlock(&t->started);
Jens Axboe7d11f872012-12-17 12:03:29 +0100468
469 c = &t->entries[0];
470 for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
471 uint64_t seq, tsc;
472
473 c->cpu = t->cpu;
474 do {
475 seq = atomic64_inc_return(t->seq);
476 tsc = get_cpu_clock();
477 } while (seq != *t->seq);
478
479 c->seq = seq;
480 c->tsc = tsc;
481 }
482
483 log_info("cs: cpu%3d: %lu clocks seen\n", t->cpu, t->entries[CLOCK_ENTRIES - 1].tsc - t->entries[0].tsc);
484 return NULL;
485}
486
487static int clock_cmp(const void *p1, const void *p2)
488{
489 const struct clock_entry *c1 = p1;
490 const struct clock_entry *c2 = p2;
491
Jens Axboeb9b34982012-12-17 14:23:47 +0100492 if (c1->seq == c2->seq)
493 log_err("cs: bug in atomic sequence!\n");
494
Jens Axboe7d11f872012-12-17 12:03:29 +0100495 return c1->seq - c2->seq;
496}
497
498int fio_monotonic_clocktest(void)
499{
500 struct clock_thread *threads;
501 unsigned int nr_cpus = cpus_online();
502 struct clock_entry *entries;
503 unsigned long tentries, failed;
504 uint64_t seq = 0;
505 int i;
506
Jens Axboed5e3f5d2013-01-18 20:13:45 +0100507 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
508
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100509 fio_debug |= 1U << FD_TIME;
510 calibrate_cpu_clock();
511 fio_debug &= ~(1U << FD_TIME);
512
Jens Axboe7d11f872012-12-17 12:03:29 +0100513 threads = malloc(nr_cpus * sizeof(struct clock_thread));
514 tentries = CLOCK_ENTRIES * nr_cpus;
515 entries = malloc(tentries * sizeof(struct clock_entry));
516
517 log_info("cs: Testing %u CPUs\n", nr_cpus);
518
519 for (i = 0; i < nr_cpus; i++) {
520 struct clock_thread *t = &threads[i];
521
522 t->cpu = i;
523 t->seq = &seq;
524 t->entries = &entries[i * CLOCK_ENTRIES];
525 pthread_mutex_init(&t->lock, NULL);
526 pthread_mutex_init(&t->started, NULL);
527 pthread_mutex_lock(&t->lock);
528 pthread_create(&t->thread, NULL, clock_thread_fn, t);
529 }
530
531 for (i = 0; i < nr_cpus; i++) {
532 struct clock_thread *t = &threads[i];
533
534 pthread_mutex_lock(&t->started);
535 }
536
537 for (i = 0; i < nr_cpus; i++) {
538 struct clock_thread *t = &threads[i];
539
540 pthread_mutex_unlock(&t->lock);
541 }
542
543 for (failed = i = 0; i < nr_cpus; i++) {
544 struct clock_thread *t = &threads[i];
545 void *ret;
546
547 pthread_join(t->thread, &ret);
548 if (ret)
549 failed++;
550 }
551 free(threads);
552
553 if (failed) {
554 log_err("Clocksource test: %u threads failed\n", failed);
555 goto err;
556 }
557
558 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
559
560 for (failed = i = 0; i < tentries; i++) {
561 struct clock_entry *prev, *this = &entries[i];
562
563 if (!i) {
564 prev = this;
565 continue;
566 }
567
568 if (prev->tsc > this->tsc) {
569 uint64_t diff = prev->tsc - this->tsc;
570
571 log_info("cs: CPU clock mismatch (diff=%lu):\n", diff);
572 log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", prev->cpu, prev->tsc, prev->seq);
573 log_info("\t CPU%3lu: TSC=%lu, SEQ=%lu\n", this->cpu, this->tsc, this->seq);
574 failed++;
575 }
576
577 prev = this;
578 }
579
580 if (failed)
581 log_info("cs: Failed: %lu\n", failed);
582 else
583 log_info("cs: Pass!\n");
584
585err:
586 free(entries);
587 return !!failed;
588}
589
590#else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
591
592int fio_monotonic_clocktest(void)
593{
594 log_info("cs: current platform does not support CPU clocks\n");
595 return 0;
596}
597
598#endif