From 2aacdcd95feb16bdfdaa2c47662c2dca561bf81e Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Sun, 28 Jan 2018 23:08:50 -0500 Subject: [PATCH] time: add update_monotonic to RateEstimator update_monotonic does not reset the counter if a new count is smaller than earlier counts. Useful when consuming an unsorted stream of eveent counts. Signed-off-by: Zygo Blaxell --- include/crucible/time.h | 3 +++ lib/time.cc | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/include/crucible/time.h b/include/crucible/time.h index c788869..7261013 100644 --- a/include/crucible/time.h +++ b/include/crucible/time.h @@ -81,6 +81,9 @@ namespace crucible { // Write count void update(uint64_t new_count); + // Ignore counts that go backwards + void update_monotonic(uint64_t new_count); + // Read count uint64_t count() const; diff --git a/lib/time.cc b/lib/time.cc index e7b4721..77a99ad 100644 --- a/lib/time.cc +++ b/lib/time.cc @@ -188,6 +188,17 @@ namespace crucible { return update_unlocked(new_count); } + void + RateEstimator::update_monotonic(uint64_t new_count) + { + unique_lock lock(m_mutex); + if (m_last_count == numeric_limits::max() || new_count > m_last_count) { + return update_unlocked(new_count); + } else { + return update_unlocked(m_last_count); + } + } + uint64_t RateEstimator::count() const {