blob: 6529aa1f7a6f17bf8d9fe8846b25ca52fccd6f3c [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
19RateStatistics::RateStatistics(uint32_t window_size_ms, float scale)
20 : num_buckets_(window_size_ms + 1), // N ms in (N+1) buckets.
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000021 buckets_(new size_t[num_buckets_]()),
sprang@webrtc.org37968a92013-12-03 10:31:59 +000022 accumulated_count_(0),
23 oldest_time_(0),
24 oldest_index_(0),
Stefan Holmerfb8fc532016-04-22 15:48:23 +020025 scale_(scale) {}
sprang@webrtc.org37968a92013-12-03 10:31:59 +000026
tkchinf75d0082016-02-23 22:49:42 -080027RateStatistics::~RateStatistics() {}
sprang@webrtc.org37968a92013-12-03 10:31:59 +000028
29void RateStatistics::Reset() {
30 accumulated_count_ = 0;
31 oldest_time_ = 0;
32 oldest_index_ = 0;
33 for (int i = 0; i < num_buckets_; i++) {
34 buckets_[i] = 0;
35 }
36}
37
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000038void RateStatistics::Update(size_t count, int64_t now_ms) {
sprang@webrtc.org37968a92013-12-03 10:31:59 +000039 if (now_ms < oldest_time_) {
40 // Too old data is ignored.
41 return;
42 }
43
44 EraseOld(now_ms);
45
46 int now_offset = static_cast<int>(now_ms - oldest_time_);
Stefan Holmerfb8fc532016-04-22 15:48:23 +020047 RTC_DCHECK_LT(now_offset, num_buckets_);
sprang@webrtc.org37968a92013-12-03 10:31:59 +000048 int index = oldest_index_ + now_offset;
49 if (index >= num_buckets_) {
50 index -= num_buckets_;
51 }
52 buckets_[index] += count;
53 accumulated_count_ += count;
54}
55
56uint32_t RateStatistics::Rate(int64_t now_ms) {
57 EraseOld(now_ms);
Stefan Holmerfb8fc532016-04-22 15:48:23 +020058 float scale = scale_ / (now_ms - oldest_time_ + 1);
59 return static_cast<uint32_t>(accumulated_count_ * scale + 0.5f);
sprang@webrtc.org37968a92013-12-03 10:31:59 +000060}
61
62void RateStatistics::EraseOld(int64_t now_ms) {
63 int64_t new_oldest_time = now_ms - num_buckets_ + 1;
64 if (new_oldest_time <= oldest_time_) {
Stefan Holmerfb8fc532016-04-22 15:48:23 +020065 if (accumulated_count_ == 0)
66 oldest_time_ = now_ms;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000067 return;
68 }
sprang@webrtc.org37968a92013-12-03 10:31:59 +000069 while (oldest_time_ < new_oldest_time) {
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000070 size_t count_in_oldest_bucket = buckets_[oldest_index_];
Stefan Holmerfb8fc532016-04-22 15:48:23 +020071 RTC_DCHECK_GE(accumulated_count_, count_in_oldest_bucket);
sprang@webrtc.org37968a92013-12-03 10:31:59 +000072 accumulated_count_ -= count_in_oldest_bucket;
73 buckets_[oldest_index_] = 0;
74 if (++oldest_index_ >= num_buckets_) {
75 oldest_index_ = 0;
76 }
77 ++oldest_time_;
78 if (accumulated_count_ == 0) {
79 // This guarantees we go through all the buckets at most once, even if
80 // |new_oldest_time| is far greater than |oldest_time_|.
Stefan Holmerfb8fc532016-04-22 15:48:23 +020081 new_oldest_time = now_ms;
sprang@webrtc.org37968a92013-12-03 10:31:59 +000082 break;
83 }
84 }
85 oldest_time_ = new_oldest_time;
86}
87
88} // namespace webrtc