helper_thread: Introduce a function for blocking Unix signals
[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 block_signals(void)
87 {
88 #ifdef HAVE_PTHREAD_SIGMASK
89         sigset_t sigmask;
90
91         ret = pthread_sigmask(SIG_UNBLOCK, NULL, &sigmask);
92         assert(ret == 0);
93         ret = pthread_sigmask(SIG_BLOCK, &sigmask, NULL);
94         assert(ret == 0);
95 #endif
96 }
97
98 static void submit_action(enum action a)
99 {
100         const char data = a;
101         int ret;
102
103         if (!helper_data)
104                 return;
105
106         ret = write_to_pipe(helper_data->pipe[1], &data, sizeof(data));
107         assert(ret == 1);
108 }
109
110 void helper_reset(void)
111 {
112         submit_action(A_RESET);
113 }
114
115 /*
116  * May be invoked in signal handler context and hence must only call functions
117  * that are async-signal-safe. See also
118  * https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03.
119  */
120 void helper_do_stat(void)
121 {
122         submit_action(A_DO_STAT);
123 }
124
125 bool helper_should_exit(void)
126 {
127         if (!helper_data)
128                 return true;
129
130         return helper_data->exit;
131 }
132
133 void helper_thread_exit(void)
134 {
135         if (!helper_data)
136                 return;
137
138         helper_data->exit = 1;
139         submit_action(A_EXIT);
140         pthread_join(helper_data->thread, NULL);
141 }
142
143 static unsigned int task_helper(struct timespec *last, struct timespec *now, unsigned int period, void do_task())
144 {
145         unsigned int next, since;
146
147         since = mtime_since(last, now);
148         if (since >= period || period - since < 10) {
149                 do_task();
150                 timespec_add_msec(last, since);
151                 if (since > period)
152                         next = period - (since - period);
153                 else
154                         next = period;
155         } else
156                 next = period - since;
157
158         return next;
159 }
160
161 static void *helper_thread_main(void *data)
162 {
163         struct helper_data *hd = data;
164         unsigned int msec_to_next_event, next_log, next_si = status_interval;
165         unsigned int next_ss = STEADYSTATE_MSEC;
166         struct timespec ts, last_du, last_ss, last_si;
167         char action;
168         int ret = 0;
169
170         sk_out_assign(hd->sk_out);
171
172         /* Let another thread handle signals. */
173         block_signals();
174
175         fio_get_mono_time(&ts);
176         memcpy(&last_du, &ts, sizeof(ts));
177         memcpy(&last_ss, &ts, sizeof(ts));
178         memcpy(&last_si, &ts, sizeof(ts));
179
180         fio_sem_up(hd->startup_sem);
181
182         msec_to_next_event = DISK_UTIL_MSEC;
183         while (!ret && !hd->exit) {
184                 uint64_t since_du;
185                 struct timeval timeout = {
186                         .tv_sec  = msec_to_next_event / 1000,
187                         .tv_usec = (msec_to_next_event % 1000) * 1000,
188                 };
189                 fd_set rfds, efds;
190
191                 if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < 0) {
192                         FD_ZERO(&rfds);
193                         FD_SET(hd->pipe[0], &rfds);
194                         FD_ZERO(&efds);
195                         FD_SET(hd->pipe[0], &efds);
196                         if (select(1, &rfds, NULL, &efds, &timeout) < 0) {
197                                 log_err("fio: select() call in helper thread failed: %s",
198                                         strerror(errno));
199                                 ret = 1;
200                         }
201                         if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) <
202                             0)
203                                 action = 0;
204                 }
205
206                 fio_get_mono_time(&ts);
207
208                 if (action == A_RESET) {
209                         last_du = ts;
210                         last_ss = ts;
211                 }
212
213                 since_du = mtime_since(&last_du, &ts);
214                 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
215                         ret = update_io_ticks();
216                         timespec_add_msec(&last_du, DISK_UTIL_MSEC);
217                         msec_to_next_event = DISK_UTIL_MSEC;
218                         if (since_du >= DISK_UTIL_MSEC)
219                                 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
220                 } else
221                         msec_to_next_event = DISK_UTIL_MSEC - since_du;
222
223                 if (action == A_DO_STAT)
224                         __show_running_run_stats();
225
226                 if (status_interval) {
227                         next_si = task_helper(&last_si, &ts, status_interval, __show_running_run_stats);
228                         msec_to_next_event = min(next_si, msec_to_next_event);
229                 }
230
231                 next_log = calc_log_samples();
232                 if (!next_log)
233                         next_log = DISK_UTIL_MSEC;
234
235                 if (steadystate_enabled) {
236                         next_ss = task_helper(&last_ss, &ts, STEADYSTATE_MSEC, steadystate_check);
237                         msec_to_next_event = min(next_ss, msec_to_next_event);
238                 }
239
240                 msec_to_next_event = min(next_log, msec_to_next_event);
241                 dprint(FD_HELPERTHREAD, "next_si: %u, next_ss: %u, next_log: %u, msec_to_next_event: %u\n",
242                         next_si, next_ss, next_log, msec_to_next_event);
243
244                 if (!is_backend)
245                         print_thread_status();
246         }
247
248         fio_writeout_logs(false);
249
250         sk_out_drop();
251         return NULL;
252 }
253
254 /*
255  * Connect two sockets to each other to emulate the pipe() system call on Windows.
256  */
257 int pipe_over_loopback(int fd[2])
258 {
259         struct sockaddr_in addr = { .sin_family = AF_INET };
260         socklen_t len = sizeof(addr);
261         int res;
262
263         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
264
265         sock_init();
266
267         fd[0] = socket(AF_INET, SOCK_STREAM, 0);
268         if (fd[0] < 0)
269                 goto err;
270         fd[1] = socket(AF_INET, SOCK_STREAM, 0);
271         if (fd[1] < 0)
272                 goto close_fd_0;
273         res = bind(fd[0], (struct sockaddr *)&addr, len);
274         if (res < 0)
275                 goto close_fd_1;
276         res = getsockname(fd[0], (struct sockaddr *)&addr, &len);
277         if (res < 0)
278                 goto close_fd_1;
279         res = listen(fd[0], 1);
280         if (res < 0)
281                 goto close_fd_1;
282         res = connect(fd[1], (struct sockaddr *)&addr, len);
283         if (res < 0)
284                 goto close_fd_1;
285         res = accept(fd[0], NULL, NULL);
286         if (res < 0)
287                 goto close_fd_1;
288         close(fd[0]);
289         fd[0] = res;
290         return 0;
291
292 close_fd_1:
293         close(fd[1]);
294
295 close_fd_0:
296         close(fd[0]);
297
298 err:
299         return -1;
300 }
301
302 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
303 {
304         struct helper_data *hd;
305         int ret;
306
307         hd = scalloc(1, sizeof(*hd));
308
309         setup_disk_util();
310         steadystate_setup();
311
312         hd->sk_out = sk_out;
313
314 #if defined(CONFIG_PIPE2)
315         ret = pipe2(hd->pipe, O_CLOEXEC);
316 #elif defined(CONFIG_PIPE)
317         ret = pipe(hd->pipe);
318 #else
319         ret = pipe_over_loopback(hd->pipe);
320 #endif
321         if (ret)
322                 return 1;
323
324         ret = make_nonblocking(hd->pipe[0]);
325         assert(ret >= 0);
326
327         hd->startup_sem = startup_sem;
328
329         DRD_IGNORE_VAR(helper_data);
330
331         ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
332         if (ret) {
333                 log_err("Can't create helper thread: %s\n", strerror(ret));
334                 return 1;
335         }
336
337         helper_data = hd;
338
339         dprint(FD_MUTEX, "wait on startup_sem\n");
340         fio_sem_down(startup_sem);
341         dprint(FD_MUTEX, "done waiting on startup_sem\n");
342         return 0;
343 }