| 1 | #include <signal.h> |
| 2 | #include <unistd.h> |
| 3 | #ifdef CONFIG_HAVE_TIMERFD_CREATE |
| 4 | #include <sys/timerfd.h> |
| 5 | #endif |
| 6 | #ifdef CONFIG_VALGRIND_DEV |
| 7 | #include <valgrind/drd.h> |
| 8 | #else |
| 9 | #define DRD_IGNORE_VAR(x) do { } while (0) |
| 10 | #endif |
| 11 | |
| 12 | #include "fio.h" |
| 13 | #include "smalloc.h" |
| 14 | #include "helper_thread.h" |
| 15 | #include "steadystate.h" |
| 16 | #include "pshared.h" |
| 17 | |
| 18 | static int sleep_accuracy_ms; |
| 19 | static int timerfd = -1; |
| 20 | |
| 21 | enum action { |
| 22 | A_EXIT = 1, |
| 23 | A_RESET = 2, |
| 24 | A_DO_STAT = 3, |
| 25 | }; |
| 26 | |
| 27 | static struct helper_data { |
| 28 | volatile int exit; |
| 29 | int pipe[2]; /* 0: read end; 1: write end. */ |
| 30 | struct sk_out *sk_out; |
| 31 | pthread_t thread; |
| 32 | struct fio_sem *startup_sem; |
| 33 | } *helper_data; |
| 34 | |
| 35 | struct interval_timer { |
| 36 | const char *name; |
| 37 | struct timespec expires; |
| 38 | uint32_t interval_ms; |
| 39 | int (*func)(void); |
| 40 | }; |
| 41 | |
| 42 | void helper_thread_destroy(void) |
| 43 | { |
| 44 | if (!helper_data) |
| 45 | return; |
| 46 | |
| 47 | close(helper_data->pipe[0]); |
| 48 | close(helper_data->pipe[1]); |
| 49 | sfree(helper_data); |
| 50 | } |
| 51 | |
| 52 | #ifdef _WIN32 |
| 53 | static void sock_init(void) |
| 54 | { |
| 55 | WSADATA wsaData; |
| 56 | int res; |
| 57 | |
| 58 | /* It is allowed to call WSAStartup() more than once. */ |
| 59 | res = WSAStartup(MAKEWORD(2, 2), &wsaData); |
| 60 | assert(res == 0); |
| 61 | } |
| 62 | |
| 63 | static int make_nonblocking(int fd) |
| 64 | { |
| 65 | unsigned long arg = 1; |
| 66 | |
| 67 | return ioctlsocket(fd, FIONBIO, &arg); |
| 68 | } |
| 69 | |
| 70 | static int write_to_pipe(int fd, const void *buf, size_t len) |
| 71 | { |
| 72 | return send(fd, buf, len, 0); |
| 73 | } |
| 74 | |
| 75 | static int read_from_pipe(int fd, void *buf, size_t len) |
| 76 | { |
| 77 | return recv(fd, buf, len, 0); |
| 78 | } |
| 79 | #else |
| 80 | static void sock_init(void) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | static int make_nonblocking(int fd) |
| 85 | { |
| 86 | return fcntl(fd, F_SETFL, O_NONBLOCK); |
| 87 | } |
| 88 | |
| 89 | static int write_to_pipe(int fd, const void *buf, size_t len) |
| 90 | { |
| 91 | return write(fd, buf, len); |
| 92 | } |
| 93 | |
| 94 | static int read_from_pipe(int fd, void *buf, size_t len) |
| 95 | { |
| 96 | return read(fd, buf, len); |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | static void block_signals(void) |
| 101 | { |
| 102 | #ifdef HAVE_PTHREAD_SIGMASK |
| 103 | sigset_t sigmask; |
| 104 | |
| 105 | ret = pthread_sigmask(SIG_UNBLOCK, NULL, &sigmask); |
| 106 | assert(ret == 0); |
| 107 | ret = pthread_sigmask(SIG_BLOCK, &sigmask, NULL); |
| 108 | assert(ret == 0); |
| 109 | #endif |
| 110 | } |
| 111 | |
| 112 | static void submit_action(enum action a) |
| 113 | { |
| 114 | const char data = a; |
| 115 | int ret; |
| 116 | |
| 117 | if (!helper_data) |
| 118 | return; |
| 119 | |
| 120 | ret = write_to_pipe(helper_data->pipe[1], &data, sizeof(data)); |
| 121 | assert(ret == 1); |
| 122 | } |
| 123 | |
| 124 | void helper_reset(void) |
| 125 | { |
| 126 | submit_action(A_RESET); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * May be invoked in signal handler context and hence must only call functions |
| 131 | * that are async-signal-safe. See also |
| 132 | * https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03. |
| 133 | */ |
| 134 | void helper_do_stat(void) |
| 135 | { |
| 136 | submit_action(A_DO_STAT); |
| 137 | } |
| 138 | |
| 139 | bool helper_should_exit(void) |
| 140 | { |
| 141 | if (!helper_data) |
| 142 | return true; |
| 143 | |
| 144 | return helper_data->exit; |
| 145 | } |
| 146 | |
| 147 | void helper_thread_exit(void) |
| 148 | { |
| 149 | if (!helper_data) |
| 150 | return; |
| 151 | |
| 152 | helper_data->exit = 1; |
| 153 | submit_action(A_EXIT); |
| 154 | pthread_join(helper_data->thread, NULL); |
| 155 | } |
| 156 | |
| 157 | /* Resets timers and returns the time in milliseconds until the next event. */ |
| 158 | static int reset_timers(struct interval_timer timer[], int num_timers, |
| 159 | struct timespec *now) |
| 160 | { |
| 161 | uint32_t msec_to_next_event = INT_MAX; |
| 162 | int i; |
| 163 | |
| 164 | for (i = 0; i < num_timers; ++i) { |
| 165 | timer[i].expires = *now; |
| 166 | timespec_add_msec(&timer[i].expires, timer[i].interval_ms); |
| 167 | msec_to_next_event = min_not_zero(msec_to_next_event, |
| 168 | timer[i].interval_ms); |
| 169 | } |
| 170 | |
| 171 | return msec_to_next_event; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Waits for an action from fd during at least timeout_ms. `fd` must be in |
| 176 | * non-blocking mode. |
| 177 | */ |
| 178 | static uint8_t wait_for_action(int fd, unsigned int timeout_ms) |
| 179 | { |
| 180 | struct timeval timeout = { |
| 181 | .tv_sec = timeout_ms / 1000, |
| 182 | .tv_usec = (timeout_ms % 1000) * 1000, |
| 183 | }; |
| 184 | fd_set rfds, efds; |
| 185 | uint8_t action = 0; |
| 186 | uint64_t exp; |
| 187 | int res; |
| 188 | |
| 189 | res = read_from_pipe(fd, &action, sizeof(action)); |
| 190 | if (res > 0 || timeout_ms == 0) |
| 191 | return action; |
| 192 | FD_ZERO(&rfds); |
| 193 | FD_SET(fd, &rfds); |
| 194 | FD_ZERO(&efds); |
| 195 | FD_SET(fd, &efds); |
| 196 | #ifdef CONFIG_HAVE_TIMERFD_CREATE |
| 197 | { |
| 198 | /* |
| 199 | * If the timer frequency is 100 Hz, select() will round up |
| 200 | * `timeout` to the next multiple of 1 / 100 Hz = 10 ms. Hence |
| 201 | * use a high-resolution timer if possible to increase |
| 202 | * select() timeout accuracy. |
| 203 | */ |
| 204 | struct itimerspec delta = {}; |
| 205 | |
| 206 | delta.it_value.tv_sec = timeout.tv_sec; |
| 207 | delta.it_value.tv_nsec = timeout.tv_usec * 1000; |
| 208 | res = timerfd_settime(timerfd, 0, &delta, NULL); |
| 209 | assert(res == 0); |
| 210 | FD_SET(timerfd, &rfds); |
| 211 | } |
| 212 | #endif |
| 213 | res = select(max(fd, timerfd) + 1, &rfds, NULL, &efds, |
| 214 | timerfd >= 0 ? NULL : &timeout); |
| 215 | if (res < 0) { |
| 216 | log_err("fio: select() call in helper thread failed: %s", |
| 217 | strerror(errno)); |
| 218 | return A_EXIT; |
| 219 | } |
| 220 | if (FD_ISSET(fd, &rfds)) |
| 221 | read_from_pipe(fd, &action, sizeof(action)); |
| 222 | if (timerfd >= 0 && FD_ISSET(timerfd, &rfds)) { |
| 223 | res = read(timerfd, &exp, sizeof(exp)); |
| 224 | assert(res == sizeof(exp)); |
| 225 | } |
| 226 | return action; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Verify whether or not timer @it has expired. If timer @it has expired, call |
| 231 | * @it->func(). @now is the current time. @msec_to_next_event is an |
| 232 | * input/output parameter that represents the time until the next event. |
| 233 | */ |
| 234 | static int eval_timer(struct interval_timer *it, const struct timespec *now, |
| 235 | unsigned int *msec_to_next_event) |
| 236 | { |
| 237 | int64_t delta_ms; |
| 238 | bool expired; |
| 239 | |
| 240 | /* interval == 0 means that the timer is disabled. */ |
| 241 | if (it->interval_ms == 0) |
| 242 | return 0; |
| 243 | |
| 244 | delta_ms = rel_time_since(now, &it->expires); |
| 245 | expired = delta_ms <= sleep_accuracy_ms; |
| 246 | if (expired) { |
| 247 | timespec_add_msec(&it->expires, it->interval_ms); |
| 248 | delta_ms = rel_time_since(now, &it->expires); |
| 249 | if (delta_ms < it->interval_ms - sleep_accuracy_ms || |
| 250 | delta_ms > it->interval_ms + sleep_accuracy_ms) { |
| 251 | dprint(FD_HELPERTHREAD, |
| 252 | "%s: delta = %" PRIi64 " <> %u. Clock jump?\n", |
| 253 | it->name, delta_ms, it->interval_ms); |
| 254 | delta_ms = it->interval_ms; |
| 255 | it->expires = *now; |
| 256 | timespec_add_msec(&it->expires, it->interval_ms); |
| 257 | } |
| 258 | } |
| 259 | *msec_to_next_event = min((unsigned int)delta_ms, *msec_to_next_event); |
| 260 | return expired ? it->func() : 0; |
| 261 | } |
| 262 | |
| 263 | static void *helper_thread_main(void *data) |
| 264 | { |
| 265 | struct helper_data *hd = data; |
| 266 | unsigned int msec_to_next_event, next_log; |
| 267 | struct interval_timer timer[] = { |
| 268 | { |
| 269 | .name = "disk_util", |
| 270 | .interval_ms = DISK_UTIL_MSEC, |
| 271 | .func = update_io_ticks, |
| 272 | }, |
| 273 | { |
| 274 | .name = "status_interval", |
| 275 | .interval_ms = status_interval, |
| 276 | .func = __show_running_run_stats, |
| 277 | }, |
| 278 | { |
| 279 | .name = "steadystate", |
| 280 | .interval_ms = steadystate_enabled ? STEADYSTATE_MSEC : |
| 281 | 0, |
| 282 | .func = steadystate_check, |
| 283 | } |
| 284 | }; |
| 285 | struct timespec ts; |
| 286 | int clk_tck, ret = 0; |
| 287 | |
| 288 | #ifdef _SC_CLK_TCK |
| 289 | clk_tck = sysconf(_SC_CLK_TCK); |
| 290 | #else |
| 291 | /* |
| 292 | * The timer frequence is variable on Windows. Instead of trying to |
| 293 | * query it, use 64 Hz, the clock frequency lower bound. See also |
| 294 | * https://carpediemsystems.co.uk/2019/07/18/windows-system-timer-granularity/. |
| 295 | */ |
| 296 | clk_tck = 64; |
| 297 | #endif |
| 298 | dprint(FD_HELPERTHREAD, "clk_tck = %d\n", clk_tck); |
| 299 | assert(clk_tck > 0); |
| 300 | sleep_accuracy_ms = (1000 + clk_tck - 1) / clk_tck; |
| 301 | |
| 302 | #ifdef CONFIG_HAVE_TIMERFD_CREATE |
| 303 | timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); |
| 304 | assert(timerfd >= 0); |
| 305 | sleep_accuracy_ms = 1; |
| 306 | #endif |
| 307 | |
| 308 | sk_out_assign(hd->sk_out); |
| 309 | |
| 310 | /* Let another thread handle signals. */ |
| 311 | block_signals(); |
| 312 | |
| 313 | fio_get_mono_time(&ts); |
| 314 | msec_to_next_event = reset_timers(timer, ARRAY_SIZE(timer), &ts); |
| 315 | |
| 316 | fio_sem_up(hd->startup_sem); |
| 317 | |
| 318 | while (!ret && !hd->exit) { |
| 319 | uint8_t action; |
| 320 | int i; |
| 321 | |
| 322 | action = wait_for_action(hd->pipe[0], msec_to_next_event); |
| 323 | if (action == A_EXIT) |
| 324 | break; |
| 325 | |
| 326 | fio_get_mono_time(&ts); |
| 327 | |
| 328 | msec_to_next_event = INT_MAX; |
| 329 | |
| 330 | if (action == A_RESET) |
| 331 | msec_to_next_event = reset_timers(timer, |
| 332 | ARRAY_SIZE(timer), &ts); |
| 333 | |
| 334 | for (i = 0; i < ARRAY_SIZE(timer); ++i) |
| 335 | ret = eval_timer(&timer[i], &ts, &msec_to_next_event); |
| 336 | |
| 337 | if (action == A_DO_STAT) |
| 338 | __show_running_run_stats(); |
| 339 | |
| 340 | next_log = calc_log_samples(); |
| 341 | if (!next_log) |
| 342 | next_log = DISK_UTIL_MSEC; |
| 343 | |
| 344 | msec_to_next_event = min(next_log, msec_to_next_event); |
| 345 | dprint(FD_HELPERTHREAD, |
| 346 | "next_log: %u, msec_to_next_event: %u\n", |
| 347 | next_log, msec_to_next_event); |
| 348 | |
| 349 | if (!is_backend) |
| 350 | print_thread_status(); |
| 351 | } |
| 352 | |
| 353 | if (timerfd >= 0) { |
| 354 | close(timerfd); |
| 355 | timerfd = -1; |
| 356 | } |
| 357 | |
| 358 | fio_writeout_logs(false); |
| 359 | |
| 360 | sk_out_drop(); |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Connect two sockets to each other to emulate the pipe() system call on Windows. |
| 366 | */ |
| 367 | int pipe_over_loopback(int fd[2]) |
| 368 | { |
| 369 | struct sockaddr_in addr = { .sin_family = AF_INET }; |
| 370 | socklen_t len = sizeof(addr); |
| 371 | int res; |
| 372 | |
| 373 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 374 | |
| 375 | sock_init(); |
| 376 | |
| 377 | fd[0] = socket(AF_INET, SOCK_STREAM, 0); |
| 378 | if (fd[0] < 0) |
| 379 | goto err; |
| 380 | fd[1] = socket(AF_INET, SOCK_STREAM, 0); |
| 381 | if (fd[1] < 0) |
| 382 | goto close_fd_0; |
| 383 | res = bind(fd[0], (struct sockaddr *)&addr, len); |
| 384 | if (res < 0) |
| 385 | goto close_fd_1; |
| 386 | res = getsockname(fd[0], (struct sockaddr *)&addr, &len); |
| 387 | if (res < 0) |
| 388 | goto close_fd_1; |
| 389 | res = listen(fd[0], 1); |
| 390 | if (res < 0) |
| 391 | goto close_fd_1; |
| 392 | res = connect(fd[1], (struct sockaddr *)&addr, len); |
| 393 | if (res < 0) |
| 394 | goto close_fd_1; |
| 395 | res = accept(fd[0], NULL, NULL); |
| 396 | if (res < 0) |
| 397 | goto close_fd_1; |
| 398 | close(fd[0]); |
| 399 | fd[0] = res; |
| 400 | return 0; |
| 401 | |
| 402 | close_fd_1: |
| 403 | close(fd[1]); |
| 404 | |
| 405 | close_fd_0: |
| 406 | close(fd[0]); |
| 407 | |
| 408 | err: |
| 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out) |
| 413 | { |
| 414 | struct helper_data *hd; |
| 415 | int ret; |
| 416 | |
| 417 | hd = scalloc(1, sizeof(*hd)); |
| 418 | |
| 419 | setup_disk_util(); |
| 420 | steadystate_setup(); |
| 421 | |
| 422 | hd->sk_out = sk_out; |
| 423 | |
| 424 | #if defined(CONFIG_PIPE2) |
| 425 | ret = pipe2(hd->pipe, O_CLOEXEC); |
| 426 | #elif defined(CONFIG_PIPE) |
| 427 | ret = pipe(hd->pipe); |
| 428 | #else |
| 429 | ret = pipe_over_loopback(hd->pipe); |
| 430 | #endif |
| 431 | if (ret) |
| 432 | return 1; |
| 433 | |
| 434 | ret = make_nonblocking(hd->pipe[0]); |
| 435 | assert(ret >= 0); |
| 436 | |
| 437 | hd->startup_sem = startup_sem; |
| 438 | |
| 439 | DRD_IGNORE_VAR(helper_data); |
| 440 | |
| 441 | ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd); |
| 442 | if (ret) { |
| 443 | log_err("Can't create helper thread: %s\n", strerror(ret)); |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | helper_data = hd; |
| 448 | |
| 449 | dprint(FD_MUTEX, "wait on startup_sem\n"); |
| 450 | fio_sem_down(startup_sem); |
| 451 | dprint(FD_MUTEX, "done waiting on startup_sem\n"); |
| 452 | return 0; |
| 453 | } |