c890da4a3869327a9a5387d1f306487e8488cb1a
[fio.git] / helper_thread.c
1 #include "fio.h"
2 #include "smalloc.h"
3 #include "helper_thread.h"
4 #include "steadystate.h"
5 #include "pshared.h"
6
7 static struct helper_data {
8         volatile int exit;
9         volatile int reset;
10         volatile int do_stat;
11         struct sk_out *sk_out;
12         pthread_t thread;
13         pthread_mutex_t lock;
14         pthread_cond_t cond;
15         struct fio_sem *startup_sem;
16 } *helper_data;
17
18 void helper_thread_destroy(void)
19 {
20         pthread_cond_destroy(&helper_data->cond);
21         pthread_mutex_destroy(&helper_data->lock);
22         sfree(helper_data);
23 }
24
25 void helper_reset(void)
26 {
27         if (!helper_data)
28                 return;
29
30         pthread_mutex_lock(&helper_data->lock);
31
32         if (!helper_data->reset) {
33                 helper_data->reset = 1;
34                 pthread_cond_signal(&helper_data->cond);
35         }
36
37         pthread_mutex_unlock(&helper_data->lock);
38 }
39
40 void helper_do_stat(void)
41 {
42         if (!helper_data)
43                 return;
44
45         pthread_mutex_lock(&helper_data->lock);
46         helper_data->do_stat = 1;
47         pthread_cond_signal(&helper_data->cond);
48         pthread_mutex_unlock(&helper_data->lock);
49 }
50
51 bool helper_should_exit(void)
52 {
53         if (!helper_data)
54                 return true;
55
56         return helper_data->exit;
57 }
58
59 void helper_thread_exit(void)
60 {
61         void *ret;
62
63         pthread_mutex_lock(&helper_data->lock);
64         helper_data->exit = 1;
65         pthread_cond_signal(&helper_data->cond);
66         pthread_mutex_unlock(&helper_data->lock);
67
68         pthread_join(helper_data->thread, &ret);
69 }
70
71 static void *helper_thread_main(void *data)
72 {
73         struct helper_data *hd = data;
74         unsigned int msec_to_next_event, next_log, next_ss = STEADYSTATE_MSEC;
75         struct timeval tv;
76         struct timespec ts, last_du, last_ss;
77         int ret = 0;
78
79         sk_out_assign(hd->sk_out);
80
81         gettimeofday(&tv, NULL);
82         ts.tv_sec = tv.tv_sec;
83         ts.tv_nsec = tv.tv_usec * 1000;
84         memcpy(&last_du, &ts, sizeof(ts));
85         memcpy(&last_ss, &ts, sizeof(ts));
86
87         fio_sem_up(hd->startup_sem);
88
89         msec_to_next_event = DISK_UTIL_MSEC;
90         while (!ret && !hd->exit) {
91                 uint64_t since_du, since_ss = 0;
92
93                 timespec_add_msec(&ts, msec_to_next_event);
94
95                 pthread_mutex_lock(&hd->lock);
96                 pthread_cond_timedwait(&hd->cond, &hd->lock, &ts);
97
98                 gettimeofday(&tv, NULL);
99                 ts.tv_sec = tv.tv_sec;
100                 ts.tv_nsec = tv.tv_usec * 1000;
101
102                 if (hd->reset) {
103                         memcpy(&last_du, &ts, sizeof(ts));
104                         memcpy(&last_ss, &ts, sizeof(ts));
105                         hd->reset = 0;
106                 }
107
108                 pthread_mutex_unlock(&hd->lock);
109
110                 since_du = mtime_since(&last_du, &ts);
111                 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
112                         ret = update_io_ticks();
113                         timespec_add_msec(&last_du, DISK_UTIL_MSEC);
114                         msec_to_next_event = DISK_UTIL_MSEC;
115                         if (since_du >= DISK_UTIL_MSEC)
116                                 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
117                 } else
118                         msec_to_next_event = DISK_UTIL_MSEC - since_du;
119
120                 if (hd->do_stat) {
121                         hd->do_stat = 0;
122                         __show_running_run_stats();
123                 }
124
125                 next_log = calc_log_samples();
126                 if (!next_log)
127                         next_log = DISK_UTIL_MSEC;
128
129                 if (steadystate_enabled) {
130                         since_ss = mtime_since(&last_ss, &ts);
131                         if (since_ss >= STEADYSTATE_MSEC || STEADYSTATE_MSEC - since_ss < 10) {
132                                 steadystate_check();
133                                 timespec_add_msec(&last_ss, since_ss);
134                                 if (since_ss > STEADYSTATE_MSEC)
135                                         next_ss = STEADYSTATE_MSEC - (since_ss - STEADYSTATE_MSEC);
136                                 else
137                                         next_ss = STEADYSTATE_MSEC;
138                         } else
139                                 next_ss = STEADYSTATE_MSEC - since_ss;
140                 }
141
142                 msec_to_next_event = min(min(next_log, msec_to_next_event), next_ss);
143                 dprint(FD_HELPERTHREAD, "since_ss: %llu, next_ss: %u, next_log: %u, msec_to_next_event: %u\n", (unsigned long long)since_ss, next_ss, next_log, msec_to_next_event);
144
145                 if (!is_backend)
146                         print_thread_status();
147         }
148
149         fio_writeout_logs(false);
150
151         sk_out_drop();
152         return NULL;
153 }
154
155 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
156 {
157         struct helper_data *hd;
158         int ret;
159
160         hd = scalloc(1, sizeof(*hd));
161
162         setup_disk_util();
163         steadystate_setup();
164
165         hd->sk_out = sk_out;
166
167         ret = mutex_cond_init_pshared(&hd->lock, &hd->cond);
168         if (ret)
169                 return 1;
170
171         hd->startup_sem = startup_sem;
172
173         ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
174         if (ret) {
175                 log_err("Can't create helper thread: %s\n", strerror(ret));
176                 return 1;
177         }
178
179         helper_data = hd;
180
181         dprint(FD_MUTEX, "wait on startup_sem\n");
182         fio_sem_down(startup_sem);
183         dprint(FD_MUTEX, "done waiting on startup_sem\n");
184         return 0;
185 }