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