diff --git a/timing.cc b/timing.cc index 2df4bc20a..410148bf6 100644 --- a/timing.cc +++ b/timing.cc @@ -302,6 +302,8 @@ RateMeter::RateMeter(double current_rate_history) { stop_tv.tv_usec = 0; last_update_tv.tv_sec = 0; last_update_tv.tv_usec = 0; + history_threshold.tv_sec = 0; + history_threshold.tv_usec = 0; total = 0.0; current_rate = 0.0; assert(!isSet(&start_tv)); @@ -315,6 +317,10 @@ void RateMeter::start(const struct timeval *now) { gettimeofday(&start_tv, NULL); else start_tv = *now; + /* Find the time current_rate_history seconds after the start. That's our + threshold for deciding how far back to look for moving average. */ + TIMEVAL_ADD(history_threshold, start_tv, + (time_t) (current_rate_history * 1000000.0)); } void RateMeter::stop(const struct timeval *now) { @@ -369,11 +375,7 @@ void RateMeter::update(double amount, const struct timeval *now) { /* Find out how far back in time to look. We want to look back current_rate_history seconds, or to when the last update occurred, whichever is longer. However, we never look past the start. */ - struct timeval tmp; - /* Find the time current_rate_history seconds after the start. That's our - threshold for deciding how far back to look. */ - TIMEVAL_ADD(tmp, start_tv, (time_t) (current_rate_history * 1000000.0)); - if (TIMEVAL_AFTER(*now, tmp)) + if (TIMEVAL_AFTER(*now, history_threshold)) interval = MAX(current_rate_history, diff); else interval = TIMEVAL_FSEC_SUBTRACT(*now, start_tv); diff --git a/timing.h b/timing.h index 2f2cf187e..285165a67 100644 --- a/timing.h +++ b/timing.h @@ -183,6 +183,8 @@ class RateMeter { struct timeval stop_tv; /* The last time the current sample rates were updated. */ struct timeval last_update_tv; + /* The time current_rate_history after start_tv. */ + struct timeval history_threshold; double total; double current_rate;