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 void *helper_thread_main(void *data)
133 struct helper_data *hd = data;
134 unsigned int msec_to_next_event, next_log, next_si;
135 unsigned int next_ss = STEADYSTATE_MSEC;
136 struct timespec ts, last_du, last_ss, last_si;
140 sk_out_assign(hd->sk_out);
142 #ifdef HAVE_PTHREAD_SIGMASK
146 /* Let another thread handle signals. */
147 ret = pthread_sigmask(SIG_UNBLOCK, NULL, &sigmask);
149 ret = pthread_sigmask(SIG_BLOCK, &sigmask, NULL);
154 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
155 clock_gettime(CLOCK_MONOTONIC, &ts);
157 clock_gettime(CLOCK_REALTIME, &ts);
159 memcpy(&last_du, &ts, sizeof(ts));
160 memcpy(&last_ss, &ts, sizeof(ts));
161 memcpy(&last_si, &ts, sizeof(ts));
163 fio_sem_up(hd->startup_sem);
165 msec_to_next_event = DISK_UTIL_MSEC;
166 while (!ret && !hd->exit) {
167 uint64_t since_du, since_si, since_ss = 0;
168 struct timeval timeout = {
169 .tv_sec = msec_to_next_event / 1000,
170 .tv_usec = (msec_to_next_event % 1000) * 1000,
174 if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < 0) {
176 FD_SET(hd->pipe[0], &rfds);
178 FD_SET(hd->pipe[0], &efds);
179 ret = select(1, &rfds, NULL, &efds, &timeout);
181 log_err("fio: select() call in helper thread failed: %s",
183 if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) <
188 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
189 clock_gettime(CLOCK_MONOTONIC, &ts);
191 clock_gettime(CLOCK_REALTIME, &ts);
194 if (action == A_RESET) {
199 since_du = mtime_since(&last_du, &ts);
200 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
201 ret = update_io_ticks();
202 timespec_add_msec(&last_du, DISK_UTIL_MSEC);
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);
207 msec_to_next_event = DISK_UTIL_MSEC - since_du;
209 if (action == A_DO_STAT)
210 __show_running_run_stats();
212 if (status_interval) {
213 since_si = mtime_since(&last_si, &ts);
214 if (since_si >= status_interval || status_interval - since_si < 10) {
215 __show_running_run_stats();
216 timespec_add_msec(&last_si, since_si);
217 if (since_si > status_interval)
218 next_si = status_interval - (since_si - status_interval);
220 next_si = status_interval;
222 next_si = status_interval - since_si;
223 msec_to_next_event = min(next_si, msec_to_next_event);
226 next_log = calc_log_samples();
228 next_log = DISK_UTIL_MSEC;
230 if (steadystate_enabled) {
231 since_ss = mtime_since(&last_ss, &ts);
232 if (since_ss >= STEADYSTATE_MSEC || STEADYSTATE_MSEC - since_ss < 10) {
234 timespec_add_msec(&last_ss, since_ss);
235 if (since_ss > STEADYSTATE_MSEC)
236 next_ss = STEADYSTATE_MSEC - (since_ss - STEADYSTATE_MSEC);
238 next_ss = STEADYSTATE_MSEC;
240 next_ss = STEADYSTATE_MSEC - since_ss;
243 msec_to_next_event = min(min(next_log, msec_to_next_event), next_ss);
244 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);
247 print_thread_status();
250 fio_writeout_logs(false);
257 * Connect two sockets to each other to emulate the pipe() system call on Windows.
259 int pipe_over_loopback(int fd[2])
261 struct sockaddr_in addr = { .sin_family = AF_INET };
262 socklen_t len = sizeof(addr);
265 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
269 fd[0] = socket(AF_INET, SOCK_STREAM, 0);
272 fd[1] = socket(AF_INET, SOCK_STREAM, 0);
275 res = bind(fd[0], (struct sockaddr *)&addr, len);
278 res = getsockname(fd[0], (struct sockaddr *)&addr, &len);
281 res = listen(fd[0], 1);
284 res = connect(fd[1], (struct sockaddr *)&addr, len);
287 res = accept(fd[0], NULL, NULL);
304 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
306 struct helper_data *hd;
309 hd = scalloc(1, sizeof(*hd));
316 #if defined(CONFIG_PIPE2)
317 ret = pipe2(hd->pipe, O_CLOEXEC);
318 #elif defined(CONFIG_PIPE)
319 ret = pipe(hd->pipe);
321 ret = pipe_over_loopback(hd->pipe);
326 ret = make_nonblocking(hd->pipe[0]);
329 hd->startup_sem = startup_sem;
331 DRD_IGNORE_VAR(helper_data);
333 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
335 log_err("Can't create helper thread: %s\n", strerror(ret));
341 dprint(FD_MUTEX, "wait on startup_sem\n");
342 fio_sem_down(startup_sem);
343 dprint(FD_MUTEX, "done waiting on startup_sem\n");