moving avarage rate: Keep computed rates in zero until they are

meaningful

When computing moving average rates too early after startup, the
rate can be infinite, this is simply because the sample interval
since the system started is too small to generate meaningful results.
Here we check for this situation and keep the rate at 0 if it happens
to signal that there are still no meaningful results.
This incident is unlikely to happen since it can happen only during a
very small time window after restart, so we add a hint to the compiler
to optimize for that in order to have a minimum impact on the normal
usecase.

Fixes #4469
This commit is contained in:
Eliran Sinvani
2020-11-02 09:29:49 +02:00
committed by Avi Kivity
parent 8aa842614a
commit 4c434f3fa4

View File

@@ -256,9 +256,15 @@ public:
rate_moving_average rate() const {
rate_moving_average res;
if (_count > 0) {
double elapsed = std::chrono::duration_cast<std::chrono::seconds>(latency_counter::now() - start_time).count();
double elapsed = std::chrono::duration_cast<std::chrono::seconds>(latency_counter::now() - start_time).count();
// We condition also in elapsed because it can happen that the call
// for the rate calculation was performed too early and will not yield
// meaningful results (i.e mean_rate is infinity) so the best thing is
// to return 0 as it best reflects the state.
if ((_count > 0) && (elapsed >= 1.0)) [[likely]] {
res.mean_rate = (_count / elapsed);
} else {
res.mean_rate = 0;
}
res.count = _count;
for (int i = 0; i < 3; i++) {