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