Commit | Line | Data |
---|---|---|
02bcaa8c | 1 | /* |
f5cc024a | 2 | * Clock functions |
02bcaa8c | 3 | */ |
f5cc024a | 4 | |
02bcaa8c | 5 | #include <unistd.h> |
c223da83 | 6 | #include <math.h> |
02bcaa8c | 7 | #include <sys/time.h> |
03e20d68 | 8 | #include <time.h> |
02bcaa8c JA |
9 | |
10 | #include "fio.h" | |
17031c6d | 11 | #include "fio_sem.h" |
be4ecfdf | 12 | #include "smalloc.h" |
02bcaa8c JA |
13 | |
14 | #include "hash.h" | |
7d11f871 | 15 | #include "os/os.h" |
02bcaa8c | 16 | |
dac499a0 AV |
17 | #if defined(ARCH_HAVE_CPU_CLOCK) |
18 | #ifndef ARCH_CPU_CLOCK_CYCLES_PER_USEC | |
cae9edd9 | 19 | static unsigned long long cycles_per_msec; |
6d02b37b VF |
20 | static unsigned long long cycles_start; |
21 | static unsigned long long clock_mult; | |
22 | static unsigned long long max_cycles_mask; | |
23 | static unsigned long long nsecs_for_max_cycles; | |
24 | static unsigned int clock_shift; | |
25 | static unsigned int max_cycles_shift; | |
26 | #define MAX_CLOCK_SEC 60*60 | |
96170421 CE |
27 | #endif |
28 | #ifdef ARCH_CPU_CLOCK_WRAPS | |
6d02b37b | 29 | static unsigned int cycles_wrap; |
09a32402 | 30 | #endif |
dac499a0 | 31 | #endif |
24575392 | 32 | bool tsc_reliable = false; |
5d879392 JA |
33 | |
34 | struct tv_valid { | |
02dcf81c | 35 | int warned; |
5d879392 | 36 | }; |
03be65c9 | 37 | #ifdef ARCH_HAVE_CPU_CLOCK |
67bf9823 | 38 | #ifdef CONFIG_TLS_THREAD |
b4ea84da | 39 | static __thread struct tv_valid static_tv_valid; |
67bf9823 | 40 | #else |
5d879392 | 41 | static pthread_key_t tv_tls_key; |
67bf9823 | 42 | #endif |
03be65c9 | 43 | #endif |
02bcaa8c | 44 | |
16de1bf9 | 45 | enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE; |
fa80feae | 46 | int fio_clock_source_set = 0; |
10aa136b | 47 | static enum fio_cs fio_clock_source_inited = CS_INVAL; |
c223da83 | 48 | |
02bcaa8c JA |
49 | #ifdef FIO_DEBUG_TIME |
50 | ||
51 | #define HASH_BITS 8 | |
52 | #define HASH_SIZE (1 << HASH_BITS) | |
53 | ||
01743ee1 | 54 | static struct flist_head hash[HASH_SIZE]; |
02bcaa8c JA |
55 | static int gtod_inited; |
56 | ||
57 | struct gtod_log { | |
01743ee1 | 58 | struct flist_head list; |
02bcaa8c JA |
59 | void *caller; |
60 | unsigned long calls; | |
61 | }; | |
62 | ||
63 | static struct gtod_log *find_hash(void *caller) | |
64 | { | |
65 | unsigned long h = hash_ptr(caller, HASH_BITS); | |
01743ee1 | 66 | struct flist_head *entry; |
02bcaa8c | 67 | |
01743ee1 JA |
68 | flist_for_each(entry, &hash[h]) { |
69 | struct gtod_log *log = flist_entry(entry, struct gtod_log, | |
70 | list); | |
02bcaa8c JA |
71 | |
72 | if (log->caller == caller) | |
73 | return log; | |
74 | } | |
75 | ||
76 | return NULL; | |
77 | } | |
78 | ||
d5e16441 | 79 | static void inc_caller(void *caller) |
02bcaa8c JA |
80 | { |
81 | struct gtod_log *log = find_hash(caller); | |
82 | ||
83 | if (!log) { | |
84 | unsigned long h; | |
85 | ||
86 | log = malloc(sizeof(*log)); | |
01743ee1 | 87 | INIT_FLIST_HEAD(&log->list); |
02bcaa8c JA |
88 | log->caller = caller; |
89 | log->calls = 0; | |
90 | ||
91 | h = hash_ptr(caller, HASH_BITS); | |
01743ee1 | 92 | flist_add_tail(&log->list, &hash[h]); |
02bcaa8c JA |
93 | } |
94 | ||
d5e16441 | 95 | log->calls++; |
02bcaa8c JA |
96 | } |
97 | ||
98 | static void gtod_log_caller(void *caller) | |
99 | { | |
d5e16441 JA |
100 | if (gtod_inited) |
101 | inc_caller(caller); | |
02bcaa8c JA |
102 | } |
103 | ||
104 | static void fio_exit fio_dump_gtod(void) | |
105 | { | |
106 | unsigned long total_calls = 0; | |
107 | int i; | |
108 | ||
109 | for (i = 0; i < HASH_SIZE; i++) { | |
01743ee1 | 110 | struct flist_head *entry; |
02bcaa8c JA |
111 | struct gtod_log *log; |
112 | ||
01743ee1 JA |
113 | flist_for_each(entry, &hash[i]) { |
114 | log = flist_entry(entry, struct gtod_log, list); | |
02bcaa8c | 115 | |
5ec10eaa JA |
116 | printf("function %p, calls %lu\n", log->caller, |
117 | log->calls); | |
02bcaa8c JA |
118 | total_calls += log->calls; |
119 | } | |
120 | } | |
121 | ||
122 | printf("Total %lu gettimeofday\n", total_calls); | |
123 | } | |
124 | ||
125 | static void fio_init gtod_init(void) | |
126 | { | |
127 | int i; | |
128 | ||
129 | for (i = 0; i < HASH_SIZE; i++) | |
01743ee1 | 130 | INIT_FLIST_HEAD(&hash[i]); |
02bcaa8c JA |
131 | |
132 | gtod_inited = 1; | |
133 | } | |
134 | ||
135 | #endif /* FIO_DEBUG_TIME */ | |
136 | ||
67bf9823 | 137 | #ifdef CONFIG_CLOCK_GETTIME |
9ff1c070 JA |
138 | static int fill_clock_gettime(struct timespec *ts) |
139 | { | |
c544f604 SN |
140 | #if defined(CONFIG_CLOCK_MONOTONIC_RAW) |
141 | return clock_gettime(CLOCK_MONOTONIC_RAW, ts); | |
142 | #elif defined(CONFIG_CLOCK_MONOTONIC) | |
9ff1c070 JA |
143 | return clock_gettime(CLOCK_MONOTONIC, ts); |
144 | #else | |
145 | return clock_gettime(CLOCK_REALTIME, ts); | |
146 | #endif | |
147 | } | |
1e97cce9 | 148 | #endif |
67bf9823 | 149 | |
8b6a404c | 150 | static void __fio_gettime(struct timespec *tp) |
02bcaa8c | 151 | { |
c223da83 | 152 | switch (fio_clock_source) { |
67bf9823 | 153 | #ifdef CONFIG_GETTIMEOFDAY |
8b6a404c VF |
154 | case CS_GTOD: { |
155 | struct timeval tv; | |
156 | gettimeofday(&tv, NULL); | |
157 | ||
158 | tp->tv_sec = tv.tv_sec; | |
159 | tp->tv_nsec = tv.tv_usec * 1000; | |
c223da83 | 160 | break; |
8b6a404c | 161 | } |
67bf9823 JA |
162 | #endif |
163 | #ifdef CONFIG_CLOCK_GETTIME | |
c223da83 | 164 | case CS_CGETTIME: { |
8b6a404c | 165 | if (fill_clock_gettime(tp) < 0) { |
c223da83 JA |
166 | log_err("fio: clock_gettime fails\n"); |
167 | assert(0); | |
02bcaa8c | 168 | } |
c223da83 JA |
169 | break; |
170 | } | |
67bf9823 | 171 | #endif |
c223da83 JA |
172 | #ifdef ARCH_HAVE_CPU_CLOCK |
173 | case CS_CPUCLOCK: { | |
6d02b37b | 174 | uint64_t nsecs, t, multiples; |
03be65c9 JA |
175 | struct tv_valid *tv; |
176 | ||
177 | #ifdef CONFIG_TLS_THREAD | |
178 | tv = &static_tv_valid; | |
179 | #else | |
180 | tv = pthread_getspecific(tv_tls_key); | |
181 | #endif | |
c223da83 JA |
182 | |
183 | t = get_cpu_clock(); | |
96170421 | 184 | #ifdef ARCH_CPU_CLOCK_WRAPS |
73df3e07 JA |
185 | if (t < cycles_start && !cycles_wrap) |
186 | cycles_wrap = 1; | |
03be65c9 JA |
187 | else if (cycles_wrap && t >= cycles_start && !tv->warned) { |
188 | log_err("fio: double CPU clock wrap\n"); | |
189 | tv->warned = 1; | |
02dcf81c | 190 | } |
96170421 | 191 | #endif |
919e789d | 192 | #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC |
6d02b37b | 193 | nsecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC * 1000; |
919e789d | 194 | #else |
6d02b37b VF |
195 | t -= cycles_start; |
196 | multiples = t >> max_cycles_shift; | |
197 | nsecs = multiples * nsecs_for_max_cycles; | |
198 | nsecs += ((t & max_cycles_mask) * clock_mult) >> clock_shift; | |
919e789d | 199 | #endif |
8b6a404c VF |
200 | tp->tv_sec = nsecs / 1000000000ULL; |
201 | tp->tv_nsec = nsecs % 1000000000ULL; | |
c223da83 JA |
202 | break; |
203 | } | |
204 | #endif | |
205 | default: | |
206 | log_err("fio: invalid clock source %d\n", fio_clock_source); | |
207 | break; | |
02bcaa8c | 208 | } |
67bf9823 JA |
209 | } |
210 | ||
211 | #ifdef FIO_DEBUG_TIME | |
8b6a404c | 212 | void fio_gettime(struct timespec *tp, void *caller) |
67bf9823 | 213 | #else |
8b6a404c | 214 | void fio_gettime(struct timespec *tp, void fio_unused *caller) |
67bf9823 JA |
215 | #endif |
216 | { | |
67bf9823 JA |
217 | #ifdef FIO_DEBUG_TIME |
218 | if (!caller) | |
219 | caller = __builtin_return_address(0); | |
220 | ||
221 | gtod_log_caller(caller); | |
222 | #endif | |
27325ed5 | 223 | if (fio_unlikely(fio_gettime_offload(tp))) |
67bf9823 | 224 | return; |
67bf9823 | 225 | |
9eb271b9 | 226 | __fio_gettime(tp); |
02bcaa8c | 227 | } |
be4ecfdf | 228 | |
919e789d | 229 | #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC) |
2aebc5a1 | 230 | static unsigned long get_cycles_per_msec(void) |
c223da83 | 231 | { |
8b6a404c | 232 | struct timespec s, e; |
ba458c2f | 233 | uint64_t c_s, c_e; |
67bf9823 | 234 | enum fio_cs old_cs = fio_clock_source; |
99afcdb5 | 235 | uint64_t elapsed; |
c223da83 | 236 | |
67bf9823 JA |
237 | #ifdef CONFIG_CLOCK_GETTIME |
238 | fio_clock_source = CS_CGETTIME; | |
239 | #else | |
240 | fio_clock_source = CS_GTOD; | |
241 | #endif | |
242 | __fio_gettime(&s); | |
9ff1c070 | 243 | |
c223da83 JA |
244 | c_s = get_cpu_clock(); |
245 | do { | |
67bf9823 | 246 | __fio_gettime(&e); |
9ff1c070 | 247 | |
c223da83 | 248 | elapsed = utime_since(&s, &e); |
486332e5 | 249 | if (elapsed >= 1280) { |
c223da83 JA |
250 | c_e = get_cpu_clock(); |
251 | break; | |
252 | } | |
253 | } while (1); | |
254 | ||
67bf9823 | 255 | fio_clock_source = old_cs; |
2aebc5a1 | 256 | return (c_e - c_s) * 1000 / elapsed; |
c223da83 JA |
257 | } |
258 | ||
fa80feae JA |
259 | #define NR_TIME_ITERS 50 |
260 | ||
e259879e | 261 | static int calibrate_cpu_clock(void) |
c223da83 JA |
262 | { |
263 | double delta, mean, S; | |
e51a6629 | 264 | uint64_t minc, maxc, avg, cycles[NR_TIME_ITERS]; |
6d02b37b VF |
265 | int i, samples, sft = 0; |
266 | unsigned long long tmp, max_ticks, max_mult; | |
c223da83 | 267 | |
2aebc5a1 | 268 | cycles[0] = get_cycles_per_msec(); |
c223da83 | 269 | S = delta = mean = 0.0; |
fa80feae | 270 | for (i = 0; i < NR_TIME_ITERS; i++) { |
2aebc5a1 | 271 | cycles[i] = get_cycles_per_msec(); |
c223da83 JA |
272 | delta = cycles[i] - mean; |
273 | if (delta) { | |
274 | mean += delta / (i + 1.0); | |
275 | S += delta * (cycles[i] - mean); | |
276 | } | |
277 | } | |
278 | ||
e259879e JA |
279 | /* |
280 | * The most common platform clock breakage is returning zero | |
281 | * indefinitely. Check for that and return failure. | |
282 | */ | |
283 | if (!cycles[0] && !cycles[NR_TIME_ITERS - 1]) | |
284 | return 1; | |
285 | ||
fa80feae | 286 | S = sqrt(S / (NR_TIME_ITERS - 1.0)); |
c223da83 | 287 | |
e51a6629 JA |
288 | minc = -1ULL; |
289 | maxc = samples = avg = 0; | |
fa80feae | 290 | for (i = 0; i < NR_TIME_ITERS; i++) { |
c223da83 JA |
291 | double this = cycles[i]; |
292 | ||
e51a6629 JA |
293 | minc = min(cycles[i], minc); |
294 | maxc = max(cycles[i], maxc); | |
295 | ||
03e20d68 | 296 | if ((fmax(this, mean) - fmin(this, mean)) > S) |
c223da83 JA |
297 | continue; |
298 | samples++; | |
299 | avg += this; | |
300 | } | |
301 | ||
fa80feae | 302 | S /= (double) NR_TIME_ITERS; |
c223da83 | 303 | |
fa80feae | 304 | for (i = 0; i < NR_TIME_ITERS; i++) |
13aa415a | 305 | dprint(FD_TIME, "cycles[%d]=%llu\n", i, (unsigned long long) cycles[i]); |
c223da83 | 306 | |
d7abad3d | 307 | avg /= samples; |
2aebc5a1 | 308 | cycles_per_msec = avg; |
4b91ee8f | 309 | dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg); |
e51a6629 JA |
310 | dprint(FD_TIME, "min=%llu, max=%llu, mean=%f, S=%f\n", |
311 | (unsigned long long) minc, | |
312 | (unsigned long long) maxc, mean, S); | |
c223da83 | 313 | |
2aebc5a1 | 314 | max_ticks = MAX_CLOCK_SEC * cycles_per_msec * 1000ULL; |
74558486 JA |
315 | max_mult = ULLONG_MAX / max_ticks; |
316 | dprint(FD_TIME, "\n\nmax_ticks=%llu, __builtin_clzll=%d, " | |
317 | "max_mult=%llu\n", max_ticks, | |
318 | __builtin_clzll(max_ticks), max_mult); | |
6d02b37b VF |
319 | |
320 | /* | |
321 | * Find the largest shift count that will produce | |
322 | * a multiplier that does not exceed max_mult | |
323 | */ | |
2aebc5a1 | 324 | tmp = max_mult * cycles_per_msec / 1000000; |
6d02b37b VF |
325 | while (tmp > 1) { |
326 | tmp >>= 1; | |
327 | sft++; | |
328 | dprint(FD_TIME, "tmp=%llu, sft=%u\n", tmp, sft); | |
329 | } | |
330 | ||
74558486 JA |
331 | clock_shift = sft; |
332 | clock_mult = (1ULL << sft) * 1000000 / cycles_per_msec; | |
333 | dprint(FD_TIME, "clock_shift=%u, clock_mult=%llu\n", clock_shift, | |
334 | clock_mult); | |
6d02b37b | 335 | |
74558486 JA |
336 | /* |
337 | * Find the greatest power of 2 clock ticks that is less than the | |
338 | * ticks in MAX_CLOCK_SEC_2STAGE | |
339 | */ | |
6d02b37b | 340 | max_cycles_shift = max_cycles_mask = 0; |
2aebc5a1 | 341 | tmp = MAX_CLOCK_SEC * 1000ULL * cycles_per_msec; |
74558486 JA |
342 | dprint(FD_TIME, "tmp=%llu, max_cycles_shift=%u\n", tmp, |
343 | max_cycles_shift); | |
6d02b37b VF |
344 | while (tmp > 1) { |
345 | tmp >>= 1; | |
346 | max_cycles_shift++; | |
347 | dprint(FD_TIME, "tmp=%llu, max_cycles_shift=%u\n", tmp, max_cycles_shift); | |
348 | } | |
74558486 JA |
349 | /* |
350 | * if use use (1ULL << max_cycles_shift) * 1000 / cycles_per_msec | |
351 | * here we will have a discontinuity every | |
352 | * (1ULL << max_cycles_shift) cycles | |
353 | */ | |
354 | nsecs_for_max_cycles = ((1ULL << max_cycles_shift) * clock_mult) | |
355 | >> clock_shift; | |
6d02b37b | 356 | |
74558486 | 357 | /* Use a bitmask to calculate ticks % (1ULL << max_cycles_shift) */ |
6d02b37b VF |
358 | for (tmp = 0; tmp < max_cycles_shift; tmp++) |
359 | max_cycles_mask |= 1ULL << tmp; | |
360 | ||
74558486 JA |
361 | dprint(FD_TIME, "max_cycles_shift=%u, 2^max_cycles_shift=%llu, " |
362 | "nsecs_for_max_cycles=%llu, " | |
363 | "max_cycles_mask=%016llx\n", | |
364 | max_cycles_shift, (1ULL << max_cycles_shift), | |
365 | nsecs_for_max_cycles, max_cycles_mask); | |
6d02b37b | 366 | |
73df3e07 JA |
367 | cycles_start = get_cpu_clock(); |
368 | dprint(FD_TIME, "cycles_start=%llu\n", cycles_start); | |
e259879e | 369 | return 0; |
09a32402 JA |
370 | } |
371 | #else | |
e259879e | 372 | static int calibrate_cpu_clock(void) |
09a32402 | 373 | { |
919e789d CE |
374 | #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC |
375 | return 0; | |
376 | #else | |
e259879e | 377 | return 1; |
09a32402 | 378 | #endif |
919e789d CE |
379 | } |
380 | #endif // ARCH_HAVE_CPU_CLOCK | |
09a32402 | 381 | |
67bf9823 | 382 | #ifndef CONFIG_TLS_THREAD |
5d879392 JA |
383 | void fio_local_clock_init(int is_thread) |
384 | { | |
385 | struct tv_valid *t; | |
386 | ||
572cfb3f | 387 | t = calloc(1, sizeof(*t)); |
9eb271b9 | 388 | if (pthread_setspecific(tv_tls_key, t)) { |
5d879392 | 389 | log_err("fio: can't set TLS key\n"); |
9eb271b9 JA |
390 | assert(0); |
391 | } | |
5d879392 JA |
392 | } |
393 | ||
394 | static void kill_tv_tls_key(void *data) | |
395 | { | |
396 | free(data); | |
397 | } | |
67bf9823 JA |
398 | #else |
399 | void fio_local_clock_init(int is_thread) | |
400 | { | |
401 | } | |
402 | #endif | |
5d879392 | 403 | |
09a32402 JA |
404 | void fio_clock_init(void) |
405 | { | |
01423eae JA |
406 | if (fio_clock_source == fio_clock_source_inited) |
407 | return; | |
408 | ||
67bf9823 | 409 | #ifndef CONFIG_TLS_THREAD |
5d879392 JA |
410 | if (pthread_key_create(&tv_tls_key, kill_tv_tls_key)) |
411 | log_err("fio: can't create TLS key\n"); | |
67bf9823 | 412 | #endif |
5d879392 | 413 | |
01423eae | 414 | fio_clock_source_inited = fio_clock_source; |
e259879e JA |
415 | |
416 | if (calibrate_cpu_clock()) | |
24575392 | 417 | tsc_reliable = false; |
fa80feae JA |
418 | |
419 | /* | |
420 | * If the arch sets tsc_reliable != 0, then it must be good enough | |
421 | * to use as THE clock source. For x86 CPUs, this means the TSC | |
422 | * runs at a constant rate and is synced across CPU cores. | |
423 | */ | |
424 | if (tsc_reliable) { | |
aad918e4 | 425 | if (!fio_clock_source_set && !fio_monotonic_clocktest(0)) |
fa80feae JA |
426 | fio_clock_source = CS_CPUCLOCK; |
427 | } else if (fio_clock_source == CS_CPUCLOCK) | |
428 | log_info("fio: clocksource=cpu may not be reliable\n"); | |
dae89ac9 | 429 | dprint(FD_TIME, "gettime: clocksource=%d\n", (int) fio_clock_source); |
c223da83 JA |
430 | } |
431 | ||
d6bb626e VF |
432 | uint64_t ntime_since(const struct timespec *s, const struct timespec *e) |
433 | { | |
434 | int64_t sec, nsec; | |
435 | ||
436 | sec = e->tv_sec - s->tv_sec; | |
437 | nsec = e->tv_nsec - s->tv_nsec; | |
438 | if (sec > 0 && nsec < 0) { | |
6d02b37b VF |
439 | sec--; |
440 | nsec += 1000000000LL; | |
d6bb626e VF |
441 | } |
442 | ||
443 | /* | |
6d02b37b VF |
444 | * time warp bug on some kernels? |
445 | */ | |
d6bb626e | 446 | if (sec < 0 || (sec == 0 && nsec < 0)) |
6d02b37b | 447 | return 0; |
d6bb626e VF |
448 | |
449 | return nsec + (sec * 1000000000LL); | |
450 | } | |
451 | ||
0410e783 JA |
452 | uint64_t ntime_since_now(const struct timespec *s) |
453 | { | |
454 | struct timespec now; | |
455 | ||
456 | fio_gettime(&now, NULL); | |
457 | return ntime_since(s, &now); | |
458 | } | |
459 | ||
8b6a404c | 460 | uint64_t utime_since(const struct timespec *s, const struct timespec *e) |
be4ecfdf | 461 | { |
90eff1c9 | 462 | int64_t sec, usec; |
39ab7da2 JA |
463 | |
464 | sec = e->tv_sec - s->tv_sec; | |
8b6a404c | 465 | usec = (e->tv_nsec - s->tv_nsec) / 1000; |
39ab7da2 JA |
466 | if (sec > 0 && usec < 0) { |
467 | sec--; | |
468 | usec += 1000000; | |
469 | } | |
470 | ||
471 | /* | |
472 | * time warp bug on some kernels? | |
473 | */ | |
474 | if (sec < 0 || (sec == 0 && usec < 0)) | |
475 | return 0; | |
476 | ||
20ac4e77 | 477 | return usec + (sec * 1000000); |
be4ecfdf JA |
478 | } |
479 | ||
8b6a404c | 480 | uint64_t utime_since_now(const struct timespec *s) |
be4ecfdf | 481 | { |
8b6a404c | 482 | struct timespec t; |
f52e9198 VF |
483 | #ifdef FIO_DEBUG_TIME |
484 | void *p = __builtin_return_address(0); | |
39ab7da2 | 485 | |
f52e9198 VF |
486 | fio_gettime(&t, p); |
487 | #else | |
39ab7da2 | 488 | fio_gettime(&t, NULL); |
f52e9198 VF |
489 | #endif |
490 | ||
39ab7da2 | 491 | return utime_since(s, &t); |
be4ecfdf | 492 | } |
783a3eb1 | 493 | |
8b6a404c | 494 | uint64_t mtime_since_tv(const struct timeval *s, const struct timeval *e) |
783a3eb1 | 495 | { |
8b6a404c | 496 | int64_t sec, usec; |
783a3eb1 | 497 | |
39ab7da2 | 498 | sec = e->tv_sec - s->tv_sec; |
8b6a404c | 499 | usec = (e->tv_usec - s->tv_usec); |
39ab7da2 JA |
500 | if (sec > 0 && usec < 0) { |
501 | sec--; | |
502 | usec += 1000000; | |
783a3eb1 JA |
503 | } |
504 | ||
39ab7da2 JA |
505 | if (sec < 0 || (sec == 0 && usec < 0)) |
506 | return 0; | |
507 | ||
be6bb2b7 Y |
508 | sec *= 1000; |
509 | usec /= 1000; | |
20ac4e77 | 510 | return sec + usec; |
783a3eb1 JA |
511 | } |
512 | ||
8b6a404c | 513 | uint64_t mtime_since_now(const struct timespec *s) |
783a3eb1 | 514 | { |
8b6a404c | 515 | struct timespec t; |
f52e9198 | 516 | #ifdef FIO_DEBUG_TIME |
39ab7da2 | 517 | void *p = __builtin_return_address(0); |
783a3eb1 | 518 | |
39ab7da2 | 519 | fio_gettime(&t, p); |
f52e9198 VF |
520 | #else |
521 | fio_gettime(&t, NULL); | |
522 | #endif | |
523 | ||
39ab7da2 JA |
524 | return mtime_since(s, &t); |
525 | } | |
783a3eb1 | 526 | |
8b6a404c VF |
527 | uint64_t mtime_since(const struct timespec *s, const struct timespec *e) |
528 | { | |
529 | int64_t sec, usec; | |
530 | ||
531 | sec = e->tv_sec - s->tv_sec; | |
532 | usec = (e->tv_nsec - s->tv_nsec) / 1000; | |
533 | if (sec > 0 && usec < 0) { | |
534 | sec--; | |
535 | usec += 1000000; | |
536 | } | |
537 | ||
538 | if (sec < 0 || (sec == 0 && usec < 0)) | |
539 | return 0; | |
540 | ||
541 | sec *= 1000; | |
542 | usec /= 1000; | |
543 | return sec + usec; | |
544 | } | |
545 | ||
546 | uint64_t time_since_now(const struct timespec *s) | |
39ab7da2 JA |
547 | { |
548 | return mtime_since_now(s) / 1000; | |
783a3eb1 | 549 | } |
7d11f871 | 550 | |
67bf9823 | 551 | #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) && \ |
a250edd0 | 552 | defined(CONFIG_SYNC_SYNC) && defined(CONFIG_CMP_SWAP) |
7d11f871 | 553 | |
aad918e4 | 554 | #define CLOCK_ENTRIES_DEBUG 100000 |
5283741f | 555 | #define CLOCK_ENTRIES_TEST 1000 |
7d11f871 JA |
556 | |
557 | struct clock_entry { | |
58002f9a JA |
558 | uint32_t seq; |
559 | uint32_t cpu; | |
ba458c2f | 560 | uint64_t tsc; |
7d11f871 JA |
561 | }; |
562 | ||
563 | struct clock_thread { | |
564 | pthread_t thread; | |
565 | int cpu; | |
aad918e4 | 566 | int debug; |
17031c6d | 567 | struct fio_sem lock; |
aad918e4 | 568 | unsigned long nr_entries; |
58002f9a | 569 | uint32_t *seq; |
7d11f871 JA |
570 | struct clock_entry *entries; |
571 | }; | |
572 | ||
a250edd0 JA |
573 | static inline uint32_t atomic32_compare_and_swap(uint32_t *ptr, uint32_t old, |
574 | uint32_t new) | |
7d11f871 | 575 | { |
a250edd0 | 576 | return __sync_val_compare_and_swap(ptr, old, new); |
7d11f871 JA |
577 | } |
578 | ||
579 | static void *clock_thread_fn(void *data) | |
580 | { | |
581 | struct clock_thread *t = data; | |
582 | struct clock_entry *c; | |
583 | os_cpu_mask_t cpu_mask; | |
5896d827 | 584 | unsigned long long first; |
7d11f871 JA |
585 | int i; |
586 | ||
c763aea6 | 587 | if (fio_cpuset_init(&cpu_mask)) { |
37e20021 | 588 | int __err = errno; |
c763aea6 JA |
589 | |
590 | log_err("clock cpuset init failed: %s\n", strerror(__err)); | |
591 | goto err_out; | |
592 | } | |
593 | ||
7d11f871 JA |
594 | fio_cpu_set(&cpu_mask, t->cpu); |
595 | ||
596 | if (fio_setaffinity(gettid(), cpu_mask) == -1) { | |
e66d7f90 JA |
597 | int __err = errno; |
598 | ||
599 | log_err("clock setaffinity failed: %s\n", strerror(__err)); | |
c763aea6 | 600 | goto err; |
7d11f871 JA |
601 | } |
602 | ||
17031c6d | 603 | fio_sem_down(&t->lock); |
7d11f871 | 604 | |
5896d827 | 605 | first = get_cpu_clock(); |
7d11f871 | 606 | c = &t->entries[0]; |
aad918e4 | 607 | for (i = 0; i < t->nr_entries; i++, c++) { |
58002f9a JA |
608 | uint32_t seq; |
609 | uint64_t tsc; | |
7d11f871 JA |
610 | |
611 | c->cpu = t->cpu; | |
612 | do { | |
a250edd0 JA |
613 | seq = *t->seq; |
614 | if (seq == UINT_MAX) | |
58002f9a | 615 | break; |
a250edd0 | 616 | __sync_synchronize(); |
7d11f871 | 617 | tsc = get_cpu_clock(); |
a250edd0 JA |
618 | } while (seq != atomic32_compare_and_swap(t->seq, seq, seq + 1)); |
619 | ||
620 | if (seq == UINT_MAX) | |
621 | break; | |
7d11f871 JA |
622 | |
623 | c->seq = seq; | |
624 | c->tsc = tsc; | |
625 | } | |
626 | ||
aad918e4 JA |
627 | if (t->debug) { |
628 | unsigned long long clocks; | |
629 | ||
630 | clocks = t->entries[i - 1].tsc - t->entries[0].tsc; | |
5896d827 JA |
631 | log_info("cs: cpu%3d: %llu clocks seen, first %llu\n", t->cpu, |
632 | clocks, first); | |
aad918e4 | 633 | } |
58002f9a | 634 | |
e259879e JA |
635 | /* |
636 | * The most common platform clock breakage is returning zero | |
637 | * indefinitely. Check for that and return failure. | |
638 | */ | |
a250edd0 | 639 | if (i > 1 && !t->entries[i - 1].tsc && !t->entries[0].tsc) |
c763aea6 | 640 | goto err; |
e259879e | 641 | |
d47d7cb3 | 642 | fio_cpuset_exit(&cpu_mask); |
7d11f871 | 643 | return NULL; |
c763aea6 JA |
644 | err: |
645 | fio_cpuset_exit(&cpu_mask); | |
646 | err_out: | |
647 | return (void *) 1; | |
7d11f871 JA |
648 | } |
649 | ||
650 | static int clock_cmp(const void *p1, const void *p2) | |
651 | { | |
652 | const struct clock_entry *c1 = p1; | |
653 | const struct clock_entry *c2 = p2; | |
654 | ||
b9b3498e JA |
655 | if (c1->seq == c2->seq) |
656 | log_err("cs: bug in atomic sequence!\n"); | |
657 | ||
7d11f871 JA |
658 | return c1->seq - c2->seq; |
659 | } | |
660 | ||
aad918e4 | 661 | int fio_monotonic_clocktest(int debug) |
7d11f871 | 662 | { |
8a1db9a1 | 663 | struct clock_thread *cthreads; |
7d11f871 JA |
664 | unsigned int nr_cpus = cpus_online(); |
665 | struct clock_entry *entries; | |
aad918e4 | 666 | unsigned long nr_entries, tentries, failed = 0; |
80da8a8f | 667 | struct clock_entry *prev, *this; |
58002f9a | 668 | uint32_t seq = 0; |
caa3eb1c | 669 | unsigned int i; |
7d11f871 | 670 | |
aad918e4 JA |
671 | if (debug) { |
672 | log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no"); | |
d5e3f5d8 | 673 | |
b5b571a3 | 674 | #ifdef FIO_INC_DEBUG |
aad918e4 | 675 | fio_debug |= 1U << FD_TIME; |
b5b571a3 | 676 | #endif |
aad918e4 JA |
677 | nr_entries = CLOCK_ENTRIES_DEBUG; |
678 | } else | |
679 | nr_entries = CLOCK_ENTRIES_TEST; | |
680 | ||
4f1d43c2 | 681 | calibrate_cpu_clock(); |
aad918e4 JA |
682 | |
683 | if (debug) { | |
b5b571a3 | 684 | #ifdef FIO_INC_DEBUG |
aad918e4 | 685 | fio_debug &= ~(1U << FD_TIME); |
b5b571a3 | 686 | #endif |
aad918e4 | 687 | } |
4f1d43c2 | 688 | |
8a1db9a1 | 689 | cthreads = malloc(nr_cpus * sizeof(struct clock_thread)); |
aad918e4 | 690 | tentries = nr_entries * nr_cpus; |
7d11f871 JA |
691 | entries = malloc(tentries * sizeof(struct clock_entry)); |
692 | ||
aad918e4 JA |
693 | if (debug) |
694 | log_info("cs: Testing %u CPUs\n", nr_cpus); | |
7d11f871 JA |
695 | |
696 | for (i = 0; i < nr_cpus; i++) { | |
8a1db9a1 | 697 | struct clock_thread *t = &cthreads[i]; |
7d11f871 JA |
698 | |
699 | t->cpu = i; | |
aad918e4 | 700 | t->debug = debug; |
7d11f871 | 701 | t->seq = &seq; |
aad918e4 JA |
702 | t->nr_entries = nr_entries; |
703 | t->entries = &entries[i * nr_entries]; | |
17031c6d | 704 | __fio_sem_init(&t->lock, FIO_SEM_LOCKED); |
6b0110cd JA |
705 | if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) { |
706 | failed++; | |
707 | nr_cpus = i; | |
708 | break; | |
709 | } | |
7d11f871 JA |
710 | } |
711 | ||
712 | for (i = 0; i < nr_cpus; i++) { | |
8a1db9a1 | 713 | struct clock_thread *t = &cthreads[i]; |
7d11f871 | 714 | |
17031c6d | 715 | fio_sem_up(&t->lock); |
7d11f871 JA |
716 | } |
717 | ||
814917be | 718 | for (i = 0; i < nr_cpus; i++) { |
8a1db9a1 | 719 | struct clock_thread *t = &cthreads[i]; |
7d11f871 JA |
720 | void *ret; |
721 | ||
722 | pthread_join(t->thread, &ret); | |
723 | if (ret) | |
724 | failed++; | |
17031c6d | 725 | __fio_sem_remove(&t->lock); |
7d11f871 | 726 | } |
8a1db9a1 | 727 | free(cthreads); |
7d11f871 JA |
728 | |
729 | if (failed) { | |
aad918e4 JA |
730 | if (debug) |
731 | log_err("Clocksource test: %lu threads failed\n", failed); | |
7d11f871 JA |
732 | goto err; |
733 | } | |
734 | ||
735 | qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp); | |
736 | ||
a4aa3dc0 JA |
737 | /* silence silly gcc */ |
738 | prev = NULL; | |
7d11f871 | 739 | for (failed = i = 0; i < tentries; i++) { |
80da8a8f | 740 | this = &entries[i]; |
7d11f871 JA |
741 | |
742 | if (!i) { | |
743 | prev = this; | |
744 | continue; | |
745 | } | |
746 | ||
747 | if (prev->tsc > this->tsc) { | |
748 | uint64_t diff = prev->tsc - this->tsc; | |
749 | ||
aad918e4 JA |
750 | if (!debug) { |
751 | failed++; | |
752 | break; | |
753 | } | |
754 | ||
4e0a8fa2 JA |
755 | log_info("cs: CPU clock mismatch (diff=%llu):\n", |
756 | (unsigned long long) diff); | |
757 | log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq); | |
758 | log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq); | |
7d11f871 JA |
759 | failed++; |
760 | } | |
761 | ||
762 | prev = this; | |
763 | } | |
764 | ||
aad918e4 JA |
765 | if (debug) { |
766 | if (failed) | |
767 | log_info("cs: Failed: %lu\n", failed); | |
768 | else | |
769 | log_info("cs: Pass!\n"); | |
770 | } | |
7d11f871 JA |
771 | err: |
772 | free(entries); | |
773 | return !!failed; | |
774 | } | |
775 | ||
776 | #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */ | |
777 | ||
aad918e4 | 778 | int fio_monotonic_clocktest(int debug) |
7d11f871 | 779 | { |
aad918e4 JA |
780 | if (debug) |
781 | log_info("cs: current platform does not support CPU clocks\n"); | |
782 | return 1; | |
7d11f871 JA |
783 | } |
784 | ||
785 | #endif |