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