Add locking around reads of td->io_blocks and io_byes.
[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                 td_io_u_lock(td);
73                 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
74                         td_iops += td->io_blocks[ddir];
75                         td_bytes += td->io_bytes[ddir];
76                 }
77                 td_io_u_unlock(td);
78
79                 rate_time = mtime_since(&ss->prev_time, &now);
80                 memcpy(&ss->prev_time, &now, sizeof(now));
81
82                 /* 
83                  * Begin monitoring when job starts but don't actually use
84                  * data in checking stopping criterion until ss->ramp_time is
85                  * over. This ensures that we will have a sane value in
86                  * prev_iops/bw the first time through after ss->ramp_time
87                  * is done.
88                  */
89                 if (ss->ramp_time_over) {
90                         group_bw += 1000 * (td_bytes - ss->prev_bytes) / rate_time;
91                         group_iops += 1000 * (td_iops - ss->prev_iops) / rate_time;
92                         ++group_ramp_time_over;
93                 }
94                 ss->prev_iops = td_iops;
95                 ss->prev_bytes = td_bytes;
96
97                 if (td->o.group_reporting && !ss->last_in_group)
98                         continue;
99
100                 /* don't begin checking criterion until ss->ramp_time is over for at least one thread in group */
101                 if (!group_ramp_time_over)
102                         continue;
103
104                 dprint(FD_STEADYSTATE, "steadystate_check() thread: %d, groupid: %u, rate_msec: %ld, iops: %lu, bw: %lu, head: %d, tail: %d\n", 
105                         i, td->groupid, rate_time, group_iops, group_bw, ss->head, ss->tail);
106
107                 if (ss->evaluate(group_iops, group_bw, td))
108                 {
109                         if (td->o.group_reporting)
110                                 for_each_td(td2, j) {
111                                         if (td2->groupid == td->groupid) {
112                                                 td2->ss.attained = 1;
113                                                 fio_mark_td_terminate(td2);
114                                         }
115                                 }
116                         else {
117                                 ss->attained = 1;
118                                 fio_mark_td_terminate(td);
119                         }
120                 }
121         }
122 }
123
124 bool steadystate_slope(unsigned long iops, unsigned long bw, struct thread_data *td)
125 {
126         int i, x;
127         double result;
128         double slope;
129         struct steadystate_data *ss = &td->ss;
130
131         ss->cache[ss->tail] = ss->check_iops ? iops : bw;
132
133         if (ss->tail < ss->head || (ss->tail - ss->head == ss->dur - 1))
134         {
135                 if (ss->sum_y == 0)     /* first time through */
136                 {
137                         for(i = 0; i < ss->dur; i++)
138                         {
139                                 ss->sum_y += ss->cache[i];
140                                 x = ss->head + i;
141                                 if (x >= ss->dur)
142                                         x -= ss->dur;
143                                 ss->sum_xy += ss->cache[x] * i;
144                         }
145                 } else {                /* easy to update the sums */
146                         ss->sum_y -= ss->oldest_y;
147                         ss->sum_y += ss->cache[ss->tail];
148                         ss->sum_xy = ss->sum_xy - ss->sum_y + ss->dur * ss->cache[ss->tail];
149                 }
150
151                 ss->oldest_y = ss->cache[ss->head];
152
153                 /*
154                  * calculate slope as (sum_xy - sum_x * sum_y / n) / (sum_(x^2) - (sum_x)^2 / n)
155                  * This code assumes that all x values are equally spaced when they are often
156                  * off by a few milliseconds. This assumption greatly simplifies the
157                  * calculations.
158                  */
159                 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);
160                 ss->criterion = ss->pct ? slope / (ss->sum_y / ss->dur) * 100.0: slope;
161
162                 dprint(FD_STEADYSTATE, "sum_y: %llu, sum_xy: %llu, slope: %f, criterion: %f, limit: %f\n",
163                         ss->sum_y, ss->sum_xy, slope, ss->criterion, ss->limit);
164
165                 result = ss->criterion * (ss->criterion < 0.0 ? -1 : 1);
166                 if (result < ss->limit)
167                         return true;
168         }
169
170         ss->tail = (ss->tail + 1) % ss->dur;
171         if (ss->tail <= ss->head)
172                 ss->head = (ss->head + 1) % ss->dur;
173         return false;
174 }
175
176 bool steadystate_deviation(unsigned long iops, unsigned long bw, struct thread_data *td)
177 {
178         int i;
179         double diff;
180         double mean;
181         double deviation;
182
183         struct steadystate_data *ss = &td->ss;
184
185         ss->cache[ss->tail] = ss->check_iops ? iops : bw;
186
187         if (ss->tail < ss->head || (ss->tail - ss->head == ss->dur - 1))
188         {
189                 if (ss->sum_y == 0)     /* first time through */
190                 {
191                         for(i = 0; i < ss->dur; i++)
192                                 ss->sum_y += ss->cache[i];
193                 } else {                /* easy to update the sum */
194                         ss->sum_y -= ss->oldest_y;
195                         ss->sum_y += ss->cache[ss->tail];
196                 }
197
198                 ss->oldest_y = ss->cache[ss->head];
199                 mean = (double) ss->sum_y / ss->dur;
200                 deviation = 0.0;
201
202                 for (i = 0; i < ss->dur; i++)
203                 {       
204                         diff = (double) ss->cache[i] - mean;
205                         deviation = max(deviation, diff * (diff < 0.0 ? -1 : 1));
206                 }
207
208                 ss->criterion = ss->pct ? deviation / mean * 100.0 : deviation;
209
210                 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);
211
212                 if (ss->criterion < ss->limit)
213                         return true;
214         }
215
216         ss->tail = (ss->tail + 1) % ss->dur;
217         if (ss->tail <= ss->head)
218                 ss->head = (ss->head + 1) % ss->dur;
219         return false;
220 }