gettime-thread: fix missing startup mutex
[fio.git] / gettime-thread.c
... / ...
CommitLineData
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
9struct timeval *fio_tv = NULL;
10int fio_gtod_offload = 0;
11int fio_gtod_cpu = -1;
12static pthread_t gtod_thread;
13
14void fio_gtod_init(void)
15{
16 fio_tv = smalloc(sizeof(struct timeval));
17 if (!fio_tv)
18 log_err("fio: smalloc pool exhausted\n");
19}
20
21static void fio_gtod_update(void)
22{
23 if (fio_tv) {
24 struct timeval __tv;
25
26 gettimeofday(&__tv, NULL);
27 fio_tv->tv_sec = __tv.tv_sec;
28 write_barrier();
29 fio_tv->tv_usec = __tv.tv_usec;
30 write_barrier();
31 }
32}
33
34static void *gtod_thread_main(void *data)
35{
36 struct fio_mutex *mutex = data;
37
38 fio_mutex_up(mutex);
39
40 /*
41 * As long as we have jobs around, update the clock. It would be nice
42 * to have some way of NOT hammering that CPU with gettimeofday(),
43 * but I'm not sure what to use outside of a simple CPU nop to relax
44 * it - we don't want to lose precision.
45 */
46 while (threads) {
47 fio_gtod_update();
48 nop;
49 }
50
51 return NULL;
52}
53
54int fio_start_gtod_thread(void)
55{
56 struct fio_mutex *mutex;
57 pthread_attr_t attr;
58 int ret;
59
60 mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
61 if (!mutex)
62 return 1;
63
64 pthread_attr_init(&attr);
65 pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
66 ret = pthread_create(&gtod_thread, &attr, gtod_thread_main, mutex);
67 pthread_attr_destroy(&attr);
68 if (ret) {
69 log_err("Can't create gtod thread: %s\n", strerror(ret));
70 goto err;
71 }
72
73 ret = pthread_detach(gtod_thread);
74 if (ret) {
75 log_err("Can't detatch gtod thread: %s\n", strerror(ret));
76 goto err;
77 }
78
79 dprint(FD_MUTEX, "wait on startup_mutex\n");
80 fio_mutex_down(mutex);
81 dprint(FD_MUTEX, "done waiting on startup_mutex\n");
82err:
83 fio_mutex_remove(mutex);
84 return ret;
85}
86
87