9#include <userver/concurrent/striped_counter.hpp>
10#include <userver/utils/statistics/fwd.hpp>
11#include <userver/utils/statistics/rate.hpp>
13USERVER_NAMESPACE_BEGIN
15namespace utils::statistics {
31class StripedRateCounter
final {
33 using ValueType = Rate;
35 StripedRateCounter() =
default;
37 explicit StripedRateCounter(Rate desired) { Add(desired); }
39 explicit StripedRateCounter(Rate::ValueType desired)
40 : StripedRateCounter(Rate{desired})
43 StripedRateCounter(
const StripedRateCounter& other) { val_.Add(other.LoadImpl()); }
45 StripedRateCounter& operator=(
const StripedRateCounter& other)
noexcept {
50 offset_ = other.LoadImpl() - val_.Read();
54 StripedRateCounter& operator=(Rate desired)
noexcept {
59 void Store(Rate desired)
noexcept { offset_ =
static_cast<std::uintptr_t>(desired.value) - val_.Read(); }
61 Rate Load()
const noexcept {
return Rate{
static_cast<std::uint64_t>(LoadImpl())}; }
63 void Add(Rate arg)
noexcept { val_.Add(
static_cast<std::uintptr_t>(arg.value)); }
65 StripedRateCounter& operator++()
noexcept {
70 StripedRateCounter& operator+=(Rate arg)
noexcept {
75 StripedRateCounter& operator+=(
const StripedRateCounter& arg)
noexcept {
76 val_.Add(arg.LoadImpl());
84 std::uintptr_t LoadImpl()
const noexcept {
return val_.Read() + offset_.load(std::memory_order_relaxed); }
86 USERVER_NAMESPACE::concurrent::StripedCounter val_;
87 std::atomic<std::uintptr_t> offset_{0};
90void DumpMetric(Writer& writer,
const StripedRateCounter& value);
92void ResetMetric(StripedRateCounter& value);