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