t/run-fio-tests: improve error handling
[fio.git] / steadystate.c
CommitLineData
ba8fb6f6
VF
1#include <stdlib.h>
2
16e56d25
VF
3#include "fio.h"
4#include "steadystate.h"
16e56d25 5
84784e07 6bool steadystate_enabled = false;
4cd5b75b 7
c27cc65f
JA
8void steadystate_free(struct thread_data *td)
9{
10 free(td->ss.iops_data);
11 free(td->ss.bw_data);
12 td->ss.iops_data = NULL;
13 td->ss.bw_data = NULL;
14}
15
f0c50c66
JA
16static void steadystate_alloc(struct thread_data *td)
17{
4c14c109
TK
18 td->ss.bw_data = calloc(td->ss.dur, sizeof(uint64_t));
19 td->ss.iops_data = calloc(td->ss.dur, sizeof(uint64_t));
bb49c8bd 20
c8caba48 21 td->ss.state |= FIO_SS_DATA;
f0c50c66
JA
22}
23
4cd5b75b 24void steadystate_setup(void)
16e56d25 25{
16e56d25 26 struct thread_data *td, *prev_td;
af7fb4aa 27 int i, prev_groupid;
16e56d25 28
84784e07 29 if (!steadystate_enabled)
16e56d25
VF
30 return;
31
32 /*
33 * if group reporting is enabled, identify the last td
34 * for each group and use it for storing steady state
35 * data
36 */
37 prev_groupid = -1;
38 prev_td = NULL;
39 for_each_td(td, i) {
bb49c8bd 40 if (!td->ss.dur)
ba8fb6f6
VF
41 continue;
42
43 if (!td->o.group_reporting) {
44 steadystate_alloc(td);
16e56d25 45 continue;
ba8fb6f6 46 }
16e56d25
VF
47
48 if (prev_groupid != td->groupid) {
af7fb4aa 49 if (prev_td)
ba8fb6f6 50 steadystate_alloc(prev_td);
16e56d25
VF
51 prev_groupid = td->groupid;
52 }
53 prev_td = td;
54 }
55
af7fb4aa 56 if (prev_td && prev_td->o.group_reporting)
ba8fb6f6 57 steadystate_alloc(prev_td);
ba8fb6f6
VF
58}
59
9d193019 60static bool steadystate_slope(uint64_t iops, uint64_t bw,
4cd5b75b 61 struct thread_data *td)
16e56d25 62{
ba8fb6f6 63 int i, j;
16e56d25 64 double result;
16e56d25 65 struct steadystate_data *ss = &td->ss;
9d193019 66 uint64_t new_val;
16e56d25 67
ba8fb6f6
VF
68 ss->bw_data[ss->tail] = bw;
69 ss->iops_data[ss->tail] = iops;
16e56d25 70
c8caba48 71 if (ss->state & FIO_SS_IOPS)
7be78a8a
JA
72 new_val = iops;
73 else
74 new_val = bw;
75
c8caba48
JA
76 if (ss->state & FIO_SS_BUFFER_FULL || ss->tail - ss->head == ss->dur - 1) {
77 if (!(ss->state & FIO_SS_BUFFER_FULL)) {
17df7023
VF
78 /* first time through */
79 for(i = 0, ss->sum_y = 0; i < ss->dur; i++) {
c8caba48 80 if (ss->state & FIO_SS_IOPS)
7be78a8a
JA
81 ss->sum_y += ss->iops_data[i];
82 else
83 ss->sum_y += ss->bw_data[i];
17df7023 84 j = (ss->head + i) % ss->dur;
c8caba48 85 if (ss->state & FIO_SS_IOPS)
94f218f6 86 ss->sum_xy += i * ss->iops_data[j];
7be78a8a 87 else
94f218f6 88 ss->sum_xy += i * ss->bw_data[j];
16e56d25 89 }
c8caba48 90 ss->state |= FIO_SS_BUFFER_FULL;
16e56d25
VF
91 } else { /* easy to update the sums */
92 ss->sum_y -= ss->oldest_y;
ba8fb6f6
VF
93 ss->sum_y += new_val;
94 ss->sum_xy = ss->sum_xy - ss->sum_y + ss->dur * new_val;
16e56d25
VF
95 }
96
c8caba48 97 if (ss->state & FIO_SS_IOPS)
7be78a8a
JA
98 ss->oldest_y = ss->iops_data[ss->head];
99 else
100 ss->oldest_y = ss->bw_data[ss->head];
16e56d25
VF
101
102 /*
7be78a8a
JA
103 * calculate slope as (sum_xy - sum_x * sum_y / n) / (sum_(x^2)
104 * - (sum_x)^2 / n) This code assumes that all x values are
105 * equally spaced when they are often off by a few milliseconds.
106 * This assumption greatly simplifies the calculations.
16e56d25 107 */
7be78a8a
JA
108 ss->slope = (ss->sum_xy - (double) ss->sum_x * ss->sum_y / ss->dur) /
109 (ss->sum_x_sq - (double) ss->sum_x * ss->sum_x / ss->dur);
c8caba48 110 if (ss->state & FIO_SS_PCT)
7be78a8a
JA
111 ss->criterion = 100.0 * ss->slope / (ss->sum_y / ss->dur);
112 else
113 ss->criterion = ss->slope;
16e56d25 114
7be78a8a
JA
115 dprint(FD_STEADYSTATE, "sum_y: %llu, sum_xy: %llu, slope: %f, "
116 "criterion: %f, limit: %f\n",
9d193019
VF
117 (unsigned long long) ss->sum_y,
118 (unsigned long long) ss->sum_xy,
119 ss->slope, ss->criterion, ss->limit);
16e56d25 120
6da94b07 121 result = ss->criterion * (ss->criterion < 0.0 ? -1.0 : 1.0);
16e56d25
VF
122 if (result < ss->limit)
123 return true;
124 }
125
126 ss->tail = (ss->tail + 1) % ss->dur;
127 if (ss->tail <= ss->head)
128 ss->head = (ss->head + 1) % ss->dur;
4cd5b75b 129
16e56d25
VF
130 return false;
131}
132
9d193019 133static bool steadystate_deviation(uint64_t iops, uint64_t bw,
4cd5b75b 134 struct thread_data *td)
16e56d25
VF
135{
136 int i;
137 double diff;
138 double mean;
16e56d25
VF
139
140 struct steadystate_data *ss = &td->ss;
141
ba8fb6f6
VF
142 ss->bw_data[ss->tail] = bw;
143 ss->iops_data[ss->tail] = iops;
16e56d25 144
c8caba48
JA
145 if (ss->state & FIO_SS_BUFFER_FULL || ss->tail - ss->head == ss->dur - 1) {
146 if (!(ss->state & FIO_SS_BUFFER_FULL)) {
17df7023
VF
147 /* first time through */
148 for(i = 0, ss->sum_y = 0; i < ss->dur; i++)
c8caba48 149 if (ss->state & FIO_SS_IOPS)
7be78a8a
JA
150 ss->sum_y += ss->iops_data[i];
151 else
152 ss->sum_y += ss->bw_data[i];
c8caba48 153 ss->state |= FIO_SS_BUFFER_FULL;
16e56d25
VF
154 } else { /* easy to update the sum */
155 ss->sum_y -= ss->oldest_y;
c8caba48 156 if (ss->state & FIO_SS_IOPS)
7be78a8a
JA
157 ss->sum_y += ss->iops_data[ss->tail];
158 else
159 ss->sum_y += ss->bw_data[ss->tail];
16e56d25
VF
160 }
161
c8caba48 162 if (ss->state & FIO_SS_IOPS)
7be78a8a
JA
163 ss->oldest_y = ss->iops_data[ss->head];
164 else
165 ss->oldest_y = ss->bw_data[ss->head];
166
16e56d25 167 mean = (double) ss->sum_y / ss->dur;
ba8fb6f6 168 ss->deviation = 0.0;
16e56d25 169
4cd5b75b 170 for (i = 0; i < ss->dur; i++) {
c8caba48 171 if (ss->state & FIO_SS_IOPS)
7be78a8a
JA
172 diff = ss->iops_data[i] - mean;
173 else
174 diff = ss->bw_data[i] - mean;
6da94b07 175 ss->deviation = max(ss->deviation, diff * (diff < 0.0 ? -1.0 : 1.0));
16e56d25
VF
176 }
177
c8caba48 178 if (ss->state & FIO_SS_PCT)
7be78a8a
JA
179 ss->criterion = 100.0 * ss->deviation / mean;
180 else
181 ss->criterion = ss->deviation;
16e56d25 182
7be78a8a
JA
183 dprint(FD_STEADYSTATE, "sum_y: %llu, mean: %f, max diff: %f, "
184 "objective: %f, limit: %f\n",
9d193019
VF
185 (unsigned long long) ss->sum_y, mean,
186 ss->deviation, ss->criterion, ss->limit);
16e56d25
VF
187
188 if (ss->criterion < ss->limit)
189 return true;
190 }
191
192 ss->tail = (ss->tail + 1) % ss->dur;
193 if (ss->tail <= ss->head)
194 ss->head = (ss->head + 1) % ss->dur;
4cd5b75b 195
16e56d25
VF
196 return false;
197}
4cd5b75b
JA
198
199void steadystate_check(void)
200{
201 int i, j, ddir, prev_groupid, group_ramp_time_over = 0;
202 unsigned long rate_time;
203 struct thread_data *td, *td2;
8b6a404c 204 struct timespec now;
9d193019
VF
205 uint64_t group_bw = 0, group_iops = 0;
206 uint64_t td_iops, td_bytes;
4cd5b75b
JA
207 bool ret;
208
209 prev_groupid = -1;
210 for_each_td(td, i) {
26b3a188 211 const bool needs_lock = td_async_processing(td);
4cd5b75b
JA
212 struct steadystate_data *ss = &td->ss;
213
7be78a8a 214 if (!ss->dur || td->runstate <= TD_SETTING_UP ||
0c13c969 215 td->runstate >= TD_EXITED || !ss->state ||
c8caba48 216 ss->state & FIO_SS_ATTAINED)
4cd5b75b
JA
217 continue;
218
219 td_iops = 0;
220 td_bytes = 0;
221 if (!td->o.group_reporting ||
222 (td->o.group_reporting && td->groupid != prev_groupid)) {
223 group_bw = 0;
224 group_iops = 0;
225 group_ramp_time_over = 0;
226 }
227 prev_groupid = td->groupid;
228
229 fio_gettime(&now, NULL);
c8caba48 230 if (ss->ramp_time && !(ss->state & FIO_SS_RAMP_OVER)) {
4cd5b75b
JA
231 /*
232 * Begin recording data one second after ss->ramp_time
233 * has elapsed
234 */
235 if (utime_since(&td->epoch, &now) >= (ss->ramp_time + 1000000L))
c8caba48 236 ss->state |= FIO_SS_RAMP_OVER;
5b4b6586 237 }
4cd5b75b 238
26b3a188
JA
239 if (needs_lock)
240 __td_io_u_lock(td);
241
c1f50f76 242 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
4cd5b75b
JA
243 td_iops += td->io_blocks[ddir];
244 td_bytes += td->io_bytes[ddir];
245 }
26b3a188
JA
246
247 if (needs_lock)
248 __td_io_u_unlock(td);
4cd5b75b
JA
249
250 rate_time = mtime_since(&ss->prev_time, &now);
251 memcpy(&ss->prev_time, &now, sizeof(now));
252
253 /*
254 * Begin monitoring when job starts but don't actually use
255 * data in checking stopping criterion until ss->ramp_time is
256 * over. This ensures that we will have a sane value in
257 * prev_iops/bw the first time through after ss->ramp_time
258 * is done.
259 */
c8caba48 260 if (ss->state & FIO_SS_RAMP_OVER) {
4cd5b75b
JA
261 group_bw += 1000 * (td_bytes - ss->prev_bytes) / rate_time;
262 group_iops += 1000 * (td_iops - ss->prev_iops) / rate_time;
263 ++group_ramp_time_over;
264 }
265 ss->prev_iops = td_iops;
266 ss->prev_bytes = td_bytes;
267
c8caba48 268 if (td->o.group_reporting && !(ss->state & FIO_SS_DATA))
4cd5b75b
JA
269 continue;
270
7be78a8a
JA
271 /*
272 * Don't begin checking criterion until ss->ramp_time is over
273 * for at least one thread in group
274 */
4cd5b75b
JA
275 if (!group_ramp_time_over)
276 continue;
277
7be78a8a
JA
278 dprint(FD_STEADYSTATE, "steadystate_check() thread: %d, "
279 "groupid: %u, rate_msec: %ld, "
9d193019
VF
280 "iops: %llu, bw: %llu, head: %d, tail: %d\n",
281 i, td->groupid, rate_time,
282 (unsigned long long) group_iops,
283 (unsigned long long) group_bw,
284 ss->head, ss->tail);
4cd5b75b 285
c8caba48 286 if (ss->state & FIO_SS_SLOPE)
4cd5b75b
JA
287 ret = steadystate_slope(group_iops, group_bw, td);
288 else
289 ret = steadystate_deviation(group_iops, group_bw, td);
290
291 if (ret) {
292 if (td->o.group_reporting) {
293 for_each_td(td2, j) {
294 if (td2->groupid == td->groupid) {
c8caba48 295 td2->ss.state |= FIO_SS_ATTAINED;
4cd5b75b
JA
296 fio_mark_td_terminate(td2);
297 }
298 }
299 } else {
c8caba48 300 ss->state |= FIO_SS_ATTAINED;
4cd5b75b
JA
301 fio_mark_td_terminate(td);
302 }
303 }
304 }
305}
306
56a90eba 307int td_steadystate_init(struct thread_data *td)
f0c50c66
JA
308{
309 struct steadystate_data *ss = &td->ss;
310 struct thread_options *o = &td->o;
56a90eba
VF
311 struct thread_data *td2;
312 int j;
f0c50c66
JA
313
314 memset(ss, 0, sizeof(*ss));
315
56a90eba
VF
316 if (o->ss_dur) {
317 steadystate_enabled = true;
318 o->ss_dur /= 1000000L;
319
320 /* put all steady state info in one place */
321 ss->dur = o->ss_dur;
322 ss->limit = o->ss_limit.u.f;
323 ss->ramp_time = o->ss_ramp_time;
f0c50c66 324
2c5d94bc 325 ss->state = o->ss_state;
56a90eba 326 if (!td->ss.ramp_time)
c8caba48 327 ss->state |= FIO_SS_RAMP_OVER;
4cd5b75b 328
56a90eba
VF
329 ss->sum_x = o->ss_dur * (o->ss_dur - 1) / 2;
330 ss->sum_x_sq = (o->ss_dur - 1) * (o->ss_dur) * (2*o->ss_dur - 1) / 6;
56a90eba 331 }
5b4b6586 332
56a90eba
VF
333 /* make sure that ss options are consistent within reporting group */
334 for_each_td(td2, j) {
335 if (td2->groupid == td->groupid) {
336 struct steadystate_data *ss2 = &td2->ss;
337
338 if (ss2->dur != ss->dur ||
339 ss2->limit != ss->limit ||
340 ss2->ramp_time != ss->ramp_time ||
56a90eba
VF
341 ss2->state != ss->state ||
342 ss2->sum_x != ss->sum_x ||
343 ss2->sum_x_sq != ss->sum_x_sq) {
344 td_verror(td, EINVAL, "job rejected: steadystate options must be consistent within reporting groups");
345 return 1;
346 }
347 }
348 }
f0c50c66 349
56a90eba 350 return 0;
f0c50c66 351}
d685adfb 352
9d193019 353uint64_t steadystate_bw_mean(struct thread_stat *ts)
d685adfb
VF
354{
355 int i;
9d193019 356 uint64_t sum;
d685adfb 357
9fac0db7
JA
358 if (!ts->ss_dur)
359 return 0;
360
bb49c8bd
VF
361 for (i = 0, sum = 0; i < ts->ss_dur; i++)
362 sum += ts->ss_bw_data[i];
d685adfb 363
bb49c8bd 364 return sum / ts->ss_dur;
d685adfb
VF
365}
366
9d193019 367uint64_t steadystate_iops_mean(struct thread_stat *ts)
d685adfb
VF
368{
369 int i;
9d193019 370 uint64_t sum;
d685adfb 371
9fac0db7
JA
372 if (!ts->ss_dur)
373 return 0;
374
bb49c8bd
VF
375 for (i = 0, sum = 0; i < ts->ss_dur; i++)
376 sum += ts->ss_iops_data[i];
d685adfb 377
bb49c8bd 378 return sum / ts->ss_dur;
d685adfb 379}