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