blob: 95f35c32e46cff25f6b43005462f608e1975fabf [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;
11int fio_gtod_cpu = -1;
12static pthread_t gtod_thread;
13
14void fio_gtod_init(void)
15{
16 fio_tv = smalloc(sizeof(struct timeval));
Jens Axboefba5c5f2013-01-29 22:29:09 +010017 if (!fio_tv)
18 log_err("fio: smalloc pool exhausted\n");
Jens Axboe39ab7da2012-11-06 22:10:43 +010019}
20
21static void fio_gtod_update(void)
22{
Jens Axboe04194192014-12-16 19:43:55 -070023 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 }
Jens Axboe39ab7da2012-11-06 22:10:43 +010032}
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);
Jens Axboe24961012014-12-16 19:49:54 -070066 ret = pthread_create(&gtod_thread, &attr, gtod_thread_main, mutex);
Jens Axboe39ab7da2012-11-06 22:10:43 +010067 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