7#include <userver/utils/datetime.hpp>
8#include <userver/utils/fixed_array.hpp>
9#include <userver/utils/statistics/fwd.hpp>
10#include <userver/utils/statistics/recentperiod_detail.hpp>
12USERVER_NAMESPACE_BEGIN
14namespace utils::statistics {
17
18
19
20
21
22
23template <
typename Counter,
typename Result,
27 using Duration =
typename Timer::duration;
30 (detail::kResultWantsAddFunction<Result, Counter, Duration> ||
31 detail::kResultCanUseAddAssign<Result, Counter>),
32 "The Result template type argument must provide either Add(Counter, "
33 "Duration, Duration) function or add assignment operator");
35 static constexpr bool kUseAddFunction =
36 detail::kResultWantsAddFunction<Result, Counter, Duration>;
39
40
41
42
50 Counter& GetCurrentCounter() {
return items_[GetCurrentIndex()].counter; }
52 Counter& GetPreviousCounter(
int epochs_ago) {
53 return items_[GetPreviousIndex(epochs_ago)].counter;
57
58
59
60
61
62
63
64
65
68 bool with_current_epoch =
false)
const {
69 if (duration == Duration::min()) {
70 duration = max_duration_;
74 Duration now = Timer::now().time_since_epoch();
75 Duration current_epoch = GetEpochForDuration(now);
76 Duration start_epoch = current_epoch - duration;
77 Duration first_epoch_duration = now - current_epoch;
78 std::size_t index = epoch_index_.load();
80 for (std::size_t i = 0; i < items_.size();
81 i++, index = (index + items_.size() - 1) % items_.size()) {
82 Duration epoch = items_[index].epoch;
84 if (epoch > current_epoch)
continue;
85 if (epoch == current_epoch && !with_current_epoch)
continue;
86 if (epoch < start_epoch)
break;
88 if constexpr (kUseAddFunction) {
89 Duration this_epoch_duration =
90 (i == 0) ? first_epoch_duration : epoch_duration_;
92 Duration before_this_epoch_duration = epoch - start_epoch;
93 result.Add(items_[index].counter, this_epoch_duration,
94 before_this_epoch_duration);
96 result += items_[index].counter;
103 Duration GetEpochDuration()
const {
return epoch_duration_; }
105 Duration GetMaxDuration()
const {
return max_duration_; }
107 void UpdateEpochIfOld() { [[maybe_unused]]
auto ignore = GetCurrentIndex(); }
110 for (
auto& item : items_) {
116 size_t GetCurrentIndex()
const {
118 Duration now = Timer::now().time_since_epoch();
119 Duration epoch = GetEpochForDuration(now);
120 std::size_t index = epoch_index_.load();
121 Duration bucket_epoch = items_[index].epoch.load();
123 if (epoch != bucket_epoch) {
124 std::size_t new_index = (index + 1) % items_.size();
126 if (epoch_index_.compare_exchange_weak(index, new_index)) {
127 items_[new_index].epoch = epoch;
128 items_[(new_index + 1) % items_.size()].Reset();
137 std::size_t GetPreviousIndex(
int epochs_ago) {
138 int index =
static_cast<
int>(GetCurrentIndex()) - epochs_ago;
139 while (index < 0) index += items_.size();
140 return index % items_.size();
143 Duration GetEpochForDuration(Duration duration)
const {
144 auto now = std::chrono::duration_cast<Duration>(duration);
145 return now - now % epoch_duration_;
148 static std::size_t GetSizeForDuration(Duration epoch_duration,
149 Duration max_duration) {
151
152 return max_duration.count() / epoch_duration.count() + 3;
156 static constexpr bool kUseReset = detail::kCanReset<Counter>;
157 std::atomic<Duration> epoch;
160 EpochBucket() { Reset(); }
163 epoch = Duration::min();
164 if constexpr (kUseReset) {
172 const Duration epoch_duration_;
173 const Duration max_duration_;
174 mutable std::atomic_size_t epoch_index_;
175 mutable utils::FixedArray<EpochBucket> items_;
179template <
typename Counter,
typename Result,
typename Timer>
180void DumpMetric(Writer& writer,
181 const RecentPeriod<Counter, Result, Timer>& recent_period) {
182 writer = recent_period.GetStatsForPeriod();
186template <
typename Counter,
typename Result,
typename Timer>
187void ResetMetric(
RecentPeriod<Counter, Result, Timer>& recent_period) {
188 recent_period.Reset();