- /*
- * Position at which the busy sample is considered to be taken.
- * (Allows to quickly skew our average without editing much code)
- */
- enum BusySamplePosition {
- RIGHT = 0, // busy sample placed at the end of the iteration
- CENTER = 1, // busy sample placed at the middle point of the iteration
- LEFT = 2, // busy sample placed at the beginning of the iteration
- };
-
- // See http://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
- // and D676020 for more info on this calculation.
- VLOG(11) << "idle " << idle.count() << " oldBusyLeftover_ "
- << oldBusyLeftover_.count() << " idle + oldBusyLeftover_ "
- << (idle + oldBusyLeftover_).count() << " busy " << busy.count()
- << " " << __PRETTY_FUNCTION__;
- idle += oldBusyLeftover_ + busy;
- oldBusyLeftover_ = (busy * BusySamplePosition::CENTER) / 2;
- idle -= oldBusyLeftover_;
-
- double coeff = exp(idle.count() * expCoeff_);
- value_ *= coeff;
- value_ += (1.0 - coeff) * busy.count();
+ if ((buffer_time_ + total) > buffer_interval_ && buffer_cnt_ > 0) {
+ // See https://en.wikipedia.org/wiki/Exponential_smoothing for
+ // more info on this calculation.
+ double coeff = exp(buffer_time_.count() * expCoeff_);
+ value_ =
+ value_ * coeff + (1.0 - coeff) * (busy_buffer_.count() / buffer_cnt_);
+ buffer_time_ = std::chrono::microseconds{0};
+ busy_buffer_ = std::chrono::microseconds{0};
+ buffer_cnt_ = 0;
+ }
+ buffer_time_ += total;
+ busy_buffer_ += busy;
+ buffer_cnt_++;