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