Handle helper_thread_create() failures properly
[fio.git] / helper_thread.c
CommitLineData
4f37732a
BVA
1#ifdef CONFIG_VALGRIND_DEV
2#include <valgrind/drd.h>
3#else
4#define DRD_IGNORE_VAR(x) do { } while (0)
5#endif
6
a39fb9ea
JA
7#include "fio.h"
8#include "smalloc.h"
9#include "helper_thread.h"
16e56d25 10#include "steadystate.h"
ae626d4e 11#include "pshared.h"
a39fb9ea
JA
12
13static struct helper_data {
14 volatile int exit;
dda11987
JA
15 volatile int reset;
16 volatile int do_stat;
a39fb9ea
JA
17 struct sk_out *sk_out;
18 pthread_t thread;
dda11987
JA
19 pthread_mutex_t lock;
20 pthread_cond_t cond;
971caeb1 21 struct fio_sem *startup_sem;
a39fb9ea
JA
22} *helper_data;
23
24void helper_thread_destroy(void)
25{
998e9ebb
BVA
26 if (!helper_data)
27 return;
28
dda11987
JA
29 pthread_cond_destroy(&helper_data->cond);
30 pthread_mutex_destroy(&helper_data->lock);
a39fb9ea
JA
31 sfree(helper_data);
32}
33
dda11987 34void helper_reset(void)
a39fb9ea
JA
35{
36 if (!helper_data)
37 return;
38
dda11987 39 pthread_mutex_lock(&helper_data->lock);
a39fb9ea 40
dda11987
JA
41 if (!helper_data->reset) {
42 helper_data->reset = 1;
43 pthread_cond_signal(&helper_data->cond);
44 }
45
46 pthread_mutex_unlock(&helper_data->lock);
a39fb9ea
JA
47}
48
49void helper_do_stat(void)
50{
dda11987
JA
51 if (!helper_data)
52 return;
53
54 pthread_mutex_lock(&helper_data->lock);
55 helper_data->do_stat = 1;
56 pthread_cond_signal(&helper_data->cond);
57 pthread_mutex_unlock(&helper_data->lock);
a39fb9ea
JA
58}
59
60bool helper_should_exit(void)
61{
62 if (!helper_data)
63 return true;
64
65 return helper_data->exit;
66}
67
68void helper_thread_exit(void)
69{
dda11987
JA
70 void *ret;
71
998e9ebb
BVA
72 if (!helper_data)
73 return;
74
dda11987 75 pthread_mutex_lock(&helper_data->lock);
a39fb9ea 76 helper_data->exit = 1;
dda11987
JA
77 pthread_cond_signal(&helper_data->cond);
78 pthread_mutex_unlock(&helper_data->lock);
79
80 pthread_join(helper_data->thread, &ret);
a39fb9ea
JA
81}
82
83static void *helper_thread_main(void *data)
84{
85 struct helper_data *hd = data;
16e56d25 86 unsigned int msec_to_next_event, next_log, next_ss = STEADYSTATE_MSEC;
8b6a404c 87 struct timespec ts, last_du, last_ss;
a39fb9ea
JA
88 int ret = 0;
89
90 sk_out_assign(hd->sk_out);
91
78b66d32
BVA
92#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
93 clock_gettime(CLOCK_MONOTONIC, &ts);
94#else
95 clock_gettime(CLOCK_REALTIME, &ts);
96#endif
8b6a404c
VF
97 memcpy(&last_du, &ts, sizeof(ts));
98 memcpy(&last_ss, &ts, sizeof(ts));
a39fb9ea 99
971caeb1 100 fio_sem_up(hd->startup_sem);
a39fb9ea
JA
101
102 msec_to_next_event = DISK_UTIL_MSEC;
103 while (!ret && !hd->exit) {
16e56d25 104 uint64_t since_du, since_ss = 0;
a39fb9ea 105
8b6a404c 106 timespec_add_msec(&ts, msec_to_next_event);
a39fb9ea 107
dda11987
JA
108 pthread_mutex_lock(&hd->lock);
109 pthread_cond_timedwait(&hd->cond, &hd->lock, &ts);
a39fb9ea 110
78b66d32
BVA
111#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
112 clock_gettime(CLOCK_MONOTONIC, &ts);
113#else
114 clock_gettime(CLOCK_REALTIME, &ts);
115#endif
a39fb9ea 116
dda11987
JA
117 if (hd->reset) {
118 memcpy(&last_du, &ts, sizeof(ts));
119 memcpy(&last_ss, &ts, sizeof(ts));
120 hd->reset = 0;
a39fb9ea
JA
121 }
122
dda11987
JA
123 pthread_mutex_unlock(&hd->lock);
124
8b6a404c 125 since_du = mtime_since(&last_du, &ts);
a39fb9ea
JA
126 if (since_du >= DISK_UTIL_MSEC || DISK_UTIL_MSEC - since_du < 10) {
127 ret = update_io_ticks();
8b6a404c 128 timespec_add_msec(&last_du, DISK_UTIL_MSEC);
a39fb9ea
JA
129 msec_to_next_event = DISK_UTIL_MSEC;
130 if (since_du >= DISK_UTIL_MSEC)
131 msec_to_next_event -= (since_du - DISK_UTIL_MSEC);
dd290fb4
VF
132 } else
133 msec_to_next_event = DISK_UTIL_MSEC - since_du;
a39fb9ea 134
dda11987
JA
135 if (hd->do_stat) {
136 hd->do_stat = 0;
a39fb9ea 137 __show_running_run_stats();
dda11987 138 }
a39fb9ea
JA
139
140 next_log = calc_log_samples();
141 if (!next_log)
142 next_log = DISK_UTIL_MSEC;
143
84784e07 144 if (steadystate_enabled) {
8b6a404c 145 since_ss = mtime_since(&last_ss, &ts);
16e56d25
VF
146 if (since_ss >= STEADYSTATE_MSEC || STEADYSTATE_MSEC - since_ss < 10) {
147 steadystate_check();
8b6a404c 148 timespec_add_msec(&last_ss, since_ss);
16e56d25
VF
149 if (since_ss > STEADYSTATE_MSEC)
150 next_ss = STEADYSTATE_MSEC - (since_ss - STEADYSTATE_MSEC);
151 else
152 next_ss = STEADYSTATE_MSEC;
c27cc65f 153 } else
16e56d25
VF
154 next_ss = STEADYSTATE_MSEC - since_ss;
155 }
156
157 msec_to_next_event = min(min(next_log, msec_to_next_event), next_ss);
e569ca6b 158 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);
a39fb9ea
JA
159
160 if (!is_backend)
161 print_thread_status();
162 }
163
164 fio_writeout_logs(false);
165
166 sk_out_drop();
167 return NULL;
168}
169
971caeb1 170int helper_thread_create(struct fio_sem *startup_sem, struct sk_out *sk_out)
a39fb9ea
JA
171{
172 struct helper_data *hd;
173 int ret;
174
b3090ff4 175 hd = scalloc(1, sizeof(*hd));
a39fb9ea
JA
176
177 setup_disk_util();
16e56d25 178 steadystate_setup();
a39fb9ea
JA
179
180 hd->sk_out = sk_out;
34febb23 181
dda11987 182 ret = mutex_cond_init_pshared(&hd->lock, &hd->cond);
34febb23 183 if (ret)
f9e5b5ee 184 return 1;
34febb23 185
971caeb1 186 hd->startup_sem = startup_sem;
a39fb9ea 187
4f37732a
BVA
188 DRD_IGNORE_VAR(helper_data);
189
a39fb9ea
JA
190 ret = pthread_create(&hd->thread, NULL, helper_thread_main, hd);
191 if (ret) {
192 log_err("Can't create helper thread: %s\n", strerror(ret));
193 return 1;
194 }
195
196 helper_data = hd;
197
971caeb1
BVA
198 dprint(FD_MUTEX, "wait on startup_sem\n");
199 fio_sem_down(startup_sem);
200 dprint(FD_MUTEX, "done waiting on startup_sem\n");
a39fb9ea
JA
201 return 0;
202}