userver: userver/utils/statistics/min_max_avg.hpp Source File
Loading...
Searching...
No Matches
min_max_avg.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/statistics/min_max_avg.hpp
4/// @brief @copybrief utils::statistics::MinMaxAvg
5
6#include <atomic>
7#include <chrono>
8#include <cstddef>
9#include <type_traits>
10
11#include <userver/formats/json/inline.hpp>
12#include <userver/formats/serialize/to.hpp>
13#include <userver/utils/assert.hpp>
14#include <userver/utils/statistics/writer.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace utils::statistics {
19
20/// @brief Class for concurrent safe calculation of minimum, maximum and
21/// average over series of values.
22template <typename ValueType, typename AverageType = ValueType>
23class MinMaxAvg final {
24 static_assert(
25 std::is_integral_v<ValueType> && !std::is_same_v<ValueType, bool>,
26 "only integral value types are supported in MinMaxAvg"
27 );
28 static_assert(
29 std::is_same_v<AverageType, ValueType> || std::is_floating_point_v<AverageType>,
30 "MinMaxAvg average type must either be equal to value type or "
31 "be a floating point type"
32 );
33 static_assert(
34 std::atomic<ValueType>::is_always_lock_free && std::atomic<size_t>::is_always_lock_free,
35 "refusing to use locking atomics"
36 );
37
38public:
39 struct Current {
40 ValueType minimum;
41 ValueType maximum;
42 AverageType average;
43 };
44
45 constexpr MinMaxAvg() noexcept : minimum_(0), maximum_(0), sum_(0), count_(0) {}
46
47 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
48 MinMaxAvg(const MinMaxAvg& other) noexcept { *this = other; }
49
50 MinMaxAvg& operator=(const MinMaxAvg& rhs) noexcept {
51 if (this == &rhs) return *this;
52
53 const auto count = rhs.count_.load(std::memory_order_acquire);
54 minimum_ = rhs.minimum_.load(std::memory_order_relaxed);
55 maximum_ = rhs.maximum_.load(std::memory_order_relaxed);
56 sum_ = rhs.sum_.load(std::memory_order_relaxed);
57 count_ = count;
58 return *this;
59 }
60
61 Current GetCurrent() const {
62 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
63 Current current;
64 const auto count = count_.load(std::memory_order_acquire);
65 UASSERT(count >= 0);
66 current.minimum = minimum_.load(std::memory_order_relaxed);
67 current.maximum = maximum_.load(std::memory_order_relaxed);
68 current.average =
69 count ? static_cast<AverageType>(sum_.load(std::memory_order_relaxed)) / static_cast<AverageType>(count)
70 : AverageType{0};
71 return current;
72 }
73
74 void Account(ValueType value) {
75 ValueType current_minimum = minimum_.load(std::memory_order_relaxed);
76 while (current_minimum > value || !count_.load(std::memory_order_relaxed)) {
77 if (minimum_.compare_exchange_weak(current_minimum, value, std::memory_order_relaxed)) {
78 break;
79 }
80 }
81 ValueType current_maximum = maximum_.load(std::memory_order_relaxed);
82 while (current_maximum < value || !count_.load(std::memory_order_relaxed)) {
83 if (maximum_.compare_exchange_weak(current_maximum, value, std::memory_order_relaxed)) {
84 break;
85 }
86 }
87 sum_.fetch_add(value, std::memory_order_relaxed);
88 count_.fetch_add(1, std::memory_order_release);
89 }
90
91 template <class Duration = std::chrono::seconds>
92 void Add(
93 const MinMaxAvg& other,
94 [[maybe_unused]] Duration this_epoch_duration = Duration(),
95 [[maybe_unused]] Duration before_this_epoch_duration = Duration()
96 ) {
97 ValueType current_minimum = minimum_.load(std::memory_order_relaxed);
98 while (current_minimum > other.minimum_.load(std::memory_order_relaxed) ||
99 !count_.load(std::memory_order_relaxed)) {
100 if (minimum_.compare_exchange_weak(
101 current_minimum, other.minimum_.load(std::memory_order_relaxed), std::memory_order_relaxed
102 )) {
103 break;
104 }
105 }
106 ValueType current_maximum = maximum_.load(std::memory_order_relaxed);
107 while (current_maximum < other.maximum_.load(std::memory_order_relaxed) ||
108 !count_.load(std::memory_order_relaxed)) {
109 if (maximum_.compare_exchange_weak(
110 current_maximum, other.maximum_.load(std::memory_order_relaxed), std::memory_order_relaxed
111 )) {
112 break;
113 }
114 }
115 sum_.fetch_add(other.sum_.load(std::memory_order_relaxed), std::memory_order_relaxed);
116 count_.fetch_add(other.count_.load(std::memory_order_acquire), std::memory_order_release);
117 }
118
119 void Reset() {
120 minimum_.store(0, std::memory_order_relaxed);
121 maximum_.store(0, std::memory_order_relaxed);
122 sum_.store(0, std::memory_order_relaxed);
123 count_ = 0;
124 }
125
126 bool IsEmpty() const noexcept { return count_.load() == 0; }
127
128private:
129 std::atomic<ValueType> minimum_;
130 std::atomic<ValueType> maximum_;
131 std::atomic<ValueType> sum_;
132 std::atomic<ssize_t> count_;
133};
134
135template <typename ValueType, typename AverageType>
136auto Serialize(const MinMaxAvg<ValueType, AverageType>& mma, formats::serialize::To<formats::json::Value>) {
137 const auto current = mma.GetCurrent();
138 return formats::json::MakeObject("min", current.minimum, "max", current.maximum, "avg", current.average);
139}
140
141template <typename ValueType, typename AverageType>
142void DumpMetric(Writer& writer, const MinMaxAvg<ValueType, AverageType>& value) {
143 const auto current = value.GetCurrent();
144 writer["min"] = current.minimum;
145 writer["max"] = current.maximum;
146 writer["avg"] = current.average;
147}
148
149} // namespace utils::statistics
150
151USERVER_NAMESPACE_END