steadystate: rename options->ss to options->ss_state since ss is used elsewhere to...
[fio.git] / steadystate.c
index 6cd7c13f19a83694a98908aadbc05d25fb380cbf..951376f86796bebea605d80a3c8b279caa4040d2 100644 (file)
@@ -4,14 +4,27 @@
 #include "steadystate.h"
 #include "helper_thread.h"
 
-bool steadystate = false;
+bool steadystate_enabled = false;
+
+static void steadystate_alloc(struct thread_data *td)
+{
+       int i;
+
+       td->ss.bw_data = malloc(td->ss.dur * sizeof(uint64_t));
+       td->ss.iops_data = malloc(td->ss.dur * sizeof(uint64_t));
+       /* initialize so that it is obvious if the cache is not full in the output */
+       for (i = 0; i < td->ss.dur; i++)
+               td->ss.iops_data[i] = td->ss.bw_data[i] = 0;
+
+       td->ss.state |= __FIO_SS_DATA;
+}
 
 void steadystate_setup(void)
 {
        int i, prev_groupid;
        struct thread_data *td, *prev_td;
 
-       if (!steadystate)
+       if (!steadystate_enabled)
                return;
 
        /*
@@ -22,7 +35,7 @@ void steadystate_setup(void)
        prev_groupid = -1;
        prev_td = NULL;
        for_each_td(td, i) {
-               if (td->ts.ss == NULL)
+               if (!td->ss.dur)
                        continue;
 
                if (!td->o.group_reporting) {
@@ -32,7 +45,6 @@ void steadystate_setup(void)
 
                if (prev_groupid != td->groupid) {
                        if (prev_td != NULL) {
-                               prev_td->ss.last_in_group = 1;
                                steadystate_alloc(prev_td);
                        }
                        prev_groupid = td->groupid;
@@ -41,61 +53,70 @@ void steadystate_setup(void)
        }
 
        if (prev_td != NULL && prev_td->o.group_reporting) {
-               prev_td->ss.last_in_group = 1;
                steadystate_alloc(prev_td);
        }
 }
 
-void steadystate_alloc(struct thread_data *td)
-{
-       int i;
-
-       td->ss.bw_data = malloc(td->ss.dur * sizeof(unsigned long));
-       td->ss.iops_data = malloc(td->ss.dur * sizeof(unsigned long));
-       /* initialize so that it is obvious if the cache is not full in the output */
-       for (i = 0; i < td->ss.dur; i++)
-               td->ss.iops_data[i] = td->ss.bw_data[i] = 0;
-}
-
-static bool steadystate_slope(unsigned long iops, unsigned long bw,
+static bool steadystate_slope(uint64_t iops, uint64_t bw,
                              struct thread_data *td)
 {
        int i, j;
        double result;
        struct steadystate_data *ss = &td->ss;
-       unsigned long new_val = ss->check_iops ? iops : bw;
+       uint64_t new_val;
 
        ss->bw_data[ss->tail] = bw;
        ss->iops_data[ss->tail] = iops;
 
-       if (ss->tail < ss->head || (ss->tail - ss->head == ss->dur - 1)) {
-               if (ss->sum_y == 0) {   /* first time through */
-                       for(i = 0; i < ss->dur; i++) {
-                               ss->sum_y += ss->check_iops ? ss->iops_data[i] : ss->bw_data[i];
-                               j = ss->head + i;
-                               if (j >= ss->dur)
-                                       j -= ss->dur;
-                               ss->sum_xy += (ss->check_iops ? ss->iops_data[j] : ss->bw_data[j]) * i;
+       if (ss->state & __FIO_SS_IOPS)
+               new_val = iops;
+       else
+               new_val = bw;
+
+       if (ss->state & __FIO_SS_BUFFER_FULL || ss->tail - ss->head == ss->dur - 1) {
+               if (!(ss->state & __FIO_SS_BUFFER_FULL)) {
+                       /* first time through */
+                       for(i = 0, ss->sum_y = 0; i < ss->dur; i++) {
+                               if (ss->state & __FIO_SS_IOPS)
+                                       ss->sum_y += ss->iops_data[i];
+                               else
+                                       ss->sum_y += ss->bw_data[i];
+                               j = (ss->head + i) % ss->dur;
+                               if (ss->state & __FIO_SS_IOPS)
+                                       ss->sum_xy += i * ss->iops_data[j];
+                               else
+                                       ss->sum_xy += i * ss->bw_data[j];
                        }
+                       ss->state |= __FIO_SS_BUFFER_FULL;
                } else {                /* easy to update the sums */
                        ss->sum_y -= ss->oldest_y;
                        ss->sum_y += new_val;
                        ss->sum_xy = ss->sum_xy - ss->sum_y + ss->dur * new_val;
                }
 
-               ss->oldest_y = ss->check_iops ? ss->iops_data[ss->head] : ss->bw_data[ss->head];
+               if (ss->state & __FIO_SS_IOPS)
+                       ss->oldest_y = ss->iops_data[ss->head];
+               else
+                       ss->oldest_y = ss->bw_data[ss->head];
 
                /*
-                * calculate slope as (sum_xy - sum_x * sum_y / n) / (sum_(x^2) - (sum_x)^2 / n)
-                * This code assumes that all x values are equally spaced when they are often
-                * off by a few milliseconds. This assumption greatly simplifies the
-                * calculations.
+                * calculate slope as (sum_xy - sum_x * sum_y / n) / (sum_(x^2)
+                * - (sum_x)^2 / n) This code assumes that all x values are
+                * equally spaced when they are often off by a few milliseconds.
+                * This assumption greatly simplifies the calculations.
                 */
-               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);
-               ss->criterion = ss->pct ? 100.0 * ss->slope / (ss->sum_y / ss->dur) : ss->slope;
+               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);
+               if (ss->state & __FIO_SS_PCT)
+                       ss->criterion = 100.0 * ss->slope / (ss->sum_y / ss->dur);
+               else
+                       ss->criterion = ss->slope;
 
-               dprint(FD_STEADYSTATE, "sum_y: %llu, sum_xy: %llu, slope: %f, criterion: %f, limit: %f\n",
-                       ss->sum_y, ss->sum_xy, ss->slope, ss->criterion, ss->limit);
+               dprint(FD_STEADYSTATE, "sum_y: %llu, sum_xy: %llu, slope: %f, "
+                                       "criterion: %f, limit: %f\n",
+                                       (unsigned long long) ss->sum_y,
+                                       (unsigned long long) ss->sum_xy,
+                                       ss->slope, ss->criterion, ss->limit);
 
                result = ss->criterion * (ss->criterion < 0.0 ? -1.0 : 1.0);
                if (result < ss->limit)
@@ -109,7 +130,7 @@ static bool steadystate_slope(unsigned long iops, unsigned long bw,
        return false;
 }
 
-static bool steadystate_deviation(unsigned long iops, unsigned long bw,
+static bool steadystate_deviation(uint64_t iops, uint64_t bw,
                                  struct thread_data *td)
 {
        int i;
@@ -121,27 +142,48 @@ static bool steadystate_deviation(unsigned long iops, unsigned long bw,
        ss->bw_data[ss->tail] = bw;
        ss->iops_data[ss->tail] = iops;
 
-       if (ss->tail < ss->head || (ss->tail - ss->head == ss->dur - 1)) {
-               if (ss->sum_y == 0) {   /* first time through */
-                       for(i = 0; i < ss->dur; i++)
-                               ss->sum_y += ss->check_iops ? ss->iops_data[i] : ss->bw_data[i];
+       if (ss->state & __FIO_SS_BUFFER_FULL || ss->tail - ss->head == ss->dur - 1) {
+               if (!(ss->state & __FIO_SS_BUFFER_FULL)) {
+                       /* first time through */
+                       for(i = 0, ss->sum_y = 0; i < ss->dur; i++)
+                               if (ss->state & __FIO_SS_IOPS)
+                                       ss->sum_y += ss->iops_data[i];
+                               else
+                                       ss->sum_y += ss->bw_data[i];
+                       ss->state |= __FIO_SS_BUFFER_FULL;
                } else {                /* easy to update the sum */
                        ss->sum_y -= ss->oldest_y;
-                       ss->sum_y += ss->check_iops ? ss->iops_data[ss->tail] : ss->bw_data[ss->tail];
+                       if (ss->state & __FIO_SS_IOPS)
+                               ss->sum_y += ss->iops_data[ss->tail];
+                       else
+                               ss->sum_y += ss->bw_data[ss->tail];
                }
 
-               ss->oldest_y = ss->check_iops ? ss->iops_data[ss->head] : ss->bw_data[ss->head];
+               if (ss->state & __FIO_SS_IOPS)
+                       ss->oldest_y = ss->iops_data[ss->head];
+               else
+                       ss->oldest_y = ss->bw_data[ss->head];
+
                mean = (double) ss->sum_y / ss->dur;
                ss->deviation = 0.0;
 
                for (i = 0; i < ss->dur; i++) {
-                       diff = (double) (ss->check_iops ? ss->iops_data[i] : ss->bw_data[i]) - mean;
+                       if (ss->state & __FIO_SS_IOPS)
+                               diff = ss->iops_data[i] - mean;
+                       else
+                               diff = ss->bw_data[i] - mean;
                        ss->deviation = max(ss->deviation, diff * (diff < 0.0 ? -1.0 : 1.0));
                }
 
-               ss->criterion = ss->pct ? 100.0 * ss->deviation / mean : ss->deviation;
+               if (ss->state & __FIO_SS_PCT)
+                       ss->criterion = 100.0 * ss->deviation / mean;
+               else
+                       ss->criterion = ss->deviation;
 
-               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);
+               dprint(FD_STEADYSTATE, "sum_y: %llu, mean: %f, max diff: %f, "
+                                       "objective: %f, limit: %f\n",
+                                       (unsigned long long) ss->sum_y, mean,
+                                       ss->deviation, ss->criterion, ss->limit);
 
                if (ss->criterion < ss->limit)
                        return true;
@@ -160,16 +202,17 @@ void steadystate_check(void)
        unsigned long rate_time;
        struct thread_data *td, *td2;
        struct timeval now;
-       unsigned long group_bw = 0, group_iops = 0;
-       unsigned long long td_iops;
-       unsigned long long td_bytes;
+       uint64_t group_bw = 0, group_iops = 0;
+       uint64_t td_iops, td_bytes;
        bool ret;
 
        prev_groupid = -1;
        for_each_td(td, i) {
                struct steadystate_data *ss = &td->ss;
 
-               if (!ss->dur || td->runstate <= TD_SETTING_UP || td->runstate >= TD_EXITED || ss->attained)
+               if (!ss->dur || td->runstate <= TD_SETTING_UP ||
+                   td->runstate >= TD_EXITED || !ss->state ||
+                   ss->state & __FIO_SS_ATTAINED)
                        continue;
 
                td_iops = 0;
@@ -183,13 +226,14 @@ void steadystate_check(void)
                prev_groupid = td->groupid;
 
                fio_gettime(&now, NULL);
-               if (ss->ramp_time && !ss->ramp_time_over)
+               if (ss->ramp_time && !(ss->state & __FIO_SS_RAMP_OVER)) {
                        /*
                         * Begin recording data one second after ss->ramp_time
                         * has elapsed
                         */
                        if (utime_since(&td->epoch, &now) >= (ss->ramp_time + 1000000L))
-                               ss->ramp_time_over = 1;
+                               ss->state |= __FIO_SS_RAMP_OVER;
+               }
 
                td_io_u_lock(td);
                for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++) {
@@ -208,7 +252,7 @@ void steadystate_check(void)
                 * prev_iops/bw the first time through after ss->ramp_time
                 * is done.
                 */
-               if (ss->ramp_time_over) {
+               if (ss->state & __FIO_SS_RAMP_OVER) {
                        group_bw += 1000 * (td_bytes - ss->prev_bytes) / rate_time;
                        group_iops += 1000 * (td_iops - ss->prev_iops) / rate_time;
                        ++group_ramp_time_over;
@@ -216,17 +260,25 @@ void steadystate_check(void)
                ss->prev_iops = td_iops;
                ss->prev_bytes = td_bytes;
 
-               if (td->o.group_reporting && !ss->last_in_group)
+               if (td->o.group_reporting && !(ss->state & __FIO_SS_DATA))
                        continue;
 
-               /* don't begin checking criterion until ss->ramp_time is over for at least one thread in group */
+               /*
+                * Don't begin checking criterion until ss->ramp_time is over
+                * for at least one thread in group
+                */
                if (!group_ramp_time_over)
                        continue;
 
-               dprint(FD_STEADYSTATE, "steadystate_check() thread: %d, groupid: %u, rate_msec: %ld, iops: %lu, bw: %lu, head: %d, tail: %d\n",
-                       i, td->groupid, rate_time, group_iops, group_bw, ss->head, ss->tail);
+               dprint(FD_STEADYSTATE, "steadystate_check() thread: %d, "
+                                       "groupid: %u, rate_msec: %ld, "
+                                       "iops: %llu, bw: %llu, head: %d, tail: %d\n",
+                                       i, td->groupid, rate_time,
+                                       (unsigned long long) group_iops,
+                                       (unsigned long long) group_bw,
+                                       ss->head, ss->tail);
 
-               if (steadystate_check_slope(&td->o))
+               if (ss->state & __FIO_SS_SLOPE)
                        ret = steadystate_slope(group_iops, group_bw, td);
                else
                        ret = steadystate_deviation(group_iops, group_bw, td);
@@ -235,16 +287,82 @@ void steadystate_check(void)
                        if (td->o.group_reporting) {
                                for_each_td(td2, j) {
                                        if (td2->groupid == td->groupid) {
-                                               td2->ss.attained = 1;
+                                               td2->ss.state |= __FIO_SS_ATTAINED;
                                                fio_mark_td_terminate(td2);
                                        }
                                }
                        } else {
-                               ss->attained = 1;
+                               ss->state |= __FIO_SS_ATTAINED;
                                fio_mark_td_terminate(td);
                        }
                }
        }
 }
 
+int td_steadystate_init(struct thread_data *td)
+{
+       struct steadystate_data *ss = &td->ss;
+       struct thread_options *o = &td->o;
+       struct thread_data *td2;
+       int j;
+
+       memset(ss, 0, sizeof(*ss));
+
+       if (o->ss_dur) {
+               steadystate_enabled = true;
+               o->ss_dur /= 1000000L;
 
+               /* put all steady state info in one place */
+               ss->dur = o->ss_dur;
+               ss->limit = o->ss_limit.u.f;
+               ss->ramp_time = o->ss_ramp_time;
+
+               ss->state = o->ss_state;
+               if (!td->ss.ramp_time)
+                       ss->state |= __FIO_SS_RAMP_OVER;
+
+               ss->sum_x = o->ss_dur * (o->ss_dur - 1) / 2;
+               ss->sum_x_sq = (o->ss_dur - 1) * (o->ss_dur) * (2*o->ss_dur - 1) / 6;
+       }
+
+       /* make sure that ss options are consistent within reporting group */
+       for_each_td(td2, j) {
+               if (td2->groupid == td->groupid) {
+                       struct steadystate_data *ss2 = &td2->ss;
+
+                       if (ss2->dur != ss->dur ||
+                           ss2->limit != ss->limit ||
+                           ss2->ramp_time != ss->ramp_time ||
+                           ss2->state != ss->state ||
+                           ss2->sum_x != ss->sum_x ||
+                           ss2->sum_x_sq != ss->sum_x_sq) {
+                               td_verror(td, EINVAL, "job rejected: steadystate options must be consistent within reporting groups");
+                               return 1;
+                       }
+               }
+       }
+
+       return 0;
+}
+
+uint64_t steadystate_bw_mean(struct thread_stat *ts)
+{
+       int i;
+       uint64_t sum;
+
+       for (i = 0, sum = 0; i < ts->ss_dur; i++)
+               sum += ts->ss_bw_data[i];
+
+       return sum / ts->ss_dur;
+}
+
+uint64_t steadystate_iops_mean(struct thread_stat *ts)
+{
+       int i;
+       uint64_t sum;
+
+       for (i = 0, sum = 0; i < ts->ss_dur; i++)
+               sum += ts->ss_iops_data[i];
+
+       return sum / ts->ss_dur;
+}