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}) {}
42 StripedRateCounter(
const StripedRateCounter& other) {
43 val_.Add(other.LoadImpl());
46 StripedRateCounter& operator=(
const StripedRateCounter& other)
noexcept {
47 if (
this == &other)
return *
this;
49 offset_ = other.LoadImpl() - val_.Read();
53 StripedRateCounter& operator=(
Rate desired)
noexcept {
58 void Store(
Rate desired)
noexcept {
59 offset_ =
static_cast<std::uintptr_t>(desired.value) - val_.Read();
62 Rate Load()
const noexcept {
63 return Rate{
static_cast<std::uint64_t>(LoadImpl())};
66 void Add(
Rate arg)
noexcept {
67 val_.Add(
static_cast<std::uintptr_t>(arg.value));
70 StripedRateCounter& operator++()
noexcept {
75 StripedRateCounter& operator+=(
Rate arg)
noexcept {
80 StripedRateCounter& operator+=(
const StripedRateCounter& arg)
noexcept {
81 val_.Add(arg.LoadImpl());
89 std::uintptr_t LoadImpl()
const noexcept {
90 return val_.Read() + offset_.load(std::memory_order_relaxed);
94 std::atomic<std::uintptr_t> offset_{0};
97void DumpMetric(Writer& writer,
const StripedRateCounter& value);
99void ResetMetric(StripedRateCounter& value);