Fix a potential deadlock in helper_do_stat()
[fio.git] / helper_thread.c
1 #ifdef CONFIG_VALGRIND_DEV
2 #include <valgrind/drd.h>
3 #else
4 #define DRD_IGNORE_VAR(x) do { } while (0)
5 #endif
6
7 #include "fio.h"
8 #include "smalloc.h"
9 #include "helper_thread.h"
10 #include "steadystate.h"
11 #include "pshared.h"
12
13 enum action {
14         A_EXIT          = 1,
15         A_RESET         = 2,
16         A_DO_STAT       = 3,
17 };
18
19 static struct helper_data {
20         volatile int exit;
21         int pipe[2]; /* 0: read end; 1: write end. */
22         struct sk_out *sk_out;
23         pthread_t thread;
24         struct fio_sem *startup_sem;
25 } *helper_data;
26
27 void helper_thread_destroy(void)
28 {
29         close(helper_data->pipe[0]);
30         close(helper_data->pipe[1]);
31         sfree(helper_data);
32 }
33
34 static void submit_action(enum action a)
35 {
36         const uint8_t data = a;
37         int ret;
38
39         if (!helper_data)
40                 return;
41
42         ret = write(helper_data->pipe[1], &data, sizeof(data));
43         assert(ret == 1);
44 }
45
46 void helper_reset(void)
47 {
48         submit_action(A_RESET);
49 }
50
51 /*
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.
55  */
56 void helper_do_stat(void)
57 {
58         submit_action(A_DO_STAT);
59 }
60
61 bool helper_should_exit(void)
62 {
63         if (!helper_data)
64                 return true;
65
66         return helper_data->exit;
67 }
68
69 void helper_thread_exit(void)
70 {
71         helper_data->exit = 1;
72         submit_action(A_EXIT);
73         pthread_join(helper_data->thread, NULL);
74 }
75
76 static void *helper_thread_main(void *data)
77 {
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;
81         uint8_t action;
82         int ret = 0;
83
84         sk_out_assign(hd->sk_out);
85
86 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
87         clock_gettime(CLOCK_MONOTONIC, &ts);
88 #else
89         clock_gettime(CLOCK_REALTIME, &ts);
90 #endif
91         memcpy(&last_du, &ts, sizeof(ts));
92         memcpy(&last_ss, &ts, sizeof(ts));
93
94         fio_sem_up(hd->startup_sem);
95
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,
102                 };
103                 fd_set rfds, efds;
104
105                 timespec_add_msec(&ts, msec_to_next_event);
106
107                 if (read(hd->pipe[0], &action, sizeof(action)) < 0) {
108                         FD_ZERO(&rfds);
109                         FD_SET(hd->pipe[0], &rfds);
110                         FD_ZERO(&efds);
111                         FD_SET(hd->pipe[0], &efds);
112                         select(1, &rfds, NULL, &efds, &timeout);
113                         if (read(hd->pipe[0], &action, sizeof(action)) < 0)
114                                 action = 0;
115                 }
116
117 #ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
118                 clock_gettime(CLOCK_MONOTONIC, &ts);
119 #else
120                 clock_gettime(CLOCK_REALTIME, &ts);
121 #endif
122
123                 if (action == A_RESET) {
124                         last_du = ts;
125                         last_ss = ts;
126                 }
127
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);
135                 } else
136                         msec_to_next_event = DISK_UTIL_MSEC - since_du;
137
138                 if (action == A_DO_STAT)
139                         __show_running_run_stats();
140
141                 next_log = calc_log_samples();
142                 if (!next_log)
143                         next_log = DISK_UTIL_MSEC;
144
145                 if (steadystate_enabled) {
146                         since_ss = mtime_since(&last_ss, &ts);
147                         if (since_ss >= STEADYSTATE_MSEC || STEADYSTATE_MSEC - since_ss < 10) {
148                                 steadystate_check();
149                                 timespec_add_msec(&last_ss, since_ss);
150                                 if (since_ss > STEADYSTATE_MSEC)
151                                         next_ss = STEADYSTATE_MSEC - (since_ss - STEADYSTATE_MSEC);
152                                 else
153                                         next_ss = STEADYSTATE_MSEC;
154                         } else
155                                 next_ss = STEADYSTATE_MSEC - since_ss;
156                 }
157
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);
160
161                 if (!is_backend)
162                         print_thread_status();
163         }
164
165         fio_writeout_logs(false);
166
167         sk_out_drop();
168         return NULL;
169 }
170
171 int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
172 {
173         struct helper_data *hd;
174         int ret;
175
176         hd = scalloc(1, sizeof(*hd));
177
178         setup_disk_util();
179         steadystate_setup();
180
181         hd->sk_out = sk_out;
182
183 #ifdef __linux__
184         ret = pipe2(hd->pipe, O_CLOEXEC);
185 #else
186         ret = pipe(hd->pipe);
187 #endif
188         if (ret)
189                 return 1;
190
191         ret = fcntl(hd->pipe[0], F_SETFL, O_NONBLOCK);
192         assert(ret >= 0);
193
194         hd->startup_sem = startup_sem;
195
196         DRD_IGNORE_VAR(helper_data);
197
198         ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
199         if (ret) {
200                 log_err("Can't create helper thread: %s\n", strerror(ret));
201                 return 1;
202         }
203
204         helper_data = hd;
205
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");
209         return 0;
210 }