2 #ifdef CONFIG_VALGRIND_DEV
3 #include <valgrind/drd.h>
5 #define DRD_IGNORE_VAR(x) do { } while (0)
10 #include "helper_thread.h"
11 #include "steadystate.h"
20 static struct helper_data {
22 int pipe[2]; /* 0: read end; 1: write end. */
23 struct sk_out *sk_out;
25 struct fio_sem *startup_sem;
28 void helper_thread_destroy(void)
33 close(helper_data->pipe[0]);
34 close(helper_data->pipe[1]);
39 static void sock_init(void)
44 /* It is allowed to call WSAStartup() more than once. */
45 res = WSAStartup(MAKEWORD(2, 2), &wsaData);
49 static int make_nonblocking(int fd)
51 unsigned long arg = 1;
53 return ioctlsocket(fd, FIONBIO, &arg);
56 static int write_to_pipe(int fd, const void *buf, size_t len)
58 return send(fd, buf, len, 0);
61 static int read_from_pipe(int fd, void *buf, size_t len)
63 return recv(fd, buf, len, 0);
66 static void sock_init(void)
70 static int make_nonblocking(int fd)
72 return fcntl(fd, F_SETFL, O_NONBLOCK);
75 static int write_to_pipe(int fd, const void *buf, size_t len)
77 return write(fd, buf, len);
80 static int read_from_pipe(int fd, void *buf, size_t len)
82 return read(fd, buf, len);
86 static void block_signals(void)
88 #ifdef HAVE_PTHREAD_SIGMASK
91 ret = pthread_sigmask(SIG_UNBLOCK, NULL, &sigmask);
93 ret = pthread_sigmask(SIG_BLOCK, &sigmask, NULL);
98 static void submit_action(enum action a)
106 ret = write_to_pipe(helper_data->pipe[1], &data, sizeof(data));
110 void helper_reset(void)
112 submit_action(A_RESET);
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.
120 void helper_do_stat(void)
122 submit_action(A_DO_STAT);
125 bool helper_should_exit(void)
130 return helper_data->exit;
133 void helper_thread_exit(void)
138 helper_data->exit = 1;
139 submit_action(A_EXIT);
140 pthread_join(helper_data->thread, NULL);
144 * Waits for an action from fd during at least timeout_ms. `fd` must be in
147 static uint8_t wait_for_action(int fd, unsigned int timeout_ms)
149 struct timeval timeout = {
150 .tv_sec = timeout_ms / 1000,
151 .tv_usec = (timeout_ms % 1000) * 1000,
157 res = read_from_pipe(fd, &action, sizeof(action));
158 if (res > 0 || timeout_ms == 0)
164 res = select(fd + 1, &rfds, NULL, &efds, &timeout);
166 log_err("fio: select() call in helper thread failed: %s",
170 if (FD_ISSET(fd, &rfds))
171 read_from_pipe(fd, &action, sizeof(action));
175 static unsigned int task_helper(struct timespec *last, struct timespec *now, unsigned int period, void do_task())
177 unsigned int next, since;
179 since = mtime_since(last, now);
180 if (since >= period || period - since < 10) {
182 timespec_add_msec(last, since);
184 next = period - (since - period);
188 next = period - since;
193 static void *helper_thread_main(void *data)
195 struct helper_data *hd = data;
196 unsigned int msec_to_next_event, next_log, next_si = status_interval;
197 unsigned int next_ss = STEADYSTATE_MSEC;
198 struct timespec ts, last_du, last_ss, last_si;
201 sk_out_assign(hd->sk_out);
203 /* Let another thread handle signals. */
206 fio_get_mono_time(&ts);
207 memcpy(&last_du, &ts, sizeof(ts));
208 memcpy(&last_ss, &ts, sizeof(ts));
209 memcpy(&last_si, &ts, sizeof(ts));
211 fio_sem_up(hd->startup_sem);
213 msec_to_next_event = DISK_UTIL_MSEC;
214 while (!ret && !hd->exit) {
218 action = wait_for_action(hd->pipe[0], msec_to_next_event);
219 if (action == A_EXIT)
222 fio_get_mono_time(&ts);
224 if (action == A_RESET) {
229 since_du = mtime_since(&last_du, &ts);
230 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
231 ret = update_io_ticks();
232 timespec_add_msec(&last_du, DISK_UTIL_MSEC);
233 msec_to_next_event = DISK_UTIL_MSEC;
234 if (since_du >= DISK_UTIL_MSEC)
235 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
237 msec_to_next_event = DISK_UTIL_MSEC - since_du;
239 if (action == A_DO_STAT)
240 __show_running_run_stats();
242 if (status_interval) {
243 next_si = task_helper(&last_si, &ts, status_interval, __show_running_run_stats);
244 msec_to_next_event = min(next_si, msec_to_next_event);
247 next_log = calc_log_samples();
249 next_log = DISK_UTIL_MSEC;
251 if (steadystate_enabled) {
252 next_ss = task_helper(&last_ss, &ts, STEADYSTATE_MSEC, steadystate_check);
253 msec_to_next_event = min(next_ss, msec_to_next_event);
256 msec_to_next_event = min(next_log, msec_to_next_event);
257 dprint(FD_HELPERTHREAD, "next_si: %u, next_ss: %u, next_log: %u, msec_to_next_event: %u\n",
258 next_si, next_ss, next_log, msec_to_next_event);
261 print_thread_status();
264 fio_writeout_logs(false);
271 * Connect two sockets to each other to emulate the pipe() system call on Windows.
273 int pipe_over_loopback(int fd[2])
275 struct sockaddr_in addr = { .sin_family = AF_INET };
276 socklen_t len = sizeof(addr);
279 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
283 fd[0] = socket(AF_INET, SOCK_STREAM, 0);
286 fd[1] = socket(AF_INET, SOCK_STREAM, 0);
289 res = bind(fd[0], (struct sockaddr *)&addr, len);
292 res = getsockname(fd[0], (struct sockaddr *)&addr, &len);
295 res = listen(fd[0], 1);
298 res = connect(fd[1], (struct sockaddr *)&addr, len);
301 res = accept(fd[0], NULL, NULL);
318 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
320 struct helper_data *hd;
323 hd = scalloc(1, sizeof(*hd));
330 #if defined(CONFIG_PIPE2)
331 ret = pipe2(hd->pipe, O_CLOEXEC);
332 #elif defined(CONFIG_PIPE)
333 ret = pipe(hd->pipe);
335 ret = pipe_over_loopback(hd->pipe);
340 ret = make_nonblocking(hd->pipe[0]);
343 hd->startup_sem = startup_sem;
345 DRD_IGNORE_VAR(helper_data);
347 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
349 log_err("Can't create helper thread: %s\n", strerror(ret));
355 dprint(FD_MUTEX, "wait on startup_sem\n");
356 fio_sem_down(startup_sem);
357 dprint(FD_MUTEX, "done waiting on startup_sem\n");