steadystate: clean up checks for when steadystate termination is not engaged
[fio.git] / steadystate.c
index 696ad164ac2fe159d180a7744d9b4d8a6fd0f323..bce35b9718f237512597cba8a7710d890d705280 100644 (file)
@@ -10,11 +10,13 @@ static 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));
+       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)
@@ -33,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) {
@@ -43,7 +45,6 @@ void steadystate_setup(void)
 
                if (prev_groupid != td->groupid) {
                        if (prev_td != NULL) {
-                               prev_td->ss.state |= __FIO_SS_DATA;
                                steadystate_alloc(prev_td);
                        }
                        prev_groupid = td->groupid;
@@ -52,18 +53,17 @@ void steadystate_setup(void)
        }
 
        if (prev_td != NULL && prev_td->o.group_reporting) {
-               prev_td->ss.state |= __FIO_SS_DATA;
                steadystate_alloc(prev_td);
        }
 }
 
-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;
+       uint64_t new_val;
 
        ss->bw_data[ss->tail] = bw;
        ss->iops_data[ss->tail] = iops;
@@ -73,21 +73,21 @@ static bool steadystate_slope(unsigned long iops, unsigned long bw,
        else
                new_val = bw;
 
-       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++) {
+       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;
-                               if (j >= ss->dur)
-                                       j -= ss->dur;
+                               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;
@@ -114,8 +114,9 @@ static bool steadystate_slope(unsigned long iops, unsigned long bw,
 
                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);
+                                       (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)
@@ -129,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;
@@ -141,13 +142,15 @@ 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++)
+       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;
                        if (ss->state & __FIO_SS_IOPS)
@@ -179,8 +182,8 @@ static bool steadystate_deviation(unsigned long iops, unsigned long bw,
 
                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);
+                                       (unsigned long long) ss->sum_y, mean,
+                                       ss->deviation, ss->criterion, ss->limit);
 
                if (ss->criterion < ss->limit)
                        return true;
@@ -199,9 +202,8 @@ 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;
@@ -209,7 +211,8 @@ void steadystate_check(void)
                struct steadystate_data *ss = &td->ss;
 
                if (!ss->dur || td->runstate <= TD_SETTING_UP ||
-                   td->runstate >= TD_EXITED || (ss->state & __FIO_SS_ATTAINED))
+                   td->runstate >= TD_EXITED || !ss->state ||
+                   ss->state & __FIO_SS_ATTAINED)
                        continue;
 
                td_iops = 0;
@@ -269,9 +272,11 @@ void steadystate_check(void)
 
                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);
+                                       "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 (td->o.ss & __FIO_SS_SLOPE)
                        ret = steadystate_slope(group_iops, group_bw, td);
@@ -318,8 +323,6 @@ int td_steadystate_init(struct thread_data *td)
 
                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;
-
-               td->ts.ss = ss;
        }
 
        /* make sure that ss options are consistent within reporting group */
@@ -341,3 +344,25 @@ int td_steadystate_init(struct thread_data *td)
 
        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;
+}