chenBright commented on code in PR #3561:
URL: https://github.com/apache/brpc/pull/3561#discussion_r4073801679
##########
src/bvar/histogram.h:
##########
@@ -219,6 +259,101 @@ namespace detail {
template <>
struct HasPlottableSeries<Histogram::Value> : butil::false_type {};
+#if WITH_BABYLON_COUNTER
+
+// One thread's slice of a Histogram.
+//
+// Only the thread owning the slot writes it, so the counters are updated with
+// a relaxed load plus a relaxed store rather than an atomic read-modify-write.
+// They are atomic all the same because the sampling thread reads them while
+// they are being written, which the seqlock allows but does not by itself make
+// race free. The seqlock is what keeps the buckets, the sum and the count of
one
+// slot mutually consistent.
+class HistogramSlot {
+public:
+ HistogramSlot() {
+ for (size_t i = 0; i < MAX_HISTOGRAM_BUCKETS; ++i) {
+ _counts[i].store(0, butil::memory_order_relaxed);
+ }
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(HistogramSlot);
+
+ void add(size_t bucket_index, double value) {
+ _seqlock.store([&] {
+ relaxed_add(&_counts[bucket_index], (uint64_t)1);
+ relaxed_add(&_sum, value);
+ relaxed_add(&_num, (int64_t)1);
+ });
+ }
+
+ Histogram::Value load(size_t num_buckets) const {
+ return _seqlock.load([&] {
+ Histogram::Value v(num_buckets);
+ for (size_t i = 0; i < num_buckets; ++i) {
+ v.counts[i] = _counts[i].load(butil::memory_order_relaxed);
+ }
+ v.sum = _sum.load(butil::memory_order_relaxed);
+ v.num = _num.load(butil::memory_order_relaxed);
+ return v;
+ });
+ }
+
+private:
+ template <typename T, typename U>
+ static void relaxed_add(butil::atomic<T>* target, U delta) {
Review Comment:
`load + store` can achieve better performance than `fetch_add`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]