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>
16USERVER_NAMESPACE_BEGIN
18namespace utils::statistics {
22template <
typename ValueType,
typename AverageType = ValueType>
23class MinMaxAvg final {
25 std::is_integral_v<ValueType> && !std::is_same_v<ValueType,
bool>,
26 "only integral value types are supported in MinMaxAvg"
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"
34 std::atomic<ValueType>::is_always_lock_free && std::atomic<size_t>::is_always_lock_free,
35 "refusing to use locking atomics"
45 constexpr MinMaxAvg()
noexcept : minimum_(0), maximum_(0), sum_(0), count_(0) {}
48 MinMaxAvg(
const MinMaxAvg& other)
noexcept { *
this = other; }
50 MinMaxAvg& operator=(
const MinMaxAvg& rhs)
noexcept {
51 if (
this == &rhs)
return *
this;
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);
64 const auto count = count_.load(std::memory_order_acquire);
66 current.minimum = minimum_.load(std::memory_order_relaxed);
67 current.maximum = maximum_.load(std::memory_order_relaxed);
69 count ?
static_cast<AverageType>(sum_.load(std::memory_order_relaxed)) /
static_cast<AverageType>(count)
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)) {
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)) {
87 sum_.fetch_add(value, std::memory_order_relaxed);
88 count_.fetch_add(1, std::memory_order_release);
91 template <
class Duration = std::chrono::seconds>
93 const MinMaxAvg& other,
94 [[maybe_unused]] Duration this_epoch_duration = Duration(),
95 [[maybe_unused]] Duration before_this_epoch_duration = Duration()
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
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
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);
120 minimum_.store(0, std::memory_order_relaxed);
121 maximum_.store(0, std::memory_order_relaxed);
122 sum_.store(0, std::memory_order_relaxed);
126 bool IsEmpty()
const noexcept {
return count_.load() == 0; }
129 std::atomic<ValueType> minimum_;
130 std::atomic<ValueType> maximum_;
131 std::atomic<ValueType> sum_;
132 std::atomic<ssize_t> count_;
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);
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;