Merge branch 'master' of https://github.com/bvanassche/fio
[fio.git] / helper_thread.c
CommitLineData
4f37732a
BVA
1#ifdef CONFIG_VALGRIND_DEV
2#include <valgrind/drd.h>
3#else
4#define DRD_IGNORE_VAR(x) do { } while (0)
5#endif
6
a39fb9ea
JA
7#include "fio.h"
8#include "smalloc.h"
9#include "helper_thread.h"
16e56d25 10#include "steadystate.h"
ae626d4e 11#include "pshared.h"
a39fb9ea 12
52a552e2
BVA
13enum action {
14 A_EXIT = 1,
15 A_RESET = 2,
16 A_DO_STAT = 3,
17};
18
a39fb9ea
JA
19static struct helper_data {
20 volatile int exit;
52a552e2 21 int pipe[2]; /* 0: read end; 1: write end. */
a39fb9ea
JA
22 struct sk_out *sk_out;
23 pthread_t thread;
971caeb1 24 struct fio_sem *startup_sem;
a39fb9ea
JA
25} *helper_data;
26
27void helper_thread_destroy(void)
28{
998e9ebb
BVA
29 if (!helper_data)
30 return;
31
52a552e2
BVA
32 close(helper_data->pipe[0]);
33 close(helper_data->pipe[1]);
a39fb9ea
JA
34 sfree(helper_data);
35}
36
52a552e2
BVA
37#ifdef _WIN32
38static void sock_init(void)
a39fb9ea 39{
52a552e2
BVA
40 WSADATA wsaData;
41 int res;
a39fb9ea 42
52a552e2
BVA
43 /* It is allowed to call WSAStartup() more than once. */
44 res = WSAStartup(MAKEWORD(2, 2), &wsaData);
45 assert(res == 0);
46}
a39fb9ea 47
52a552e2
BVA
48static int make_nonblocking(int fd)
49{
50 unsigned long arg = 1;
dda11987 51
52a552e2 52 return ioctlsocket(fd, FIONBIO, &arg);
a39fb9ea
JA
53}
54
52a552e2
BVA
55static int write_to_pipe(int fd, const void *buf, size_t len)
56{
57 return send(fd, buf, len, 0);
58}
59
60static int read_from_pipe(int fd, void *buf, size_t len)
61{
62 return recv(fd, buf, len, 0);
63}
64#else
65static void sock_init(void)
66{
67}
68
69static int make_nonblocking(int fd)
70{
71 return fcntl(fd, F_SETFL, O_NONBLOCK);
72}
73
74static int write_to_pipe(int fd, const void *buf, size_t len)
75{
76 return write(fd, buf, len);
77}
78
79static int read_from_pipe(int fd, void *buf, size_t len)
80{
81 return read(fd, buf, len);
82}
83#endif
84
85static void submit_action(enum action a)
a39fb9ea 86{
52a552e2
BVA
87 const char data = a;
88 int ret;
89
dda11987
JA
90 if (!helper_data)
91 return;
92
52a552e2
BVA
93 ret = write_to_pipe(helper_data->pipe[1], &data, sizeof(data));
94 assert(ret == 1);
95}
96
97void helper_reset(void)
98{
99 submit_action(A_RESET);
100}
101
102/*
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.
106 */
107void helper_do_stat(void)
108{
109 submit_action(A_DO_STAT);
a39fb9ea
JA
110}
111
112bool helper_should_exit(void)
113{
114 if (!helper_data)
115 return true;
116
117 return helper_data->exit;
118}
119
120void helper_thread_exit(void)
121{
998e9ebb
BVA
122 if (!helper_data)
123 return;
124
a39fb9ea 125 helper_data->exit = 1;
52a552e2
BVA
126 submit_action(A_EXIT);
127 pthread_join(helper_data->thread, NULL);
a39fb9ea
JA
128}
129
130static void *helper_thread_main(void *data)
131{
132 struct helper_data *hd = data;
16e56d25 133 unsigned int msec_to_next_event, next_log, next_ss = STEADYSTATE_MSEC;
8b6a404c 134 struct timespec ts, last_du, last_ss;
52a552e2 135 char action;
a39fb9ea
JA
136 int ret = 0;
137
138 sk_out_assign(hd->sk_out);
139
78b66d32
BVA
140#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
141 clock_gettime(CLOCK_MONOTONIC, &ts);
142#else
143 clock_gettime(CLOCK_REALTIME, &ts);
144#endif
8b6a404c
VF
145 memcpy(&last_du, &ts, sizeof(ts));
146 memcpy(&last_ss, &ts, sizeof(ts));
a39fb9ea 147
971caeb1 148 fio_sem_up(hd->startup_sem);
a39fb9ea
JA
149
150 msec_to_next_event = DISK_UTIL_MSEC;
151 while (!ret && !hd->exit) {
16e56d25 152 uint64_t since_du, since_ss = 0;
52a552e2
BVA
153 struct timeval timeout = {
154 .tv_sec = DISK_UTIL_MSEC / 1000,
155 .tv_usec = (DISK_UTIL_MSEC % 1000) * 1000,
156 };
157 fd_set rfds, efds;
a39fb9ea 158
8b6a404c 159 timespec_add_msec(&ts, msec_to_next_event);
a39fb9ea 160
52a552e2
BVA
161 if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < 0) {
162 FD_ZERO(&rfds);
163 FD_SET(hd->pipe[0], &rfds);
164 FD_ZERO(&efds);
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)) <
168 0)
169 action = 0;
170 }
a39fb9ea 171
78b66d32
BVA
172#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
173 clock_gettime(CLOCK_MONOTONIC, &ts);
174#else
175 clock_gettime(CLOCK_REALTIME, &ts);
176#endif
a39fb9ea 177
52a552e2
BVA
178 if (action == A_RESET) {
179 last_du = ts;
180 last_ss = ts;
a39fb9ea
JA
181 }
182
8b6a404c 183 since_du = mtime_since(&last_du, &ts);
a39fb9ea
JA
184 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
185 ret = update_io_ticks();
8b6a404c 186 timespec_add_msec(&last_du, DISK_UTIL_MSEC);
a39fb9ea
JA
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);
dd290fb4
VF
190 } else
191 msec_to_next_event = DISK_UTIL_MSEC - since_du;
a39fb9ea 192
52a552e2 193 if (action == A_DO_STAT)
a39fb9ea 194 __show_running_run_stats();
a39fb9ea
JA
195
196 next_log = calc_log_samples();
197 if (!next_log)
198 next_log = DISK_UTIL_MSEC;
199
84784e07 200 if (steadystate_enabled) {
8b6a404c 201 since_ss = mtime_since(&last_ss, &ts);
16e56d25
VF
202 if (since_ss >= STEADYSTATE_MSEC || STEADYSTATE_MSEC - since_ss < 10) {
203 steadystate_check();
8b6a404c 204 timespec_add_msec(&last_ss, since_ss);
16e56d25
VF
205 if (since_ss > STEADYSTATE_MSEC)
206 next_ss = STEADYSTATE_MSEC - (since_ss - STEADYSTATE_MSEC);
207 else
208 next_ss = STEADYSTATE_MSEC;
c27cc65f 209 } else
16e56d25
VF
210 next_ss = STEADYSTATE_MSEC - since_ss;
211 }
212
213 msec_to_next_event = min(min(next_log, msec_to_next_event), next_ss);
e569ca6b 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);
a39fb9ea
JA
215
216 if (!is_backend)
217 print_thread_status();
218 }
219
220 fio_writeout_logs(false);
221
222 sk_out_drop();
223 return NULL;
224}
225
52a552e2
BVA
226/*
227 * Connect two sockets to each other to emulate the pipe() system call on Windows.
228 */
229int pipe_over_loopback(int fd[2])
230{
231 struct sockaddr_in addr = { .sin_family = AF_INET };
232 socklen_t len = sizeof(addr);
233 int res;
234
235 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
236
237 sock_init();
238
239 fd[0] = socket(AF_INET, SOCK_STREAM, 0);
240 if (fd[0] < 0)
241 goto err;
242 fd[1] = socket(AF_INET, SOCK_STREAM, 0);
243 if (fd[1] < 0)
244 goto close_fd_0;
245 res = bind(fd[0], (struct sockaddr *)&addr, len);
246 if (res < 0)
247 goto close_fd_1;
248 res = getsockname(fd[0], (struct sockaddr *)&addr, &len);
249 if (res < 0)
250 goto close_fd_1;
251 res = listen(fd[0], 1);
252 if (res < 0)
253 goto close_fd_1;
254 res = connect(fd[1], (struct sockaddr *)&addr, len);
255 if (res < 0)
256 goto close_fd_1;
257 res = accept(fd[0], NULL, NULL);
258 if (res < 0)
259 goto close_fd_1;
260 close(fd[0]);
261 fd[0] = res;
262 return 0;
263
264close_fd_1:
265 close(fd[1]);
266
267close_fd_0:
268 close(fd[0]);
269
270err:
271 return -1;
272}
273
971caeb1 274int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
a39fb9ea
JA
275{
276 struct helper_data *hd;
277 int ret;
278
b3090ff4 279 hd = scalloc(1, sizeof(*hd));
a39fb9ea
JA
280
281 setup_disk_util();
16e56d25 282 steadystate_setup();
a39fb9ea
JA
283
284 hd->sk_out = sk_out;
34febb23 285
52a552e2
BVA
286#if defined(CONFIG_PIPE2)
287 ret = pipe2(hd->pipe, O_CLOEXEC);
288#elif defined(CONFIG_PIPE)
289 ret = pipe(hd->pipe);
290#else
291 ret = pipe_over_loopback(hd->pipe);
292#endif
34febb23 293 if (ret)
f9e5b5ee 294 return 1;
34febb23 295
52a552e2
BVA
296 ret = make_nonblocking(hd->pipe[0]);
297 assert(ret >= 0);
298
971caeb1 299 hd->startup_sem = startup_sem;
a39fb9ea 300
4f37732a
BVA
301 DRD_IGNORE_VAR(helper_data);
302
a39fb9ea
JA
303 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
304 if (ret) {
305 log_err("Can't create helper thread: %s\n", strerror(ret));
306 return 1;
307 }
308
309 helper_data = hd;
310
971caeb1
BVA
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");
a39fb9ea
JA
314 return 0;
315}