blob: c1ad2d5e844a00b4b3842bdbcd9e31af83a36a3f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
Tim Psiaki63046262015-09-14 10:38:08 -07002 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00003 *
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
11#include "webrtc/base/ratetracker.h"
Tim Psiaki63046262015-09-14 10:38:08 -070012
13#include <stddef.h>
14
15#include <algorithm>
16
17#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include "webrtc/base/timeutils.h"
19
20namespace rtc {
21
Peter Boström0c4e06b2015-10-07 12:23:21 +020022RateTracker::RateTracker(uint32_t bucket_milliseconds, size_t bucket_count)
Tim Psiaki63046262015-09-14 10:38:08 -070023 : bucket_milliseconds_(bucket_milliseconds),
Peter Boström0c4e06b2015-10-07 12:23:21 +020024 bucket_count_(bucket_count),
25 sample_buckets_(new size_t[bucket_count + 1]),
26 total_sample_count_(0u),
27 bucket_start_time_milliseconds_(~0u) {
henrikg91d6ede2015-09-17 00:24:34 -070028 RTC_CHECK(bucket_milliseconds > 0u);
29 RTC_CHECK(bucket_count > 0u);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030}
31
Tim Psiaki63046262015-09-14 10:38:08 -070032RateTracker::~RateTracker() {
33 delete[] sample_buckets_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034}
35
Tim Psiaki63046262015-09-14 10:38:08 -070036double RateTracker::ComputeRateForInterval(
Peter Boström0c4e06b2015-10-07 12:23:21 +020037 uint32_t interval_milliseconds) const {
Tim Psiaki63046262015-09-14 10:38:08 -070038 if (bucket_start_time_milliseconds_ == ~0u) {
39 return 0.0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020041 uint32_t current_time = Time();
Tim Psiaki63046262015-09-14 10:38:08 -070042 // Calculate which buckets to sum up given the current time. If the time
43 // has passed to a new bucket then we have to skip some of the oldest buckets.
Peter Boström0c4e06b2015-10-07 12:23:21 +020044 uint32_t available_interval_milliseconds = std::min<uint32_t>(
Tim Psiaki63046262015-09-14 10:38:08 -070045 interval_milliseconds,
Peter Boström0c4e06b2015-10-07 12:23:21 +020046 bucket_milliseconds_ * static_cast<uint32_t>(bucket_count_));
Tim Psiaki63046262015-09-14 10:38:08 -070047 // number of old buckets (i.e. after the current bucket in the ring buffer)
48 // that are expired given our current time interval.
49 size_t buckets_to_skip;
50 // Number of milliseconds of the first bucket that are not a portion of the
51 // current interval.
Peter Boström0c4e06b2015-10-07 12:23:21 +020052 uint32_t milliseconds_to_skip;
Tim Psiaki63046262015-09-14 10:38:08 -070053 if (current_time >
54 initialization_time_milliseconds_ + available_interval_milliseconds) {
Peter Boström0c4e06b2015-10-07 12:23:21 +020055 uint32_t time_to_skip =
56 current_time - bucket_start_time_milliseconds_ +
57 static_cast<uint32_t>(bucket_count_) * bucket_milliseconds_ -
Tim Psiaki63046262015-09-14 10:38:08 -070058 available_interval_milliseconds;
59 buckets_to_skip = time_to_skip / bucket_milliseconds_;
60 milliseconds_to_skip = time_to_skip % bucket_milliseconds_;
61 } else {
62 buckets_to_skip = bucket_count_ - current_bucket_;
63 milliseconds_to_skip = 0u;
64 available_interval_milliseconds =
65 TimeDiff(current_time, initialization_time_milliseconds_);
asapersson799379e2016-02-02 01:46:53 -080066 // Let one bucket interval pass after initialization before reporting.
67 if (available_interval_milliseconds < bucket_milliseconds_) {
68 return 0.0;
69 }
Tim Psiaki63046262015-09-14 10:38:08 -070070 }
71 // If we're skipping all buckets that means that there have been no samples
72 // within the sampling interval so report 0.
73 if (buckets_to_skip > bucket_count_ ||
74 available_interval_milliseconds == 0u) {
75 return 0.0;
76 }
77 size_t start_bucket = NextBucketIndex(current_bucket_ + buckets_to_skip);
78 // Only count a portion of the first bucket according to how much of the
79 // first bucket is within the current interval.
Tim Psiakiad13d2f2015-11-10 16:34:50 -080080 size_t total_samples = ((sample_buckets_[start_bucket] *
81 (bucket_milliseconds_ - milliseconds_to_skip)) +
82 (bucket_milliseconds_ >> 1)) /
Tim Psiaki63046262015-09-14 10:38:08 -070083 bucket_milliseconds_;
84 // All other buckets in the interval are counted in their entirety.
85 for (size_t i = NextBucketIndex(start_bucket);
86 i != NextBucketIndex(current_bucket_);
87 i = NextBucketIndex(i)) {
88 total_samples += sample_buckets_[i];
89 }
90 // Convert to samples per second.
91 return static_cast<double>(total_samples * 1000u) /
92 static_cast<double>(available_interval_milliseconds);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093}
94
Tim Psiaki63046262015-09-14 10:38:08 -070095double RateTracker::ComputeTotalRate() const {
96 if (bucket_start_time_milliseconds_ == ~0u) {
97 return 0.0;
98 }
Peter Boström0c4e06b2015-10-07 12:23:21 +020099 uint32_t current_time = Time();
Tim Psiaki63046262015-09-14 10:38:08 -0700100 if (TimeIsLaterOrEqual(current_time, initialization_time_milliseconds_)) {
101 return 0.0;
102 }
103 return static_cast<double>(total_sample_count_ * 1000u) /
104 static_cast<double>(
105 TimeDiff(current_time, initialization_time_milliseconds_));
106}
107
108size_t RateTracker::TotalSampleCount() const {
109 return total_sample_count_;
110}
111
112void RateTracker::AddSamples(size_t sample_count) {
113 EnsureInitialized();
Peter Boström0c4e06b2015-10-07 12:23:21 +0200114 uint32_t current_time = Time();
Tim Psiaki63046262015-09-14 10:38:08 -0700115 // Advance the current bucket as needed for the current time, and reset
116 // bucket counts as we advance.
117 for (size_t i = 0u; i <= bucket_count_ &&
118 current_time >= bucket_start_time_milliseconds_ + bucket_milliseconds_;
119 ++i) {
120 bucket_start_time_milliseconds_ += bucket_milliseconds_;
121 current_bucket_ = NextBucketIndex(current_bucket_);
122 sample_buckets_[current_bucket_] = 0u;
123 }
124 // Ensure that bucket_start_time_milliseconds_ is updated appropriately if
125 // the entire buffer of samples has been expired.
126 bucket_start_time_milliseconds_ += bucket_milliseconds_ *
127 ((current_time - bucket_start_time_milliseconds_) / bucket_milliseconds_);
128 // Add all samples in the bucket that includes the current time.
129 sample_buckets_[current_bucket_] += sample_count;
130 total_sample_count_ += sample_count;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131}
132
Peter Boström0c4e06b2015-10-07 12:23:21 +0200133uint32_t RateTracker::Time() const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134 return rtc::Time();
135}
136
Tim Psiaki63046262015-09-14 10:38:08 -0700137void RateTracker::EnsureInitialized() {
138 if (bucket_start_time_milliseconds_ == ~0u) {
139 initialization_time_milliseconds_ = Time();
140 bucket_start_time_milliseconds_ = initialization_time_milliseconds_;
141 current_bucket_ = 0u;
142 // We only need to initialize the first bucket because we reset buckets when
143 // current_bucket_ increments.
144 sample_buckets_[current_bucket_] = 0u;
145 }
146}
147
148size_t RateTracker::NextBucketIndex(size_t bucket_index) const {
149 return (bucket_index + 1u) % (bucket_count_ + 1u);
150}
151
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152} // namespace rtc