4 #include "steadystate.h"
5 #include "helper_thread.h"
7 bool steadystate = false;
9 void steadystate_setup(void)
12 struct thread_data *td, *prev_td;
18 * if group reporting is enabled, identify the last td
19 * for each group and use it for storing steady state
25 if (td->ts.ss == NULL)
28 if (!td->o.group_reporting) {
29 steadystate_alloc(td);
33 if (prev_groupid != td->groupid) {
34 if (prev_td != NULL) {
35 prev_td->ss.last_in_group = 1;
36 steadystate_alloc(prev_td);
38 prev_groupid = td->groupid;
43 if (prev_td != NULL && prev_td->o.group_reporting) {
44 prev_td->ss.last_in_group = 1;
45 steadystate_alloc(prev_td);
49 void steadystate_alloc(struct thread_data *td)
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;
60 static bool steadystate_slope(unsigned long iops, unsigned long bw,
61 struct thread_data *td)
65 struct steadystate_data *ss = &td->ss;
66 unsigned long new_val;
68 ss->bw_data[ss->tail] = bw;
69 ss->iops_data[ss->tail] = iops;
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++) {
80 ss->sum_y += ss->iops_data[i];
82 ss->sum_y += ss->bw_data[i];
87 ss->sum_xy += ss->iops_data[j];
89 ss->sum_xy += ss->bw_data[j];
91 } else { /* easy to update the sums */
92 ss->sum_y -= ss->oldest_y;
94 ss->sum_xy = ss->sum_xy - ss->sum_y + ss->dur * new_val;
98 ss->oldest_y = ss->iops_data[ss->head];
100 ss->oldest_y = ss->bw_data[ss->head];
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.
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);
111 ss->criterion = 100.0 * ss->slope / (ss->sum_y / ss->dur);
113 ss->criterion = ss->slope;
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);
120 result = ss->criterion * (ss->criterion < 0.0 ? -1.0 : 1.0);
121 if (result < ss->limit)
125 ss->tail = (ss->tail + 1) % ss->dur;
126 if (ss->tail <= ss->head)
127 ss->head = (ss->head + 1) % ss->dur;
132 static bool steadystate_deviation(unsigned long iops, unsigned long bw,
133 struct thread_data *td)
139 struct steadystate_data *ss = &td->ss;
141 ss->bw_data[ss->tail] = bw;
142 ss->iops_data[ss->tail] = iops;
144 if (ss->tail < ss->head || (ss->tail - ss->head == ss->dur - 1)) {
145 if (ss->sum_y == 0) { /* first time through */
146 for(i = 0; i < ss->dur; i++)
148 ss->sum_y += ss->iops_data[i];
150 ss->sum_y += ss->bw_data[i];
151 } else { /* easy to update the sum */
152 ss->sum_y -= ss->oldest_y;
154 ss->sum_y += ss->iops_data[ss->tail];
156 ss->sum_y += ss->bw_data[ss->tail];
160 ss->oldest_y = ss->iops_data[ss->head];
162 ss->oldest_y = ss->bw_data[ss->head];
164 mean = (double) ss->sum_y / ss->dur;
167 for (i = 0; i < ss->dur; i++) {
169 diff = ss->iops_data[i] - mean;
171 diff = ss->bw_data[i] - mean;
172 ss->deviation = max(ss->deviation, diff * (diff < 0.0 ? -1.0 : 1.0));
176 ss->criterion = 100.0 * ss->deviation / mean;
178 ss->criterion = ss->deviation;
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);
185 if (ss->criterion < ss->limit)
189 ss->tail = (ss->tail + 1) % ss->dur;
190 if (ss->tail <= ss->head)
191 ss->head = (ss->head + 1) % ss->dur;
196 void steadystate_check(void)
198 int i, j, ddir, prev_groupid, group_ramp_time_over = 0;
199 unsigned long rate_time;
200 struct thread_data *td, *td2;
202 unsigned long group_bw = 0, group_iops = 0;
203 unsigned long long td_iops;
204 unsigned long long td_bytes;
209 struct steadystate_data *ss = &td->ss;
211 if (!ss->dur || td->runstate <= TD_SETTING_UP ||
212 td->runstate >= TD_EXITED || ss->attained)
217 if (!td->o.group_reporting ||
218 (td->o.group_reporting && td->groupid != prev_groupid)) {
221 group_ramp_time_over = 0;
223 prev_groupid = td->groupid;
225 fio_gettime(&now, NULL);
226 if (ss->ramp_time && !ss->ramp_time_over)
228 * Begin recording data one second after ss->ramp_time
231 if (utime_since(&td->epoch, &now) >= (ss->ramp_time + 1000000L))
232 ss->ramp_time_over = 1;
235 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
236 td_iops += td->io_blocks[ddir];
237 td_bytes += td->io_bytes[ddir];
241 rate_time = mtime_since(&ss->prev_time, &now);
242 memcpy(&ss->prev_time, &now, sizeof(now));
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
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;
256 ss->prev_iops = td_iops;
257 ss->prev_bytes = td_bytes;
259 if (td->o.group_reporting && !ss->last_in_group)
263 * Don't begin checking criterion until ss->ramp_time is over
264 * for at least one thread in group
266 if (!group_ramp_time_over)
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);
275 if (steadystate_check_slope(&td->o))
276 ret = steadystate_slope(group_iops, group_bw, td);
278 ret = steadystate_deviation(group_iops, group_bw, td);
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);
290 fio_mark_td_terminate(td);