Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: webrtc/base/ratetracker.cc

Issue 1279433006: Add a rate tracker that tracks rate over a given interval split up into buckets that accumulate uni… (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Win64 fix Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/base/ratetracker.h" 11 #include "webrtc/base/ratetracker.h"
12
13 #include <stddef.h>
14 #include <assert.h>
noahric 2015/09/03 21:42:24 Use webrtc/base/checks.h and CHECK/DCHECK
tpsiaki 2015/09/03 23:28:59 Done.
15
16 #include <algorithm>
17
12 #include "webrtc/base/timeutils.h" 18 #include "webrtc/base/timeutils.h"
13 19
14 namespace rtc { 20 namespace rtc {
15 21
16 RateTracker::RateTracker() 22 RateTracker::RateTracker(
17 : total_units_(0), units_second_(0), 23 uint32 bucket_milliseconds, size_t bucket_count)
18 last_units_second_time_(~0u), 24 : bucket_milliseconds_(bucket_milliseconds),
19 last_units_second_calc_(0) { 25 bucket_count_(bucket_count),
26 sample_buckets_(new size_t[bucket_count + 1]),
27 total_sample_count_(0u),
28 bucket_start_time_(~0u) {
29 assert(bucket_milliseconds > 0u);
30 assert(bucket_count > 0u);
20 } 31 }
21 32
22 size_t RateTracker::total_units() const { 33 RateTracker::~RateTracker() {
23 return total_units_; 34 delete[] sample_buckets_;
24 } 35 }
25 36
26 size_t RateTracker::units_second() { 37 double RateTracker::ComputeCurrentRate(
27 // Snapshot units / second calculator. Determine how many seconds have 38 uint32 interval_milliseconds) const {
28 // elapsed since our last reference point. If over 1 second, establish 39 if (bucket_start_time_ == ~0u) {
29 // a new reference point that is an integer number of seconds since the 40 return 0.0;
30 // last one, and compute the units over that interval. 41 }
31 uint32 current_time = Time(); 42 uint32 current_time = Time();
32 if (last_units_second_time_ == ~0u) { 43 // Calculate which buckets to sum up given the current time. If the time
33 last_units_second_time_ = current_time; 44 // has passed to a new bucket then we have to skip some of the oldest buckets.
34 last_units_second_calc_ = total_units_; 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 > initialization_time_ + available_interval_milliseconds) {
55 uint32 time_to_skip = current_time - bucket_start_time_ +
56 static_cast<uint32>(bucket_count_) * bucket_milliseconds_ -
57 available_interval_milliseconds;
58 buckets_to_skip = time_to_skip / bucket_milliseconds_;
59 milliseconds_to_skip = time_to_skip % bucket_milliseconds_;
35 } else { 60 } else {
36 int delta = rtc::TimeDiff(current_time, last_units_second_time_); 61 buckets_to_skip = bucket_count_ - current_bucket_;
37 if (delta >= 1000) { 62 milliseconds_to_skip = 0u;
38 int fraction_time = delta % 1000; 63 available_interval_milliseconds = current_time - initialization_time_;
39 int seconds = delta / 1000;
40 int fraction_units =
41 static_cast<int>(total_units_ - last_units_second_calc_) *
42 fraction_time / delta;
43 // Compute "units received during the interval" / "seconds in interval"
44 units_second_ =
45 (total_units_ - last_units_second_calc_ - fraction_units) / seconds;
46 last_units_second_time_ = current_time - fraction_time;
47 last_units_second_calc_ = total_units_ - fraction_units;
48 }
49 } 64 }
50 65 // If we're skipping all buckets that means that there have been no samples
51 return units_second_; 66 // within the sampling interval so report 0.
67 if (buckets_to_skip > bucket_count_ ||
68 available_interval_milliseconds == 0u) {
69 return 0.0;
70 }
71 size_t start_bucket = NextBucketIndex(current_bucket_ + buckets_to_skip);
72 // Only count a portion of the first bucket according to how much of the
73 // first bucket is within the current interval.
74 size_t total_samples = sample_buckets_[start_bucket] *
75 (bucket_milliseconds_ - milliseconds_to_skip) /
76 bucket_milliseconds_;
77 // All other buckets in the interval are counted in their entirety.
78 for (size_t i = NextBucketIndex(start_bucket);
79 i != NextBucketIndex(current_bucket_);
80 i = NextBucketIndex(i)) {
81 total_samples += sample_buckets_[i];
82 }
83 // Convert to samples per second.
84 return static_cast<double>(total_samples * 1000u) /
85 static_cast<double>(available_interval_milliseconds);
52 } 86 }
53 87
54 void RateTracker::Update(size_t units) { 88 double RateTracker::ComputeTotalRate() const {
55 if (last_units_second_time_ == ~0u) 89 if (bucket_start_time_ == ~0u) {
56 last_units_second_time_ = Time(); 90 return 0.0;
57 total_units_ += units; 91 }
92 uint32 current_time = Time();
93 if (current_time <= initialization_time_) {
noahric 2015/09/03 21:42:24 Wraparound? You can use TimeIsLater and TimeDiff,
tpsiaki 2015/09/03 23:28:59 Done.
94 return 0.0;
95 }
96 return static_cast<double>(total_sample_count_ * 1000u) /
97 static_cast<double>(current_time - initialization_time_);
98 }
99
100 size_t RateTracker::TotalSampleCount() const {
101 return total_sample_count_;
102 }
103
104 void RateTracker::AddSamples(size_t sample_count) {
105 EnsureInitialized();
106 uint32 current_time = Time();
107 // Advance the current bucket as needed for the current time, and reset
108 // bucket counts as we advance.
109 for (size_t i = 0u; i <= bucket_count_ &&
110 current_time >= bucket_start_time_ + bucket_milliseconds_; ++i) {
111 bucket_start_time_ += bucket_milliseconds_;
112 current_bucket_ = NextBucketIndex(current_bucket_);
113 sample_buckets_[current_bucket_] = 0u;
114 }
115 // Ensure that bucket_start_time_ is updated appropriately if we've expired
116 // the entire buffer of samples.
117 bucket_start_time_ += bucket_milliseconds_ *
118 ((current_time - bucket_start_time_) / bucket_milliseconds_);
119 // Add all samples in the bucket that includes the current time.
120 sample_buckets_[current_bucket_] += sample_count;
121 total_sample_count_ += sample_count;
58 } 122 }
59 123
60 uint32 RateTracker::Time() const { 124 uint32 RateTracker::Time() const {
61 return rtc::Time(); 125 return rtc::Time();
62 } 126 }
63 127
128 void RateTracker::EnsureInitialized() {
129 if (bucket_start_time_ == ~0u) {
130 initialization_time_ = Time();
131 bucket_start_time_ = initialization_time_;
132 current_bucket_ = 0u;
133 // We only need to initialize the first bucket because we reset buckets when
134 // current_bucket_ increments.
135 sample_buckets_[current_bucket_] = 0u;
136 }
137 }
138
139 size_t RateTracker::NextBucketIndex(size_t bucket_index) const {
140 return (bucket_index + 1u) % (bucket_count_ + 1u);
141 }
142
64 } // namespace rtc 143 } // namespace rtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698