30 using Duration =
typename Timer::duration;
33 (detail::ResultWantsAddFunction<Result, Counter, Duration> || detail::ResultCanUseAddAssign<Result, Counter>),
34 "The Result template type argument must provide either Add(Counter, "
35 "Duration, Duration) function or add assignment operator"
38 static constexpr bool kUseAddFunction = detail::ResultWantsAddFunction<Result, Counter, Duration>;
41
42
43
44
45 RecentPeriod(Duration epoch_duration = std::chrono::seconds(5), Duration max_duration = std::chrono::seconds(60))
46 : epoch_duration_(epoch_duration),
47 max_duration_(max_duration),
49 items_(GetSizeForDuration(epoch_duration, max_duration))
51 UINVARIANT(Duration::zero() < epoch_duration_,
"epoch_duration should be greater than 0");
54 Counter& GetCurrentCounter() {
return items_[GetCurrentIndex()].counter; }
56 Counter& GetPreviousCounter(
int epochs_ago) {
return items_[GetPreviousIndex(epochs_ago)].counter; }
59
60
61
62
63
64
65
66
67
69 const Result
GetStatsForPeriod(Duration duration = Duration::min(),
bool with_current_epoch =
false)
const {
70 if (duration == Duration::min()) {
71 duration = max_duration_;
75 const Duration now = Timer::now().time_since_epoch();
76 const Duration current_epoch = GetEpochForDuration(now);
77 const Duration start_epoch = current_epoch - duration;
78 const Duration first_epoch_duration = now - current_epoch;
79 std::size_t index = epoch_index_.load();
81 for (std::size_t i = 0; i < items_.size(); i++, index = (index + items_.size() - 1) % items_.size()) {
82 const Duration epoch = items_[index].epoch;
84 if (epoch > current_epoch) {
87 if (epoch == current_epoch && !with_current_epoch) {
90 if (epoch < start_epoch) {
94 if constexpr (kUseAddFunction) {
95 const Duration this_epoch_duration = (i == 0) ? first_epoch_duration : epoch_duration_;
97 const Duration before_this_epoch_duration = epoch - start_epoch;
98 result.Add(items_[index].counter, this_epoch_duration, before_this_epoch_duration);
100 result += items_[index].counter;
107 Duration GetEpochDuration()
const {
return epoch_duration_; }
109 Duration GetMaxDuration()
const {
return max_duration_; }
111 void UpdateEpochIfOld() { [[maybe_unused]]
auto ignore = GetCurrentIndex(); }
114 for (
auto& item : items_) {
115 item.Reset(Duration::min());
120 size_t GetCurrentIndex()
const {
122 const Duration now = Timer::now().time_since_epoch();
123 const Duration epoch = GetEpochForDuration(now);
124 std::size_t index = epoch_index_.load();
125 const Duration bucket_epoch = items_[index].epoch.load();
129 if (epoch > bucket_epoch || epoch + max_duration_ < bucket_epoch) {
130 const std::size_t new_index = (index + 1) % items_.size();
132 if (epoch_index_.compare_exchange_weak(index, new_index)) {
133 items_[new_index].epoch = epoch;
134 items_[(new_index + 1) % items_.size()].Reset(epoch + epoch_duration_);
143 std::size_t GetPreviousIndex(
int epochs_ago) {
144 int index =
static_cast<
int>(GetCurrentIndex()) - epochs_ago;
146 index += items_.size();
148 return index % items_.size();
151 Duration GetEpochForDuration(Duration duration)
const {
152 auto now = std::chrono::duration_cast<Duration>(duration);
153 return now - now % epoch_duration_;
156 static std::size_t GetSizeForDuration(Duration epoch_duration, Duration max_duration) {
158
159 return max_duration.count() / epoch_duration.count() + 3;
163 static constexpr bool kUseReset = detail::CanReset<Counter>;
164 std::atomic<Duration> epoch;
167 EpochBucket() { Reset(Duration::min()); }
169 void Reset(Duration epoch_duration) {
170 epoch = epoch_duration;
171 if constexpr (kUseReset) {
179 const Duration epoch_duration_;
180 const Duration max_duration_;
181 mutable std::atomic_size_t epoch_index_;
182 mutable utils::FixedArray<EpochBucket> items_;