blob: 2dc976fb42c7bbb5b4deedc0746d5577bd5c1e0b [file] [log] [blame]
Jens Axboe39ab7da2012-11-06 22:10:43 +01001#include <unistd.h>
2#include <math.h>
3#include <sys/time.h>
4#include <time.h>
5
6#include "fio.h"
7#include "smalloc.h"
8
Jens Axboe5be4c942013-02-11 16:33:25 +01009struct timeval *fio_tv = NULL;
Jens Axboe39ab7da2012-11-06 22:10:43 +010010int fio_gtod_offload = 0;
Jens Axboe39ab7da2012-11-06 22:10:43 +010011static pthread_t gtod_thread;
Jens Axboed1be5f82014-12-19 15:04:10 -070012#ifdef FIO_HAVE_CPU_AFFINITY
13static os_cpu_mask_t fio_gtod_cpumask;
14#endif
Jens Axboe39ab7da2012-11-06 22:10:43 +010015
16void fio_gtod_init(void)
17{
Jens Axboe80ac0202014-12-16 20:38:53 -070018 if (fio_tv)
19 return;
20
Jens Axboe39ab7da2012-11-06 22:10:43 +010021 fio_tv = smalloc(sizeof(struct timeval));
Jens Axboefba5c5f2013-01-29 22:29:09 +010022 if (!fio_tv)
23 log_err("fio: smalloc pool exhausted\n");
Jens Axboe39ab7da2012-11-06 22:10:43 +010024}
25
26static void fio_gtod_update(void)
27{
Jens Axboe04194192014-12-16 19:43:55 -070028 if (fio_tv) {
29 struct timeval __tv;
30
31 gettimeofday(&__tv, NULL);
32 fio_tv->tv_sec = __tv.tv_sec;
33 write_barrier();
34 fio_tv->tv_usec = __tv.tv_usec;
35 write_barrier();
36 }
Jens Axboe39ab7da2012-11-06 22:10:43 +010037}
38
Jens Axboe80ac0202014-12-16 20:38:53 -070039struct gtod_cpu_data {
40 struct fio_mutex *mutex;
41 unsigned int cpu;
42};
43
Jens Axboe39ab7da2012-11-06 22:10:43 +010044static void *gtod_thread_main(void *data)
45{
46 struct fio_mutex *mutex = data;
47
Jens Axboe80ac0202014-12-16 20:38:53 -070048 fio_setaffinity(gettid(), fio_gtod_cpumask);
Jens Axboe39ab7da2012-11-06 22:10:43 +010049 fio_mutex_up(mutex);
50
51 /*
52 * As long as we have jobs around, update the clock. It would be nice
53 * to have some way of NOT hammering that CPU with gettimeofday(),
54 * but I'm not sure what to use outside of a simple CPU nop to relax
55 * it - we don't want to lose precision.
56 */
57 while (threads) {
58 fio_gtod_update();
59 nop;
60 }
61
62 return NULL;
63}
64
65int fio_start_gtod_thread(void)
66{
67 struct fio_mutex *mutex;
68 pthread_attr_t attr;
69 int ret;
70
71 mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
72 if (!mutex)
73 return 1;
74
75 pthread_attr_init(&attr);
76 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
Jens Axboe24961012014-12-16 19:49:54 -070077 ret = pthread_create(&gtod_thread, &attr, gtod_thread_main, mutex);
Jens Axboe39ab7da2012-11-06 22:10:43 +010078 pthread_attr_destroy(&attr);
79 if (ret) {
80 log_err("Can't create gtod thread: %s\n", strerror(ret));
81 goto err;
82 }
83
84 ret = pthread_detach(gtod_thread);
85 if (ret) {
86 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
87 goto err;
88 }
89
90 dprint(FD_MUTEX, "wait on startup_mutex\n");
91 fio_mutex_down(mutex);
92 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
93err:
94 fio_mutex_remove(mutex);
95 return ret;
96}
97
Jens Axboe80ac0202014-12-16 20:38:53 -070098void fio_gtod_set_cpu(unsigned int cpu)
99{
Jens Axboe32bbea42014-12-19 15:03:15 -0700100#ifdef FIO_HAVE_CPU_AFFINITY
Jens Axboe80ac0202014-12-16 20:38:53 -0700101 fio_cpu_set(&fio_gtod_cpumask, cpu);
Jens Axboe32bbea42014-12-19 15:03:15 -0700102#endif
Jens Axboe80ac0202014-12-16 20:38:53 -0700103}