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) : StripedRateCounter(
Rate{desired}) {}
41 StripedRateCounter(
const StripedRateCounter& other) { val_.Add(other.LoadImpl()); }
43 StripedRateCounter& operator=(
const StripedRateCounter& other)
noexcept {
44 if (
this == &other)
return *
this;
46 offset_ = other.LoadImpl() - val_.Read();
50 StripedRateCounter& operator=(
Rate desired)
noexcept {
55 void Store(
Rate desired)
noexcept { offset_ =
static_cast<std::uintptr_t>(desired.value) - val_.Read(); }
57 Rate Load()
const noexcept {
return Rate{
static_cast<std::uint64_t>(LoadImpl())}; }
59 void Add(
Rate arg)
noexcept { val_.Add(
static_cast<std::uintptr_t>(arg.value)); }
61 StripedRateCounter& operator++()
noexcept {
66 StripedRateCounter& operator+=(
Rate arg)
noexcept {
71 StripedRateCounter& operator+=(
const StripedRateCounter& arg)
noexcept {
72 val_.Add(arg.LoadImpl());
80 std::uintptr_t LoadImpl()
const noexcept {
return val_.Read() + offset_.load(std::memory_order_relaxed); }
82 USERVER_NAMESPACE::
concurrent::StripedCounter val_;
83 std::atomic<std::uintptr_t> offset_{0};
86void DumpMetric(Writer& writer,
const StripedRateCounter& value);
88void ResetMetric(StripedRateCounter& value);