Commit | Line | Data |
---|---|---|
39ab7da2 JA |
1 | #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 | ||
8b6a404c | 9 | struct timespec *fio_ts = NULL; |
39ab7da2 | 10 | int fio_gtod_offload = 0; |
39ab7da2 | 11 | static pthread_t gtod_thread; |
8a2b393b | 12 | static os_cpu_mask_t fio_gtod_cpumask; |
39ab7da2 JA |
13 | |
14 | void fio_gtod_init(void) | |
15 | { | |
8b6a404c | 16 | if (fio_ts) |
79c896a1 JA |
17 | return; |
18 | ||
8b6a404c VF |
19 | fio_ts = smalloc(sizeof(*fio_ts)); |
20 | if (!fio_ts) | |
fba5c5ff | 21 | log_err("fio: smalloc pool exhausted\n"); |
39ab7da2 JA |
22 | } |
23 | ||
24 | static void fio_gtod_update(void) | |
25 | { | |
8b6a404c | 26 | if (fio_ts) { |
27325ed5 JA |
27 | struct timeval __tv; |
28 | ||
29 | gettimeofday(&__tv, NULL); | |
8b6a404c | 30 | fio_ts->tv_sec = __tv.tv_sec; |
27325ed5 | 31 | write_barrier(); |
8b6a404c | 32 | fio_ts->tv_nsec = __tv.tv_usec * 1000; |
27325ed5 JA |
33 | write_barrier(); |
34 | } | |
39ab7da2 JA |
35 | } |
36 | ||
79c896a1 JA |
37 | struct gtod_cpu_data { |
38 | struct fio_mutex *mutex; | |
39 | unsigned int cpu; | |
40 | }; | |
41 | ||
39ab7da2 JA |
42 | static void *gtod_thread_main(void *data) |
43 | { | |
44 | struct fio_mutex *mutex = data; | |
48d86be5 JA |
45 | int ret; |
46 | ||
47 | ret = fio_setaffinity(gettid(), fio_gtod_cpumask); | |
39ab7da2 JA |
48 | |
49 | fio_mutex_up(mutex); | |
50 | ||
48d86be5 JA |
51 | if (ret == -1) { |
52 | log_err("gtod: setaffinity failed\n"); | |
53 | return NULL; | |
54 | } | |
55 | ||
39ab7da2 JA |
56 | /* |
57 | * As long as we have jobs around, update the clock. It would be nice | |
58 | * to have some way of NOT hammering that CPU with gettimeofday(), | |
59 | * but I'm not sure what to use outside of a simple CPU nop to relax | |
60 | * it - we don't want to lose precision. | |
61 | */ | |
62 | while (threads) { | |
63 | fio_gtod_update(); | |
64 | nop; | |
65 | } | |
66 | ||
67 | return NULL; | |
68 | } | |
69 | ||
70 | int fio_start_gtod_thread(void) | |
71 | { | |
72 | struct fio_mutex *mutex; | |
73 | pthread_attr_t attr; | |
74 | int ret; | |
75 | ||
76 | mutex = fio_mutex_init(FIO_MUTEX_LOCKED); | |
77 | if (!mutex) | |
78 | return 1; | |
79 | ||
80 | pthread_attr_init(&attr); | |
45213f1b | 81 | pthread_attr_setstacksize(&attr, 2 * PTHREAD_STACK_MIN); |
80fa0520 | 82 | ret = pthread_create(>od_thread, &attr, gtod_thread_main, mutex); |
39ab7da2 JA |
83 | pthread_attr_destroy(&attr); |
84 | if (ret) { | |
85 | log_err("Can't create gtod thread: %s\n", strerror(ret)); | |
86 | goto err; | |
87 | } | |
88 | ||
89 | ret = pthread_detach(gtod_thread); | |
90 | if (ret) { | |
bc0fe8b8 | 91 | log_err("Can't detach gtod thread: %s\n", strerror(ret)); |
39ab7da2 JA |
92 | goto err; |
93 | } | |
94 | ||
95 | dprint(FD_MUTEX, "wait on startup_mutex\n"); | |
96 | fio_mutex_down(mutex); | |
97 | dprint(FD_MUTEX, "done waiting on startup_mutex\n"); | |
98 | err: | |
99 | fio_mutex_remove(mutex); | |
100 | return ret; | |
101 | } | |
102 | ||
79c896a1 JA |
103 | void fio_gtod_set_cpu(unsigned int cpu) |
104 | { | |
2a2f27b8 | 105 | #ifdef FIO_HAVE_CPU_AFFINITY |
79c896a1 | 106 | fio_cpu_set(&fio_gtod_cpumask, cpu); |
2a2f27b8 | 107 | #endif |
79c896a1 | 108 | } |