blob: 1fd63cc6d2a071251edb0a9f4f3744fd700571f2 [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
tkchinf75d0082016-02-23 22:49:42 -080011#include "webrtc/base/rate_statistics.h"
sprang@webrtc.org37968a92013-12-03 10:31:59 +000012
Stefan Holmerfb8fc532016-04-22 15:48:23 +020013#include <algorithm>
14
15#include "webrtc/base/checks.h"
henrike@webrtc.orgf2aafe42014-04-29 17:54:17 +000016
sprang@webrtc.org37968a92013-12-03 10:31:59 +000017namespace webrtc {
18
Erik Språng51e60302016-06-10 22:13:21 +020019RateStatistics::RateStatistics(int64_t window_size_ms, float scale)
20 : buckets_(new Bucket[window_size_ms]()),
sprang@webrtc.org37968a92013-12-03 10:31:59 +000021 accumulated_count_(0),
Erik Språng51e60302016-06-10 22:13:21 +020022 num_samples_(0),
23 oldest_time_(-window_size_ms),
sprang@webrtc.org37968a92013-12-03 10:31:59 +000024 oldest_index_(0),
Erik Språng51e60302016-06-10 22:13:21 +020025 scale_(scale),
26 max_window_size_ms_(window_size_ms),
27 current_window_size_ms_(max_window_size_ms_) {}
sprang@webrtc.org37968a92013-12-03 10:31:59 +000028
tkchinf75d0082016-02-23 22:49:42 -080029RateStatistics::~RateStatistics() {}
sprang@webrtc.org37968a92013-12-03 10:31:59 +000030
31void RateStatistics::Reset() {
32 accumulated_count_ = 0;
Erik Språng51e60302016-06-10 22:13:21 +020033 num_samples_ = 0;
34 oldest_time_ = -max_window_size_ms_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000035 oldest_index_ = 0;
Erik Språng51e60302016-06-10 22:13:21 +020036 current_window_size_ms_ = max_window_size_ms_;
37 for (int64_t i = 0; i < max_window_size_ms_; i++)
38 buckets_[i] = Bucket();
sprang@webrtc.org37968a92013-12-03 10:31:59 +000039}
40
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000041void RateStatistics::Update(size_t count, int64_t now_ms) {
sprang@webrtc.org37968a92013-12-03 10:31:59 +000042 if (now_ms < oldest_time_) {
43 // Too old data is ignored.
44 return;
45 }
46
47 EraseOld(now_ms);
48
Erik Språng51e60302016-06-10 22:13:21 +020049 // First ever sample, reset window to start now.
50 if (!IsInitialized())
51 oldest_time_ = now_ms;
52
53 uint32_t now_offset = static_cast<uint32_t>(now_ms - oldest_time_);
54 RTC_DCHECK_LT(now_offset, max_window_size_ms_);
55 uint32_t index = oldest_index_ + now_offset;
56 if (index >= max_window_size_ms_)
57 index -= max_window_size_ms_;
58 buckets_[index].sum += count;
59 ++buckets_[index].samples;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000060 accumulated_count_ += count;
Erik Språng51e60302016-06-10 22:13:21 +020061 ++num_samples_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000062}
63
aluebsa49f1102016-07-08 11:01:59 -070064rtc::Optional<uint32_t> RateStatistics::Rate(int64_t now_ms) {
65 EraseOld(now_ms);
Erik Språng51e60302016-06-10 22:13:21 +020066
67 // If window is a single bucket or there is only one sample in a data set that
68 // has not grown to the full window size, treat this as rate unavailable.
69 int64_t active_window_size = now_ms - oldest_time_ + 1;
70 if (num_samples_ == 0 || active_window_size <= 1 ||
71 (num_samples_ <= 1 && active_window_size < current_window_size_ms_)) {
72 return rtc::Optional<uint32_t>();
73 }
74
75 float scale = scale_ / active_window_size;
76 return rtc::Optional<uint32_t>(
77 static_cast<uint32_t>(accumulated_count_ * scale + 0.5f));
sprang@webrtc.org37968a92013-12-03 10:31:59 +000078}
79
80void RateStatistics::EraseOld(int64_t now_ms) {
Erik Språng51e60302016-06-10 22:13:21 +020081 if (!IsInitialized())
sprang@webrtc.org37968a92013-12-03 10:31:59 +000082 return;
Erik Språng51e60302016-06-10 22:13:21 +020083
84 // New oldest time that is included in data set.
85 int64_t new_oldest_time = now_ms - current_window_size_ms_ + 1;
86
87 // New oldest time is older than the current one, no need to cull data.
88 if (new_oldest_time <= oldest_time_)
89 return;
90
91 // Loop over buckets and remove too old data points.
92 while (num_samples_ > 0 && oldest_time_ < new_oldest_time) {
93 const Bucket& oldest_bucket = buckets_[oldest_index_];
94 RTC_DCHECK_GE(accumulated_count_, oldest_bucket.sum);
95 RTC_DCHECK_GE(num_samples_, oldest_bucket.samples);
96 accumulated_count_ -= oldest_bucket.sum;
97 num_samples_ -= oldest_bucket.samples;
98 buckets_[oldest_index_] = Bucket();
99 if (++oldest_index_ >= max_window_size_ms_)
sprang@webrtc.org37968a92013-12-03 10:31:59 +0000100 oldest_index_ = 0;
sprang@webrtc.org37968a92013-12-03 10:31:59 +0000101 ++oldest_time_;
sprang@webrtc.org37968a92013-12-03 10:31:59 +0000102 }
103 oldest_time_ = new_oldest_time;
104}
105
Erik Språng51e60302016-06-10 22:13:21 +0200106bool RateStatistics::SetWindowSize(int64_t window_size_ms, int64_t now_ms) {
107 if (window_size_ms <= 0 || window_size_ms > max_window_size_ms_)
108 return false;
109
110 current_window_size_ms_ = window_size_ms;
111 EraseOld(now_ms);
112 return true;
113}
114
aluebsa49f1102016-07-08 11:01:59 -0700115bool RateStatistics::IsInitialized() {
Erik Språng51e60302016-06-10 22:13:21 +0200116 return oldest_time_ != -max_window_size_ms_;
117}
118
sprang@webrtc.org37968a92013-12-03 10:31:59 +0000119} // namespace webrtc