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 submit_action(enum action a)
94 ret = write_to_pipe(helper_data->pipe[1], &data, sizeof(data));
98 void helper_reset(void)
100 submit_action(A_RESET);
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.
108 void helper_do_stat(void)
110 submit_action(A_DO_STAT);
113 bool helper_should_exit(void)
118 return helper_data->exit;
121 void helper_thread_exit(void)
126 helper_data->exit = 1;
127 submit_action(A_EXIT);
128 pthread_join(helper_data->thread, NULL);
131 static unsigned int task_helper(struct timespec *last, struct timespec *now, unsigned int period, void do_task())
133 unsigned int next, since;
135 since = mtime_since(last, now);
136 if (since >= period || period - since < 10) {
138 timespec_add_msec(last, since);
140 next = period - (since - period);
144 next = period - since;
149 static void *helper_thread_main(void *data)
151 struct helper_data *hd = data;
152 unsigned int msec_to_next_event, next_log, next_si = status_interval;
153 unsigned int next_ss = STEADYSTATE_MSEC;
154 struct timespec ts, last_du, last_ss, last_si;
158 sk_out_assign(hd->sk_out);
160 #ifdef HAVE_PTHREAD_SIGMASK
164 /* Let another thread handle signals. */
165 ret = pthread_sigmask(SIG_UNBLOCK, NULL, &sigmask);
167 ret = pthread_sigmask(SIG_BLOCK, &sigmask, NULL);
172 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
173 clock_gettime(CLOCK_MONOTONIC, &ts);
175 clock_gettime(CLOCK_REALTIME, &ts);
177 memcpy(&last_du, &ts, sizeof(ts));
178 memcpy(&last_ss, &ts, sizeof(ts));
179 memcpy(&last_si, &ts, sizeof(ts));
181 fio_sem_up(hd->startup_sem);
183 msec_to_next_event = DISK_UTIL_MSEC;
184 while (!ret && !hd->exit) {
186 struct timeval timeout = {
187 .tv_sec = msec_to_next_event / 1000,
188 .tv_usec = (msec_to_next_event % 1000) * 1000,
192 if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < 0) {
194 FD_SET(hd->pipe[0], &rfds);
196 FD_SET(hd->pipe[0], &efds);
197 if (select(1, &rfds, NULL, &efds, &timeout) < 0) {
198 log_err("fio: select() call in helper thread failed: %s",
202 if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) <
207 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
208 clock_gettime(CLOCK_MONOTONIC, &ts);
210 clock_gettime(CLOCK_REALTIME, &ts);
213 if (action == A_RESET) {
218 since_du = mtime_since(&last_du, &ts);
219 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
220 ret = update_io_ticks();
221 timespec_add_msec(&last_du, DISK_UTIL_MSEC);
222 msec_to_next_event = DISK_UTIL_MSEC;
223 if (since_du >= DISK_UTIL_MSEC)
224 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
226 msec_to_next_event = DISK_UTIL_MSEC - since_du;
228 if (action == A_DO_STAT)
229 __show_running_run_stats();
231 if (status_interval) {
232 next_si = task_helper(&last_si, &ts, status_interval, __show_running_run_stats);
233 msec_to_next_event = min(next_si, msec_to_next_event);
236 next_log = calc_log_samples();
238 next_log = DISK_UTIL_MSEC;
240 if (steadystate_enabled) {
241 next_ss = task_helper(&last_ss, &ts, STEADYSTATE_MSEC, steadystate_check);
242 msec_to_next_event = min(next_ss, msec_to_next_event);
245 msec_to_next_event = min(next_log, msec_to_next_event);
246 dprint(FD_HELPERTHREAD, "next_si: %u, next_ss: %u, next_log: %u, msec_to_next_event: %u\n",
247 next_si, next_ss, next_log, msec_to_next_event);
250 print_thread_status();
253 fio_writeout_logs(false);
260 * Connect two sockets to each other to emulate the pipe() system call on Windows.
262 int pipe_over_loopback(int fd[2])
264 struct sockaddr_in addr = { .sin_family = AF_INET };
265 socklen_t len = sizeof(addr);
268 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
272 fd[0] = socket(AF_INET, SOCK_STREAM, 0);
275 fd[1] = socket(AF_INET, SOCK_STREAM, 0);
278 res = bind(fd[0], (struct sockaddr *)&addr, len);
281 res = getsockname(fd[0], (struct sockaddr *)&addr, &len);
284 res = listen(fd[0], 1);
287 res = connect(fd[1], (struct sockaddr *)&addr, len);
290 res = accept(fd[0], NULL, NULL);
307 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
309 struct helper_data *hd;
312 hd = scalloc(1, sizeof(*hd));
319 #if defined(CONFIG_PIPE2)
320 ret = pipe2(hd->pipe, O_CLOEXEC);
321 #elif defined(CONFIG_PIPE)
322 ret = pipe(hd->pipe);
324 ret = pipe_over_loopback(hd->pipe);
329 ret = make_nonblocking(hd->pipe[0]);
332 hd->startup_sem = startup_sem;
334 DRD_IGNORE_VAR(helper_data);
336 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
338 log_err("Can't create helper thread: %s\n", strerror(ret));
344 dprint(FD_MUTEX, "wait on startup_sem\n");
345 fio_sem_down(startup_sem);
346 dprint(FD_MUTEX, "done waiting on startup_sem\n");