1 #ifdef CONFIG_VALGRIND_DEV
2 #include <valgrind/drd.h>
4 #define DRD_IGNORE_VAR(x) do { } while (0)
9 #include "helper_thread.h"
10 #include "steadystate.h"
19 static struct helper_data {
21 int pipe[2]; /* 0: read end; 1: write end. */
22 struct sk_out *sk_out;
24 struct fio_sem *startup_sem;
27 void helper_thread_destroy(void)
32 close(helper_data->pipe[0]);
33 close(helper_data->pipe[1]);
38 static void sock_init(void)
43 /* It is allowed to call WSAStartup() more than once. */
44 res = WSAStartup(MAKEWORD(2, 2), &wsaData);
48 static int make_nonblocking(int fd)
50 unsigned long arg = 1;
52 return ioctlsocket(fd, FIONBIO, &arg);
55 static int write_to_pipe(int fd, const void *buf, size_t len)
57 return send(fd, buf, len, 0);
60 static int read_from_pipe(int fd, void *buf, size_t len)
62 return recv(fd, buf, len, 0);
65 static void sock_init(void)
69 static int make_nonblocking(int fd)
71 return fcntl(fd, F_SETFL, O_NONBLOCK);
74 static int write_to_pipe(int fd, const void *buf, size_t len)
76 return write(fd, buf, len);
79 static int read_from_pipe(int fd, void *buf, size_t len)
81 return read(fd, buf, len);
85 static void submit_action(enum action a)
93 ret = write_to_pipe(helper_data->pipe[1], &data, sizeof(data));
97 void helper_reset(void)
99 submit_action(A_RESET);
103 * May be invoked in signal handler context and hence must only call functions
104 * that are async-signal-safe. See also
105 * https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03.
107 void helper_do_stat(void)
109 submit_action(A_DO_STAT);
112 bool helper_should_exit(void)
117 return helper_data->exit;
120 void helper_thread_exit(void)
125 helper_data->exit = 1;
126 submit_action(A_EXIT);
127 pthread_join(helper_data->thread, NULL);
130 static void *helper_thread_main(void *data)
132 struct helper_data *hd = data;
133 unsigned int msec_to_next_event, next_log, next_ss = STEADYSTATE_MSEC;
134 struct timespec ts, last_du, last_ss;
138 sk_out_assign(hd->sk_out);
140 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
141 clock_gettime(CLOCK_MONOTONIC, &ts);
143 clock_gettime(CLOCK_REALTIME, &ts);
145 memcpy(&last_du, &ts, sizeof(ts));
146 memcpy(&last_ss, &ts, sizeof(ts));
148 fio_sem_up(hd->startup_sem);
150 msec_to_next_event = DISK_UTIL_MSEC;
151 while (!ret && !hd->exit) {
152 uint64_t since_du, since_ss = 0;
153 struct timeval timeout = {
154 .tv_sec = DISK_UTIL_MSEC / 1000,
155 .tv_usec = (DISK_UTIL_MSEC % 1000) * 1000,
159 timespec_add_msec(&ts, msec_to_next_event);
161 if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < 0) {
163 FD_SET(hd->pipe[0], &rfds);
165 FD_SET(hd->pipe[0], &efds);
166 select(1, &rfds, NULL, &efds, &timeout);
167 if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) <
172 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
173 clock_gettime(CLOCK_MONOTONIC, &ts);
175 clock_gettime(CLOCK_REALTIME, &ts);
178 if (action == A_RESET) {
183 since_du = mtime_since(&last_du, &ts);
184 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
185 ret = update_io_ticks();
186 timespec_add_msec(&last_du, DISK_UTIL_MSEC);
187 msec_to_next_event = DISK_UTIL_MSEC;
188 if (since_du >= DISK_UTIL_MSEC)
189 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
191 msec_to_next_event = DISK_UTIL_MSEC - since_du;
193 if (action == A_DO_STAT)
194 __show_running_run_stats();
196 next_log = calc_log_samples();
198 next_log = DISK_UTIL_MSEC;
200 if (steadystate_enabled) {
201 since_ss = mtime_since(&last_ss, &ts);
202 if (since_ss >= STEADYSTATE_MSEC || STEADYSTATE_MSEC - since_ss < 10) {
204 timespec_add_msec(&last_ss, since_ss);
205 if (since_ss > STEADYSTATE_MSEC)
206 next_ss = STEADYSTATE_MSEC - (since_ss - STEADYSTATE_MSEC);
208 next_ss = STEADYSTATE_MSEC;
210 next_ss = STEADYSTATE_MSEC - since_ss;
213 msec_to_next_event = min(min(next_log, msec_to_next_event), next_ss);
214 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);
217 print_thread_status();
220 fio_writeout_logs(false);
227 * Connect two sockets to each other to emulate the pipe() system call on Windows.
229 int pipe_over_loopback(int fd[2])
231 struct sockaddr_in addr = { .sin_family = AF_INET };
232 socklen_t len = sizeof(addr);
235 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
239 fd[0] = socket(AF_INET, SOCK_STREAM, 0);
242 fd[1] = socket(AF_INET, SOCK_STREAM, 0);
245 res = bind(fd[0], (struct sockaddr *)&addr, len);
248 res = getsockname(fd[0], (struct sockaddr *)&addr, &len);
251 res = listen(fd[0], 1);
254 res = connect(fd[1], (struct sockaddr *)&addr, len);
257 res = accept(fd[0], NULL, NULL);
274 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
276 struct helper_data *hd;
279 hd = scalloc(1, sizeof(*hd));
286 #if defined(CONFIG_PIPE2)
287 ret = pipe2(hd->pipe, O_CLOEXEC);
288 #elif defined(CONFIG_PIPE)
289 ret = pipe(hd->pipe);
291 ret = pipe_over_loopback(hd->pipe);
296 ret = make_nonblocking(hd->pipe[0]);
299 hd->startup_sem = startup_sem;
301 DRD_IGNORE_VAR(helper_data);
303 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
305 log_err("Can't create helper thread: %s\n", strerror(ret));
311 dprint(FD_MUTEX, "wait on startup_sem\n");
312 fio_sem_down(startup_sem);
313 dprint(FD_MUTEX, "done waiting on startup_sem\n");