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 | ||
86 | static void submit_action(enum action a) | |
a39fb9ea | 87 | { |
52a552e2 BVA |
88 | const char data = a; |
89 | int ret; | |
90 | ||
dda11987 JA |
91 | if (!helper_data) |
92 | return; | |
93 | ||
52a552e2 BVA |
94 | ret = write_to_pipe(helper_data->pipe[1], &data, sizeof(data)); |
95 | assert(ret == 1); | |
96 | } | |
97 | ||
98 | void helper_reset(void) | |
99 | { | |
100 | submit_action(A_RESET); | |
101 | } | |
102 | ||
103 | /* | |
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. | |
107 | */ | |
108 | void helper_do_stat(void) | |
109 | { | |
110 | submit_action(A_DO_STAT); | |
a39fb9ea JA |
111 | } |
112 | ||
113 | bool helper_should_exit(void) | |
114 | { | |
115 | if (!helper_data) | |
116 | return true; | |
117 | ||
118 | return helper_data->exit; | |
119 | } | |
120 | ||
121 | void helper_thread_exit(void) | |
122 | { | |
998e9ebb BVA |
123 | if (!helper_data) |
124 | return; | |
125 | ||
a39fb9ea | 126 | helper_data->exit = 1; |
52a552e2 BVA |
127 | submit_action(A_EXIT); |
128 | pthread_join(helper_data->thread, NULL); | |
a39fb9ea JA |
129 | } |
130 | ||
3da71e37 VF |
131 | static unsigned int task_helper(struct timespec *last, struct timespec *now, unsigned int period, void do_task()) |
132 | { | |
133 | unsigned int next, since; | |
134 | ||
135 | since = mtime_since(last, now); | |
136 | if (since >= period || period - since < 10) { | |
137 | do_task(); | |
138 | timespec_add_msec(last, since); | |
139 | if (since > period) | |
140 | next = period - (since - period); | |
141 | else | |
142 | next = period; | |
143 | } else | |
144 | next = period - since; | |
145 | ||
146 | return next; | |
147 | } | |
148 | ||
a39fb9ea JA |
149 | static void *helper_thread_main(void *data) |
150 | { | |
151 | struct helper_data *hd = data; | |
3da71e37 | 152 | unsigned int msec_to_next_event, next_log, next_si = status_interval; |
0f77d30d VF |
153 | unsigned int next_ss = STEADYSTATE_MSEC; |
154 | struct timespec ts, last_du, last_ss, last_si; | |
52a552e2 | 155 | char action; |
a39fb9ea JA |
156 | int ret = 0; |
157 | ||
158 | sk_out_assign(hd->sk_out); | |
159 | ||
c31092b8 BVA |
160 | #ifdef HAVE_PTHREAD_SIGMASK |
161 | { | |
162 | sigset_t sigmask; | |
163 | ||
164 | /* Let another thread handle signals. */ | |
165 | ret = pthread_sigmask(SIG_UNBLOCK, NULL, &sigmask); | |
166 | assert(ret == 0); | |
167 | ret = pthread_sigmask(SIG_BLOCK, &sigmask, NULL); | |
168 | assert(ret == 0); | |
169 | } | |
170 | #endif | |
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 | |
8b6a404c VF |
177 | memcpy(&last_du, &ts, sizeof(ts)); |
178 | memcpy(&last_ss, &ts, sizeof(ts)); | |
0f77d30d | 179 | memcpy(&last_si, &ts, sizeof(ts)); |
a39fb9ea | 180 | |
971caeb1 | 181 | fio_sem_up(hd->startup_sem); |
a39fb9ea JA |
182 | |
183 | msec_to_next_event = DISK_UTIL_MSEC; | |
184 | while (!ret && !hd->exit) { | |
3da71e37 | 185 | uint64_t since_du; |
52a552e2 | 186 | struct timeval timeout = { |
0e59dd68 VF |
187 | .tv_sec = msec_to_next_event / 1000, |
188 | .tv_usec = (msec_to_next_event % 1000) * 1000, | |
52a552e2 BVA |
189 | }; |
190 | fd_set rfds, efds; | |
a39fb9ea | 191 | |
52a552e2 BVA |
192 | if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < 0) { |
193 | FD_ZERO(&rfds); | |
194 | FD_SET(hd->pipe[0], &rfds); | |
195 | FD_ZERO(&efds); | |
196 | FD_SET(hd->pipe[0], &efds); | |
8c55e40e | 197 | if (select(1, &rfds, NULL, &efds, &timeout) < 0) { |
700ad386 BVA |
198 | log_err("fio: select() call in helper thread failed: %s", |
199 | strerror(errno)); | |
8c55e40e VF |
200 | ret = 1; |
201 | } | |
52a552e2 BVA |
202 | if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < |
203 | 0) | |
204 | action = 0; | |
205 | } | |
a39fb9ea | 206 | |
78b66d32 BVA |
207 | #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK |
208 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
209 | #else | |
210 | clock_gettime(CLOCK_REALTIME, &ts); | |
211 | #endif | |
a39fb9ea | 212 | |
52a552e2 BVA |
213 | if (action == A_RESET) { |
214 | last_du = ts; | |
215 | last_ss = ts; | |
a39fb9ea JA |
216 | } |
217 | ||
8b6a404c | 218 | since_du = mtime_since(&last_du, &ts); |
a39fb9ea JA |
219 | if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) { |
220 | ret = update_io_ticks(); | |
8b6a404c | 221 | timespec_add_msec(&last_du, DISK_UTIL_MSEC); |
a39fb9ea JA |
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); | |
dd290fb4 VF |
225 | } else |
226 | msec_to_next_event = DISK_UTIL_MSEC - since_du; | |
a39fb9ea | 227 | |
52a552e2 | 228 | if (action == A_DO_STAT) |
a39fb9ea | 229 | __show_running_run_stats(); |
a39fb9ea | 230 | |
0f77d30d | 231 | if (status_interval) { |
3da71e37 | 232 | next_si = task_helper(&last_si, &ts, status_interval, __show_running_run_stats); |
0f77d30d VF |
233 | msec_to_next_event = min(next_si, msec_to_next_event); |
234 | } | |
235 | ||
a39fb9ea JA |
236 | next_log = calc_log_samples(); |
237 | if (!next_log) | |
238 | next_log = DISK_UTIL_MSEC; | |
239 | ||
84784e07 | 240 | if (steadystate_enabled) { |
3da71e37 VF |
241 | next_ss = task_helper(&last_ss, &ts, STEADYSTATE_MSEC, steadystate_check); |
242 | msec_to_next_event = min(next_ss, msec_to_next_event); | |
16e56d25 VF |
243 | } |
244 | ||
3da71e37 VF |
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); | |
a39fb9ea JA |
248 | |
249 | if (!is_backend) | |
250 | print_thread_status(); | |
251 | } | |
252 | ||
253 | fio_writeout_logs(false); | |
254 | ||
255 | sk_out_drop(); | |
256 | return NULL; | |
257 | } | |
258 | ||
52a552e2 BVA |
259 | /* |
260 | * Connect two sockets to each other to emulate the pipe() system call on Windows. | |
261 | */ | |
262 | int pipe_over_loopback(int fd[2]) | |
263 | { | |
264 | struct sockaddr_in addr = { .sin_family = AF_INET }; | |
265 | socklen_t len = sizeof(addr); | |
266 | int res; | |
267 | ||
268 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
269 | ||
270 | sock_init(); | |
271 | ||
272 | fd[0] = socket(AF_INET, SOCK_STREAM, 0); | |
273 | if (fd[0] < 0) | |
274 | goto err; | |
275 | fd[1] = socket(AF_INET, SOCK_STREAM, 0); | |
276 | if (fd[1] < 0) | |
277 | goto close_fd_0; | |
278 | res = bind(fd[0], (struct sockaddr *)&addr, len); | |
279 | if (res < 0) | |
280 | goto close_fd_1; | |
281 | res = getsockname(fd[0], (struct sockaddr *)&addr, &len); | |
282 | if (res < 0) | |
283 | goto close_fd_1; | |
284 | res = listen(fd[0], 1); | |
285 | if (res < 0) | |
286 | goto close_fd_1; | |
287 | res = connect(fd[1], (struct sockaddr *)&addr, len); | |
288 | if (res < 0) | |
289 | goto close_fd_1; | |
290 | res = accept(fd[0], NULL, NULL); | |
291 | if (res < 0) | |
292 | goto close_fd_1; | |
293 | close(fd[0]); | |
294 | fd[0] = res; | |
295 | return 0; | |
296 | ||
297 | close_fd_1: | |
298 | close(fd[1]); | |
299 | ||
300 | close_fd_0: | |
301 | close(fd[0]); | |
302 | ||
303 | err: | |
304 | return -1; | |
305 | } | |
306 | ||
971caeb1 | 307 | int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out) |
a39fb9ea JA |
308 | { |
309 | struct helper_data *hd; | |
310 | int ret; | |
311 | ||
b3090ff4 | 312 | hd = scalloc(1, sizeof(*hd)); |
a39fb9ea JA |
313 | |
314 | setup_disk_util(); | |
16e56d25 | 315 | steadystate_setup(); |
a39fb9ea JA |
316 | |
317 | hd->sk_out = sk_out; | |
34febb23 | 318 | |
52a552e2 BVA |
319 | #if defined(CONFIG_PIPE2) |
320 | ret = pipe2(hd->pipe, O_CLOEXEC); | |
321 | #elif defined(CONFIG_PIPE) | |
322 | ret = pipe(hd->pipe); | |
323 | #else | |
324 | ret = pipe_over_loopback(hd->pipe); | |
325 | #endif | |
34febb23 | 326 | if (ret) |
f9e5b5ee | 327 | return 1; |
34febb23 | 328 | |
52a552e2 BVA |
329 | ret = make_nonblocking(hd->pipe[0]); |
330 | assert(ret >= 0); | |
331 | ||
971caeb1 | 332 | hd->startup_sem = startup_sem; |
a39fb9ea | 333 | |
4f37732a BVA |
334 | DRD_IGNORE_VAR(helper_data); |
335 | ||
a39fb9ea JA |
336 | ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd); |
337 | if (ret) { | |
338 | log_err("Can't create helper thread: %s\n", strerror(ret)); | |
339 | return 1; | |
340 | } | |
341 | ||
342 | helper_data = hd; | |
343 | ||
971caeb1 BVA |
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"); | |
a39fb9ea JA |
347 | return 0; |
348 | } |