client: switch to per-client buffer
[fio.git] / steadystate.c
... / ...
CommitLineData
1#include <stdlib.h>
2
3#include "fio.h"
4#include "steadystate.h"
5
6bool steadystate_enabled = false;
7
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
16static void steadystate_alloc(struct thread_data *td)
17{
18 td->ss.bw_data = calloc(td->ss.dur, sizeof(uint64_t));
19 td->ss.iops_data = calloc(td->ss.dur, sizeof(uint64_t));
20
21 td->ss.state |= FIO_SS_DATA;
22}
23
24void steadystate_setup(void)
25{
26 struct thread_data *td, *prev_td;
27 int i, prev_groupid;
28
29 if (!steadystate_enabled)
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) {
40 if (!td->ss.dur)
41 continue;
42
43 if (!td->o.group_reporting) {
44 steadystate_alloc(td);
45 continue;
46 }
47
48 if (prev_groupid != td->groupid) {
49 if (prev_td)
50 steadystate_alloc(prev_td);
51 prev_groupid = td->groupid;
52 }
53 prev_td = td;
54 }
55
56 if (prev_td && prev_td->o.group_reporting)
57 steadystate_alloc(prev_td);
58}
59
60static bool steadystate_slope(uint64_t iops, uint64_t bw,
61 struct thread_data *td)
62{
63 int i, j;
64 double result;
65 struct steadystate_data *ss = &td->ss;
66 uint64_t new_val;
67
68 ss->bw_data[ss->tail] = bw;
69 ss->iops_data[ss->tail] = iops;
70
71 if (ss->state & FIO_SS_IOPS)
72 new_val = iops;
73 else
74 new_val = bw;
75
76 if (ss->state & FIO_SS_BUFFER_FULL || ss->tail - ss->head == ss->dur - 1) {
77 if (!(ss->state & FIO_SS_BUFFER_FULL)) {
78 /* first time through */
79 for(i = 0, ss->sum_y = 0; i < ss->dur; i++) {
80 if (ss->state & FIO_SS_IOPS)
81 ss->sum_y += ss->iops_data[i];
82 else
83 ss->sum_y += ss->bw_data[i];
84 j = (ss->head + i) % ss->dur;
85 if (ss->state & FIO_SS_IOPS)
86 ss->sum_xy += i * ss->iops_data[j];
87 else
88 ss->sum_xy += i * ss->bw_data[j];
89 }
90 ss->state |= FIO_SS_BUFFER_FULL;
91 } else { /* easy to update the sums */
92 ss->sum_y -= ss->oldest_y;
93 ss->sum_y += new_val;
94 ss->sum_xy = ss->sum_xy - ss->sum_y + ss->dur * new_val;
95 }
96
97 if (ss->state & FIO_SS_IOPS)
98 ss->oldest_y = ss->iops_data[ss->head];
99 else
100 ss->oldest_y = ss->bw_data[ss->head];
101
102 /*
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.
107 */
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);
110 if (ss->state & FIO_SS_PCT)
111 ss->criterion = 100.0 * ss->slope / (ss->sum_y / ss->dur);
112 else
113 ss->criterion = ss->slope;
114
115 dprint(FD_STEADYSTATE, "sum_y: %llu, sum_xy: %llu, slope: %f, "
116 "criterion: %f, limit: %f\n",
117 (unsigned long long) ss->sum_y,
118 (unsigned long long) ss->sum_xy,
119 ss->slope, ss->criterion, ss->limit);
120
121 result = ss->criterion * (ss->criterion < 0.0 ? -1.0 : 1.0);
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;
129
130 return false;
131}
132
133static bool steadystate_deviation(uint64_t iops, uint64_t bw,
134 struct thread_data *td)
135{
136 int i;
137 double diff;
138 double mean;
139
140 struct steadystate_data *ss = &td->ss;
141
142 ss->bw_data[ss->tail] = bw;
143 ss->iops_data[ss->tail] = iops;
144
145 if (ss->state & FIO_SS_BUFFER_FULL || ss->tail - ss->head == ss->dur - 1) {
146 if (!(ss->state & FIO_SS_BUFFER_FULL)) {
147 /* first time through */
148 for(i = 0, ss->sum_y = 0; i < ss->dur; i++)
149 if (ss->state & FIO_SS_IOPS)
150 ss->sum_y += ss->iops_data[i];
151 else
152 ss->sum_y += ss->bw_data[i];
153 ss->state |= FIO_SS_BUFFER_FULL;
154 } else { /* easy to update the sum */
155 ss->sum_y -= ss->oldest_y;
156 if (ss->state & FIO_SS_IOPS)
157 ss->sum_y += ss->iops_data[ss->tail];
158 else
159 ss->sum_y += ss->bw_data[ss->tail];
160 }
161
162 if (ss->state & FIO_SS_IOPS)
163 ss->oldest_y = ss->iops_data[ss->head];
164 else
165 ss->oldest_y = ss->bw_data[ss->head];
166
167 mean = (double) ss->sum_y / ss->dur;
168 ss->deviation = 0.0;
169
170 for (i = 0; i < ss->dur; i++) {
171 if (ss->state & FIO_SS_IOPS)
172 diff = ss->iops_data[i] - mean;
173 else
174 diff = ss->bw_data[i] - mean;
175 ss->deviation = max(ss->deviation, diff * (diff < 0.0 ? -1.0 : 1.0));
176 }
177
178 if (ss->state & FIO_SS_PCT)
179 ss->criterion = 100.0 * ss->deviation / mean;
180 else
181 ss->criterion = ss->deviation;
182
183 dprint(FD_STEADYSTATE, "sum_y: %llu, mean: %f, max diff: %f, "
184 "objective: %f, limit: %f\n",
185 (unsigned long long) ss->sum_y, mean,
186 ss->deviation, ss->criterion, ss->limit);
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;
195
196 return false;
197}
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;
204 struct timespec now;
205 uint64_t group_bw = 0, group_iops = 0;
206 uint64_t td_iops, td_bytes;
207 bool ret;
208
209 prev_groupid = -1;
210 for_each_td(td, i) {
211 const bool needs_lock = td_async_processing(td);
212 struct steadystate_data *ss = &td->ss;
213
214 if (!ss->dur || td->runstate <= TD_SETTING_UP ||
215 td->runstate >= TD_EXITED || !ss->state ||
216 ss->state & FIO_SS_ATTAINED)
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);
230 if (ss->ramp_time && !(ss->state & FIO_SS_RAMP_OVER)) {
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))
236 ss->state |= FIO_SS_RAMP_OVER;
237 }
238
239 if (needs_lock)
240 __td_io_u_lock(td);
241
242 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++) {
243 td_iops += td->io_blocks[ddir];
244 td_bytes += td->io_bytes[ddir];
245 }
246
247 if (needs_lock)
248 __td_io_u_unlock(td);
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 */
260 if (ss->state & FIO_SS_RAMP_OVER) {
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
268 if (td->o.group_reporting && !(ss->state & FIO_SS_DATA))
269 continue;
270
271 /*
272 * Don't begin checking criterion until ss->ramp_time is over
273 * for at least one thread in group
274 */
275 if (!group_ramp_time_over)
276 continue;
277
278 dprint(FD_STEADYSTATE, "steadystate_check() thread: %d, "
279 "groupid: %u, rate_msec: %ld, "
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);
285
286 if (ss->state & FIO_SS_SLOPE)
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) {
295 td2->ss.state |= FIO_SS_ATTAINED;
296 fio_mark_td_terminate(td2);
297 }
298 }
299 } else {
300 ss->state |= FIO_SS_ATTAINED;
301 fio_mark_td_terminate(td);
302 }
303 }
304 }
305}
306
307int td_steadystate_init(struct thread_data *td)
308{
309 struct steadystate_data *ss = &td->ss;
310 struct thread_options *o = &td->o;
311 struct thread_data *td2;
312 int j;
313
314 memset(ss, 0, sizeof(*ss));
315
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;
324
325 ss->state = o->ss_state;
326 if (!td->ss.ramp_time)
327 ss->state |= FIO_SS_RAMP_OVER;
328
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;
331 }
332
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 ||
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 }
349
350 return 0;
351}
352
353uint64_t steadystate_bw_mean(struct thread_stat *ts)
354{
355 int i;
356 uint64_t sum;
357
358 if (!ts->ss_dur)
359 return 0;
360
361 for (i = 0, sum = 0; i < ts->ss_dur; i++)
362 sum += ts->ss_bw_data[i];
363
364 return sum / ts->ss_dur;
365}
366
367uint64_t steadystate_iops_mean(struct thread_stat *ts)
368{
369 int i;
370 uint64_t sum;
371
372 if (!ts->ss_dur)
373 return 0;
374
375 for (i = 0, sum = 0; i < ts->ss_dur; i++)
376 sum += ts->ss_iops_data[i];
377
378 return sum / ts->ss_dur;
379}