blob: e2746711d2e4e2b51b914a98a2c9d30e914beeda [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 {
Jens Axboeba458c22013-01-21 05:38:22 -070023 uint64_t last_cycles;
Jens Axboe02c7e292014-12-16 15:37:25 -070024 int last_tv_valid;
25 int warned;
Jens Axboe5d879392012-12-18 09:12:27 +010026};
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
Jens Axboea7d78f22014-12-16 15:13:45 -0700139static 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 Axboe02c7e292014-12-16 15:37:25 -0700174 if (t < tv->last_cycles && tv->last_tv_valid &&
175 !tv->warned) {
Jens Axboea7d78f22014-12-16 15:13:45 -0700176 log_err("fio: CPU clock going back in time\n");
Jens Axboe02c7e292014-12-16 15:37:25 -0700177 tv->warned = 1;
178 }
Jens Axboec223da82010-03-24 13:23:53 +0100179
Jens Axboea7d78f22014-12-16 15:13:45 -0700180 tv->last_cycles = t;
181 tv->last_tv_valid = 1;
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200182#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
183 usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
184#else
Jens Axboe71339112012-12-21 22:54:56 +0100185 usecs = (t * inv_cycles_per_usec) / 16777216UL;
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200186#endif
Jens Axboec223da82010-03-24 13:23:53 +0100187 tp->tv_sec = usecs / 1000000;
188 tp->tv_usec = usecs % 1000000;
Jens Axboec223da82010-03-24 13:23:53 +0100189 break;
190 }
191#endif
192 default:
193 log_err("fio: invalid clock source %d\n", fio_clock_source);
194 break;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100195 }
Jens Axboe67bf9822013-01-10 11:23:19 +0100196}
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{
Jens Axboe67bf9822013-01-10 11:23:19 +0100204#ifdef FIO_DEBUG_TIME
205 if (!caller)
206 caller = __builtin_return_address(0);
207
208 gtod_log_caller(caller);
209#endif
Jens Axboe04194192014-12-16 19:43:55 -0700210 if (fio_unlikely(fio_gettime_offload(tp)))
Jens Axboe67bf9822013-01-10 11:23:19 +0100211 return;
Jens Axboe67bf9822013-01-10 11:23:19 +0100212
Jens Axboea7d78f22014-12-16 15:13:45 -0700213 __fio_gettime(tp);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100214}
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100215
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200216#if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
Jens Axboec223da82010-03-24 13:23:53 +0100217static unsigned long get_cycles_per_usec(void)
218{
219 struct timeval s, e;
Jens Axboeba458c22013-01-21 05:38:22 -0700220 uint64_t c_s, c_e;
Jens Axboe67bf9822013-01-10 11:23:19 +0100221 enum fio_cs old_cs = fio_clock_source;
Jens Axboec223da82010-03-24 13:23:53 +0100222
Jens Axboe67bf9822013-01-10 11:23:19 +0100223#ifdef CONFIG_CLOCK_GETTIME
224 fio_clock_source = CS_CGETTIME;
225#else
226 fio_clock_source = CS_GTOD;
227#endif
228 __fio_gettime(&s);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100229
Jens Axboec223da82010-03-24 13:23:53 +0100230 c_s = get_cpu_clock();
231 do {
Jens Axboeba458c22013-01-21 05:38:22 -0700232 uint64_t elapsed;
Jens Axboec223da82010-03-24 13:23:53 +0100233
Jens Axboe67bf9822013-01-10 11:23:19 +0100234 __fio_gettime(&e);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100235
Jens Axboec223da82010-03-24 13:23:53 +0100236 elapsed = utime_since(&s, &e);
Jens Axboe486332e2012-12-10 08:07:14 +0100237 if (elapsed >= 1280) {
Jens Axboec223da82010-03-24 13:23:53 +0100238 c_e = get_cpu_clock();
239 break;
240 }
241 } while (1);
242
Jens Axboe67bf9822013-01-10 11:23:19 +0100243 fio_clock_source = old_cs;
Jens Axboec0110002012-12-10 08:29:03 +0100244 return (c_e - c_s + 127) >> 7;
Jens Axboec223da82010-03-24 13:23:53 +0100245}
246
Jens Axboefa80fea2012-12-09 20:29:00 +0100247#define NR_TIME_ITERS 50
248
Jens Axboee2598792013-02-24 21:29:35 +0100249static int calibrate_cpu_clock(void)
Jens Axboec223da82010-03-24 13:23:53 +0100250{
251 double delta, mean, S;
Jens Axboeba458c22013-01-21 05:38:22 -0700252 uint64_t avg, cycles[NR_TIME_ITERS];
Jens Axboec223da82010-03-24 13:23:53 +0100253 int i, samples;
254
Jens Axboec223da82010-03-24 13:23:53 +0100255 cycles[0] = get_cycles_per_usec();
256 S = delta = mean = 0.0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100257 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100258 cycles[i] = get_cycles_per_usec();
259 delta = cycles[i] - mean;
260 if (delta) {
261 mean += delta / (i + 1.0);
262 S += delta * (cycles[i] - mean);
263 }
264 }
265
Jens Axboee2598792013-02-24 21:29:35 +0100266 /*
267 * The most common platform clock breakage is returning zero
268 * indefinitely. Check for that and return failure.
269 */
270 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
271 return 1;
272
Jens Axboefa80fea2012-12-09 20:29:00 +0100273 S = sqrt(S / (NR_TIME_ITERS - 1.0));
Jens Axboec223da82010-03-24 13:23:53 +0100274
275 samples = avg = 0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100276 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100277 double this = cycles[i];
278
Bruce Cran03e20d62011-01-02 20:14:54 +0100279 if ((fmax(this, mean) - fmin(this, mean)) > S)
Jens Axboec223da82010-03-24 13:23:53 +0100280 continue;
281 samples++;
282 avg += this;
283 }
284
Jens Axboefa80fea2012-12-09 20:29:00 +0100285 S /= (double) NR_TIME_ITERS;
Jens Axboe89db7272012-12-10 08:29:56 +0100286 mean /= 10.0;
Jens Axboec223da82010-03-24 13:23:53 +0100287
Jens Axboefa80fea2012-12-09 20:29:00 +0100288 for (i = 0; i < NR_TIME_ITERS; i++)
Jens Axboe4b91ee82013-02-25 10:18:33 +0100289 dprint(FD_TIME, "cycles[%d]=%llu\n", i,
290 (unsigned long long) cycles[i] / 10);
Jens Axboec223da82010-03-24 13:23:53 +0100291
Jens Axboed7abad32012-12-10 10:15:59 +0100292 avg /= samples;
Jens Axboeb0ff22d2013-01-01 13:38:18 +0100293 avg = (avg + 5) / 10;
Jens Axboe4b91ee82013-02-25 10:18:33 +0100294 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
Jens Axboec223da82010-03-24 13:23:53 +0100295 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
296
297 cycles_per_usec = avg;
Jens Axboe71339112012-12-21 22:54:56 +0100298 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
299 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
Jens Axboee2598792013-02-24 21:29:35 +0100300 return 0;
Jens Axboe09a32402010-08-15 15:01:51 -0400301}
302#else
Jens Axboee2598792013-02-24 21:29:35 +0100303static int calibrate_cpu_clock(void)
Jens Axboe09a32402010-08-15 15:01:51 -0400304{
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200305#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
306 return 0;
307#else
Jens Axboee2598792013-02-24 21:29:35 +0100308 return 1;
Jens Axboe09a32402010-08-15 15:01:51 -0400309#endif
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200310}
311#endif // ARCH_HAVE_CPU_CLOCK
Jens Axboe09a32402010-08-15 15:01:51 -0400312
Jens Axboe67bf9822013-01-10 11:23:19 +0100313#ifndef CONFIG_TLS_THREAD
Jens Axboe5d879392012-12-18 09:12:27 +0100314void fio_local_clock_init(int is_thread)
315{
316 struct tv_valid *t;
317
Jens Axboe572cfb32013-12-06 12:02:10 -0700318 t = calloc(1, sizeof(*t));
Jens Axboea7d78f22014-12-16 15:13:45 -0700319 if (pthread_setspecific(tv_tls_key, t)) {
Jens Axboe5d879392012-12-18 09:12:27 +0100320 log_err("fio: can't set TLS key\n");
Jens Axboea7d78f22014-12-16 15:13:45 -0700321 assert(0);
322 }
Jens Axboe5d879392012-12-18 09:12:27 +0100323}
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 Axboee2598792013-02-24 21:29:35 +0100346
347 if (calibrate_cpu_clock())
348 tsc_reliable = 0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100349
350 /*
351 * If the arch sets tsc_reliable != 0, then it must be good enough
352 * to use as THE clock source. For x86 CPUs, this means the TSC
353 * runs at a constant rate and is synced across CPU cores.
354 */
355 if (tsc_reliable) {
356 if (!fio_clock_source_set)
357 fio_clock_source = CS_CPUCLOCK;
358 } else if (fio_clock_source == CS_CPUCLOCK)
359 log_info("fio: clocksource=cpu may not be reliable\n");
Jens Axboec223da82010-03-24 13:23:53 +0100360}
361
Jens Axboe0cbbc392014-09-30 16:04:12 -0600362uint64_t utime_since(const struct timeval *s, const struct timeval *e)
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100363{
Jens Axboe39ab7da2012-11-06 22:10:43 +0100364 long sec, usec;
Jens Axboeaa60bc52013-01-04 13:24:52 +0100365 uint64_t ret;
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100366
Jens Axboe39ab7da2012-11-06 22:10:43 +0100367 sec = e->tv_sec - s->tv_sec;
368 usec = e->tv_usec - s->tv_usec;
369 if (sec > 0 && usec < 0) {
370 sec--;
371 usec += 1000000;
372 }
Jens Axboe783a3eb2012-02-09 10:27:10 +0100373
374 /*
Jens Axboe39ab7da2012-11-06 22:10:43 +0100375 * time warp bug on some kernels?
Jens Axboe783a3eb2012-02-09 10:27:10 +0100376 */
Jens Axboe39ab7da2012-11-06 22:10:43 +0100377 if (sec < 0 || (sec == 0 && usec < 0))
378 return 0;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100379
Jens Axboe39ab7da2012-11-06 22:10:43 +0100380 ret = sec * 1000000ULL + usec;
381
382 return ret;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100383}
384
Jens Axboe0cbbc392014-09-30 16:04:12 -0600385uint64_t utime_since_now(const struct timeval *s)
Jens Axboe783a3eb2012-02-09 10:27:10 +0100386{
Jens Axboe39ab7da2012-11-06 22:10:43 +0100387 struct timeval t;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100388
Jens Axboe39ab7da2012-11-06 22:10:43 +0100389 fio_gettime(&t, NULL);
390 return utime_since(s, &t);
391}
Jens Axboe783a3eb2012-02-09 10:27:10 +0100392
Jens Axboe0cbbc392014-09-30 16:04:12 -0600393uint64_t mtime_since(const struct timeval *s, const struct timeval *e)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100394{
395 long sec, usec, ret;
396
397 sec = e->tv_sec - s->tv_sec;
398 usec = e->tv_usec - s->tv_usec;
399 if (sec > 0 && usec < 0) {
400 sec--;
401 usec += 1000000;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100402 }
403
Jens Axboe39ab7da2012-11-06 22:10:43 +0100404 if (sec < 0 || (sec == 0 && usec < 0))
405 return 0;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100406
Jens Axboe39ab7da2012-11-06 22:10:43 +0100407 sec *= 1000UL;
408 usec /= 1000UL;
409 ret = sec + usec;
410
Jens Axboe783a3eb2012-02-09 10:27:10 +0100411 return ret;
412}
Jens Axboe39ab7da2012-11-06 22:10:43 +0100413
Jens Axboe0cbbc392014-09-30 16:04:12 -0600414uint64_t mtime_since_now(const struct timeval *s)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100415{
416 struct timeval t;
417 void *p = __builtin_return_address(0);
418
419 fio_gettime(&t, p);
420 return mtime_since(s, &t);
421}
422
Jens Axboe0cbbc392014-09-30 16:04:12 -0600423uint64_t time_since_now(const struct timeval *s)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100424{
425 return mtime_since_now(s) / 1000;
426}
Jens Axboe7d11f872012-12-17 12:03:29 +0100427
Jens Axboe67bf9822013-01-10 11:23:19 +0100428#if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
429 defined(CONFIG_SFAA)
Jens Axboe7d11f872012-12-17 12:03:29 +0100430
431#define CLOCK_ENTRIES 100000
432
433struct clock_entry {
Jens Axboe58002f92013-02-25 10:23:58 +0100434 uint32_t seq;
435 uint32_t cpu;
Jens Axboeba458c22013-01-21 05:38:22 -0700436 uint64_t tsc;
Jens Axboe7d11f872012-12-17 12:03:29 +0100437};
438
439struct clock_thread {
440 pthread_t thread;
441 int cpu;
442 pthread_mutex_t lock;
443 pthread_mutex_t started;
Jens Axboe58002f92013-02-25 10:23:58 +0100444 uint32_t *seq;
Jens Axboe7d11f872012-12-17 12:03:29 +0100445 struct clock_entry *entries;
446};
447
Jens Axboe58002f92013-02-25 10:23:58 +0100448static inline uint32_t atomic32_inc_return(uint32_t *seq)
Jens Axboe7d11f872012-12-17 12:03:29 +0100449{
450 return 1 + __sync_fetch_and_add(seq, 1);
451}
452
453static void *clock_thread_fn(void *data)
454{
455 struct clock_thread *t = data;
456 struct clock_entry *c;
457 os_cpu_mask_t cpu_mask;
Jens Axboe58002f92013-02-25 10:23:58 +0100458 uint32_t last_seq;
Jens Axboe7d11f872012-12-17 12:03:29 +0100459 int i;
460
461 memset(&cpu_mask, 0, sizeof(cpu_mask));
462 fio_cpu_set(&cpu_mask, t->cpu);
463
464 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
465 log_err("clock setaffinity failed\n");
466 return (void *) 1;
467 }
468
Jens Axboe7d11f872012-12-17 12:03:29 +0100469 pthread_mutex_lock(&t->lock);
Jens Axboeb9b34982012-12-17 14:23:47 +0100470 pthread_mutex_unlock(&t->started);
Jens Axboe7d11f872012-12-17 12:03:29 +0100471
Jens Axboe58002f92013-02-25 10:23:58 +0100472 last_seq = 0;
Jens Axboe7d11f872012-12-17 12:03:29 +0100473 c = &t->entries[0];
474 for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
Jens Axboe58002f92013-02-25 10:23:58 +0100475 uint32_t seq;
476 uint64_t tsc;
Jens Axboe7d11f872012-12-17 12:03:29 +0100477
478 c->cpu = t->cpu;
479 do {
Jens Axboe58002f92013-02-25 10:23:58 +0100480 seq = atomic32_inc_return(t->seq);
481 if (seq < last_seq)
482 break;
Jens Axboe7d11f872012-12-17 12:03:29 +0100483 tsc = get_cpu_clock();
484 } while (seq != *t->seq);
485
486 c->seq = seq;
487 c->tsc = tsc;
488 }
489
Jens Axboe0f78b222013-02-26 13:54:20 +0100490 log_info("cs: cpu%3d: %llu clocks seen\n", t->cpu,
491 (unsigned long long) t->entries[i - 1].tsc - t->entries[0].tsc);
Jens Axboe58002f92013-02-25 10:23:58 +0100492
Jens Axboee2598792013-02-24 21:29:35 +0100493 /*
494 * The most common platform clock breakage is returning zero
495 * indefinitely. Check for that and return failure.
496 */
Jens Axboe58002f92013-02-25 10:23:58 +0100497 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
Jens Axboee2598792013-02-24 21:29:35 +0100498 return (void *) 1;
499
Jens Axboe7d11f872012-12-17 12:03:29 +0100500 return NULL;
501}
502
503static int clock_cmp(const void *p1, const void *p2)
504{
505 const struct clock_entry *c1 = p1;
506 const struct clock_entry *c2 = p2;
507
Jens Axboeb9b34982012-12-17 14:23:47 +0100508 if (c1->seq == c2->seq)
509 log_err("cs: bug in atomic sequence!\n");
510
Jens Axboe7d11f872012-12-17 12:03:29 +0100511 return c1->seq - c2->seq;
512}
513
514int fio_monotonic_clocktest(void)
515{
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700516 struct clock_thread *cthreads;
Jens Axboe7d11f872012-12-17 12:03:29 +0100517 unsigned int nr_cpus = cpus_online();
518 struct clock_entry *entries;
Jens Axboe814917b2014-04-14 12:20:04 -0600519 unsigned long tentries, failed = 0;
Bruce Cran80da8a82013-02-21 14:16:17 +0100520 struct clock_entry *prev, *this;
Jens Axboe58002f92013-02-25 10:23:58 +0100521 uint32_t seq = 0;
Jens Axboecaa3eb12014-04-14 09:05:46 -0600522 unsigned int i;
Jens Axboe7d11f872012-12-17 12:03:29 +0100523
Jens Axboed5e3f5d2013-01-18 20:13:45 +0100524 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
525
Jens Axboefc3f3e42014-09-24 09:54:24 -0600526#ifdef FIO_INC_DEBUG
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100527 fio_debug |= 1U << FD_TIME;
Jens Axboefc3f3e42014-09-24 09:54:24 -0600528#endif
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100529 calibrate_cpu_clock();
Jens Axboefc3f3e42014-09-24 09:54:24 -0600530#ifdef FIO_INC_DEBUG
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100531 fio_debug &= ~(1U << FD_TIME);
Jens Axboefc3f3e42014-09-24 09:54:24 -0600532#endif
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100533
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700534 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
Jens Axboe7d11f872012-12-17 12:03:29 +0100535 tentries = CLOCK_ENTRIES * nr_cpus;
536 entries = malloc(tentries * sizeof(struct clock_entry));
537
538 log_info("cs: Testing %u CPUs\n", nr_cpus);
539
540 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700541 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100542
543 t->cpu = i;
544 t->seq = &seq;
545 t->entries = &entries[i * CLOCK_ENTRIES];
546 pthread_mutex_init(&t->lock, NULL);
547 pthread_mutex_init(&t->started, NULL);
548 pthread_mutex_lock(&t->lock);
Jens Axboe6b0110c2014-04-14 12:14:17 -0600549 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
550 failed++;
551 nr_cpus = i;
552 break;
553 }
Jens Axboe7d11f872012-12-17 12:03:29 +0100554 }
555
556 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700557 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100558
559 pthread_mutex_lock(&t->started);
560 }
561
562 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700563 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100564
565 pthread_mutex_unlock(&t->lock);
566 }
567
Jens Axboe814917b2014-04-14 12:20:04 -0600568 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700569 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100570 void *ret;
571
572 pthread_join(t->thread, &ret);
573 if (ret)
574 failed++;
575 }
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700576 free(cthreads);
Jens Axboe7d11f872012-12-17 12:03:29 +0100577
578 if (failed) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200579 log_err("Clocksource test: %lu threads failed\n", failed);
Jens Axboe7d11f872012-12-17 12:03:29 +0100580 goto err;
581 }
582
583 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
584
585 for (failed = i = 0; i < tentries; i++) {
Bruce Cran80da8a82013-02-21 14:16:17 +0100586 this = &entries[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100587
588 if (!i) {
589 prev = this;
590 continue;
591 }
592
593 if (prev->tsc > this->tsc) {
594 uint64_t diff = prev->tsc - this->tsc;
595
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200596 log_info("cs: CPU clock mismatch (diff=%llu):\n",
597 (unsigned long long) diff);
598 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
599 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 +0100600 failed++;
601 }
602
603 prev = this;
604 }
605
606 if (failed)
607 log_info("cs: Failed: %lu\n", failed);
608 else
609 log_info("cs: Pass!\n");
610
611err:
612 free(entries);
613 return !!failed;
614}
615
616#else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
617
618int fio_monotonic_clocktest(void)
619{
620 log_info("cs: current platform does not support CPU clocks\n");
621 return 0;
622}
623
624#endif