helper_thread: cleanups
[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 void *helper_thread_main(void *data)
132 {
133         struct helper_data *hd = data;
134         unsigned int msec_to_next_event, next_log, next_ss = STEADYSTATE_MSEC;
135         struct timespec ts, last_du, last_ss;
136         char action;
137         int ret = 0;
138
139         sk_out_assign(hd->sk_out);
140
141 #ifdef HAVE_PTHREAD_SIGMASK
142         {
143         sigset_t sigmask;
144
145         /* Let another thread handle signals. */
146         ret = pthread_sigmask(SIG_UNBLOCK, NULL, &sigmask);
147         assert(ret == 0);
148         ret = pthread_sigmask(SIG_BLOCK, &sigmask, NULL);
149         assert(ret == 0);
150         }
151 #endif
152
153 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
154         clock_gettime(CLOCK_MONOTONIC, &ts);
155 #else
156         clock_gettime(CLOCK_REALTIME, &ts);
157 #endif
158         memcpy(&last_du, &ts, sizeof(ts));
159         memcpy(&last_ss, &ts, sizeof(ts));
160
161         fio_sem_up(hd->startup_sem);
162
163         msec_to_next_event = DISK_UTIL_MSEC;
164         while (!ret && !hd->exit) {
165                 uint64_t since_du, since_ss = 0;
166                 struct timeval timeout = {
167                         .tv_sec  = msec_to_next_event / 1000,
168                         .tv_usec = (msec_to_next_event % 1000) * 1000,
169                 };
170                 fd_set rfds, efds;
171
172                 if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < 0) {
173                         FD_ZERO(&rfds);
174                         FD_SET(hd->pipe[0], &rfds);
175                         FD_ZERO(&efds);
176                         FD_SET(hd->pipe[0], &efds);
177                         ret = select(1, &rfds, NULL, &efds, &timeout);
178                         if (ret < 0)
179                                 log_err("fio: select() call in helper thread failed: %s",
180                                         strerror(errno));
181                         if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) <
182                             0)
183                                 action = 0;
184                 }
185
186 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
187                 clock_gettime(CLOCK_MONOTONIC, &ts);
188 #else
189                 clock_gettime(CLOCK_REALTIME, &ts);
190 #endif
191
192                 if (action == A_RESET) {
193                         last_du = ts;
194                         last_ss = ts;
195                 }
196
197                 since_du = mtime_since(&last_du, &ts);
198                 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
199                         ret = update_io_ticks();
200                         timespec_add_msec(&last_du, DISK_UTIL_MSEC);
201                         msec_to_next_event = DISK_UTIL_MSEC;
202                         if (since_du >= DISK_UTIL_MSEC)
203                                 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
204                 } else
205                         msec_to_next_event = DISK_UTIL_MSEC - since_du;
206
207                 if (action == A_DO_STAT)
208                         __show_running_run_stats();
209
210                 next_log = calc_log_samples();
211                 if (!next_log)
212                         next_log = DISK_UTIL_MSEC;
213
214                 if (steadystate_enabled) {
215                         since_ss = mtime_since(&last_ss, &ts);
216                         if (since_ss >= STEADYSTATE_MSEC || STEADYSTATE_MSEC - since_ss < 10) {
217                                 steadystate_check();
218                                 timespec_add_msec(&last_ss, since_ss);
219                                 if (since_ss > STEADYSTATE_MSEC)
220                                         next_ss = STEADYSTATE_MSEC - (since_ss - STEADYSTATE_MSEC);
221                                 else
222                                         next_ss = STEADYSTATE_MSEC;
223                         } else
224                                 next_ss = STEADYSTATE_MSEC - since_ss;
225                 }
226
227                 msec_to_next_event = min(min(next_log, msec_to_next_event), next_ss);
228                 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);
229
230                 if (!is_backend)
231                         print_thread_status();
232         }
233
234         fio_writeout_logs(false);
235
236         sk_out_drop();
237         return NULL;
238 }
239
240 /*
241  * Connect two sockets to each other to emulate the pipe() system call on Windows.
242  */
243 int pipe_over_loopback(int fd[2])
244 {
245         struct sockaddr_in addr = { .sin_family = AF_INET };
246         socklen_t len = sizeof(addr);
247         int res;
248
249         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
250
251         sock_init();
252
253         fd[0] = socket(AF_INET, SOCK_STREAM, 0);
254         if (fd[0] < 0)
255                 goto err;
256         fd[1] = socket(AF_INET, SOCK_STREAM, 0);
257         if (fd[1] < 0)
258                 goto close_fd_0;
259         res = bind(fd[0], (struct sockaddr *)&addr, len);
260         if (res < 0)
261                 goto close_fd_1;
262         res = getsockname(fd[0], (struct sockaddr *)&addr, &len);
263         if (res < 0)
264                 goto close_fd_1;
265         res = listen(fd[0], 1);
266         if (res < 0)
267                 goto close_fd_1;
268         res = connect(fd[1], (struct sockaddr *)&addr, len);
269         if (res < 0)
270                 goto close_fd_1;
271         res = accept(fd[0], NULL, NULL);
272         if (res < 0)
273                 goto close_fd_1;
274         close(fd[0]);
275         fd[0] = res;
276         return 0;
277
278 close_fd_1:
279         close(fd[1]);
280
281 close_fd_0:
282         close(fd[0]);
283
284 err:
285         return -1;
286 }
287
288 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
289 {
290         struct helper_data *hd;
291         int ret;
292
293         hd = scalloc(1, sizeof(*hd));
294
295         setup_disk_util();
296         steadystate_setup();
297
298         hd->sk_out = sk_out;
299
300 #if defined(CONFIG_PIPE2)
301         ret = pipe2(hd->pipe, O_CLOEXEC);
302 #elif defined(CONFIG_PIPE)
303         ret = pipe(hd->pipe);
304 #else
305         ret = pipe_over_loopback(hd->pipe);
306 #endif
307         if (ret)
308                 return 1;
309
310         ret = make_nonblocking(hd->pipe[0]);
311         assert(ret >= 0);
312
313         hd->startup_sem = startup_sem;
314
315         DRD_IGNORE_VAR(helper_data);
316
317         ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
318         if (ret) {
319                 log_err("Can't create helper thread: %s\n", strerror(ret));
320                 return 1;
321         }
322
323         helper_data = hd;
324
325         dprint(FD_MUTEX, "wait on startup_sem\n");
326         fio_sem_down(startup_sem);
327         dprint(FD_MUTEX, "done waiting on startup_sem\n");
328         return 0;
329 }