• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Clock functions
3  */
4 
5 #include <unistd.h>
6 #include <math.h>
7 #include <sys/time.h>
8 #include <time.h>
9 
10 #include "fio.h"
11 #include "smalloc.h"
12 
13 #include "hash.h"
14 #include "os/os.h"
15 
16 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
17 static unsigned long cycles_per_usec;
18 static unsigned long inv_cycles_per_usec;
19 #endif
20 int tsc_reliable = 0;
21 
22 struct tv_valid {
23 	struct timeval last_tv;
24 	uint64_t last_cycles;
25 	int last_tv_valid;
26 };
27 #ifdef CONFIG_TLS_THREAD
28 static __thread struct tv_valid static_tv_valid;
29 #else
30 static pthread_key_t tv_tls_key;
31 #endif
32 
33 enum fio_cs fio_clock_source = FIO_PREFERRED_CLOCK_SOURCE;
34 int fio_clock_source_set = 0;
35 static enum fio_cs fio_clock_source_inited = CS_INVAL;
36 
37 #ifdef FIO_DEBUG_TIME
38 
39 #define HASH_BITS	8
40 #define HASH_SIZE	(1 << HASH_BITS)
41 
42 static struct flist_head hash[HASH_SIZE];
43 static int gtod_inited;
44 
45 struct gtod_log {
46 	struct flist_head list;
47 	void *caller;
48 	unsigned long calls;
49 };
50 
find_hash(void * caller)51 static struct gtod_log *find_hash(void *caller)
52 {
53 	unsigned long h = hash_ptr(caller, HASH_BITS);
54 	struct flist_head *entry;
55 
56 	flist_for_each(entry, &hash[h]) {
57 		struct gtod_log *log = flist_entry(entry, struct gtod_log,
58 									list);
59 
60 		if (log->caller == caller)
61 			return log;
62 	}
63 
64 	return NULL;
65 }
66 
find_log(void * caller)67 static struct gtod_log *find_log(void *caller)
68 {
69 	struct gtod_log *log = find_hash(caller);
70 
71 	if (!log) {
72 		unsigned long h;
73 
74 		log = malloc(sizeof(*log));
75 		INIT_FLIST_HEAD(&log->list);
76 		log->caller = caller;
77 		log->calls = 0;
78 
79 		h = hash_ptr(caller, HASH_BITS);
80 		flist_add_tail(&log->list, &hash[h]);
81 	}
82 
83 	return log;
84 }
85 
gtod_log_caller(void * caller)86 static void gtod_log_caller(void *caller)
87 {
88 	if (gtod_inited) {
89 		struct gtod_log *log = find_log(caller);
90 
91 		log->calls++;
92 	}
93 }
94 
fio_dump_gtod(void)95 static void fio_exit fio_dump_gtod(void)
96 {
97 	unsigned long total_calls = 0;
98 	int i;
99 
100 	for (i = 0; i < HASH_SIZE; i++) {
101 		struct flist_head *entry;
102 		struct gtod_log *log;
103 
104 		flist_for_each(entry, &hash[i]) {
105 			log = flist_entry(entry, struct gtod_log, list);
106 
107 			printf("function %p, calls %lu\n", log->caller,
108 								log->calls);
109 			total_calls += log->calls;
110 		}
111 	}
112 
113 	printf("Total %lu gettimeofday\n", total_calls);
114 }
115 
gtod_init(void)116 static void fio_init gtod_init(void)
117 {
118 	int i;
119 
120 	for (i = 0; i < HASH_SIZE; i++)
121 		INIT_FLIST_HEAD(&hash[i]);
122 
123 	gtod_inited = 1;
124 }
125 
126 #endif /* FIO_DEBUG_TIME */
127 
128 #ifdef CONFIG_CLOCK_GETTIME
fill_clock_gettime(struct timespec * ts)129 static int fill_clock_gettime(struct timespec *ts)
130 {
131 #ifdef CONFIG_CLOCK_MONOTONIC
132 	return clock_gettime(CLOCK_MONOTONIC, ts);
133 #else
134 	return clock_gettime(CLOCK_REALTIME, ts);
135 #endif
136 }
137 #endif
138 
__fio_gettime(struct timeval * tp)139 static void *__fio_gettime(struct timeval *tp)
140 {
141 	struct tv_valid *tv;
142 
143 #ifdef CONFIG_TLS_THREAD
144 	tv = &static_tv_valid;
145 #else
146 	tv = pthread_getspecific(tv_tls_key);
147 #endif
148 
149 	switch (fio_clock_source) {
150 #ifdef CONFIG_GETTIMEOFDAY
151 	case CS_GTOD:
152 		gettimeofday(tp, NULL);
153 		break;
154 #endif
155 #ifdef CONFIG_CLOCK_GETTIME
156 	case CS_CGETTIME: {
157 		struct timespec ts;
158 
159 		if (fill_clock_gettime(&ts) < 0) {
160 			log_err("fio: clock_gettime fails\n");
161 			assert(0);
162 		}
163 
164 		tp->tv_sec = ts.tv_sec;
165 		tp->tv_usec = ts.tv_nsec / 1000;
166 		break;
167 		}
168 #endif
169 #ifdef ARCH_HAVE_CPU_CLOCK
170 	case CS_CPUCLOCK: {
171 		uint64_t usecs, t;
172 
173 		t = get_cpu_clock();
174 		if (tv && t < tv->last_cycles) {
175 			dprint(FD_TIME, "CPU clock going back in time\n");
176 			t = tv->last_cycles;
177 		} else if (tv)
178 			tv->last_cycles = t;
179 
180 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
181 		usecs = t / ARCH_CPU_CLOCK_CYCLES_PER_USEC;
182 #else
183 		usecs = (t * inv_cycles_per_usec) / 16777216UL;
184 #endif
185 		tp->tv_sec = usecs / 1000000;
186 		tp->tv_usec = usecs % 1000000;
187 		break;
188 		}
189 #endif
190 	default:
191 		log_err("fio: invalid clock source %d\n", fio_clock_source);
192 		break;
193 	}
194 
195 	return tv;
196 }
197 
198 #ifdef FIO_DEBUG_TIME
fio_gettime(struct timeval * tp,void * caller)199 void fio_gettime(struct timeval *tp, void *caller)
200 #else
201 void fio_gettime(struct timeval *tp, void fio_unused *caller)
202 #endif
203 {
204 	struct tv_valid *tv;
205 
206 #ifdef FIO_DEBUG_TIME
207 	if (!caller)
208 		caller = __builtin_return_address(0);
209 
210 	gtod_log_caller(caller);
211 #endif
212 	if (fio_unlikely(fio_tv)) {
213 		memcpy(tp, fio_tv, sizeof(*tp));
214 		return;
215 	}
216 
217 	tv = __fio_gettime(tp);
218 
219 	/*
220 	 * If Linux is using the tsc clock on non-synced processors,
221 	 * sometimes time can appear to drift backwards. Fix that up.
222 	 */
223 	if (tv) {
224 		if (tv->last_tv_valid) {
225 			if (tp->tv_sec < tv->last_tv.tv_sec)
226 				tp->tv_sec = tv->last_tv.tv_sec;
227 			else if (tv->last_tv.tv_sec == tp->tv_sec &&
228 				 tp->tv_usec < tv->last_tv.tv_usec)
229 				tp->tv_usec = tv->last_tv.tv_usec;
230 		}
231 		tv->last_tv_valid = 1;
232 		memcpy(&tv->last_tv, tp, sizeof(*tp));
233 	}
234 }
235 
236 #if defined(ARCH_HAVE_CPU_CLOCK) && !defined(ARCH_CPU_CLOCK_CYCLES_PER_USEC)
get_cycles_per_usec(void)237 static unsigned long get_cycles_per_usec(void)
238 {
239 	struct timeval s, e;
240 	uint64_t c_s, c_e;
241 	enum fio_cs old_cs = fio_clock_source;
242 
243 #ifdef CONFIG_CLOCK_GETTIME
244 	fio_clock_source = CS_CGETTIME;
245 #else
246 	fio_clock_source = CS_GTOD;
247 #endif
248 	__fio_gettime(&s);
249 
250 	c_s = get_cpu_clock();
251 	do {
252 		uint64_t elapsed;
253 
254 		__fio_gettime(&e);
255 
256 		elapsed = utime_since(&s, &e);
257 		if (elapsed >= 1280) {
258 			c_e = get_cpu_clock();
259 			break;
260 		}
261 	} while (1);
262 
263 	fio_clock_source = old_cs;
264 	return (c_e - c_s + 127) >> 7;
265 }
266 
267 #define NR_TIME_ITERS	50
268 
calibrate_cpu_clock(void)269 static int calibrate_cpu_clock(void)
270 {
271 	double delta, mean, S;
272 	uint64_t avg, cycles[NR_TIME_ITERS];
273 	int i, samples;
274 
275 	cycles[0] = get_cycles_per_usec();
276 	S = delta = mean = 0.0;
277 	for (i = 0; i < NR_TIME_ITERS; i++) {
278 		cycles[i] = get_cycles_per_usec();
279 		delta = cycles[i] - mean;
280 		if (delta) {
281 			mean += delta / (i + 1.0);
282 			S += delta * (cycles[i] - mean);
283 		}
284 	}
285 
286 	/*
287 	 * The most common platform clock breakage is returning zero
288 	 * indefinitely. Check for that and return failure.
289 	 */
290 	if (!cycles[0] && !cycles[NR_TIME_ITERS - 1])
291 		return 1;
292 
293 	S = sqrt(S / (NR_TIME_ITERS - 1.0));
294 
295 	samples = avg = 0;
296 	for (i = 0; i < NR_TIME_ITERS; i++) {
297 		double this = cycles[i];
298 
299 		if ((fmax(this, mean) - fmin(this, mean)) > S)
300 			continue;
301 		samples++;
302 		avg += this;
303 	}
304 
305 	S /= (double) NR_TIME_ITERS;
306 	mean /= 10.0;
307 
308 	for (i = 0; i < NR_TIME_ITERS; i++)
309 		dprint(FD_TIME, "cycles[%d]=%llu\n", i,
310 					(unsigned long long) cycles[i] / 10);
311 
312 	avg /= samples;
313 	avg = (avg + 5) / 10;
314 	dprint(FD_TIME, "avg: %llu\n", (unsigned long long) avg);
315 	dprint(FD_TIME, "mean=%f, S=%f\n", mean, S);
316 
317 	cycles_per_usec = avg;
318 	inv_cycles_per_usec = 16777216UL / cycles_per_usec;
319 	dprint(FD_TIME, "inv_cycles_per_usec=%lu\n", inv_cycles_per_usec);
320 	return 0;
321 }
322 #else
calibrate_cpu_clock(void)323 static int calibrate_cpu_clock(void)
324 {
325 #ifdef ARCH_CPU_CLOCK_CYCLES_PER_USEC
326 	return 0;
327 #else
328 	return 1;
329 #endif
330 }
331 #endif // ARCH_HAVE_CPU_CLOCK
332 
333 #ifndef CONFIG_TLS_THREAD
fio_local_clock_init(int is_thread)334 void fio_local_clock_init(int is_thread)
335 {
336 	struct tv_valid *t;
337 
338 	t = calloc(1, sizeof(*t));
339 	if (pthread_setspecific(tv_tls_key, t))
340 		log_err("fio: can't set TLS key\n");
341 }
342 
kill_tv_tls_key(void * data)343 static void kill_tv_tls_key(void *data)
344 {
345 	free(data);
346 }
347 #else
fio_local_clock_init(int is_thread)348 void fio_local_clock_init(int is_thread)
349 {
350 }
351 #endif
352 
fio_clock_init(void)353 void fio_clock_init(void)
354 {
355 	if (fio_clock_source == fio_clock_source_inited)
356 		return;
357 
358 #ifndef CONFIG_TLS_THREAD
359 	if (pthread_key_create(&tv_tls_key, kill_tv_tls_key))
360 		log_err("fio: can't create TLS key\n");
361 #endif
362 
363 	fio_clock_source_inited = fio_clock_source;
364 
365 	if (calibrate_cpu_clock())
366 		tsc_reliable = 0;
367 
368 	/*
369 	 * If the arch sets tsc_reliable != 0, then it must be good enough
370 	 * to use as THE clock source. For x86 CPUs, this means the TSC
371 	 * runs at a constant rate and is synced across CPU cores.
372 	 */
373 	if (tsc_reliable) {
374 		if (!fio_clock_source_set)
375 			fio_clock_source = CS_CPUCLOCK;
376 	} else if (fio_clock_source == CS_CPUCLOCK)
377 		log_info("fio: clocksource=cpu may not be reliable\n");
378 }
379 
utime_since(struct timeval * s,struct timeval * e)380 uint64_t utime_since(struct timeval *s, struct timeval *e)
381 {
382 	long sec, usec;
383 	uint64_t ret;
384 
385 	sec = e->tv_sec - s->tv_sec;
386 	usec = e->tv_usec - s->tv_usec;
387 	if (sec > 0 && usec < 0) {
388 		sec--;
389 		usec += 1000000;
390 	}
391 
392 	/*
393 	 * time warp bug on some kernels?
394 	 */
395 	if (sec < 0 || (sec == 0 && usec < 0))
396 		return 0;
397 
398 	ret = sec * 1000000ULL + usec;
399 
400 	return ret;
401 }
402 
utime_since_now(struct timeval * s)403 uint64_t utime_since_now(struct timeval *s)
404 {
405 	struct timeval t;
406 
407 	fio_gettime(&t, NULL);
408 	return utime_since(s, &t);
409 }
410 
mtime_since(struct timeval * s,struct timeval * e)411 uint64_t mtime_since(struct timeval *s, struct timeval *e)
412 {
413 	long sec, usec, ret;
414 
415 	sec = e->tv_sec - s->tv_sec;
416 	usec = e->tv_usec - s->tv_usec;
417 	if (sec > 0 && usec < 0) {
418 		sec--;
419 		usec += 1000000;
420 	}
421 
422 	if (sec < 0 || (sec == 0 && usec < 0))
423 		return 0;
424 
425 	sec *= 1000UL;
426 	usec /= 1000UL;
427 	ret = sec + usec;
428 
429 	return ret;
430 }
431 
mtime_since_now(struct timeval * s)432 uint64_t mtime_since_now(struct timeval *s)
433 {
434 	struct timeval t;
435 	void *p = __builtin_return_address(0);
436 
437 	fio_gettime(&t, p);
438 	return mtime_since(s, &t);
439 }
440 
time_since_now(struct timeval * s)441 uint64_t time_since_now(struct timeval *s)
442 {
443 	return mtime_since_now(s) / 1000;
444 }
445 
446 #if defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK)  && \
447     defined(CONFIG_SFAA)
448 
449 #define CLOCK_ENTRIES	100000
450 
451 struct clock_entry {
452 	uint32_t seq;
453 	uint32_t cpu;
454 	uint64_t tsc;
455 };
456 
457 struct clock_thread {
458 	pthread_t thread;
459 	int cpu;
460 	pthread_mutex_t lock;
461 	pthread_mutex_t started;
462 	uint32_t *seq;
463 	struct clock_entry *entries;
464 };
465 
atomic32_inc_return(uint32_t * seq)466 static inline uint32_t atomic32_inc_return(uint32_t *seq)
467 {
468 	return 1 + __sync_fetch_and_add(seq, 1);
469 }
470 
clock_thread_fn(void * data)471 static void *clock_thread_fn(void *data)
472 {
473 	struct clock_thread *t = data;
474 	struct clock_entry *c;
475 	os_cpu_mask_t cpu_mask;
476 	uint32_t last_seq;
477 	int i;
478 
479 	memset(&cpu_mask, 0, sizeof(cpu_mask));
480 	fio_cpu_set(&cpu_mask, t->cpu);
481 
482 	if (fio_setaffinity(gettid(), cpu_mask) == -1) {
483 		log_err("clock setaffinity failed\n");
484 		return (void *) 1;
485 	}
486 
487 	pthread_mutex_lock(&t->lock);
488 	pthread_mutex_unlock(&t->started);
489 
490 	last_seq = 0;
491 	c = &t->entries[0];
492 	for (i = 0; i < CLOCK_ENTRIES; i++, c++) {
493 		uint32_t seq;
494 		uint64_t tsc;
495 
496 		c->cpu = t->cpu;
497 		do {
498 			seq = atomic32_inc_return(t->seq);
499 			if (seq < last_seq)
500 				break;
501 			tsc = get_cpu_clock();
502 		} while (seq != *t->seq);
503 
504 		c->seq = seq;
505 		c->tsc = tsc;
506 	}
507 
508 	log_info("cs: cpu%3d: %llu clocks seen\n", t->cpu,
509 		(unsigned long long) t->entries[i - 1].tsc - t->entries[0].tsc);
510 
511 	/*
512 	 * The most common platform clock breakage is returning zero
513 	 * indefinitely. Check for that and return failure.
514 	 */
515 	if (!t->entries[i - 1].tsc && !t->entries[0].tsc)
516 		return (void *) 1;
517 
518 	return NULL;
519 }
520 
clock_cmp(const void * p1,const void * p2)521 static int clock_cmp(const void *p1, const void *p2)
522 {
523 	const struct clock_entry *c1 = p1;
524 	const struct clock_entry *c2 = p2;
525 
526 	if (c1->seq == c2->seq)
527 		log_err("cs: bug in atomic sequence!\n");
528 
529 	return c1->seq - c2->seq;
530 }
531 
fio_monotonic_clocktest(void)532 int fio_monotonic_clocktest(void)
533 {
534 	struct clock_thread *threads;
535 	unsigned int nr_cpus = cpus_online();
536 	struct clock_entry *entries;
537 	unsigned long tentries, failed = 0;
538 	struct clock_entry *prev, *this;
539 	uint32_t seq = 0;
540 	unsigned int i;
541 
542 	log_info("cs: reliable_tsc: %s\n", tsc_reliable ? "yes" : "no");
543 
544 	fio_debug |= 1U << FD_TIME;
545 	calibrate_cpu_clock();
546 	fio_debug &= ~(1U << FD_TIME);
547 
548 	threads = malloc(nr_cpus * sizeof(struct clock_thread));
549 	tentries = CLOCK_ENTRIES * nr_cpus;
550 	entries = malloc(tentries * sizeof(struct clock_entry));
551 
552 	log_info("cs: Testing %u CPUs\n", nr_cpus);
553 
554 	for (i = 0; i < nr_cpus; i++) {
555 		struct clock_thread *t = &threads[i];
556 
557 		t->cpu = i;
558 		t->seq = &seq;
559 		t->entries = &entries[i * CLOCK_ENTRIES];
560 		pthread_mutex_init(&t->lock, NULL);
561 		pthread_mutex_init(&t->started, NULL);
562 		pthread_mutex_lock(&t->lock);
563 		if (pthread_create(&t->thread, NULL, clock_thread_fn, t)) {
564 			failed++;
565 			nr_cpus = i;
566 			break;
567 		}
568 	}
569 
570 	for (i = 0; i < nr_cpus; i++) {
571 		struct clock_thread *t = &threads[i];
572 
573 		pthread_mutex_lock(&t->started);
574 	}
575 
576 	for (i = 0; i < nr_cpus; i++) {
577 		struct clock_thread *t = &threads[i];
578 
579 		pthread_mutex_unlock(&t->lock);
580 	}
581 
582 	for (i = 0; i < nr_cpus; i++) {
583 		struct clock_thread *t = &threads[i];
584 		void *ret;
585 
586 		pthread_join(t->thread, &ret);
587 		if (ret)
588 			failed++;
589 	}
590 	free(threads);
591 
592 	if (failed) {
593 		log_err("Clocksource test: %lu threads failed\n", failed);
594 		goto err;
595 	}
596 
597 	qsort(entries, tentries, sizeof(struct clock_entry), clock_cmp);
598 
599 	for (failed = i = 0; i < tentries; i++) {
600 		this = &entries[i];
601 
602 		if (!i) {
603 			prev = this;
604 			continue;
605 		}
606 
607 		if (prev->tsc > this->tsc) {
608 			uint64_t diff = prev->tsc - this->tsc;
609 
610 			log_info("cs: CPU clock mismatch (diff=%llu):\n",
611 						(unsigned long long) diff);
612 			log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", prev->cpu, (unsigned long long) prev->tsc, prev->seq);
613 			log_info("\t CPU%3u: TSC=%llu, SEQ=%u\n", this->cpu, (unsigned long long) this->tsc, this->seq);
614 			failed++;
615 		}
616 
617 		prev = this;
618 	}
619 
620 	if (failed)
621 		log_info("cs: Failed: %lu\n", failed);
622 	else
623 		log_info("cs: Pass!\n");
624 
625 err:
626 	free(entries);
627 	return !!failed;
628 }
629 
630 #else /* defined(FIO_HAVE_CPU_AFFINITY) && defined(ARCH_HAVE_CPU_CLOCK) */
631 
fio_monotonic_clocktest(void)632 int fio_monotonic_clocktest(void)
633 {
634 	log_info("cs: current platform does not support CPU clocks\n");
635 	return 0;
636 }
637 
638 #endif
639