Split helper thread debug logging away from steadystate debug logging
[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
7void steadystate_setup()
8{
9 int i, prev_groupid;
10 struct thread_data *td, *prev_td;
11
12 if (!steadystate)
13 return;
14
15 /*
16 * if group reporting is enabled, identify the last td
17 * for each group and use it for storing steady state
18 * data
19 */
20 prev_groupid = -1;
21 prev_td = NULL;
22 for_each_td(td, i) {
ba8fb6f6
VF
23 if (td->ts.ss == NULL)
24 continue;
25
26 if (!td->o.group_reporting) {
27 steadystate_alloc(td);
16e56d25 28 continue;
ba8fb6f6 29 }
16e56d25
VF
30
31 if (prev_groupid != td->groupid) {
ba8fb6f6 32 if (prev_td != NULL) {
16e56d25 33 prev_td->ss.last_in_group = 1;
ba8fb6f6
VF
34 steadystate_alloc(prev_td);
35 }
16e56d25
VF
36 prev_groupid = td->groupid;
37 }
38 prev_td = td;
39 }
40
ba8fb6f6 41 if (prev_td != NULL && prev_td->o.group_reporting) {
16e56d25 42 prev_td->ss.last_in_group = 1;
ba8fb6f6
VF
43 steadystate_alloc(prev_td);
44 }
45}
46
47void steadystate_alloc(struct thread_data *td)
48{
49 int i;
50
51 td->ss.bw_data = malloc(td->ss.dur * sizeof(unsigned long));
52 td->ss.iops_data = malloc(td->ss.dur * sizeof(unsigned long));
53 /* initialize so that it is obvious if the cache is not full in the output */
54 for (i = 0; i < td->ss.dur; i++)
55 td->ss.iops_data[i] = td->ss.bw_data[i] = 0;
16e56d25
VF
56}
57
58void steadystate_check()
59{
60 int i, j, ddir, prev_groupid, group_ramp_time_over = 0;
61 unsigned long rate_time;
62 struct thread_data *td, *td2;
63 struct timeval now;
64 unsigned long group_bw = 0, group_iops = 0;
65 unsigned long long td_iops;
66 unsigned long long td_bytes;
67
68 prev_groupid = -1;
69 for_each_td(td, i) {
70 struct steadystate_data *ss = &td->ss;
71
72 if (!ss->dur || td->runstate <= TD_SETTING_UP || td->runstate >= TD_EXITED || ss->attained)
73 continue;
74
75 td_iops = 0;
76 td_bytes = 0;
77 if (!td->o.group_reporting ||
78 (td->o.group_reporting && td->groupid != prev_groupid)) {
79 group_bw = 0;
80 group_iops = 0;
81 group_ramp_time_over = 0;
82 }
83 prev_groupid = td->groupid;
84
85 fio_gettime(&now, NULL);
86 if (ss->ramp_time && !ss->ramp_time_over)
87 /*
88 * Begin recording data one second after ss->ramp_time
89 * has elapsed
90 */
91 if (utime_since(&td->epoch, &now) >= (ss->ramp_time + 1000000L))
92 ss->ramp_time_over = 1;
93
0587ef32 94 td_io_u_lock(td);
16e56d25
VF
95 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
96 td_iops += td->io_blocks[ddir];
97 td_bytes += td->io_bytes[ddir];
98 }
0587ef32 99 td_io_u_unlock(td);
16e56d25
VF
100
101 rate_time = mtime_since(&ss->prev_time, &now);
102 memcpy(&ss->prev_time, &now, sizeof(now));
103
104 /*
105 * Begin monitoring when job starts but don't actually use
106 * data in checking stopping criterion until ss->ramp_time is
107 * over. This ensures that we will have a sane value in
108 * prev_iops/bw the first time through after ss->ramp_time
109 * is done.
110 */
111 if (ss->ramp_time_over) {
112 group_bw += 1000 * (td_bytes - ss->prev_bytes) / rate_time;
113 group_iops += 1000 * (td_iops - ss->prev_iops) / rate_time;
114 ++group_ramp_time_over;
115 }
116 ss->prev_iops = td_iops;
117 ss->prev_bytes = td_bytes;
118
119 if (td->o.group_reporting && !ss->last_in_group)
120 continue;
121
122 /* don't begin checking criterion until ss->ramp_time is over for at least one thread in group */
123 if (!group_ramp_time_over)
124 continue;
125
126 dprint(FD_STEADYSTATE, "steadystate_check() thread: %d, groupid: %u, rate_msec: %ld, iops: %lu, bw: %lu, head: %d, tail: %d\n",
127 i, td->groupid, rate_time, group_iops, group_bw, ss->head, ss->tail);
128
129 if (ss->evaluate(group_iops, group_bw, td))
130 {
131 if (td->o.group_reporting)
132 for_each_td(td2, j) {
133 if (td2->groupid == td->groupid) {
134 td2->ss.attained = 1;
135 fio_mark_td_terminate(td2);
136 }
137 }
138 else {
139 ss->attained = 1;
140 fio_mark_td_terminate(td);
141 }
142 }
143 }
144}
145
146bool steadystate_slope(unsigned long iops, unsigned long bw, struct thread_data *td)
147{
ba8fb6f6 148 int i, j;
16e56d25 149 double result;
16e56d25 150 struct steadystate_data *ss = &td->ss;
ba8fb6f6 151 unsigned long new_val = ss->check_iops ? iops : bw;
16e56d25 152
ba8fb6f6
VF
153 ss->bw_data[ss->tail] = bw;
154 ss->iops_data[ss->tail] = iops;
16e56d25
VF
155
156 if (ss->tail < ss->head || (ss->tail - ss->head == ss->dur - 1))
157 {
158 if (ss->sum_y == 0) /* first time through */
159 {
160 for(i = 0; i < ss->dur; i++)
161 {
ba8fb6f6
VF
162 ss->sum_y += ss->check_iops ? ss->iops_data[i] : ss->bw_data[i];
163 j = ss->head + i;
164 if (j >= ss->dur)
165 j -= ss->dur;
166 ss->sum_xy += (ss->check_iops ? ss->iops_data[j] : ss->bw_data[j]) * i;
16e56d25
VF
167 }
168 } else { /* easy to update the sums */
169 ss->sum_y -= ss->oldest_y;
ba8fb6f6
VF
170 ss->sum_y += new_val;
171 ss->sum_xy = ss->sum_xy - ss->sum_y + ss->dur * new_val;
16e56d25
VF
172 }
173
ba8fb6f6 174 ss->oldest_y = ss->check_iops ? ss->iops_data[ss->head] : ss->bw_data[ss->head];
16e56d25
VF
175
176 /*
177 * calculate slope as (sum_xy - sum_x * sum_y / n) / (sum_(x^2) - (sum_x)^2 / n)
178 * This code assumes that all x values are equally spaced when they are often
179 * off by a few milliseconds. This assumption greatly simplifies the
180 * calculations.
181 */
ba8fb6f6 182 ss->slope = (ss->sum_xy - (double) ss->sum_x * ss->sum_y / ss->dur) / (ss->sum_x_sq - (double) ss->sum_x * ss->sum_x / ss->dur);
6da94b07 183 ss->criterion = ss->pct ? 100.0 * ss->slope / (ss->sum_y / ss->dur) : ss->slope;
16e56d25
VF
184
185 dprint(FD_STEADYSTATE, "sum_y: %llu, sum_xy: %llu, slope: %f, criterion: %f, limit: %f\n",
ba8fb6f6 186 ss->sum_y, ss->sum_xy, ss->slope, ss->criterion, ss->limit);
16e56d25 187
6da94b07 188 result = ss->criterion * (ss->criterion < 0.0 ? -1.0 : 1.0);
16e56d25
VF
189 if (result < ss->limit)
190 return true;
191 }
192
193 ss->tail = (ss->tail + 1) % ss->dur;
194 if (ss->tail <= ss->head)
195 ss->head = (ss->head + 1) % ss->dur;
196 return false;
197}
198
199bool steadystate_deviation(unsigned long iops, unsigned long bw, struct thread_data *td)
200{
201 int i;
202 double diff;
203 double mean;
16e56d25
VF
204
205 struct steadystate_data *ss = &td->ss;
206
ba8fb6f6
VF
207 ss->bw_data[ss->tail] = bw;
208 ss->iops_data[ss->tail] = iops;
16e56d25
VF
209
210 if (ss->tail < ss->head || (ss->tail - ss->head == ss->dur - 1))
211 {
212 if (ss->sum_y == 0) /* first time through */
213 {
214 for(i = 0; i < ss->dur; i++)
ba8fb6f6 215 ss->sum_y += ss->check_iops ? ss->iops_data[i] : ss->bw_data[i];
16e56d25
VF
216 } else { /* easy to update the sum */
217 ss->sum_y -= ss->oldest_y;
ba8fb6f6 218 ss->sum_y += ss->check_iops ? ss->iops_data[ss->tail] : ss->bw_data[ss->tail];
16e56d25
VF
219 }
220
ba8fb6f6 221 ss->oldest_y = ss->check_iops ? ss->iops_data[ss->head] : ss->bw_data[ss->head];
16e56d25 222 mean = (double) ss->sum_y / ss->dur;
ba8fb6f6 223 ss->deviation = 0.0;
16e56d25
VF
224
225 for (i = 0; i < ss->dur; i++)
226 {
ba8fb6f6 227 diff = (double) (ss->check_iops ? ss->iops_data[i] : ss->bw_data[i]) - mean;
6da94b07 228 ss->deviation = max(ss->deviation, diff * (diff < 0.0 ? -1.0 : 1.0));
16e56d25
VF
229 }
230
6da94b07 231 ss->criterion = ss->pct ? 100.0 * ss->deviation / mean : ss->deviation;
16e56d25 232
ba8fb6f6 233 dprint(FD_STEADYSTATE, "sum_y: %llu, mean: %f, max diff: %f, objective: %f, limit: %f\n", ss->sum_y, mean, ss->deviation, ss->criterion, ss->limit);
16e56d25
VF
234
235 if (ss->criterion < ss->limit)
236 return true;
237 }
238
239 ss->tail = (ss->tail + 1) % ss->dur;
240 if (ss->tail <= ss->head)
241 ss->head = (ss->head + 1) % ss->dur;
242 return false;
243}