Commit | Line | Data |
---|---|---|
c31092b8 | 1 | #include <signal.h> |
4f37732a BVA |
2 | #ifdef CONFIG_VALGRIND_DEV |
3 | #include <valgrind/drd.h> | |
4 | #else | |
5 | #define DRD_IGNORE_VAR(x) do { } while (0) | |
6 | #endif | |
7 | ||
a39fb9ea JA |
8 | #include "fio.h" |
9 | #include "smalloc.h" | |
10 | #include "helper_thread.h" | |
16e56d25 | 11 | #include "steadystate.h" |
ae626d4e | 12 | #include "pshared.h" |
a39fb9ea | 13 | |
52a552e2 BVA |
14 | enum action { |
15 | A_EXIT = 1, | |
16 | A_RESET = 2, | |
17 | A_DO_STAT = 3, | |
18 | }; | |
19 | ||
a39fb9ea JA |
20 | static struct helper_data { |
21 | volatile int exit; | |
52a552e2 | 22 | int pipe[2]; /* 0: read end; 1: write end. */ |
a39fb9ea JA |
23 | struct sk_out *sk_out; |
24 | pthread_t thread; | |
971caeb1 | 25 | struct fio_sem *startup_sem; |
a39fb9ea JA |
26 | } *helper_data; |
27 | ||
28 | void helper_thread_destroy(void) | |
29 | { | |
998e9ebb BVA |
30 | if (!helper_data) |
31 | return; | |
32 | ||
52a552e2 BVA |
33 | close(helper_data->pipe[0]); |
34 | close(helper_data->pipe[1]); | |
a39fb9ea JA |
35 | sfree(helper_data); |
36 | } | |
37 | ||
52a552e2 BVA |
38 | #ifdef _WIN32 |
39 | static void sock_init(void) | |
a39fb9ea | 40 | { |
52a552e2 BVA |
41 | WSADATA wsaData; |
42 | int res; | |
a39fb9ea | 43 | |
52a552e2 BVA |
44 | /* It is allowed to call WSAStartup() more than once. */ |
45 | res = WSAStartup(MAKEWORD(2, 2), &wsaData); | |
46 | assert(res == 0); | |
47 | } | |
a39fb9ea | 48 | |
52a552e2 BVA |
49 | static int make_nonblocking(int fd) |
50 | { | |
51 | unsigned long arg = 1; | |
dda11987 | 52 | |
52a552e2 | 53 | return ioctlsocket(fd, FIONBIO, &arg); |
a39fb9ea JA |
54 | } |
55 | ||
52a552e2 BVA |
56 | static int write_to_pipe(int fd, const void *buf, size_t len) |
57 | { | |
58 | return send(fd, buf, len, 0); | |
59 | } | |
60 | ||
61 | static int read_from_pipe(int fd, void *buf, size_t len) | |
62 | { | |
63 | return recv(fd, buf, len, 0); | |
64 | } | |
65 | #else | |
66 | static void sock_init(void) | |
67 | { | |
68 | } | |
69 | ||
70 | static int make_nonblocking(int fd) | |
71 | { | |
72 | return fcntl(fd, F_SETFL, O_NONBLOCK); | |
73 | } | |
74 | ||
75 | static int write_to_pipe(int fd, const void *buf, size_t len) | |
76 | { | |
77 | return write(fd, buf, len); | |
78 | } | |
79 | ||
80 | static int read_from_pipe(int fd, void *buf, size_t len) | |
81 | { | |
82 | return read(fd, buf, len); | |
83 | } | |
84 | #endif | |
85 | ||
2575407f BVA |
86 | static void block_signals(void) |
87 | { | |
88 | #ifdef HAVE_PTHREAD_SIGMASK | |
89 | sigset_t sigmask; | |
90 | ||
91 | ret = pthread_sigmask(SIG_UNBLOCK, NULL, &sigmask); | |
92 | assert(ret == 0); | |
93 | ret = pthread_sigmask(SIG_BLOCK, &sigmask, NULL); | |
94 | assert(ret == 0); | |
95 | #endif | |
96 | } | |
97 | ||
52a552e2 | 98 | static void submit_action(enum action a) |
a39fb9ea | 99 | { |
52a552e2 BVA |
100 | const char data = a; |
101 | int ret; | |
102 | ||
dda11987 JA |
103 | if (!helper_data) |
104 | return; | |
105 | ||
52a552e2 BVA |
106 | ret = write_to_pipe(helper_data->pipe[1], &data, sizeof(data)); |
107 | assert(ret == 1); | |
108 | } | |
109 | ||
110 | void helper_reset(void) | |
111 | { | |
112 | submit_action(A_RESET); | |
113 | } | |
114 | ||
115 | /* | |
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. | |
119 | */ | |
120 | void helper_do_stat(void) | |
121 | { | |
122 | submit_action(A_DO_STAT); | |
a39fb9ea JA |
123 | } |
124 | ||
125 | bool helper_should_exit(void) | |
126 | { | |
127 | if (!helper_data) | |
128 | return true; | |
129 | ||
130 | return helper_data->exit; | |
131 | } | |
132 | ||
133 | void helper_thread_exit(void) | |
134 | { | |
998e9ebb BVA |
135 | if (!helper_data) |
136 | return; | |
137 | ||
a39fb9ea | 138 | helper_data->exit = 1; |
52a552e2 BVA |
139 | submit_action(A_EXIT); |
140 | pthread_join(helper_data->thread, NULL); | |
a39fb9ea JA |
141 | } |
142 | ||
335210df BVA |
143 | /* |
144 | * Waits for an action from fd during at least timeout_ms. `fd` must be in | |
145 | * non-blocking mode. | |
146 | */ | |
147 | static uint8_t wait_for_action(int fd, unsigned int timeout_ms) | |
148 | { | |
149 | struct timeval timeout = { | |
150 | .tv_sec = timeout_ms / 1000, | |
151 | .tv_usec = (timeout_ms % 1000) * 1000, | |
152 | }; | |
153 | fd_set rfds, efds; | |
154 | uint8_t action = 0; | |
155 | int res; | |
156 | ||
157 | res = read_from_pipe(fd, &action, sizeof(action)); | |
158 | if (res > 0 || timeout_ms == 0) | |
159 | return action; | |
160 | FD_ZERO(&rfds); | |
161 | FD_SET(fd, &rfds); | |
162 | FD_ZERO(&efds); | |
163 | FD_SET(fd, &efds); | |
164 | res = select(fd + 1, &rfds, NULL, &efds, &timeout); | |
165 | if (res < 0) { | |
166 | log_err("fio: select() call in helper thread failed: %s", | |
167 | strerror(errno)); | |
168 | return A_EXIT; | |
169 | } | |
170 | if (FD_ISSET(fd, &rfds)) | |
171 | read_from_pipe(fd, &action, sizeof(action)); | |
172 | return action; | |
173 | } | |
174 | ||
3da71e37 VF |
175 | static unsigned int task_helper(struct timespec *last, struct timespec *now, unsigned int period, void do_task()) |
176 | { | |
177 | unsigned int next, since; | |
178 | ||
179 | since = mtime_since(last, now); | |
180 | if (since >= period || period - since < 10) { | |
181 | do_task(); | |
182 | timespec_add_msec(last, since); | |
183 | if (since > period) | |
184 | next = period - (since - period); | |
185 | else | |
186 | next = period; | |
187 | } else | |
188 | next = period - since; | |
189 | ||
190 | return next; | |
191 | } | |
192 | ||
a39fb9ea JA |
193 | static void *helper_thread_main(void *data) |
194 | { | |
195 | struct helper_data *hd = data; | |
3da71e37 | 196 | unsigned int msec_to_next_event, next_log, next_si = status_interval; |
0f77d30d VF |
197 | unsigned int next_ss = STEADYSTATE_MSEC; |
198 | struct timespec ts, last_du, last_ss, last_si; | |
a39fb9ea JA |
199 | int ret = 0; |
200 | ||
201 | sk_out_assign(hd->sk_out); | |
202 | ||
c31092b8 | 203 | /* Let another thread handle signals. */ |
2575407f | 204 | block_signals(); |
c31092b8 | 205 | |
69212fc4 | 206 | fio_get_mono_time(&ts); |
8b6a404c VF |
207 | memcpy(&last_du, &ts, sizeof(ts)); |
208 | memcpy(&last_ss, &ts, sizeof(ts)); | |
0f77d30d | 209 | memcpy(&last_si, &ts, sizeof(ts)); |
a39fb9ea | 210 | |
971caeb1 | 211 | fio_sem_up(hd->startup_sem); |
a39fb9ea JA |
212 | |
213 | msec_to_next_event = DISK_UTIL_MSEC; | |
214 | while (!ret && !hd->exit) { | |
3da71e37 | 215 | uint64_t since_du; |
335210df BVA |
216 | uint8_t action; |
217 | ||
218 | action = wait_for_action(hd->pipe[0], msec_to_next_event); | |
219 | if (action == A_EXIT) | |
220 | break; | |
a39fb9ea | 221 | |
69212fc4 | 222 | fio_get_mono_time(&ts); |
a39fb9ea | 223 | |
52a552e2 BVA |
224 | if (action == A_RESET) { |
225 | last_du = ts; | |
226 | last_ss = ts; | |
a39fb9ea JA |
227 | } |
228 | ||
8b6a404c | 229 | since_du = mtime_since(&last_du, &ts); |
a39fb9ea JA |
230 | if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) { |
231 | ret = update_io_ticks(); | |
8b6a404c | 232 | timespec_add_msec(&last_du, DISK_UTIL_MSEC); |
a39fb9ea JA |
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); | |
dd290fb4 VF |
236 | } else |
237 | msec_to_next_event = DISK_UTIL_MSEC - since_du; | |
a39fb9ea | 238 | |
52a552e2 | 239 | if (action == A_DO_STAT) |
a39fb9ea | 240 | __show_running_run_stats(); |
a39fb9ea | 241 | |
0f77d30d | 242 | if (status_interval) { |
3da71e37 | 243 | next_si = task_helper(&last_si, &ts, status_interval, __show_running_run_stats); |
0f77d30d VF |
244 | msec_to_next_event = min(next_si, msec_to_next_event); |
245 | } | |
246 | ||
a39fb9ea JA |
247 | next_log = calc_log_samples(); |
248 | if (!next_log) | |
249 | next_log = DISK_UTIL_MSEC; | |
250 | ||
84784e07 | 251 | if (steadystate_enabled) { |
3da71e37 VF |
252 | next_ss = task_helper(&last_ss, &ts, STEADYSTATE_MSEC, steadystate_check); |
253 | msec_to_next_event = min(next_ss, msec_to_next_event); | |
16e56d25 VF |
254 | } |
255 | ||
3da71e37 VF |
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); | |
a39fb9ea JA |
259 | |
260 | if (!is_backend) | |
261 | print_thread_status(); | |
262 | } | |
263 | ||
264 | fio_writeout_logs(false); | |
265 | ||
266 | sk_out_drop(); | |
267 | return NULL; | |
268 | } | |
269 | ||
52a552e2 BVA |
270 | /* |
271 | * Connect two sockets to each other to emulate the pipe() system call on Windows. | |
272 | */ | |
273 | int pipe_over_loopback(int fd[2]) | |
274 | { | |
275 | struct sockaddr_in addr = { .sin_family = AF_INET }; | |
276 | socklen_t len = sizeof(addr); | |
277 | int res; | |
278 | ||
279 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
280 | ||
281 | sock_init(); | |
282 | ||
283 | fd[0] = socket(AF_INET, SOCK_STREAM, 0); | |
284 | if (fd[0] < 0) | |
285 | goto err; | |
286 | fd[1] = socket(AF_INET, SOCK_STREAM, 0); | |
287 | if (fd[1] < 0) | |
288 | goto close_fd_0; | |
289 | res = bind(fd[0], (struct sockaddr *)&addr, len); | |
290 | if (res < 0) | |
291 | goto close_fd_1; | |
292 | res = getsockname(fd[0], (struct sockaddr *)&addr, &len); | |
293 | if (res < 0) | |
294 | goto close_fd_1; | |
295 | res = listen(fd[0], 1); | |
296 | if (res < 0) | |
297 | goto close_fd_1; | |
298 | res = connect(fd[1], (struct sockaddr *)&addr, len); | |
299 | if (res < 0) | |
300 | goto close_fd_1; | |
301 | res = accept(fd[0], NULL, NULL); | |
302 | if (res < 0) | |
303 | goto close_fd_1; | |
304 | close(fd[0]); | |
305 | fd[0] = res; | |
306 | return 0; | |
307 | ||
308 | close_fd_1: | |
309 | close(fd[1]); | |
310 | ||
311 | close_fd_0: | |
312 | close(fd[0]); | |
313 | ||
314 | err: | |
315 | return -1; | |
316 | } | |
317 | ||
971caeb1 | 318 | int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out) |
a39fb9ea JA |
319 | { |
320 | struct helper_data *hd; | |
321 | int ret; | |
322 | ||
b3090ff4 | 323 | hd = scalloc(1, sizeof(*hd)); |
a39fb9ea JA |
324 | |
325 | setup_disk_util(); | |
16e56d25 | 326 | steadystate_setup(); |
a39fb9ea JA |
327 | |
328 | hd->sk_out = sk_out; | |
34febb23 | 329 | |
52a552e2 BVA |
330 | #if defined(CONFIG_PIPE2) |
331 | ret = pipe2(hd->pipe, O_CLOEXEC); | |
332 | #elif defined(CONFIG_PIPE) | |
333 | ret = pipe(hd->pipe); | |
334 | #else | |
335 | ret = pipe_over_loopback(hd->pipe); | |
336 | #endif | |
34febb23 | 337 | if (ret) |
f9e5b5ee | 338 | return 1; |
34febb23 | 339 | |
52a552e2 BVA |
340 | ret = make_nonblocking(hd->pipe[0]); |
341 | assert(ret >= 0); | |
342 | ||
971caeb1 | 343 | hd->startup_sem = startup_sem; |
a39fb9ea | 344 | |
4f37732a BVA |
345 | DRD_IGNORE_VAR(helper_data); |
346 | ||
a39fb9ea JA |
347 | ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd); |
348 | if (ret) { | |
349 | log_err("Can't create helper thread: %s\n", strerror(ret)); | |
350 | return 1; | |
351 | } | |
352 | ||
353 | helper_data = hd; | |
354 | ||
971caeb1 BVA |
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"); | |
a39fb9ea JA |
358 | return 0; |
359 | } |