blob: 30513e03162187c3154b9b9097a201b731bbdde9 [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 Axboe400531d2014-12-16 22:40:37 -070019static uint64_t max_cycles_for_mult;
Jens Axboe09a32402010-08-15 15:01:51 -040020#endif
Jens Axboe4de98eb2013-01-01 10:59:04 +010021int tsc_reliable = 0;
Jens Axboe5d879392012-12-18 09:12:27 +010022
23struct tv_valid {
Jens Axboeba458c22013-01-21 05:38:22 -070024 uint64_t last_cycles;
Jens Axboe02c7e292014-12-16 15:37:25 -070025 int last_tv_valid;
26 int warned;
Jens Axboe5d879392012-12-18 09:12:27 +010027};
Jens Axboe67bf9822013-01-10 11:23:19 +010028#ifdef CONFIG_TLS_THREAD
Jens Axboeb4ea84d2013-04-11 14:20:33 +020029static __thread struct tv_valid static_tv_valid;
Jens Axboe67bf9822013-01-10 11:23:19 +010030#else
Jens Axboe5d879392012-12-18 09:12:27 +010031static pthread_key_t tv_tls_key;
Jens Axboe67bf9822013-01-10 11:23:19 +010032#endif
Jens Axboe02bcaa82006-11-24 10:42:00 +010033
Bruce Cran16de1bf2012-02-20 17:07:32 +000034enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
Jens Axboefa80fea2012-12-09 20:29:00 +010035int fio_clock_source_set = 0;
Jens Axboe10aa1362014-04-01 21:10:36 -060036static enum fio_cs fio_clock_source_inited = CS_INVAL;
Jens Axboec223da82010-03-24 13:23:53 +010037
Jens Axboe02bcaa82006-11-24 10:42:00 +010038#ifdef FIO_DEBUG_TIME
39
40#define HASH_BITS 8
41#define HASH_SIZE (1 << HASH_BITS)
42
Jens Axboe01743ee2008-06-02 12:19:19 +020043static struct flist_head hash[HASH_SIZE];
Jens Axboe02bcaa82006-11-24 10:42:00 +010044static int gtod_inited;
45
46struct gtod_log {
Jens Axboe01743ee2008-06-02 12:19:19 +020047 struct flist_head list;
Jens Axboe02bcaa82006-11-24 10:42:00 +010048 void *caller;
49 unsigned long calls;
50};
51
52static struct gtod_log *find_hash(void *caller)
53{
54 unsigned long h = hash_ptr(caller, HASH_BITS);
Jens Axboe01743ee2008-06-02 12:19:19 +020055 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +010056
Jens Axboe01743ee2008-06-02 12:19:19 +020057 flist_for_each(entry, &hash[h]) {
58 struct gtod_log *log = flist_entry(entry, struct gtod_log,
59 list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010060
61 if (log->caller == caller)
62 return log;
63 }
64
65 return NULL;
66}
67
68static struct gtod_log *find_log(void *caller)
69{
70 struct gtod_log *log = find_hash(caller);
71
72 if (!log) {
73 unsigned long h;
74
75 log = malloc(sizeof(*log));
Jens Axboe01743ee2008-06-02 12:19:19 +020076 INIT_FLIST_HEAD(&log->list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010077 log->caller = caller;
78 log->calls = 0;
79
80 h = hash_ptr(caller, HASH_BITS);
Jens Axboe01743ee2008-06-02 12:19:19 +020081 flist_add_tail(&log->list, &hash[h]);
Jens Axboe02bcaa82006-11-24 10:42:00 +010082 }
83
84 return log;
85}
86
87static void gtod_log_caller(void *caller)
88{
89 if (gtod_inited) {
90 struct gtod_log *log = find_log(caller);
91
92 log->calls++;
93 }
94}
95
96static void fio_exit fio_dump_gtod(void)
97{
98 unsigned long total_calls = 0;
99 int i;
100
101 for (i = 0; i < HASH_SIZE; i++) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200102 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100103 struct gtod_log *log;
104
Jens Axboe01743ee2008-06-02 12:19:19 +0200105 flist_for_each(entry, &hash[i]) {
106 log = flist_entry(entry, struct gtod_log, list);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100107
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100108 printf("function %p, calls %lu\n", log->caller,
109 log->calls);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100110 total_calls += log->calls;
111 }
112 }
113
114 printf("Total %lu gettimeofday\n", total_calls);
115}
116
117static void fio_init gtod_init(void)
118{
119 int i;
120
121 for (i = 0; i < HASH_SIZE; i++)
Jens Axboe01743ee2008-06-02 12:19:19 +0200122 INIT_FLIST_HEAD(&hash[i]);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100123
124 gtod_inited = 1;
125}
126
127#endif /* FIO_DEBUG_TIME */
128
Jens Axboe67bf9822013-01-10 11:23:19 +0100129#ifdef CONFIG_CLOCK_GETTIME
Jens Axboe9ff1c072012-12-20 15:17:57 +0100130static int fill_clock_gettime(struct timespec *ts)
131{
Jens Axboe67bf9822013-01-10 11:23:19 +0100132#ifdef CONFIG_CLOCK_MONOTONIC
Jens Axboe9ff1c072012-12-20 15:17:57 +0100133 return clock_gettime(CLOCK_MONOTONIC, ts);
134#else
135 return clock_gettime(CLOCK_REALTIME, ts);
136#endif
137}
Jens Axboe1e97cce2006-12-05 11:44:16 +0100138#endif
Jens Axboe67bf9822013-01-10 11:23:19 +0100139
Jens Axboea7d78f22014-12-16 15:13:45 -0700140static void __fio_gettime(struct timeval *tp)
Jens Axboe02bcaa82006-11-24 10:42:00 +0100141{
Jens Axboe93f0b092012-12-18 09:41:59 +0100142 struct tv_valid *tv;
Jens Axboe5d879392012-12-18 09:12:27 +0100143
Jens Axboe67bf9822013-01-10 11:23:19 +0100144#ifdef CONFIG_TLS_THREAD
145 tv = &static_tv_valid;
146#else
Jens Axboe93f0b092012-12-18 09:41:59 +0100147 tv = pthread_getspecific(tv_tls_key);
Jens Axboe67bf9822013-01-10 11:23:19 +0100148#endif
Jens Axboe93f0b092012-12-18 09:41:59 +0100149
Jens Axboec223da82010-03-24 13:23:53 +0100150 switch (fio_clock_source) {
Jens Axboe67bf9822013-01-10 11:23:19 +0100151#ifdef CONFIG_GETTIMEOFDAY
Jens Axboec223da82010-03-24 13:23:53 +0100152 case CS_GTOD:
Jens Axboe02bcaa82006-11-24 10:42:00 +0100153 gettimeofday(tp, NULL);
Jens Axboec223da82010-03-24 13:23:53 +0100154 break;
Jens Axboe67bf9822013-01-10 11:23:19 +0100155#endif
156#ifdef CONFIG_CLOCK_GETTIME
Jens Axboec223da82010-03-24 13:23:53 +0100157 case CS_CGETTIME: {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100158 struct timespec ts;
159
Jens Axboe9ff1c072012-12-20 15:17:57 +0100160 if (fill_clock_gettime(&ts) < 0) {
Jens Axboec223da82010-03-24 13:23:53 +0100161 log_err("fio: clock_gettime fails\n");
162 assert(0);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100163 }
164
165 tp->tv_sec = ts.tv_sec;
166 tp->tv_usec = ts.tv_nsec / 1000;
Jens Axboec223da82010-03-24 13:23:53 +0100167 break;
168 }
Jens Axboe67bf9822013-01-10 11:23:19 +0100169#endif
Jens Axboec223da82010-03-24 13:23:53 +0100170#ifdef ARCH_HAVE_CPU_CLOCK
171 case CS_CPUCLOCK: {
Jens Axboeba458c22013-01-21 05:38:22 -0700172 uint64_t usecs, t;
Jens Axboec223da82010-03-24 13:23:53 +0100173
174 t = get_cpu_clock();
Jens Axboe02c7e292014-12-16 15:37:25 -0700175 if (t < tv->last_cycles && tv->last_tv_valid &&
176 !tv->warned) {
Jens Axboea7d78f22014-12-16 15:13:45 -0700177 log_err("fio: CPU clock going back in time\n");
Jens Axboe02c7e292014-12-16 15:37:25 -0700178 tv->warned = 1;
179 }
Jens Axboec223da82010-03-24 13:23:53 +0100180
Jens Axboea7d78f22014-12-16 15:13:45 -0700181 tv->last_cycles = t;
182 tv->last_tv_valid = 1;
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200183#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
184 usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
185#else
Jens Axboe400531d2014-12-16 22:40:37 -0700186 if (t < max_cycles_for_mult)
187 usecs = (t * inv_cycles_per_usec) / 16777216UL;
188 else
189 usecs = t / cycles_per_usec;
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200190#endif
Jens Axboec223da82010-03-24 13:23:53 +0100191 tp->tv_sec = usecs / 1000000;
192 tp->tv_usec = usecs % 1000000;
Jens Axboec223da82010-03-24 13:23:53 +0100193 break;
194 }
195#endif
196 default:
197 log_err("fio: invalid clock source %d\n", fio_clock_source);
198 break;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100199 }
Jens Axboe67bf9822013-01-10 11:23:19 +0100200}
201
202#ifdef FIO_DEBUG_TIME
203void fio_gettime(struct timeval *tp, void *caller)
204#else
205void fio_gettime(struct timeval *tp, void fio_unused *caller)
206#endif
207{
Jens Axboe67bf9822013-01-10 11:23:19 +0100208#ifdef FIO_DEBUG_TIME
209 if (!caller)
210 caller = __builtin_return_address(0);
211
212 gtod_log_caller(caller);
213#endif
Jens Axboe04194192014-12-16 19:43:55 -0700214 if (fio_unlikely(fio_gettime_offload(tp)))
Jens Axboe67bf9822013-01-10 11:23:19 +0100215 return;
Jens Axboe67bf9822013-01-10 11:23:19 +0100216
Jens Axboea7d78f22014-12-16 15:13:45 -0700217 __fio_gettime(tp);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100218}
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100219
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200220#if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
Jens Axboec223da82010-03-24 13:23:53 +0100221static unsigned long get_cycles_per_usec(void)
222{
223 struct timeval s, e;
Jens Axboeba458c22013-01-21 05:38:22 -0700224 uint64_t c_s, c_e;
Jens Axboe67bf9822013-01-10 11:23:19 +0100225 enum fio_cs old_cs = fio_clock_source;
Jens Axboec223da82010-03-24 13:23:53 +0100226
Jens Axboe67bf9822013-01-10 11:23:19 +0100227#ifdef CONFIG_CLOCK_GETTIME
228 fio_clock_source = CS_CGETTIME;
229#else
230 fio_clock_source = CS_GTOD;
231#endif
232 __fio_gettime(&s);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100233
Jens Axboec223da82010-03-24 13:23:53 +0100234 c_s = get_cpu_clock();
235 do {
Jens Axboeba458c22013-01-21 05:38:22 -0700236 uint64_t elapsed;
Jens Axboec223da82010-03-24 13:23:53 +0100237
Jens Axboe67bf9822013-01-10 11:23:19 +0100238 __fio_gettime(&e);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100239
Jens Axboec223da82010-03-24 13:23:53 +0100240 elapsed = utime_since(&s, &e);
Jens Axboe486332e2012-12-10 08:07:14 +0100241 if (elapsed >= 1280) {
Jens Axboec223da82010-03-24 13:23:53 +0100242 c_e = get_cpu_clock();
243 break;
244 }
245 } while (1);
246
Jens Axboe67bf9822013-01-10 11:23:19 +0100247 fio_clock_source = old_cs;
Jens Axboec0110002012-12-10 08:29:03 +0100248 return (c_e - c_s + 127) >> 7;
Jens Axboec223da82010-03-24 13:23:53 +0100249}
250
Jens Axboefa80fea2012-12-09 20:29:00 +0100251#define NR_TIME_ITERS 50
252
Jens Axboee2598792013-02-24 21:29:35 +0100253static int calibrate_cpu_clock(void)
Jens Axboec223da82010-03-24 13:23:53 +0100254{
255 double delta, mean, S;
Jens Axboeba458c22013-01-21 05:38:22 -0700256 uint64_t avg, cycles[NR_TIME_ITERS];
Jens Axboec223da82010-03-24 13:23:53 +0100257 int i, samples;
258
Jens Axboec223da82010-03-24 13:23:53 +0100259 cycles[0] = get_cycles_per_usec();
260 S = delta = mean = 0.0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100261 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100262 cycles[i] = get_cycles_per_usec();
263 delta = cycles[i] - mean;
264 if (delta) {
265 mean += delta / (i + 1.0);
266 S += delta * (cycles[i] - mean);
267 }
268 }
269
Jens Axboee2598792013-02-24 21:29:35 +0100270 /*
271 * The most common platform clock breakage is returning zero
272 * indefinitely. Check for that and return failure.
273 */
274 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
275 return 1;
276
Jens Axboefa80fea2012-12-09 20:29:00 +0100277 S = sqrt(S / (NR_TIME_ITERS - 1.0));
Jens Axboec223da82010-03-24 13:23:53 +0100278
279 samples = avg = 0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100280 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100281 double this = cycles[i];
282
Bruce Cran03e20d62011-01-02 20:14:54 +0100283 if ((fmax(this, mean) - fmin(this, mean)) > S)
Jens Axboec223da82010-03-24 13:23:53 +0100284 continue;
285 samples++;
286 avg += this;
287 }
288
Jens Axboefa80fea2012-12-09 20:29:00 +0100289 S /= (double) NR_TIME_ITERS;
Jens Axboe89db7272012-12-10 08:29:56 +0100290 mean /= 10.0;
Jens Axboec223da82010-03-24 13:23:53 +0100291
Jens Axboefa80fea2012-12-09 20:29:00 +0100292 for (i = 0; i < NR_TIME_ITERS; i++)
Jens Axboe4b91ee82013-02-25 10:18:33 +0100293 dprint(FD_TIME, "cycles[%d]=%llu\n", i,
294 (unsigned long long) cycles[i] / 10);
Jens Axboec223da82010-03-24 13:23:53 +0100295
Jens Axboed7abad32012-12-10 10:15:59 +0100296 avg /= samples;
Jens Axboeb0ff22d2013-01-01 13:38:18 +0100297 avg = (avg + 5) / 10;
Jens Axboe4b91ee82013-02-25 10:18:33 +0100298 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
Jens Axboec223da82010-03-24 13:23:53 +0100299 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
300
301 cycles_per_usec = avg;
Jens Axboe71339112012-12-21 22:54:56 +0100302 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
Jens Axboe400531d2014-12-16 22:40:37 -0700303 max_cycles_for_mult = ~0ULL / inv_cycles_per_usec;
Jens Axboe71339112012-12-21 22:54:56 +0100304 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
Jens Axboee2598792013-02-24 21:29:35 +0100305 return 0;
Jens Axboe09a32402010-08-15 15:01:51 -0400306}
307#else
Jens Axboee2598792013-02-24 21:29:35 +0100308static int calibrate_cpu_clock(void)
Jens Axboe09a32402010-08-15 15:01:51 -0400309{
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200310#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
311 return 0;
312#else
Jens Axboee2598792013-02-24 21:29:35 +0100313 return 1;
Jens Axboe09a32402010-08-15 15:01:51 -0400314#endif
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200315}
316#endif // ARCH_HAVE_CPU_CLOCK
Jens Axboe09a32402010-08-15 15:01:51 -0400317
Jens Axboe67bf9822013-01-10 11:23:19 +0100318#ifndef CONFIG_TLS_THREAD
Jens Axboe5d879392012-12-18 09:12:27 +0100319void fio_local_clock_init(int is_thread)
320{
321 struct tv_valid *t;
322
Jens Axboe572cfb32013-12-06 12:02:10 -0700323 t = calloc(1, sizeof(*t));
Jens Axboea7d78f22014-12-16 15:13:45 -0700324 if (pthread_setspecific(tv_tls_key, t)) {
Jens Axboe5d879392012-12-18 09:12:27 +0100325 log_err("fio: can't set TLS key\n");
Jens Axboea7d78f22014-12-16 15:13:45 -0700326 assert(0);
327 }
Jens Axboe5d879392012-12-18 09:12:27 +0100328}
329
330static void kill_tv_tls_key(void *data)
331{
332 free(data);
333}
Jens Axboe67bf9822013-01-10 11:23:19 +0100334#else
335void fio_local_clock_init(int is_thread)
336{
337}
338#endif
Jens Axboe5d879392012-12-18 09:12:27 +0100339
Jens Axboe09a32402010-08-15 15:01:51 -0400340void fio_clock_init(void)
341{
Jens Axboe01423ea2012-12-14 20:37:06 +0100342 if (fio_clock_source == fio_clock_source_inited)
343 return;
344
Jens Axboe67bf9822013-01-10 11:23:19 +0100345#ifndef CONFIG_TLS_THREAD
Jens Axboe5d879392012-12-18 09:12:27 +0100346 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
347 log_err("fio: can't create TLS key\n");
Jens Axboe67bf9822013-01-10 11:23:19 +0100348#endif
Jens Axboe5d879392012-12-18 09:12:27 +0100349
Jens Axboe01423ea2012-12-14 20:37:06 +0100350 fio_clock_source_inited = fio_clock_source;
Jens Axboee2598792013-02-24 21:29:35 +0100351
352 if (calibrate_cpu_clock())
353 tsc_reliable = 0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100354
355 /*
356 * If the arch sets tsc_reliable != 0, then it must be good enough
357 * to use as THE clock source. For x86 CPUs, this means the TSC
358 * runs at a constant rate and is synced across CPU cores.
359 */
360 if (tsc_reliable) {
361 if (!fio_clock_source_set)
362 fio_clock_source = CS_CPUCLOCK;
363 } else if (fio_clock_source == CS_CPUCLOCK)
364 log_info("fio: clocksource=cpu may not be reliable\n");
Jens Axboec223da82010-03-24 13:23:53 +0100365}
366
Jens Axboe0cbbc392014-09-30 16:04:12 -0600367uint64_t utime_since(const struct timeval *s, const struct timeval *e)
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100368{
Jens Axboe39ab7da2012-11-06 22:10:43 +0100369 long sec, usec;
Jens Axboeaa60bc52013-01-04 13:24:52 +0100370 uint64_t ret;
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100371
Jens Axboe39ab7da2012-11-06 22:10:43 +0100372 sec = e->tv_sec - s->tv_sec;
373 usec = e->tv_usec - s->tv_usec;
374 if (sec > 0 && usec < 0) {
375 sec--;
376 usec += 1000000;
377 }
Jens Axboe783a3eb2012-02-09 10:27:10 +0100378
379 /*
Jens Axboe39ab7da2012-11-06 22:10:43 +0100380 * time warp bug on some kernels?
Jens Axboe783a3eb2012-02-09 10:27:10 +0100381 */
Jens Axboe39ab7da2012-11-06 22:10:43 +0100382 if (sec < 0 || (sec == 0 && usec < 0))
383 return 0;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100384
Jens Axboe39ab7da2012-11-06 22:10:43 +0100385 ret = sec * 1000000ULL + usec;
386
387 return ret;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100388}
389
Jens Axboe0cbbc392014-09-30 16:04:12 -0600390uint64_t utime_since_now(const struct timeval *s)
Jens Axboe783a3eb2012-02-09 10:27:10 +0100391{
Jens Axboe39ab7da2012-11-06 22:10:43 +0100392 struct timeval t;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100393
Jens Axboe39ab7da2012-11-06 22:10:43 +0100394 fio_gettime(&t, NULL);
395 return utime_since(s, &t);
396}
Jens Axboe783a3eb2012-02-09 10:27:10 +0100397
Jens Axboe0cbbc392014-09-30 16:04:12 -0600398uint64_t mtime_since(const struct timeval *s, const struct timeval *e)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100399{
400 long sec, usec, ret;
401
402 sec = e->tv_sec - s->tv_sec;
403 usec = e->tv_usec - s->tv_usec;
404 if (sec > 0 && usec < 0) {
405 sec--;
406 usec += 1000000;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100407 }
408
Jens Axboe39ab7da2012-11-06 22:10:43 +0100409 if (sec < 0 || (sec == 0 && usec < 0))
410 return 0;
Jens Axboe783a3eb2012-02-09 10:27:10 +0100411
Jens Axboe39ab7da2012-11-06 22:10:43 +0100412 sec *= 1000UL;
413 usec /= 1000UL;
414 ret = sec + usec;
415
Jens Axboe783a3eb2012-02-09 10:27:10 +0100416 return ret;
417}
Jens Axboe39ab7da2012-11-06 22:10:43 +0100418
Jens Axboe0cbbc392014-09-30 16:04:12 -0600419uint64_t mtime_since_now(const struct timeval *s)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100420{
421 struct timeval t;
422 void *p = __builtin_return_address(0);
423
424 fio_gettime(&t, p);
425 return mtime_since(s, &t);
426}
427
Jens Axboe0cbbc392014-09-30 16:04:12 -0600428uint64_t time_since_now(const struct timeval *s)
Jens Axboe39ab7da2012-11-06 22:10:43 +0100429{
430 return mtime_since_now(s) / 1000;
431}
Jens Axboe7d11f872012-12-17 12:03:29 +0100432
Jens Axboe67bf9822013-01-10 11:23:19 +0100433#if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \
434 defined(CONFIG_SFAA)
Jens Axboe7d11f872012-12-17 12:03:29 +0100435
436#define CLOCK_ENTRIES 100000
437
438struct clock_entry {
Jens Axboe58002f92013-02-25 10:23:58 +0100439 uint32_t seq;
440 uint32_t cpu;
Jens Axboeba458c22013-01-21 05:38:22 -0700441 uint64_t tsc;
Jens Axboe7d11f872012-12-17 12:03:29 +0100442};
443
444struct clock_thread {
445 pthread_t thread;
446 int cpu;
447 pthread_mutex_t lock;
448 pthread_mutex_t started;
Jens Axboe58002f92013-02-25 10:23:58 +0100449 uint32_t *seq;
Jens Axboe7d11f872012-12-17 12:03:29 +0100450 struct clock_entry *entries;
451};
452
Jens Axboe58002f92013-02-25 10:23:58 +0100453static inline uint32_t atomic32_inc_return(uint32_t *seq)
Jens Axboe7d11f872012-12-17 12:03:29 +0100454{
455 return 1 + __sync_fetch_and_add(seq, 1);
456}
457
458static void *clock_thread_fn(void *data)
459{
460 struct clock_thread *t = data;
461 struct clock_entry *c;
462 os_cpu_mask_t cpu_mask;
Jens Axboe58002f92013-02-25 10:23:58 +0100463 uint32_t last_seq;
Jens Axboe7d11f872012-12-17 12:03:29 +0100464 int i;
465
466 memset(&cpu_mask, 0, sizeof(cpu_mask));
467 fio_cpu_set(&cpu_mask, t->cpu);
468
469 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
470 log_err("clock setaffinity failed\n");
471 return (void *) 1;
472 }
473
Jens Axboe7d11f872012-12-17 12:03:29 +0100474 pthread_mutex_lock(&t->lock);
Jens Axboeb9b34982012-12-17 14:23:47 +0100475 pthread_mutex_unlock(&t->started);
Jens Axboe7d11f872012-12-17 12:03:29 +0100476
Jens Axboe58002f92013-02-25 10:23:58 +0100477 last_seq = 0;
Jens Axboe7d11f872012-12-17 12:03:29 +0100478 c = &t->entries[0];
479 for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
Jens Axboe58002f92013-02-25 10:23:58 +0100480 uint32_t seq;
481 uint64_t tsc;
Jens Axboe7d11f872012-12-17 12:03:29 +0100482
483 c->cpu = t->cpu;
484 do {
Jens Axboe58002f92013-02-25 10:23:58 +0100485 seq = atomic32_inc_return(t->seq);
486 if (seq < last_seq)
487 break;
Jens Axboe7d11f872012-12-17 12:03:29 +0100488 tsc = get_cpu_clock();
489 } while (seq != *t->seq);
490
491 c->seq = seq;
492 c->tsc = tsc;
493 }
494
Jens Axboe0f78b222013-02-26 13:54:20 +0100495 log_info("cs: cpu%3d: %llu clocks seen\n", t->cpu,
496 (unsigned long long) t->entries[i - 1].tsc - t->entries[0].tsc);
Jens Axboe58002f92013-02-25 10:23:58 +0100497
Jens Axboee2598792013-02-24 21:29:35 +0100498 /*
499 * The most common platform clock breakage is returning zero
500 * indefinitely. Check for that and return failure.
501 */
Jens Axboe58002f92013-02-25 10:23:58 +0100502 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
Jens Axboee2598792013-02-24 21:29:35 +0100503 return (void *) 1;
504
Jens Axboe7d11f872012-12-17 12:03:29 +0100505 return NULL;
506}
507
508static int clock_cmp(const void *p1, const void *p2)
509{
510 const struct clock_entry *c1 = p1;
511 const struct clock_entry *c2 = p2;
512
Jens Axboeb9b34982012-12-17 14:23:47 +0100513 if (c1->seq == c2->seq)
514 log_err("cs: bug in atomic sequence!\n");
515
Jens Axboe7d11f872012-12-17 12:03:29 +0100516 return c1->seq - c2->seq;
517}
518
519int fio_monotonic_clocktest(void)
520{
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700521 struct clock_thread *cthreads;
Jens Axboe7d11f872012-12-17 12:03:29 +0100522 unsigned int nr_cpus = cpus_online();
523 struct clock_entry *entries;
Jens Axboe814917b2014-04-14 12:20:04 -0600524 unsigned long tentries, failed = 0;
Bruce Cran80da8a82013-02-21 14:16:17 +0100525 struct clock_entry *prev, *this;
Jens Axboe58002f92013-02-25 10:23:58 +0100526 uint32_t seq = 0;
Jens Axboecaa3eb12014-04-14 09:05:46 -0600527 unsigned int i;
Jens Axboe7d11f872012-12-17 12:03:29 +0100528
Jens Axboed5e3f5d2013-01-18 20:13:45 +0100529 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
530
Jens Axboefc3f3e42014-09-24 09:54:24 -0600531#ifdef FIO_INC_DEBUG
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100532 fio_debug |= 1U << FD_TIME;
Jens Axboefc3f3e42014-09-24 09:54:24 -0600533#endif
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100534 calibrate_cpu_clock();
Jens Axboefc3f3e42014-09-24 09:54:24 -0600535#ifdef FIO_INC_DEBUG
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100536 fio_debug &= ~(1U << FD_TIME);
Jens Axboefc3f3e42014-09-24 09:54:24 -0600537#endif
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100538
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700539 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
Jens Axboe7d11f872012-12-17 12:03:29 +0100540 tentries = CLOCK_ENTRIES * nr_cpus;
541 entries = malloc(tentries * sizeof(struct clock_entry));
542
543 log_info("cs: Testing %u CPUs\n", nr_cpus);
544
545 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700546 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100547
548 t->cpu = i;
549 t->seq = &seq;
550 t->entries = &entries[i * CLOCK_ENTRIES];
551 pthread_mutex_init(&t->lock, NULL);
552 pthread_mutex_init(&t->started, NULL);
553 pthread_mutex_lock(&t->lock);
Jens Axboe6b0110c2014-04-14 12:14:17 -0600554 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
555 failed++;
556 nr_cpus = i;
557 break;
558 }
Jens Axboe7d11f872012-12-17 12:03:29 +0100559 }
560
561 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700562 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100563
564 pthread_mutex_lock(&t->started);
565 }
566
567 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700568 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100569
570 pthread_mutex_unlock(&t->lock);
571 }
572
Jens Axboe814917b2014-04-14 12:20:04 -0600573 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700574 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100575 void *ret;
576
577 pthread_join(t->thread, &ret);
578 if (ret)
579 failed++;
580 }
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700581 free(cthreads);
Jens Axboe7d11f872012-12-17 12:03:29 +0100582
583 if (failed) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200584 log_err("Clocksource test: %lu threads failed\n", failed);
Jens Axboe7d11f872012-12-17 12:03:29 +0100585 goto err;
586 }
587
588 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
589
590 for (failed = i = 0; i < tentries; i++) {
Bruce Cran80da8a82013-02-21 14:16:17 +0100591 this = &entries[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100592
593 if (!i) {
594 prev = this;
595 continue;
596 }
597
598 if (prev->tsc > this->tsc) {
599 uint64_t diff = prev->tsc - this->tsc;
600
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200601 log_info("cs: CPU clock mismatch (diff=%llu):\n",
602 (unsigned long long) diff);
603 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
604 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 +0100605 failed++;
606 }
607
608 prev = this;
609 }
610
611 if (failed)
612 log_info("cs: Failed: %lu\n", failed);
613 else
614 log_info("cs: Pass!\n");
615
616err:
617 free(entries);
618 return !!failed;
619}
620
621#else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
622
623int fio_monotonic_clocktest(void)
624{
625 log_info("cs: current platform does not support CPU clocks\n");
626 return 0;
627}
628
629#endif