blob: 8e53f83fd3b22e368f104d683c776e8aeb6868d1 [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 Axboea7d78f22014-12-16 15:13:45 -070024 uint64_t last_tv_valid;
Jens Axboe5d879392012-12-18 09:12:27 +010025};
Jens Axboe67bf9822013-01-10 11:23:19 +010026#ifdef CONFIG_TLS_THREAD
Jens Axboeb4ea84d2013-04-11 14:20:33 +020027static __thread struct tv_valid static_tv_valid;
Jens Axboe67bf9822013-01-10 11:23:19 +010028#else
Jens Axboe5d879392012-12-18 09:12:27 +010029static pthread_key_t tv_tls_key;
Jens Axboe67bf9822013-01-10 11:23:19 +010030#endif
Jens Axboe02bcaa82006-11-24 10:42:00 +010031
Bruce Cran16de1bf2012-02-20 17:07:32 +000032enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
Jens Axboefa80fea2012-12-09 20:29:00 +010033int fio_clock_source_set = 0;
Jens Axboe10aa1362014-04-01 21:10:36 -060034static enum fio_cs fio_clock_source_inited = CS_INVAL;
Jens Axboec223da82010-03-24 13:23:53 +010035
Jens Axboe02bcaa82006-11-24 10:42:00 +010036#ifdef FIO_DEBUG_TIME
37
38#define HASH_BITS 8
39#define HASH_SIZE (1 << HASH_BITS)
40
Jens Axboe01743ee2008-06-02 12:19:19 +020041static struct flist_head hash[HASH_SIZE];
Jens Axboe02bcaa82006-11-24 10:42:00 +010042static int gtod_inited;
43
44struct gtod_log {
Jens Axboe01743ee2008-06-02 12:19:19 +020045 struct flist_head list;
Jens Axboe02bcaa82006-11-24 10:42:00 +010046 void *caller;
47 unsigned long calls;
48};
49
50static struct gtod_log *find_hash(void *caller)
51{
52 unsigned long h = hash_ptr(caller, HASH_BITS);
Jens Axboe01743ee2008-06-02 12:19:19 +020053 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +010054
Jens Axboe01743ee2008-06-02 12:19:19 +020055 flist_for_each(entry, &hash[h]) {
56 struct gtod_log *log = flist_entry(entry, struct gtod_log,
57 list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010058
59 if (log->caller == caller)
60 return log;
61 }
62
63 return NULL;
64}
65
66static struct gtod_log *find_log(void *caller)
67{
68 struct gtod_log *log = find_hash(caller);
69
70 if (!log) {
71 unsigned long h;
72
73 log = malloc(sizeof(*log));
Jens Axboe01743ee2008-06-02 12:19:19 +020074 INIT_FLIST_HEAD(&log->list);
Jens Axboe02bcaa82006-11-24 10:42:00 +010075 log->caller = caller;
76 log->calls = 0;
77
78 h = hash_ptr(caller, HASH_BITS);
Jens Axboe01743ee2008-06-02 12:19:19 +020079 flist_add_tail(&log->list, &hash[h]);
Jens Axboe02bcaa82006-11-24 10:42:00 +010080 }
81
82 return log;
83}
84
85static void gtod_log_caller(void *caller)
86{
87 if (gtod_inited) {
88 struct gtod_log *log = find_log(caller);
89
90 log->calls++;
91 }
92}
93
94static void fio_exit fio_dump_gtod(void)
95{
96 unsigned long total_calls = 0;
97 int i;
98
99 for (i = 0; i < HASH_SIZE; i++) {
Jens Axboe01743ee2008-06-02 12:19:19 +0200100 struct flist_head *entry;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100101 struct gtod_log *log;
102
Jens Axboe01743ee2008-06-02 12:19:19 +0200103 flist_for_each(entry, &hash[i]) {
104 log = flist_entry(entry, struct gtod_log, list);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100105
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100106 printf("function %p, calls %lu\n", log->caller,
107 log->calls);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100108 total_calls += log->calls;
109 }
110 }
111
112 printf("Total %lu gettimeofday\n", total_calls);
113}
114
115static void fio_init gtod_init(void)
116{
117 int i;
118
119 for (i = 0; i < HASH_SIZE; i++)
Jens Axboe01743ee2008-06-02 12:19:19 +0200120 INIT_FLIST_HEAD(&hash[i]);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100121
122 gtod_inited = 1;
123}
124
125#endif /* FIO_DEBUG_TIME */
126
Jens Axboe67bf9822013-01-10 11:23:19 +0100127#ifdef CONFIG_CLOCK_GETTIME
Jens Axboe9ff1c072012-12-20 15:17:57 +0100128static int fill_clock_gettime(struct timespec *ts)
129{
Jens Axboe67bf9822013-01-10 11:23:19 +0100130#ifdef CONFIG_CLOCK_MONOTONIC
Jens Axboe9ff1c072012-12-20 15:17:57 +0100131 return clock_gettime(CLOCK_MONOTONIC, ts);
132#else
133 return clock_gettime(CLOCK_REALTIME, ts);
134#endif
135}
Jens Axboe1e97cce2006-12-05 11:44:16 +0100136#endif
Jens Axboe67bf9822013-01-10 11:23:19 +0100137
Jens Axboea7d78f22014-12-16 15:13:45 -0700138static void __fio_gettime(struct timeval *tp)
Jens Axboe02bcaa82006-11-24 10:42:00 +0100139{
Jens Axboe93f0b092012-12-18 09:41:59 +0100140 struct tv_valid *tv;
Jens Axboe5d879392012-12-18 09:12:27 +0100141
Jens Axboe67bf9822013-01-10 11:23:19 +0100142#ifdef CONFIG_TLS_THREAD
143 tv = &static_tv_valid;
144#else
Jens Axboe93f0b092012-12-18 09:41:59 +0100145 tv = pthread_getspecific(tv_tls_key);
Jens Axboe67bf9822013-01-10 11:23:19 +0100146#endif
Jens Axboe93f0b092012-12-18 09:41:59 +0100147
Jens Axboec223da82010-03-24 13:23:53 +0100148 switch (fio_clock_source) {
Jens Axboe67bf9822013-01-10 11:23:19 +0100149#ifdef CONFIG_GETTIMEOFDAY
Jens Axboec223da82010-03-24 13:23:53 +0100150 case CS_GTOD:
Jens Axboe02bcaa82006-11-24 10:42:00 +0100151 gettimeofday(tp, NULL);
Jens Axboec223da82010-03-24 13:23:53 +0100152 break;
Jens Axboe67bf9822013-01-10 11:23:19 +0100153#endif
154#ifdef CONFIG_CLOCK_GETTIME
Jens Axboec223da82010-03-24 13:23:53 +0100155 case CS_CGETTIME: {
Jens Axboe02bcaa82006-11-24 10:42:00 +0100156 struct timespec ts;
157
Jens Axboe9ff1c072012-12-20 15:17:57 +0100158 if (fill_clock_gettime(&ts) < 0) {
Jens Axboec223da82010-03-24 13:23:53 +0100159 log_err("fio: clock_gettime fails\n");
160 assert(0);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100161 }
162
163 tp->tv_sec = ts.tv_sec;
164 tp->tv_usec = ts.tv_nsec / 1000;
Jens Axboec223da82010-03-24 13:23:53 +0100165 break;
166 }
Jens Axboe67bf9822013-01-10 11:23:19 +0100167#endif
Jens Axboec223da82010-03-24 13:23:53 +0100168#ifdef ARCH_HAVE_CPU_CLOCK
169 case CS_CPUCLOCK: {
Jens Axboeba458c22013-01-21 05:38:22 -0700170 uint64_t usecs, t;
Jens Axboec223da82010-03-24 13:23:53 +0100171
172 t = get_cpu_clock();
Jens Axboea7d78f22014-12-16 15:13:45 -0700173 if (t < tv->last_cycles && tv->last_tv_valid)
174 log_err("fio: CPU clock going back in time\n");
Jens Axboec223da82010-03-24 13:23:53 +0100175
Jens Axboea7d78f22014-12-16 15:13:45 -0700176 tv->last_cycles = t;
177 tv->last_tv_valid = 1;
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200178#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
179 usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
180#else
Jens Axboe71339112012-12-21 22:54:56 +0100181 usecs = (t * inv_cycles_per_usec) / 16777216UL;
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200182#endif
Jens Axboec223da82010-03-24 13:23:53 +0100183 tp->tv_sec = usecs / 1000000;
184 tp->tv_usec = usecs % 1000000;
Jens Axboec223da82010-03-24 13:23:53 +0100185 break;
186 }
187#endif
188 default:
189 log_err("fio: invalid clock source %d\n", fio_clock_source);
190 break;
Jens Axboe02bcaa82006-11-24 10:42:00 +0100191 }
Jens Axboe67bf9822013-01-10 11:23:19 +0100192}
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{
Jens Axboe67bf9822013-01-10 11:23:19 +0100200#ifdef FIO_DEBUG_TIME
201 if (!caller)
202 caller = __builtin_return_address(0);
203
204 gtod_log_caller(caller);
205#endif
Jens Axboe225ba9e2014-02-26 14:31:15 -0800206 if (fio_unlikely(fio_tv)) {
Jens Axboe67bf9822013-01-10 11:23:19 +0100207 memcpy(tp, fio_tv, sizeof(*tp));
208 return;
209 }
210
Jens Axboea7d78f22014-12-16 15:13:45 -0700211 __fio_gettime(tp);
Jens Axboe02bcaa82006-11-24 10:42:00 +0100212}
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100213
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200214#if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
Jens Axboec223da82010-03-24 13:23:53 +0100215static unsigned long get_cycles_per_usec(void)
216{
217 struct timeval s, e;
Jens Axboeba458c22013-01-21 05:38:22 -0700218 uint64_t c_s, c_e;
Jens Axboe67bf9822013-01-10 11:23:19 +0100219 enum fio_cs old_cs = fio_clock_source;
Jens Axboec223da82010-03-24 13:23:53 +0100220
Jens Axboe67bf9822013-01-10 11:23:19 +0100221#ifdef CONFIG_CLOCK_GETTIME
222 fio_clock_source = CS_CGETTIME;
223#else
224 fio_clock_source = CS_GTOD;
225#endif
226 __fio_gettime(&s);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100227
Jens Axboec223da82010-03-24 13:23:53 +0100228 c_s = get_cpu_clock();
229 do {
Jens Axboeba458c22013-01-21 05:38:22 -0700230 uint64_t elapsed;
Jens Axboec223da82010-03-24 13:23:53 +0100231
Jens Axboe67bf9822013-01-10 11:23:19 +0100232 __fio_gettime(&e);
Jens Axboe9ff1c072012-12-20 15:17:57 +0100233
Jens Axboec223da82010-03-24 13:23:53 +0100234 elapsed = utime_since(&s, &e);
Jens Axboe486332e2012-12-10 08:07:14 +0100235 if (elapsed >= 1280) {
Jens Axboec223da82010-03-24 13:23:53 +0100236 c_e = get_cpu_clock();
237 break;
238 }
239 } while (1);
240
Jens Axboe67bf9822013-01-10 11:23:19 +0100241 fio_clock_source = old_cs;
Jens Axboec0110002012-12-10 08:29:03 +0100242 return (c_e - c_s + 127) >> 7;
Jens Axboec223da82010-03-24 13:23:53 +0100243}
244
Jens Axboefa80fea2012-12-09 20:29:00 +0100245#define NR_TIME_ITERS 50
246
Jens Axboee2598792013-02-24 21:29:35 +0100247static int calibrate_cpu_clock(void)
Jens Axboec223da82010-03-24 13:23:53 +0100248{
249 double delta, mean, S;
Jens Axboeba458c22013-01-21 05:38:22 -0700250 uint64_t avg, cycles[NR_TIME_ITERS];
Jens Axboec223da82010-03-24 13:23:53 +0100251 int i, samples;
252
Jens Axboec223da82010-03-24 13:23:53 +0100253 cycles[0] = get_cycles_per_usec();
254 S = delta = mean = 0.0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100255 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100256 cycles[i] = get_cycles_per_usec();
257 delta = cycles[i] - mean;
258 if (delta) {
259 mean += delta / (i + 1.0);
260 S += delta * (cycles[i] - mean);
261 }
262 }
263
Jens Axboee2598792013-02-24 21:29:35 +0100264 /*
265 * The most common platform clock breakage is returning zero
266 * indefinitely. Check for that and return failure.
267 */
268 if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
269 return 1;
270
Jens Axboefa80fea2012-12-09 20:29:00 +0100271 S = sqrt(S / (NR_TIME_ITERS - 1.0));
Jens Axboec223da82010-03-24 13:23:53 +0100272
273 samples = avg = 0;
Jens Axboefa80fea2012-12-09 20:29:00 +0100274 for (i = 0; i < NR_TIME_ITERS; i++) {
Jens Axboec223da82010-03-24 13:23:53 +0100275 double this = cycles[i];
276
Bruce Cran03e20d62011-01-02 20:14:54 +0100277 if ((fmax(this, mean) - fmin(this, mean)) > S)
Jens Axboec223da82010-03-24 13:23:53 +0100278 continue;
279 samples++;
280 avg += this;
281 }
282
Jens Axboefa80fea2012-12-09 20:29:00 +0100283 S /= (double) NR_TIME_ITERS;
Jens Axboe89db7272012-12-10 08:29:56 +0100284 mean /= 10.0;
Jens Axboec223da82010-03-24 13:23:53 +0100285
Jens Axboefa80fea2012-12-09 20:29:00 +0100286 for (i = 0; i < NR_TIME_ITERS; i++)
Jens Axboe4b91ee82013-02-25 10:18:33 +0100287 dprint(FD_TIME, "cycles[%d]=%llu\n", i,
288 (unsigned long long) cycles[i] / 10);
Jens Axboec223da82010-03-24 13:23:53 +0100289
Jens Axboed7abad32012-12-10 10:15:59 +0100290 avg /= samples;
Jens Axboeb0ff22d2013-01-01 13:38:18 +0100291 avg = (avg + 5) / 10;
Jens Axboe4b91ee82013-02-25 10:18:33 +0100292 dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
Jens Axboec223da82010-03-24 13:23:53 +0100293 dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
294
295 cycles_per_usec = avg;
Jens Axboe71339112012-12-21 22:54:56 +0100296 inv_cycles_per_usec = 16777216UL / cycles_per_usec;
297 dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
Jens Axboee2598792013-02-24 21:29:35 +0100298 return 0;
Jens Axboe09a32402010-08-15 15:01:51 -0400299}
300#else
Jens Axboee2598792013-02-24 21:29:35 +0100301static int calibrate_cpu_clock(void)
Jens Axboe09a32402010-08-15 15:01:51 -0400302{
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200303#ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
304 return 0;
305#else
Jens Axboee2598792013-02-24 21:29:35 +0100306 return 1;
Jens Axboe09a32402010-08-15 15:01:51 -0400307#endif
Christian Ehrhardt919e7892014-04-08 17:52:00 +0200308}
309#endif // ARCH_HAVE_CPU_CLOCK
Jens Axboe09a32402010-08-15 15:01:51 -0400310
Jens Axboe67bf9822013-01-10 11:23:19 +0100311#ifndef CONFIG_TLS_THREAD
Jens Axboe5d879392012-12-18 09:12:27 +0100312void fio_local_clock_init(int is_thread)
313{
314 struct tv_valid *t;
315
Jens Axboe572cfb32013-12-06 12:02:10 -0700316 t = calloc(1, sizeof(*t));
Jens Axboea7d78f22014-12-16 15:13:45 -0700317 if (pthread_setspecific(tv_tls_key, t)) {
Jens Axboe5d879392012-12-18 09:12:27 +0100318 log_err("fio: can't set TLS key\n");
Jens Axboea7d78f22014-12-16 15:13:45 -0700319 assert(0);
320 }
Jens Axboe5d879392012-12-18 09:12:27 +0100321}
322
323static void kill_tv_tls_key(void *data)
324{
325 free(data);
326}
Jens Axboe67bf9822013-01-10 11:23:19 +0100327#else
328void fio_local_clock_init(int is_thread)
329{
330}
331#endif
Jens Axboe5d879392012-12-18 09:12:27 +0100332
Jens Axboe09a32402010-08-15 15:01:51 -0400333void fio_clock_init(void)
334{
Jens Axboe01423ea2012-12-14 20:37:06 +0100335 if (fio_clock_source == fio_clock_source_inited)
336 return;
337
Jens Axboe67bf9822013-01-10 11:23:19 +0100338#ifndef CONFIG_TLS_THREAD
Jens Axboe5d879392012-12-18 09:12:27 +0100339 if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
340 log_err("fio: can't create TLS key\n");
Jens Axboe67bf9822013-01-10 11:23:19 +0100341#endif
Jens Axboe5d879392012-12-18 09:12:27 +0100342
Jens Axboe01423ea2012-12-14 20:37:06 +0100343 fio_clock_source_inited = fio_clock_source;
Jens Axboee2598792013-02-24 21:29:35 +0100344
345 if (calibrate_cpu_clock())
346 tsc_reliable = 0;
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 Axboe0cbbc392014-09-30 16:04:12 -0600360uint64_t utime_since(const struct timeval *s, const 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 Axboe0cbbc392014-09-30 16:04:12 -0600383uint64_t utime_since_now(const 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 Axboe0cbbc392014-09-30 16:04:12 -0600391uint64_t mtime_since(const struct timeval *s, const 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 Axboe0cbbc392014-09-30 16:04:12 -0600412uint64_t mtime_since_now(const 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 Axboe0cbbc392014-09-30 16:04:12 -0600421uint64_t time_since_now(const 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 Axboe58002f92013-02-25 10:23:58 +0100432 uint32_t seq;
433 uint32_t cpu;
Jens Axboeba458c22013-01-21 05:38:22 -0700434 uint64_t tsc;
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;
Jens Axboe58002f92013-02-25 10:23:58 +0100442 uint32_t *seq;
Jens Axboe7d11f872012-12-17 12:03:29 +0100443 struct clock_entry *entries;
444};
445
Jens Axboe58002f92013-02-25 10:23:58 +0100446static inline uint32_t atomic32_inc_return(uint32_t *seq)
Jens Axboe7d11f872012-12-17 12:03:29 +0100447{
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;
Jens Axboe58002f92013-02-25 10:23:58 +0100456 uint32_t last_seq;
Jens Axboe7d11f872012-12-17 12:03:29 +0100457 int i;
458
459 memset(&cpu_mask, 0, sizeof(cpu_mask));
460 fio_cpu_set(&cpu_mask, t->cpu);
461
462 if (fio_setaffinity(gettid(), cpu_mask) == -1) {
463 log_err("clock setaffinity failed\n");
464 return (void *) 1;
465 }
466
Jens Axboe7d11f872012-12-17 12:03:29 +0100467 pthread_mutex_lock(&t->lock);
Jens Axboeb9b34982012-12-17 14:23:47 +0100468 pthread_mutex_unlock(&t->started);
Jens Axboe7d11f872012-12-17 12:03:29 +0100469
Jens Axboe58002f92013-02-25 10:23:58 +0100470 last_seq = 0;
Jens Axboe7d11f872012-12-17 12:03:29 +0100471 c = &t->entries[0];
472 for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
Jens Axboe58002f92013-02-25 10:23:58 +0100473 uint32_t seq;
474 uint64_t tsc;
Jens Axboe7d11f872012-12-17 12:03:29 +0100475
476 c->cpu = t->cpu;
477 do {
Jens Axboe58002f92013-02-25 10:23:58 +0100478 seq = atomic32_inc_return(t->seq);
479 if (seq < last_seq)
480 break;
Jens Axboe7d11f872012-12-17 12:03:29 +0100481 tsc = get_cpu_clock();
482 } while (seq != *t->seq);
483
484 c->seq = seq;
485 c->tsc = tsc;
486 }
487
Jens Axboe0f78b222013-02-26 13:54:20 +0100488 log_info("cs: cpu%3d: %llu clocks seen\n", t->cpu,
489 (unsigned long long) t->entries[i - 1].tsc - t->entries[0].tsc);
Jens Axboe58002f92013-02-25 10:23:58 +0100490
Jens Axboee2598792013-02-24 21:29:35 +0100491 /*
492 * The most common platform clock breakage is returning zero
493 * indefinitely. Check for that and return failure.
494 */
Jens Axboe58002f92013-02-25 10:23:58 +0100495 if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
Jens Axboee2598792013-02-24 21:29:35 +0100496 return (void *) 1;
497
Jens Axboe7d11f872012-12-17 12:03:29 +0100498 return NULL;
499}
500
501static int clock_cmp(const void *p1, const void *p2)
502{
503 const struct clock_entry *c1 = p1;
504 const struct clock_entry *c2 = p2;
505
Jens Axboeb9b34982012-12-17 14:23:47 +0100506 if (c1->seq == c2->seq)
507 log_err("cs: bug in atomic sequence!\n");
508
Jens Axboe7d11f872012-12-17 12:03:29 +0100509 return c1->seq - c2->seq;
510}
511
512int fio_monotonic_clocktest(void)
513{
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700514 struct clock_thread *cthreads;
Jens Axboe7d11f872012-12-17 12:03:29 +0100515 unsigned int nr_cpus = cpus_online();
516 struct clock_entry *entries;
Jens Axboe814917b2014-04-14 12:20:04 -0600517 unsigned long tentries, failed = 0;
Bruce Cran80da8a82013-02-21 14:16:17 +0100518 struct clock_entry *prev, *this;
Jens Axboe58002f92013-02-25 10:23:58 +0100519 uint32_t seq = 0;
Jens Axboecaa3eb12014-04-14 09:05:46 -0600520 unsigned int i;
Jens Axboe7d11f872012-12-17 12:03:29 +0100521
Jens Axboed5e3f5d2013-01-18 20:13:45 +0100522 log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
523
Jens Axboefc3f3e42014-09-24 09:54:24 -0600524#ifdef FIO_INC_DEBUG
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100525 fio_debug |= 1U << FD_TIME;
Jens Axboefc3f3e42014-09-24 09:54:24 -0600526#endif
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100527 calibrate_cpu_clock();
Jens Axboefc3f3e42014-09-24 09:54:24 -0600528#ifdef FIO_INC_DEBUG
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100529 fio_debug &= ~(1U << FD_TIME);
Jens Axboefc3f3e42014-09-24 09:54:24 -0600530#endif
Jens Axboe4f1d43c2012-12-17 14:29:28 +0100531
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700532 cthreads = malloc(nr_cpus * sizeof(struct clock_thread));
Jens Axboe7d11f872012-12-17 12:03:29 +0100533 tentries = CLOCK_ENTRIES * nr_cpus;
534 entries = malloc(tentries * sizeof(struct clock_entry));
535
536 log_info("cs: Testing %u CPUs\n", nr_cpus);
537
538 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700539 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100540
541 t->cpu = i;
542 t->seq = &seq;
543 t->entries = &entries[i * CLOCK_ENTRIES];
544 pthread_mutex_init(&t->lock, NULL);
545 pthread_mutex_init(&t->started, NULL);
546 pthread_mutex_lock(&t->lock);
Jens Axboe6b0110c2014-04-14 12:14:17 -0600547 if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
548 failed++;
549 nr_cpus = i;
550 break;
551 }
Jens Axboe7d11f872012-12-17 12:03:29 +0100552 }
553
554 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700555 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100556
557 pthread_mutex_lock(&t->started);
558 }
559
560 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700561 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100562
563 pthread_mutex_unlock(&t->lock);
564 }
565
Jens Axboe814917b2014-04-14 12:20:04 -0600566 for (i = 0; i < nr_cpus; i++) {
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700567 struct clock_thread *t = &cthreads[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100568 void *ret;
569
570 pthread_join(t->thread, &ret);
571 if (ret)
572 failed++;
573 }
Jens Axboe0f7f9a92014-11-06 09:21:10 -0700574 free(cthreads);
Jens Axboe7d11f872012-12-17 12:03:29 +0100575
576 if (failed) {
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200577 log_err("Clocksource test: %lu threads failed\n", failed);
Jens Axboe7d11f872012-12-17 12:03:29 +0100578 goto err;
579 }
580
581 qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
582
583 for (failed = i = 0; i < tentries; i++) {
Bruce Cran80da8a82013-02-21 14:16:17 +0100584 this = &entries[i];
Jens Axboe7d11f872012-12-17 12:03:29 +0100585
586 if (!i) {
587 prev = this;
588 continue;
589 }
590
591 if (prev->tsc > this->tsc) {
592 uint64_t diff = prev->tsc - this->tsc;
593
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200594 log_info("cs: CPU clock mismatch (diff=%llu):\n",
595 (unsigned long long) diff);
596 log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
597 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 +0100598 failed++;
599 }
600
601 prev = this;
602 }
603
604 if (failed)
605 log_info("cs: Failed: %lu\n", failed);
606 else
607 log_info("cs: Pass!\n");
608
609err:
610 free(entries);
611 return !!failed;
612}
613
614#else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
615
616int fio_monotonic_clocktest(void)
617{
618 log_info("cs: current platform does not support CPU clocks\n");
619 return 0;
620}
621
622#endif