steadystate: rename 'steadystate' to 'steadystate_enabled'
[fio.git] / steadystate.c
CommitLineData
ba8fb6f6
VF
1#include <stdlib.h>
2
16e56d25
VF
3#include "fio.h"
4#include "steadystate.h"
5#include "helper_thread.h"
6
84784e07 7bool steadystate_enabled = false;
4cd5b75b
JA
8
9void steadystate_setup(void)
16e56d25
VF
10{
11 int i, prev_groupid;
12 struct thread_data *td, *prev_td;
13
84784e07 14 if (!steadystate_enabled)
16e56d25
VF
15 return;
16
17 /*
18 * if group reporting is enabled, identify the last td
19 * for each group and use it for storing steady state
20 * data
21 */
22 prev_groupid = -1;
23 prev_td = NULL;
24 for_each_td(td, i) {
ba8fb6f6
VF
25 if (td->ts.ss == NULL)
26 continue;
27
28 if (!td->o.group_reporting) {
29 steadystate_alloc(td);
16e56d25 30 continue;
ba8fb6f6 31 }
16e56d25
VF
32
33 if (prev_groupid != td->groupid) {
ba8fb6f6 34 if (prev_td != NULL) {
16e56d25 35 prev_td->ss.last_in_group = 1;
ba8fb6f6
VF
36 steadystate_alloc(prev_td);
37 }
16e56d25
VF
38 prev_groupid = td->groupid;
39 }
40 prev_td = td;
41 }
42
ba8fb6f6 43 if (prev_td != NULL && prev_td->o.group_reporting) {
16e56d25 44 prev_td->ss.last_in_group = 1;
ba8fb6f6
VF
45 steadystate_alloc(prev_td);
46 }
47}
48
49void steadystate_alloc(struct thread_data *td)
50{
51 int i;
52
53 td->ss.bw_data = malloc(td->ss.dur * sizeof(unsigned long));
54 td->ss.iops_data = malloc(td->ss.dur * sizeof(unsigned long));
55 /* initialize so that it is obvious if the cache is not full in the output */
56 for (i = 0; i < td->ss.dur; i++)
57 td->ss.iops_data[i] = td->ss.bw_data[i] = 0;
16e56d25
VF
58}
59
4cd5b75b
JA
60static bool steadystate_slope(unsigned long iops, unsigned long bw,
61 struct thread_data *td)
16e56d25 62{
ba8fb6f6 63 int i, j;
16e56d25 64 double result;
16e56d25 65 struct steadystate_data *ss = &td->ss;
7be78a8a 66 unsigned long new_val;
16e56d25 67
ba8fb6f6
VF
68 ss->bw_data[ss->tail] = bw;
69 ss->iops_data[ss->tail] = iops;
16e56d25 70
7be78a8a
JA
71 if (ss->check_iops)
72 new_val = iops;
73 else
74 new_val = bw;
75
4cd5b75b
JA
76 if (ss->tail < ss->head || (ss->tail - ss->head == ss->dur - 1)) {
77 if (ss->sum_y == 0) { /* first time through */
78 for(i = 0; i < ss->dur; i++) {
7be78a8a
JA
79 if (ss->check_iops)
80 ss->sum_y += ss->iops_data[i];
81 else
82 ss->sum_y += ss->bw_data[i];
ba8fb6f6
VF
83 j = ss->head + i;
84 if (j >= ss->dur)
85 j -= ss->dur;
7be78a8a
JA
86 if (ss->check_iops)
87 ss->sum_xy += ss->iops_data[j];
88 else
89 ss->sum_xy += ss->bw_data[j];
16e56d25
VF
90 }
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
7be78a8a
JA
97 if (ss->check_iops)
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);
110 if (ss->pct)
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",
117 ss->sum_y, ss->sum_xy, ss->slope,
118 ss->criterion, ss->limit);
16e56d25 119
6da94b07 120 result = ss->criterion * (ss->criterion < 0.0 ? -1.0 : 1.0);
16e56d25
VF
121 if (result < ss->limit)
122 return true;
123 }
124
125 ss->tail = (ss->tail + 1) % ss->dur;
126 if (ss->tail <= ss->head)
127 ss->head = (ss->head + 1) % ss->dur;
4cd5b75b 128
16e56d25
VF
129 return false;
130}
131
4cd5b75b
JA
132static bool steadystate_deviation(unsigned long iops, unsigned long bw,
133 struct thread_data *td)
16e56d25
VF
134{
135 int i;
136 double diff;
137 double mean;
16e56d25
VF
138
139 struct steadystate_data *ss = &td->ss;
140
ba8fb6f6
VF
141 ss->bw_data[ss->tail] = bw;
142 ss->iops_data[ss->tail] = iops;
16e56d25 143
4cd5b75b
JA
144 if (ss->tail < ss->head || (ss->tail - ss->head == ss->dur - 1)) {
145 if (ss->sum_y == 0) { /* first time through */
16e56d25 146 for(i = 0; i < ss->dur; i++)
7be78a8a
JA
147 if (ss->check_iops)
148 ss->sum_y += ss->iops_data[i];
149 else
150 ss->sum_y += ss->bw_data[i];
16e56d25
VF
151 } else { /* easy to update the sum */
152 ss->sum_y -= ss->oldest_y;
7be78a8a
JA
153 if (ss->check_iops)
154 ss->sum_y += ss->iops_data[ss->tail];
155 else
156 ss->sum_y += ss->bw_data[ss->tail];
16e56d25
VF
157 }
158
7be78a8a
JA
159 if (ss->check_iops)
160 ss->oldest_y = ss->iops_data[ss->head];
161 else
162 ss->oldest_y = ss->bw_data[ss->head];
163
16e56d25 164 mean = (double) ss->sum_y / ss->dur;
ba8fb6f6 165 ss->deviation = 0.0;
16e56d25 166
4cd5b75b 167 for (i = 0; i < ss->dur; i++) {
7be78a8a
JA
168 if (ss->check_iops)
169 diff = ss->iops_data[i] - mean;
170 else
171 diff = ss->bw_data[i] - mean;
6da94b07 172 ss->deviation = max(ss->deviation, diff * (diff < 0.0 ? -1.0 : 1.0));
16e56d25
VF
173 }
174
7be78a8a
JA
175 if (ss->pct)
176 ss->criterion = 100.0 * ss->deviation / mean;
177 else
178 ss->criterion = ss->deviation;
16e56d25 179
7be78a8a
JA
180 dprint(FD_STEADYSTATE, "sum_y: %llu, mean: %f, max diff: %f, "
181 "objective: %f, limit: %f\n",
182 ss->sum_y, mean, ss->deviation,
183 ss->criterion, ss->limit);
16e56d25
VF
184
185 if (ss->criterion < ss->limit)
186 return true;
187 }
188
189 ss->tail = (ss->tail + 1) % ss->dur;
190 if (ss->tail <= ss->head)
191 ss->head = (ss->head + 1) % ss->dur;
4cd5b75b 192
16e56d25
VF
193 return false;
194}
4cd5b75b
JA
195
196void steadystate_check(void)
197{
198 int i, j, ddir, prev_groupid, group_ramp_time_over = 0;
199 unsigned long rate_time;
200 struct thread_data *td, *td2;
201 struct timeval now;
202 unsigned long group_bw = 0, group_iops = 0;
203 unsigned long long td_iops;
204 unsigned long long td_bytes;
205 bool ret;
206
207 prev_groupid = -1;
208 for_each_td(td, i) {
209 struct steadystate_data *ss = &td->ss;
210
7be78a8a
JA
211 if (!ss->dur || td->runstate <= TD_SETTING_UP ||
212 td->runstate >= TD_EXITED || ss->attained)
4cd5b75b
JA
213 continue;
214
215 td_iops = 0;
216 td_bytes = 0;
217 if (!td->o.group_reporting ||
218 (td->o.group_reporting && td->groupid != prev_groupid)) {
219 group_bw = 0;
220 group_iops = 0;
221 group_ramp_time_over = 0;
222 }
223 prev_groupid = td->groupid;
224
225 fio_gettime(&now, NULL);
226 if (ss->ramp_time && !ss->ramp_time_over)
227 /*
228 * Begin recording data one second after ss->ramp_time
229 * has elapsed
230 */
231 if (utime_since(&td->epoch, &now) >= (ss->ramp_time + 1000000L))
232 ss->ramp_time_over = 1;
233
234 td_io_u_lock(td);
235 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
236 td_iops += td->io_blocks[ddir];
237 td_bytes += td->io_bytes[ddir];
238 }
239 td_io_u_unlock(td);
240
241 rate_time = mtime_since(&ss->prev_time, &now);
242 memcpy(&ss->prev_time, &now, sizeof(now));
243
244 /*
245 * Begin monitoring when job starts but don't actually use
246 * data in checking stopping criterion until ss->ramp_time is
247 * over. This ensures that we will have a sane value in
248 * prev_iops/bw the first time through after ss->ramp_time
249 * is done.
250 */
251 if (ss->ramp_time_over) {
252 group_bw += 1000 * (td_bytes - ss->prev_bytes) / rate_time;
253 group_iops += 1000 * (td_iops - ss->prev_iops) / rate_time;
254 ++group_ramp_time_over;
255 }
256 ss->prev_iops = td_iops;
257 ss->prev_bytes = td_bytes;
258
259 if (td->o.group_reporting && !ss->last_in_group)
260 continue;
261
7be78a8a
JA
262 /*
263 * Don't begin checking criterion until ss->ramp_time is over
264 * for at least one thread in group
265 */
4cd5b75b
JA
266 if (!group_ramp_time_over)
267 continue;
268
7be78a8a
JA
269 dprint(FD_STEADYSTATE, "steadystate_check() thread: %d, "
270 "groupid: %u, rate_msec: %ld, "
271 "iops: %lu, bw: %lu, head: %d, tail: %d\n",
272 i, td->groupid, rate_time, group_iops,
273 group_bw, ss->head, ss->tail);
4cd5b75b
JA
274
275 if (steadystate_check_slope(&td->o))
276 ret = steadystate_slope(group_iops, group_bw, td);
277 else
278 ret = steadystate_deviation(group_iops, group_bw, td);
279
280 if (ret) {
281 if (td->o.group_reporting) {
282 for_each_td(td2, j) {
283 if (td2->groupid == td->groupid) {
284 td2->ss.attained = 1;
285 fio_mark_td_terminate(td2);
286 }
287 }
288 } else {
289 ss->attained = 1;
290 fio_mark_td_terminate(td);
291 }
292 }
293 }
294}
295
296