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