From 297d54eb95f9dfe9491c81d1a6569d2344edddd2 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Fri, 24 Jan 2014 14:56:22 -0800 Subject: [PATCH] histogram: add decay Signed-off-by: Sage Weil --- src/common/histogram.cc | 8 ++++++++ src/common/histogram.h | 3 +++ src/test/common/histogram.cc | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/common/histogram.cc b/src/common/histogram.cc index a9431d93e13..e1422923956 100644 --- a/src/common/histogram.cc +++ b/src/common/histogram.cc @@ -48,3 +48,11 @@ void pow2_hist_t::generate_test_instances(std::list& ls) ls.back()->h.push_back(0); ls.back()->h.push_back(2); } + +void pow2_hist_t::decay(int bits) +{ + for (std::vector::iterator p = h.begin(); p != h.end(); ++p) { + *p >>= bits; + } + _contract(); +} diff --git a/src/common/histogram.h b/src/common/histogram.h index 1497bc98846..ceca6d90053 100644 --- a/src/common/histogram.h +++ b/src/common/histogram.h @@ -116,6 +116,9 @@ public: return 1 << h.size(); } + /// decay histogram by N bits (default 1, for a halflife) + void decay(int bits = 1); + void dump(Formatter *f) const; void encode(bufferlist &bl) const; void decode(bufferlist::iterator &bl); diff --git a/src/test/common/histogram.cc b/src/test/common/histogram.cc index c07d6c26dba..6cbf648534d 100644 --- a/src/test/common/histogram.cc +++ b/src/test/common/histogram.cc @@ -93,3 +93,14 @@ TEST(Histogram, Position) { ASSERT_EQ(500000, ub); } } + +TEST(Histogram, Decay) { + pow2_hist_t h; + h.set_bin(0, 123); + h.set_bin(3, 12); + h.set_bin(5, 1); + h.decay(1); + ASSERT_EQ(61, h.h[0]); + ASSERT_EQ(6, h.h[3]); + ASSERT_EQ(4u, h.h.size()); +} -- 2.47.3