zbd: change some f->zbd_info conditionals to asserts
[fio.git] / helper_thread.c
CommitLineData
c31092b8 1#include <signal.h>
df9bd7d4 2#include <unistd.h>
696378af
BVA
3#ifdef CONFIG_HAVE_TIMERFD_CREATE
4#include <sys/timerfd.h>
5#endif
4f37732a
BVA
6#ifdef CONFIG_VALGRIND_DEV
7#include <valgrind/drd.h>
8#else
9#define DRD_IGNORE_VAR(x) do { } while (0)
10#endif
11
a39fb9ea
JA
12#include "fio.h"
13#include "smalloc.h"
14#include "helper_thread.h"
16e56d25 15#include "steadystate.h"
ae626d4e 16#include "pshared.h"
a39fb9ea 17
df9bd7d4 18static int sleep_accuracy_ms;
696378af 19static int timerfd = -1;
df9bd7d4 20
52a552e2
BVA
21enum action {
22 A_EXIT = 1,
23 A_RESET = 2,
24 A_DO_STAT = 3,
25};
26
a39fb9ea
JA
27static struct helper_data {
28 volatile int exit;
52a552e2 29 int pipe[2]; /* 0: read end; 1: write end. */
a39fb9ea
JA
30 struct sk_out *sk_out;
31 pthread_t thread;
971caeb1 32 struct fio_sem *startup_sem;
a39fb9ea
JA
33} *helper_data;
34
df9bd7d4
BVA
35struct interval_timer {
36 const char *name;
37 struct timespec expires;
38 uint32_t interval_ms;
39 int (*func)(void);
40};
41
a39fb9ea
JA
42void helper_thread_destroy(void)
43{
998e9ebb
BVA
44 if (!helper_data)
45 return;
46
52a552e2
BVA
47 close(helper_data->pipe[0]);
48 close(helper_data->pipe[1]);
a39fb9ea
JA
49 sfree(helper_data);
50}
51
52a552e2
BVA
52#ifdef _WIN32
53static void sock_init(void)
a39fb9ea 54{
52a552e2
BVA
55 WSADATA wsaData;
56 int res;
a39fb9ea 57
52a552e2
BVA
58 /* It is allowed to call WSAStartup() more than once. */
59 res = WSAStartup(MAKEWORD(2, 2), &wsaData);
60 assert(res == 0);
61}
a39fb9ea 62
52a552e2
BVA
63static int make_nonblocking(int fd)
64{
65 unsigned long arg = 1;
dda11987 66
52a552e2 67 return ioctlsocket(fd, FIONBIO, &arg);
a39fb9ea
JA
68}
69
52a552e2
BVA
70static int write_to_pipe(int fd, const void *buf, size_t len)
71{
72 return send(fd, buf, len, 0);
73}
74
75static int read_from_pipe(int fd, void *buf, size_t len)
76{
77 return recv(fd, buf, len, 0);
78}
79#else
80static void sock_init(void)
81{
82}
83
84static int make_nonblocking(int fd)
85{
86 return fcntl(fd, F_SETFL, O_NONBLOCK);
87}
88
89static int write_to_pipe(int fd, const void *buf, size_t len)
90{
91 return write(fd, buf, len);
92}
93
94static int read_from_pipe(int fd, void *buf, size_t len)
95{
96 return read(fd, buf, len);
97}
98#endif
99
2575407f
BVA
100static void block_signals(void)
101{
102#ifdef HAVE_PTHREAD_SIGMASK
103 sigset_t sigmask;
104
105 ret = pthread_sigmask(SIG_UNBLOCK, NULL, &sigmask);
106 assert(ret == 0);
107 ret = pthread_sigmask(SIG_BLOCK, &sigmask, NULL);
108 assert(ret == 0);
109#endif
110}
111
52a552e2 112static void submit_action(enum action a)
a39fb9ea 113{
52a552e2
BVA
114 const char data = a;
115 int ret;
116
dda11987
JA
117 if (!helper_data)
118 return;
119
52a552e2
BVA
120 ret = write_to_pipe(helper_data->pipe[1], &data, sizeof(data));
121 assert(ret == 1);
122}
123
124void helper_reset(void)
125{
126 submit_action(A_RESET);
127}
128
129/*
130 * May be invoked in signal handler context and hence must only call functions
131 * that are async-signal-safe. See also
132 * https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03.
133 */
134void helper_do_stat(void)
135{
136 submit_action(A_DO_STAT);
a39fb9ea
JA
137}
138
139bool helper_should_exit(void)
140{
141 if (!helper_data)
142 return true;
143
144 return helper_data->exit;
145}
146
147void helper_thread_exit(void)
148{
998e9ebb
BVA
149 if (!helper_data)
150 return;
151
a39fb9ea 152 helper_data->exit = 1;
52a552e2
BVA
153 submit_action(A_EXIT);
154 pthread_join(helper_data->thread, NULL);
a39fb9ea
JA
155}
156
df9bd7d4
BVA
157/* Resets timers and returns the time in milliseconds until the next event. */
158static int reset_timers(struct interval_timer timer[], int num_timers,
159 struct timespec *now)
160{
161 uint32_t msec_to_next_event = INT_MAX;
162 int i;
163
164 for (i = 0; i < num_timers; ++i) {
165 timer[i].expires = *now;
166 timespec_add_msec(&timer[i].expires, timer[i].interval_ms);
167 msec_to_next_event = min_not_zero(msec_to_next_event,
168 timer[i].interval_ms);
169 }
170
171 return msec_to_next_event;
172}
173
335210df
BVA
174/*
175 * Waits for an action from fd during at least timeout_ms. `fd` must be in
176 * non-blocking mode.
177 */
178static uint8_t wait_for_action(int fd, unsigned int timeout_ms)
179{
180 struct timeval timeout = {
181 .tv_sec = timeout_ms / 1000,
182 .tv_usec = (timeout_ms % 1000) * 1000,
183 };
184 fd_set rfds, efds;
185 uint8_t action = 0;
696378af 186 uint64_t exp;
335210df
BVA
187 int res;
188
189 res = read_from_pipe(fd, &action, sizeof(action));
190 if (res > 0 || timeout_ms == 0)
191 return action;
192 FD_ZERO(&rfds);
193 FD_SET(fd, &rfds);
194 FD_ZERO(&efds);
195 FD_SET(fd, &efds);
696378af
BVA
196#ifdef CONFIG_HAVE_TIMERFD_CREATE
197 {
198 /*
199 * If the timer frequency is 100 Hz, select() will round up
200 * `timeout` to the next multiple of 1 / 100 Hz = 10 ms. Hence
201 * use a high-resolution timer if possible to increase
202 * select() timeout accuracy.
203 */
204 struct itimerspec delta = {};
205
206 delta.it_value.tv_sec = timeout.tv_sec;
207 delta.it_value.tv_nsec = timeout.tv_usec * 1000;
208 res = timerfd_settime(timerfd, 0, &delta, NULL);
209 assert(res == 0);
210 FD_SET(timerfd, &rfds);
211 }
212#endif
213 res = select(max(fd, timerfd) + 1, &rfds, NULL, &efds,
214 timerfd >= 0 ? NULL : &timeout);
335210df
BVA
215 if (res < 0) {
216 log_err("fio: select() call in helper thread failed: %s",
217 strerror(errno));
218 return A_EXIT;
219 }
220 if (FD_ISSET(fd, &rfds))
221 read_from_pipe(fd, &action, sizeof(action));
696378af
BVA
222 if (timerfd >= 0 && FD_ISSET(timerfd, &rfds)) {
223 res = read(timerfd, &exp, sizeof(exp));
224 assert(res == sizeof(exp));
225 }
335210df
BVA
226 return action;
227}
228
df9bd7d4
BVA
229/*
230 * Verify whether or not timer @it has expired. If timer @it has expired, call
231 * @it->func(). @now is the current time. @msec_to_next_event is an
232 * input/output parameter that represents the time until the next event.
233 */
234static int eval_timer(struct interval_timer *it, const struct timespec *now,
235 unsigned int *msec_to_next_event)
3da71e37 236{
df9bd7d4
BVA
237 int64_t delta_ms;
238 bool expired;
239
240 /* interval == 0 means that the timer is disabled. */
241 if (it->interval_ms == 0)
242 return 0;
243
244 delta_ms = rel_time_since(now, &it->expires);
245 expired = delta_ms <= sleep_accuracy_ms;
246 if (expired) {
247 timespec_add_msec(&it->expires, it->interval_ms);
248 delta_ms = rel_time_since(now, &it->expires);
249 if (delta_ms < it->interval_ms - sleep_accuracy_ms ||
250 delta_ms > it->interval_ms + sleep_accuracy_ms) {
251 dprint(FD_HELPERTHREAD,
252 "%s: delta = %" PRIi64 " <> %u. Clock jump?\n",
253 it->name, delta_ms, it->interval_ms);
254 delta_ms = it->interval_ms;
255 it->expires = *now;
256 timespec_add_msec(&it->expires, it->interval_ms);
257 }
258 }
259 *msec_to_next_event = min((unsigned int)delta_ms, *msec_to_next_event);
260 return expired ? it->func() : 0;
3da71e37
VF
261}
262
a39fb9ea
JA
263static void *helper_thread_main(void *data)
264{
265 struct helper_data *hd = data;
df9bd7d4
BVA
266 unsigned int msec_to_next_event, next_log;
267 struct interval_timer timer[] = {
268 {
269 .name = "disk_util",
270 .interval_ms = DISK_UTIL_MSEC,
271 .func = update_io_ticks,
272 },
273 {
274 .name = "status_interval",
275 .interval_ms = status_interval,
276 .func = __show_running_run_stats,
277 },
278 {
279 .name = "steadystate",
280 .interval_ms = steadystate_enabled ? STEADYSTATE_MSEC :
281 0,
282 .func = steadystate_check,
283 }
284 };
285 struct timespec ts;
286 int clk_tck, ret = 0;
287
288#ifdef _SC_CLK_TCK
289 clk_tck = sysconf(_SC_CLK_TCK);
290#else
291 /*
292 * The timer frequence is variable on Windows. Instead of trying to
293 * query it, use 64 Hz, the clock frequency lower bound. See also
294 * https://carpediemsystems.co.uk/2019/07/18/windows-system-timer-granularity/.
295 */
296 clk_tck = 64;
297#endif
298 dprint(FD_HELPERTHREAD, "clk_tck = %d\n", clk_tck);
299 assert(clk_tck > 0);
300 sleep_accuracy_ms = (1000 + clk_tck - 1) / clk_tck;
a39fb9ea 301
696378af
BVA
302#ifdef CONFIG_HAVE_TIMERFD_CREATE
303 timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
304 assert(timerfd >= 0);
305 sleep_accuracy_ms = 1;
306#endif
307
a39fb9ea
JA
308 sk_out_assign(hd->sk_out);
309
c31092b8 310 /* Let another thread handle signals. */
2575407f 311 block_signals();
c31092b8 312
69212fc4 313 fio_get_mono_time(&ts);
59f94d26 314 msec_to_next_event = reset_timers(timer, FIO_ARRAY_SIZE(timer), &ts);
a39fb9ea 315
971caeb1 316 fio_sem_up(hd->startup_sem);
a39fb9ea 317
a39fb9ea 318 while (!ret && !hd->exit) {
335210df 319 uint8_t action;
df9bd7d4 320 int i;
335210df
BVA
321
322 action = wait_for_action(hd->pipe[0], msec_to_next_event);
323 if (action == A_EXIT)
324 break;
a39fb9ea 325
69212fc4 326 fio_get_mono_time(&ts);
a39fb9ea 327
df9bd7d4
BVA
328 msec_to_next_event = INT_MAX;
329
330 if (action == A_RESET)
331 msec_to_next_event = reset_timers(timer,
59f94d26 332 FIO_ARRAY_SIZE(timer), &ts);
a39fb9ea 333
59f94d26 334 for (i = 0; i < FIO_ARRAY_SIZE(timer); ++i)
df9bd7d4 335 ret = eval_timer(&timer[i], &ts, &msec_to_next_event);
a39fb9ea 336
52a552e2 337 if (action == A_DO_STAT)
a39fb9ea 338 __show_running_run_stats();
a39fb9ea
JA
339
340 next_log = calc_log_samples();
341 if (!next_log)
342 next_log = DISK_UTIL_MSEC;
343
3da71e37 344 msec_to_next_event = min(next_log, msec_to_next_event);
df9bd7d4
BVA
345 dprint(FD_HELPERTHREAD,
346 "next_log: %u, msec_to_next_event: %u\n",
347 next_log, msec_to_next_event);
a39fb9ea
JA
348
349 if (!is_backend)
350 print_thread_status();
351 }
352
696378af
BVA
353 if (timerfd >= 0) {
354 close(timerfd);
355 timerfd = -1;
356 }
357
a39fb9ea
JA
358 fio_writeout_logs(false);
359
360 sk_out_drop();
361 return NULL;
362}
363
52a552e2
BVA
364/*
365 * Connect two sockets to each other to emulate the pipe() system call on Windows.
366 */
367int pipe_over_loopback(int fd[2])
368{
369 struct sockaddr_in addr = { .sin_family = AF_INET };
370 socklen_t len = sizeof(addr);
371 int res;
372
373 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
374
375 sock_init();
376
377 fd[0] = socket(AF_INET, SOCK_STREAM, 0);
378 if (fd[0] < 0)
379 goto err;
380 fd[1] = socket(AF_INET, SOCK_STREAM, 0);
381 if (fd[1] < 0)
382 goto close_fd_0;
383 res = bind(fd[0], (struct sockaddr *)&addr, len);
384 if (res < 0)
385 goto close_fd_1;
386 res = getsockname(fd[0], (struct sockaddr *)&addr, &len);
387 if (res < 0)
388 goto close_fd_1;
389 res = listen(fd[0], 1);
390 if (res < 0)
391 goto close_fd_1;
392 res = connect(fd[1], (struct sockaddr *)&addr, len);
393 if (res < 0)
394 goto close_fd_1;
395 res = accept(fd[0], NULL, NULL);
396 if (res < 0)
397 goto close_fd_1;
398 close(fd[0]);
399 fd[0] = res;
400 return 0;
401
402close_fd_1:
403 close(fd[1]);
404
405close_fd_0:
406 close(fd[0]);
407
408err:
409 return -1;
410}
411
971caeb1 412int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
a39fb9ea
JA
413{
414 struct helper_data *hd;
415 int ret;
416
b3090ff4 417 hd = scalloc(1, sizeof(*hd));
a39fb9ea
JA
418
419 setup_disk_util();
16e56d25 420 steadystate_setup();
a39fb9ea
JA
421
422 hd->sk_out = sk_out;
34febb23 423
52a552e2
BVA
424#if defined(CONFIG_PIPE2)
425 ret = pipe2(hd->pipe, O_CLOEXEC);
426#elif defined(CONFIG_PIPE)
427 ret = pipe(hd->pipe);
428#else
429 ret = pipe_over_loopback(hd->pipe);
430#endif
34febb23 431 if (ret)
f9e5b5ee 432 return 1;
34febb23 433
52a552e2
BVA
434 ret = make_nonblocking(hd->pipe[0]);
435 assert(ret >= 0);
436
971caeb1 437 hd->startup_sem = startup_sem;
a39fb9ea 438
4f37732a
BVA
439 DRD_IGNORE_VAR(helper_data);
440
a39fb9ea
JA
441 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
442 if (ret) {
443 log_err("Can't create helper thread: %s\n", strerror(ret));
444 return 1;
445 }
446
447 helper_data = hd;
448
971caeb1
BVA
449 dprint(FD_MUTEX, "wait on startup_sem\n");
450 fio_sem_down(startup_sem);
451 dprint(FD_MUTEX, "done waiting on startup_sem\n");
a39fb9ea
JA
452 return 0;
453}