blob: bb506aa7f4fc37b37b7dcd5bef946c0d243ce216 [file] [log] [blame]
sprang@webrtc.org37968a92013-12-03 10:31:59 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/rate_statistics.h"
sprang@webrtc.org37968a92013-12-03 10:31:59 +000012
Stefan Holmerfb8fc532016-04-22 15:48:23 +020013#include <algorithm>
14
Sergey Silkin40b70502018-08-27 10:55:07 +020015#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
henrike@webrtc.orgf2aafe42014-04-29 17:54:17 +000017
sprang@webrtc.org37968a92013-12-03 10:31:59 +000018namespace webrtc {
19
Erik Språng51e60302016-06-10 22:13:21 +020020RateStatistics::RateStatistics(int64_t window_size_ms, float scale)
21 : buckets_(new Bucket[window_size_ms]()),
sprang@webrtc.org37968a92013-12-03 10:31:59 +000022 accumulated_count_(0),
Erik Språng51e60302016-06-10 22:13:21 +020023 num_samples_(0),
24 oldest_time_(-window_size_ms),
sprang@webrtc.org37968a92013-12-03 10:31:59 +000025 oldest_index_(0),
Erik Språng51e60302016-06-10 22:13:21 +020026 scale_(scale),
27 max_window_size_ms_(window_size_ms),
28 current_window_size_ms_(max_window_size_ms_) {}
sprang@webrtc.org37968a92013-12-03 10:31:59 +000029
Sergey Silkin40b70502018-08-27 10:55:07 +020030RateStatistics::RateStatistics(const RateStatistics& other)
31 : accumulated_count_(other.accumulated_count_),
32 num_samples_(other.num_samples_),
33 oldest_time_(other.oldest_time_),
34 oldest_index_(other.oldest_index_),
35 scale_(other.scale_),
36 max_window_size_ms_(other.max_window_size_ms_),
37 current_window_size_ms_(other.current_window_size_ms_) {
38 buckets_ = absl::make_unique<Bucket[]>(other.max_window_size_ms_);
39 std::copy(other.buckets_.get(),
40 other.buckets_.get() + other.max_window_size_ms_, buckets_.get());
41}
42
43RateStatistics::RateStatistics(RateStatistics&& other) = default;
44
tkchinf75d0082016-02-23 22:49:42 -080045RateStatistics::~RateStatistics() {}
sprang@webrtc.org37968a92013-12-03 10:31:59 +000046
47void RateStatistics::Reset() {
48 accumulated_count_ = 0;
Erik Språng51e60302016-06-10 22:13:21 +020049 num_samples_ = 0;
50 oldest_time_ = -max_window_size_ms_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000051 oldest_index_ = 0;
Erik Språng51e60302016-06-10 22:13:21 +020052 current_window_size_ms_ = max_window_size_ms_;
53 for (int64_t i = 0; i < max_window_size_ms_; i++)
54 buckets_[i] = Bucket();
sprang@webrtc.org37968a92013-12-03 10:31:59 +000055}
56
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000057void RateStatistics::Update(size_t count, int64_t now_ms) {
sprang@webrtc.org37968a92013-12-03 10:31:59 +000058 if (now_ms < oldest_time_) {
59 // Too old data is ignored.
60 return;
61 }
62
63 EraseOld(now_ms);
64
Erik Språng51e60302016-06-10 22:13:21 +020065 // First ever sample, reset window to start now.
66 if (!IsInitialized())
67 oldest_time_ = now_ms;
68
69 uint32_t now_offset = static_cast<uint32_t>(now_ms - oldest_time_);
70 RTC_DCHECK_LT(now_offset, max_window_size_ms_);
71 uint32_t index = oldest_index_ + now_offset;
72 if (index >= max_window_size_ms_)
73 index -= max_window_size_ms_;
74 buckets_[index].sum += count;
75 ++buckets_[index].samples;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000076 accumulated_count_ += count;
Erik Språng51e60302016-06-10 22:13:21 +020077 ++num_samples_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000078}
79
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020080absl::optional<uint32_t> RateStatistics::Rate(int64_t now_ms) const {
sprangcd349d92016-07-13 09:11:28 -070081 // Yeah, this const_cast ain't pretty, but the alternative is to declare most
82 // of the members as mutable...
83 const_cast<RateStatistics*>(this)->EraseOld(now_ms);
Erik Språng51e60302016-06-10 22:13:21 +020084
85 // If window is a single bucket or there is only one sample in a data set that
86 // has not grown to the full window size, treat this as rate unavailable.
87 int64_t active_window_size = now_ms - oldest_time_ + 1;
88 if (num_samples_ == 0 || active_window_size <= 1 ||
89 (num_samples_ <= 1 && active_window_size < current_window_size_ms_)) {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020090 return absl::nullopt;
Erik Språng51e60302016-06-10 22:13:21 +020091 }
92
93 float scale = scale_ / active_window_size;
Oskar Sundbomf8200032017-11-16 10:57:26 +010094 return static_cast<uint32_t>(accumulated_count_ * scale + 0.5f);
sprang@webrtc.org37968a92013-12-03 10:31:59 +000095}
96
97void RateStatistics::EraseOld(int64_t now_ms) {
Erik Språng51e60302016-06-10 22:13:21 +020098 if (!IsInitialized())
sprang@webrtc.org37968a92013-12-03 10:31:59 +000099 return;
Erik Språng51e60302016-06-10 22:13:21 +0200100
101 // New oldest time that is included in data set.
102 int64_t new_oldest_time = now_ms - current_window_size_ms_ + 1;
103
104 // New oldest time is older than the current one, no need to cull data.
105 if (new_oldest_time <= oldest_time_)
106 return;
107
108 // Loop over buckets and remove too old data points.
109 while (num_samples_ > 0 && oldest_time_ < new_oldest_time) {
110 const Bucket& oldest_bucket = buckets_[oldest_index_];
111 RTC_DCHECK_GE(accumulated_count_, oldest_bucket.sum);
112 RTC_DCHECK_GE(num_samples_, oldest_bucket.samples);
113 accumulated_count_ -= oldest_bucket.sum;
114 num_samples_ -= oldest_bucket.samples;
115 buckets_[oldest_index_] = Bucket();
116 if (++oldest_index_ >= max_window_size_ms_)
sprang@webrtc.org37968a92013-12-03 10:31:59 +0000117 oldest_index_ = 0;
sprang@webrtc.org37968a92013-12-03 10:31:59 +0000118 ++oldest_time_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +0000119 }
120 oldest_time_ = new_oldest_time;
121}
122
Erik Språng51e60302016-06-10 22:13:21 +0200123bool RateStatistics::SetWindowSize(int64_t window_size_ms, int64_t now_ms) {
124 if (window_size_ms <= 0 || window_size_ms > max_window_size_ms_)
125 return false;
126
127 current_window_size_ms_ = window_size_ms;
128 EraseOld(now_ms);
129 return true;
130}
131
sprangcd349d92016-07-13 09:11:28 -0700132bool RateStatistics::IsInitialized() const {
Erik Språng51e60302016-06-10 22:13:21 +0200133 return oldest_time_ != -max_window_size_ms_;
134}
135
sprang@webrtc.org37968a92013-12-03 10:31:59 +0000136} // namespace webrtc