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)
29 close(helper_data->pipe[0]);
30 close(helper_data->pipe[1]);
34 static void submit_action(enum action a)
36 const uint8_t data = a;
42 ret = write(helper_data->pipe[1], &data, sizeof(data));
46 void helper_reset(void)
48 submit_action(A_RESET);
52 * May be invoked in signal handler context and hence must only call functions
53 * that are async-signal-safe. See also
54 * https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03.
56 void helper_do_stat(void)
58 submit_action(A_DO_STAT);
61 bool helper_should_exit(void)
66 return helper_data->exit;
69 void helper_thread_exit(void)
71 helper_data->exit = 1;
72 submit_action(A_EXIT);
73 pthread_join(helper_data->thread, NULL);
76 static void *helper_thread_main(void *data)
78 struct helper_data *hd = data;
79 unsigned int msec_to_next_event, next_log, next_ss = STEADYSTATE_MSEC;
80 struct timespec ts, last_du, last_ss;
84 sk_out_assign(hd->sk_out);
86 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
87 clock_gettime(CLOCK_MONOTONIC, &ts);
89 clock_gettime(CLOCK_REALTIME, &ts);
91 memcpy(&last_du, &ts, sizeof(ts));
92 memcpy(&last_ss, &ts, sizeof(ts));
94 fio_sem_up(hd->startup_sem);
96 msec_to_next_event = DISK_UTIL_MSEC;
97 while (!ret && !hd->exit) {
98 uint64_t since_du, since_ss = 0;
99 struct timeval timeout = {
100 .tv_sec = DISK_UTIL_MSEC / 1000,
101 .tv_usec = (DISK_UTIL_MSEC % 1000) * 1000,
105 timespec_add_msec(&ts, msec_to_next_event);
107 if (read(hd->pipe[0], &action, sizeof(action)) < 0) {
109 FD_SET(hd->pipe[0], &rfds);
111 FD_SET(hd->pipe[0], &efds);
112 select(1, &rfds, NULL, &efds, &timeout);
113 if (read(hd->pipe[0], &action, sizeof(action)) < 0)
117 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
118 clock_gettime(CLOCK_MONOTONIC, &ts);
120 clock_gettime(CLOCK_REALTIME, &ts);
123 if (action == A_RESET) {
128 since_du = mtime_since(&last_du, &ts);
129 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
130 ret = update_io_ticks();
131 timespec_add_msec(&last_du, DISK_UTIL_MSEC);
132 msec_to_next_event = DISK_UTIL_MSEC;
133 if (since_du >= DISK_UTIL_MSEC)
134 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
136 msec_to_next_event = DISK_UTIL_MSEC - since_du;
138 if (action == A_DO_STAT)
139 __show_running_run_stats();
141 next_log = calc_log_samples();
143 next_log = DISK_UTIL_MSEC;
145 if (steadystate_enabled) {
146 since_ss = mtime_since(&last_ss, &ts);
147 if (since_ss >= STEADYSTATE_MSEC || STEADYSTATE_MSEC - since_ss < 10) {
149 timespec_add_msec(&last_ss, since_ss);
150 if (since_ss > STEADYSTATE_MSEC)
151 next_ss = STEADYSTATE_MSEC - (since_ss - STEADYSTATE_MSEC);
153 next_ss = STEADYSTATE_MSEC;
155 next_ss = STEADYSTATE_MSEC - since_ss;
158 msec_to_next_event = min(min(next_log, msec_to_next_event), next_ss);
159 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);
162 print_thread_status();
165 fio_writeout_logs(false);
171 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
173 struct helper_data *hd;
176 hd = scalloc(1, sizeof(*hd));
184 ret = pipe2(hd->pipe, O_CLOEXEC);
186 ret = pipe(hd->pipe);
191 ret = fcntl(hd->pipe[0], F_SETFL, O_NONBLOCK);
194 hd->startup_sem = startup_sem;
196 DRD_IGNORE_VAR(helper_data);
198 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
200 log_err("Can't create helper thread: %s\n", strerror(ret));
206 dprint(FD_MUTEX, "wait on startup_sem\n");
207 fio_sem_down(startup_sem);
208 dprint(FD_MUTEX, "done waiting on startup_sem\n");