From 4c434f3fa4d6792baddbd558b9609c036b5346ff Mon Sep 17 00:00:00 2001 From: Eliran Sinvani Date: Mon, 2 Nov 2020 09:29:49 +0200 Subject: [PATCH] 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 --- utils/histogram.hh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/utils/histogram.hh b/utils/histogram.hh index 3828cbd619..30444cdf35 100644 --- a/utils/histogram.hh +++ b/utils/histogram.hh @@ -256,9 +256,15 @@ public: rate_moving_average rate() const { rate_moving_average res; - if (_count > 0) { - double elapsed = std::chrono::duration_cast(latency_counter::now() - start_time).count(); + double elapsed = std::chrono::duration_cast(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++) {