Merge branch 'helper_thread_test' of https://github.com/vincentkfu/fio
[fio.git] / helper_thread.c
1 #include <signal.h>
2 #ifdef CONFIG_VALGRIND_DEV
3 #include <valgrind/drd.h>
4 #else
5 #define DRD_IGNORE_VAR(x) do { } while (0)
6 #endif
7
8 #include "fio.h"
9 #include "smalloc.h"
10 #include "helper_thread.h"
11 #include "steadystate.h"
12 #include "pshared.h"
13
14 enum action {
15         A_EXIT          = 1,
16         A_RESET         = 2,
17         A_DO_STAT       = 3,
18 };
19
20 static struct helper_data {
21         volatile int exit;
22         int pipe[2]; /* 0: read end; 1: write end. */
23         struct sk_out *sk_out;
24         pthread_t thread;
25         struct fio_sem *startup_sem;
26 } *helper_data;
27
28 void helper_thread_destroy(void)
29 {
30         if (!helper_data)
31                 return;
32
33         close(helper_data->pipe[0]);
34         close(helper_data->pipe[1]);
35         sfree(helper_data);
36 }
37
38 #ifdef _WIN32
39 static void sock_init(void)
40 {
41         WSADATA wsaData;
42         int res;
43
44         /* It is allowed to call WSAStartup() more than once. */
45         res = WSAStartup(MAKEWORD(2, 2), &wsaData);
46         assert(res == 0);
47 }
48
49 static int make_nonblocking(int fd)
50 {
51         unsigned long arg = 1;
52
53         return ioctlsocket(fd, FIONBIO, &arg);
54 }
55
56 static int write_to_pipe(int fd, const void *buf, size_t len)
57 {
58         return send(fd, buf, len, 0);
59 }
60
61 static int read_from_pipe(int fd, void *buf, size_t len)
62 {
63         return recv(fd, buf, len, 0);
64 }
65 #else
66 static void sock_init(void)
67 {
68 }
69
70 static int make_nonblocking(int fd)
71 {
72         return fcntl(fd, F_SETFL, O_NONBLOCK);
73 }
74
75 static int write_to_pipe(int fd, const void *buf, size_t len)
76 {
77         return write(fd, buf, len);
78 }
79
80 static int read_from_pipe(int fd, void *buf, size_t len)
81 {
82         return read(fd, buf, len);
83 }
84 #endif
85
86 static void submit_action(enum action a)
87 {
88         const char data = a;
89         int ret;
90
91         if (!helper_data)
92                 return;
93
94         ret = write_to_pipe(helper_data->pipe[1], &data, sizeof(data));
95         assert(ret == 1);
96 }
97
98 void helper_reset(void)
99 {
100         submit_action(A_RESET);
101 }
102
103 /*
104  * May be invoked in signal handler context and hence must only call functions
105  * that are async-signal-safe. See also
106  * https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03.
107  */
108 void helper_do_stat(void)
109 {
110         submit_action(A_DO_STAT);
111 }
112
113 bool helper_should_exit(void)
114 {
115         if (!helper_data)
116                 return true;
117
118         return helper_data->exit;
119 }
120
121 void helper_thread_exit(void)
122 {
123         if (!helper_data)
124                 return;
125
126         helper_data->exit = 1;
127         submit_action(A_EXIT);
128         pthread_join(helper_data->thread, NULL);
129 }
130
131 static unsigned int task_helper(struct timespec *last, struct timespec *now, unsigned int period, void do_task())
132 {
133         unsigned int next, since;
134
135         since = mtime_since(last, now);
136         if (since >= period || period - since < 10) {
137                 do_task();
138                 timespec_add_msec(last, since);
139                 if (since > period)
140                         next = period - (since - period);
141                 else
142                         next = period;
143         } else
144                 next = period - since;
145
146         return next;
147 }
148
149 static void *helper_thread_main(void *data)
150 {
151         struct helper_data *hd = data;
152         unsigned int msec_to_next_event, next_log, next_si = status_interval;
153         unsigned int next_ss = STEADYSTATE_MSEC;
154         struct timespec ts, last_du, last_ss, last_si;
155         char action;
156         int ret = 0;
157
158         sk_out_assign(hd->sk_out);
159
160 #ifdef HAVE_PTHREAD_SIGMASK
161         {
162         sigset_t sigmask;
163
164         /* Let another thread handle signals. */
165         ret = pthread_sigmask(SIG_UNBLOCK, NULL, &sigmask);
166         assert(ret == 0);
167         ret = pthread_sigmask(SIG_BLOCK, &sigmask, NULL);
168         assert(ret == 0);
169         }
170 #endif
171
172 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
173         clock_gettime(CLOCK_MONOTONIC, &ts);
174 #else
175         clock_gettime(CLOCK_REALTIME, &ts);
176 #endif
177         memcpy(&last_du, &ts, sizeof(ts));
178         memcpy(&last_ss, &ts, sizeof(ts));
179         memcpy(&last_si, &ts, sizeof(ts));
180
181         fio_sem_up(hd->startup_sem);
182
183         msec_to_next_event = DISK_UTIL_MSEC;
184         while (!ret && !hd->exit) {
185                 uint64_t since_du;
186                 struct timeval timeout = {
187                         .tv_sec  = msec_to_next_event / 1000,
188                         .tv_usec = (msec_to_next_event % 1000) * 1000,
189                 };
190                 fd_set rfds, efds;
191
192                 if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < 0) {
193                         FD_ZERO(&rfds);
194                         FD_SET(hd->pipe[0], &rfds);
195                         FD_ZERO(&efds);
196                         FD_SET(hd->pipe[0], &efds);
197                         ret = select(1, &rfds, NULL, &efds, &timeout);
198                         if (ret < 0)
199                                 log_err("fio: select() call in helper thread failed: %s",
200                                         strerror(errno));
201                         if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) <
202                             0)
203                                 action = 0;
204                 }
205
206 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
207                 clock_gettime(CLOCK_MONOTONIC, &ts);
208 #else
209                 clock_gettime(CLOCK_REALTIME, &ts);
210 #endif
211
212                 if (action == A_RESET) {
213                         last_du = ts;
214                         last_ss = ts;
215                 }
216
217                 since_du = mtime_since(&last_du, &ts);
218                 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
219                         ret = update_io_ticks();
220                         timespec_add_msec(&last_du, DISK_UTIL_MSEC);
221                         msec_to_next_event = DISK_UTIL_MSEC;
222                         if (since_du >= DISK_UTIL_MSEC)
223                                 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
224                 } else
225                         msec_to_next_event = DISK_UTIL_MSEC - since_du;
226
227                 if (action == A_DO_STAT)
228                         __show_running_run_stats();
229
230                 if (status_interval) {
231                         next_si = task_helper(&last_si, &ts, status_interval, __show_running_run_stats);
232                         msec_to_next_event = min(next_si, msec_to_next_event);
233                 }
234
235                 next_log = calc_log_samples();
236                 if (!next_log)
237                         next_log = DISK_UTIL_MSEC;
238
239                 if (steadystate_enabled) {
240                         next_ss = task_helper(&last_ss, &ts, STEADYSTATE_MSEC, steadystate_check);
241                         msec_to_next_event = min(next_ss, msec_to_next_event);
242                 }
243
244                 msec_to_next_event = min(next_log, msec_to_next_event);
245                 dprint(FD_HELPERTHREAD, "next_si: %u, next_ss: %u, next_log: %u, msec_to_next_event: %u\n",
246                         next_si, next_ss, next_log, msec_to_next_event);
247
248                 if (!is_backend)
249                         print_thread_status();
250         }
251
252         fio_writeout_logs(false);
253
254         sk_out_drop();
255         return NULL;
256 }
257
258 /*
259  * Connect two sockets to each other to emulate the pipe() system call on Windows.
260  */
261 int pipe_over_loopback(int fd[2])
262 {
263         struct sockaddr_in addr = { .sin_family = AF_INET };
264         socklen_t len = sizeof(addr);
265         int res;
266
267         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
268
269         sock_init();
270
271         fd[0] = socket(AF_INET, SOCK_STREAM, 0);
272         if (fd[0] < 0)
273                 goto err;
274         fd[1] = socket(AF_INET, SOCK_STREAM, 0);
275         if (fd[1] < 0)
276                 goto close_fd_0;
277         res = bind(fd[0], (struct sockaddr *)&addr, len);
278         if (res < 0)
279                 goto close_fd_1;
280         res = getsockname(fd[0], (struct sockaddr *)&addr, &len);
281         if (res < 0)
282                 goto close_fd_1;
283         res = listen(fd[0], 1);
284         if (res < 0)
285                 goto close_fd_1;
286         res = connect(fd[1], (struct sockaddr *)&addr, len);
287         if (res < 0)
288                 goto close_fd_1;
289         res = accept(fd[0], NULL, NULL);
290         if (res < 0)
291                 goto close_fd_1;
292         close(fd[0]);
293         fd[0] = res;
294         return 0;
295
296 close_fd_1:
297         close(fd[1]);
298
299 close_fd_0:
300         close(fd[0]);
301
302 err:
303         return -1;
304 }
305
306 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
307 {
308         struct helper_data *hd;
309         int ret;
310
311         hd = scalloc(1, sizeof(*hd));
312
313         setup_disk_util();
314         steadystate_setup();
315
316         hd->sk_out = sk_out;
317
318 #if defined(CONFIG_PIPE2)
319         ret = pipe2(hd->pipe, O_CLOEXEC);
320 #elif defined(CONFIG_PIPE)
321         ret = pipe(hd->pipe);
322 #else
323         ret = pipe_over_loopback(hd->pipe);
324 #endif
325         if (ret)
326                 return 1;
327
328         ret = make_nonblocking(hd->pipe[0]);
329         assert(ret >= 0);
330
331         hd->startup_sem = startup_sem;
332
333         DRD_IGNORE_VAR(helper_data);
334
335         ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
336         if (ret) {
337                 log_err("Can't create helper thread: %s\n", strerror(ret));
338                 return 1;
339         }
340
341         helper_data = hd;
342
343         dprint(FD_MUTEX, "wait on startup_sem\n");
344         fio_sem_down(startup_sem);
345         dprint(FD_MUTEX, "done waiting on startup_sem\n");
346         return 0;
347 }