From 5de98f2d50fec1b5b88a973e825b1156fb03a943 Mon Sep 17 00:00:00 2001 From: Andrew Kryczka Date: Fri, 11 Aug 2017 13:09:38 -0700 Subject: [PATCH] approximate histogram stats to save cpu Summary: sounds like we're willing to tradeoff minor inaccuracy in stats for speed. start with histogram stats. ticker stats will be harder (and, IMO, we shouldn't change them in this manner) as many test cases rely on them being exactly correct. Closes https://github.com/facebook/rocksdb/pull/2720 Differential Revision: D5607884 Pulled By: ajkr fbshipit-source-id: 1b754cda35ea6b252d1fdd5aa3cfb58866506372 --- monitoring/histogram.cc | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/monitoring/histogram.cc b/monitoring/histogram.cc index 56b5a3914..083ef75fd 100644 --- a/monitoring/histogram.cc +++ b/monitoring/histogram.cc @@ -105,17 +105,26 @@ void HistogramStat::Add(uint64_t value) { // by concurrent threads is tolerable. const size_t index = bucketMapper.IndexForValue(value); assert(index < num_buckets_); - buckets_[index].fetch_add(1, std::memory_order_relaxed); + buckets_[index].store(buckets_[index].load(std::memory_order_relaxed) + 1, + std::memory_order_relaxed); uint64_t old_min = min(); - while (value < old_min && !min_.compare_exchange_weak(old_min, value)) {} + if (value < old_min) { + min_.store(value, std::memory_order_relaxed); + } uint64_t old_max = max(); - while (value > old_max && !max_.compare_exchange_weak(old_max, value)) {} + if (value > old_max) { + max_.store(value, std::memory_order_relaxed); + } - num_.fetch_add(1, std::memory_order_relaxed); - sum_.fetch_add(value, std::memory_order_relaxed); - sum_squares_.fetch_add(value * value, std::memory_order_relaxed); + num_.store(num_.load(std::memory_order_relaxed) + 1, + std::memory_order_relaxed); + sum_.store(sum_.load(std::memory_order_relaxed) + value, + std::memory_order_relaxed); + sum_squares_.store( + sum_squares_.load(std::memory_order_relaxed) + value * value, + std::memory_order_relaxed); } void HistogramStat::Merge(const HistogramStat& other) {