blob: 7dcdb9116918f5096554442880f04044414489c1 [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
Tim Psiaki63046262015-09-14 10:38:08 -070022RateTracker::RateTracker(
23 uint32 bucket_milliseconds, size_t bucket_count)
24 : bucket_milliseconds_(bucket_milliseconds),
25 bucket_count_(bucket_count),
26 sample_buckets_(new size_t[bucket_count + 1]),
27 total_sample_count_(0u),
28 bucket_start_time_milliseconds_(~0u) {
29 CHECK(bucket_milliseconds > 0u);
30 CHECK(bucket_count > 0u);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031}
32
Tim Psiaki63046262015-09-14 10:38:08 -070033RateTracker::~RateTracker() {
34 delete[] sample_buckets_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035}
36
Tim Psiaki63046262015-09-14 10:38:08 -070037double RateTracker::ComputeRateForInterval(
38 uint32 interval_milliseconds) const {
39 if (bucket_start_time_milliseconds_ == ~0u) {
40 return 0.0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041 }
Tim Psiaki63046262015-09-14 10:38:08 -070042 uint32 current_time = Time();
43 // Calculate which buckets to sum up given the current time. If the time
44 // has passed to a new bucket then we have to skip some of the oldest buckets.
45 uint32 available_interval_milliseconds = std::min<uint32>(
46 interval_milliseconds,
47 bucket_milliseconds_ * static_cast<uint32>(bucket_count_));
48 // number of old buckets (i.e. after the current bucket in the ring buffer)
49 // that are expired given our current time interval.
50 size_t buckets_to_skip;
51 // Number of milliseconds of the first bucket that are not a portion of the
52 // current interval.
53 uint32 milliseconds_to_skip;
54 if (current_time >
55 initialization_time_milliseconds_ + available_interval_milliseconds) {
56 uint32 time_to_skip = current_time - bucket_start_time_milliseconds_ +
57 static_cast<uint32>(bucket_count_) * bucket_milliseconds_ -
58 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_);
66 }
67 // If we're skipping all buckets that means that there have been no samples
68 // within the sampling interval so report 0.
69 if (buckets_to_skip > bucket_count_ ||
70 available_interval_milliseconds == 0u) {
71 return 0.0;
72 }
73 size_t start_bucket = NextBucketIndex(current_bucket_ + buckets_to_skip);
74 // Only count a portion of the first bucket according to how much of the
75 // first bucket is within the current interval.
76 size_t total_samples = sample_buckets_[start_bucket] *
77 (bucket_milliseconds_ - milliseconds_to_skip) /
78 bucket_milliseconds_;
79 // All other buckets in the interval are counted in their entirety.
80 for (size_t i = NextBucketIndex(start_bucket);
81 i != NextBucketIndex(current_bucket_);
82 i = NextBucketIndex(i)) {
83 total_samples += sample_buckets_[i];
84 }
85 // Convert to samples per second.
86 return static_cast<double>(total_samples * 1000u) /
87 static_cast<double>(available_interval_milliseconds);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088}
89
Tim Psiaki63046262015-09-14 10:38:08 -070090double RateTracker::ComputeTotalRate() const {
91 if (bucket_start_time_milliseconds_ == ~0u) {
92 return 0.0;
93 }
94 uint32 current_time = Time();
95 if (TimeIsLaterOrEqual(current_time, initialization_time_milliseconds_)) {
96 return 0.0;
97 }
98 return static_cast<double>(total_sample_count_ * 1000u) /
99 static_cast<double>(
100 TimeDiff(current_time, initialization_time_milliseconds_));
101}
102
103size_t RateTracker::TotalSampleCount() const {
104 return total_sample_count_;
105}
106
107void RateTracker::AddSamples(size_t sample_count) {
108 EnsureInitialized();
109 uint32 current_time = Time();
110 // Advance the current bucket as needed for the current time, and reset
111 // bucket counts as we advance.
112 for (size_t i = 0u; i <= bucket_count_ &&
113 current_time >= bucket_start_time_milliseconds_ + bucket_milliseconds_;
114 ++i) {
115 bucket_start_time_milliseconds_ += bucket_milliseconds_;
116 current_bucket_ = NextBucketIndex(current_bucket_);
117 sample_buckets_[current_bucket_] = 0u;
118 }
119 // Ensure that bucket_start_time_milliseconds_ is updated appropriately if
120 // the entire buffer of samples has been expired.
121 bucket_start_time_milliseconds_ += bucket_milliseconds_ *
122 ((current_time - bucket_start_time_milliseconds_) / bucket_milliseconds_);
123 // Add all samples in the bucket that includes the current time.
124 sample_buckets_[current_bucket_] += sample_count;
125 total_sample_count_ += sample_count;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126}
127
128uint32 RateTracker::Time() const {
129 return rtc::Time();
130}
131
Tim Psiaki63046262015-09-14 10:38:08 -0700132void RateTracker::EnsureInitialized() {
133 if (bucket_start_time_milliseconds_ == ~0u) {
134 initialization_time_milliseconds_ = Time();
135 bucket_start_time_milliseconds_ = initialization_time_milliseconds_;
136 current_bucket_ = 0u;
137 // We only need to initialize the first bucket because we reset buckets when
138 // current_bucket_ increments.
139 sample_buckets_[current_bucket_] = 0u;
140 }
141}
142
143size_t RateTracker::NextBucketIndex(size_t bucket_index) const {
144 return (bucket_index + 1u) % (bucket_count_ + 1u);
145}
146
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147} // namespace rtc