asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "video/stats_counter.h" |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 14 | #include <limits> |
| 15 | #include <map> |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
| 18 | #include "system_wrappers/include/clock.h" |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | namespace { |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 23 | // Default periodic time interval for processing samples. |
| 24 | const int64_t kDefaultProcessIntervalMs = 2000; |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 25 | const uint32_t kStreamId0 = 0; |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 26 | } // namespace |
| 27 | |
asapersson | 43cb716 | 2016-11-15 08:20:48 -0800 | [diff] [blame] | 28 | std::string AggregatedStats::ToString() const { |
asapersson | 076c011 | 2016-11-30 05:17:16 -0800 | [diff] [blame] | 29 | return ToStringWithMultiplier(1); |
| 30 | } |
| 31 | |
| 32 | std::string AggregatedStats::ToStringWithMultiplier(int multiplier) const { |
asapersson | 43cb716 | 2016-11-15 08:20:48 -0800 | [diff] [blame] | 33 | std::stringstream ss; |
| 34 | ss << "periodic_samples:" << num_samples << ", {"; |
asapersson | 076c011 | 2016-11-30 05:17:16 -0800 | [diff] [blame] | 35 | ss << "min:" << (min * multiplier) << ", "; |
| 36 | ss << "avg:" << (average * multiplier) << ", "; |
| 37 | ss << "max:" << (max * multiplier) << "}"; |
asapersson | 43cb716 | 2016-11-15 08:20:48 -0800 | [diff] [blame] | 38 | return ss.str(); |
| 39 | } |
| 40 | |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 41 | // Class holding periodically computed metrics. |
| 42 | class AggregatedCounter { |
| 43 | public: |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 44 | AggregatedCounter() : last_sample_(0), sum_samples_(0) {} |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 45 | ~AggregatedCounter() {} |
| 46 | |
| 47 | void Add(int sample) { |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 48 | last_sample_ = sample; |
| 49 | sum_samples_ += sample; |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 50 | ++stats_.num_samples; |
| 51 | if (stats_.num_samples == 1) { |
| 52 | stats_.min = sample; |
| 53 | stats_.max = sample; |
| 54 | } |
| 55 | stats_.min = std::min(sample, stats_.min); |
| 56 | stats_.max = std::max(sample, stats_.max); |
| 57 | } |
| 58 | |
| 59 | AggregatedStats ComputeStats() { |
| 60 | Compute(); |
| 61 | return stats_; |
| 62 | } |
| 63 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 64 | bool Empty() const { return stats_.num_samples == 0; } |
| 65 | |
| 66 | int last_sample() const { return last_sample_; } |
| 67 | |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 68 | private: |
| 69 | void Compute() { |
| 70 | if (stats_.num_samples == 0) |
| 71 | return; |
| 72 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 73 | stats_.average = |
| 74 | (sum_samples_ + stats_.num_samples / 2) / stats_.num_samples; |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 75 | } |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 76 | int last_sample_; |
| 77 | int64_t sum_samples_; |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 78 | AggregatedStats stats_; |
| 79 | }; |
| 80 | |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 81 | // Class holding gathered samples within a process interval. |
| 82 | class Samples { |
| 83 | public: |
| 84 | Samples() : total_count_(0) {} |
| 85 | ~Samples() {} |
| 86 | |
| 87 | void Add(int sample, uint32_t stream_id) { |
| 88 | samples_[stream_id].Add(sample); |
| 89 | ++total_count_; |
| 90 | } |
asapersson | 93e1e23 | 2017-02-06 05:18:35 -0800 | [diff] [blame] | 91 | void Set(int64_t sample, uint32_t stream_id) { |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 92 | samples_[stream_id].Set(sample); |
| 93 | ++total_count_; |
| 94 | } |
asapersson | 93e1e23 | 2017-02-06 05:18:35 -0800 | [diff] [blame] | 95 | void SetLast(int64_t sample, uint32_t stream_id) { |
| 96 | samples_[stream_id].SetLast(sample); |
| 97 | } |
| 98 | int64_t GetLast(uint32_t stream_id) { return samples_[stream_id].GetLast(); } |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 99 | |
| 100 | int64_t Count() const { return total_count_; } |
| 101 | bool Empty() const { return total_count_ == 0; } |
| 102 | |
| 103 | int64_t Sum() const { |
| 104 | int64_t sum = 0; |
| 105 | for (const auto& it : samples_) |
| 106 | sum += it.second.sum_; |
| 107 | return sum; |
| 108 | } |
| 109 | |
| 110 | int Max() const { |
| 111 | int max = std::numeric_limits<int>::min(); |
| 112 | for (const auto& it : samples_) |
| 113 | max = std::max(it.second.max_, max); |
| 114 | return max; |
| 115 | } |
| 116 | |
| 117 | void Reset() { |
| 118 | for (auto& it : samples_) |
| 119 | it.second.Reset(); |
| 120 | total_count_ = 0; |
| 121 | } |
| 122 | |
| 123 | int64_t Diff() const { |
| 124 | int64_t sum_diff = 0; |
| 125 | int count = 0; |
| 126 | for (const auto& it : samples_) { |
| 127 | if (it.second.count_ > 0) { |
| 128 | int64_t diff = it.second.sum_ - it.second.last_sum_; |
| 129 | if (diff >= 0) { |
| 130 | sum_diff += diff; |
| 131 | ++count; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | return (count > 0) ? sum_diff : -1; |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | struct Stats { |
| 140 | void Add(int sample) { |
| 141 | sum_ += sample; |
| 142 | ++count_; |
| 143 | max_ = std::max(sample, max_); |
| 144 | } |
asapersson | 93e1e23 | 2017-02-06 05:18:35 -0800 | [diff] [blame] | 145 | void Set(int64_t sample) { |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 146 | sum_ = sample; |
| 147 | ++count_; |
| 148 | } |
asapersson | 93e1e23 | 2017-02-06 05:18:35 -0800 | [diff] [blame] | 149 | void SetLast(int64_t sample) { last_sum_ = sample; } |
| 150 | int64_t GetLast() const { return last_sum_; } |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 151 | void Reset() { |
| 152 | if (count_ > 0) |
| 153 | last_sum_ = sum_; |
| 154 | sum_ = 0; |
| 155 | count_ = 0; |
| 156 | max_ = std::numeric_limits<int>::min(); |
| 157 | } |
| 158 | |
| 159 | int max_ = std::numeric_limits<int>::min(); |
| 160 | int64_t count_ = 0; |
| 161 | int64_t sum_ = 0; |
| 162 | int64_t last_sum_ = 0; |
| 163 | }; |
| 164 | |
| 165 | int64_t total_count_; |
| 166 | std::map<uint32_t, Stats> samples_; // Gathered samples mapped by stream id. |
| 167 | }; |
| 168 | |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 169 | // StatsCounter class. |
| 170 | StatsCounter::StatsCounter(Clock* clock, |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 171 | int64_t process_intervals_ms, |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 172 | bool include_empty_intervals, |
| 173 | StatsCounterObserver* observer) |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 174 | : include_empty_intervals_(include_empty_intervals), |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 175 | process_intervals_ms_(process_intervals_ms), |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 176 | aggregated_counter_(new AggregatedCounter()), |
| 177 | samples_(new Samples()), |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 178 | clock_(clock), |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 179 | observer_(observer), |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 180 | last_process_time_ms_(-1), |
asapersson | 93e1e23 | 2017-02-06 05:18:35 -0800 | [diff] [blame] | 181 | paused_(false), |
| 182 | pause_time_ms_(-1), |
| 183 | min_pause_time_ms_(0) { |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 184 | RTC_DCHECK_GT(process_intervals_ms_, 0); |
| 185 | } |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 186 | |
| 187 | StatsCounter::~StatsCounter() {} |
| 188 | |
| 189 | AggregatedStats StatsCounter::GetStats() { |
| 190 | return aggregated_counter_->ComputeStats(); |
| 191 | } |
| 192 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 193 | AggregatedStats StatsCounter::ProcessAndGetStats() { |
| 194 | if (HasSample()) |
| 195 | TryProcess(); |
| 196 | return aggregated_counter_->ComputeStats(); |
| 197 | } |
| 198 | |
asapersson | 93e1e23 | 2017-02-06 05:18:35 -0800 | [diff] [blame] | 199 | void StatsCounter::ProcessAndPauseForDuration(int64_t min_pause_time_ms) { |
| 200 | ProcessAndPause(); |
| 201 | min_pause_time_ms_ = min_pause_time_ms; |
| 202 | } |
| 203 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 204 | void StatsCounter::ProcessAndPause() { |
| 205 | if (HasSample()) |
| 206 | TryProcess(); |
| 207 | paused_ = true; |
asapersson | 93e1e23 | 2017-02-06 05:18:35 -0800 | [diff] [blame] | 208 | pause_time_ms_ = clock_->TimeInMilliseconds(); |
| 209 | } |
| 210 | |
| 211 | void StatsCounter::ProcessAndStopPause() { |
| 212 | if (HasSample()) |
| 213 | TryProcess(); |
| 214 | Resume(); |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 215 | } |
| 216 | |
asapersson | 250fd97 | 2016-09-08 00:07:21 -0700 | [diff] [blame] | 217 | bool StatsCounter::HasSample() const { |
| 218 | return last_process_time_ms_ != -1; |
| 219 | } |
| 220 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 221 | bool StatsCounter::TimeToProcess(int* elapsed_intervals) { |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 222 | int64_t now = clock_->TimeInMilliseconds(); |
| 223 | if (last_process_time_ms_ == -1) |
| 224 | last_process_time_ms_ = now; |
| 225 | |
| 226 | int64_t diff_ms = now - last_process_time_ms_; |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 227 | if (diff_ms < process_intervals_ms_) |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 228 | return false; |
| 229 | |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 230 | // Advance number of complete |process_intervals_ms_| that have passed. |
| 231 | int64_t num_intervals = diff_ms / process_intervals_ms_; |
| 232 | last_process_time_ms_ += num_intervals * process_intervals_ms_; |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 233 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 234 | *elapsed_intervals = num_intervals; |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 238 | void StatsCounter::Add(int sample) { |
| 239 | TryProcess(); |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 240 | samples_->Add(sample, kStreamId0); |
asapersson | 93e1e23 | 2017-02-06 05:18:35 -0800 | [diff] [blame] | 241 | ResumeIfMinTimePassed(); |
| 242 | } |
| 243 | |
| 244 | void StatsCounter::Set(int64_t sample, uint32_t stream_id) { |
| 245 | if (paused_ && sample == samples_->GetLast(stream_id)) { |
| 246 | // Do not add same sample while paused (will reset pause). |
| 247 | return; |
| 248 | } |
| 249 | TryProcess(); |
| 250 | samples_->Set(sample, stream_id); |
| 251 | ResumeIfMinTimePassed(); |
| 252 | } |
| 253 | |
| 254 | void StatsCounter::SetLast(int64_t sample, uint32_t stream_id) { |
| 255 | RTC_DCHECK(!HasSample()) << "Should be set before first sample is added."; |
| 256 | samples_->SetLast(sample, stream_id); |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // Reports periodically computed metric. |
| 260 | void StatsCounter::ReportMetricToAggregatedCounter( |
| 261 | int value, |
| 262 | int num_values_to_add) const { |
| 263 | for (int i = 0; i < num_values_to_add; ++i) { |
| 264 | aggregated_counter_->Add(value); |
| 265 | if (observer_) |
| 266 | observer_->OnMetricUpdated(value); |
| 267 | } |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void StatsCounter::TryProcess() { |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 271 | int elapsed_intervals; |
| 272 | if (!TimeToProcess(&elapsed_intervals)) |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 273 | return; |
| 274 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 275 | // Get and report periodically computed metric. |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 276 | int metric; |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 277 | if (GetMetric(&metric)) |
| 278 | ReportMetricToAggregatedCounter(metric, 1); |
| 279 | |
| 280 | // Report value for elapsed intervals without samples. |
| 281 | if (IncludeEmptyIntervals()) { |
| 282 | // If there are no samples, all elapsed intervals are empty (otherwise one |
| 283 | // interval contains sample(s), discard this interval). |
| 284 | int empty_intervals = |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 285 | samples_->Empty() ? elapsed_intervals : (elapsed_intervals - 1); |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 286 | ReportMetricToAggregatedCounter(GetValueForEmptyInterval(), |
| 287 | empty_intervals); |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 288 | } |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 289 | |
| 290 | // Reset samples for elapsed interval. |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 291 | samples_->Reset(); |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 292 | } |
| 293 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 294 | bool StatsCounter::IncludeEmptyIntervals() const { |
| 295 | return include_empty_intervals_ && !paused_ && !aggregated_counter_->Empty(); |
| 296 | } |
asapersson | 93e1e23 | 2017-02-06 05:18:35 -0800 | [diff] [blame] | 297 | void StatsCounter::ResumeIfMinTimePassed() { |
| 298 | if (paused_ && |
| 299 | (clock_->TimeInMilliseconds() - pause_time_ms_) >= min_pause_time_ms_) { |
| 300 | Resume(); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | void StatsCounter::Resume() { |
| 305 | paused_ = false; |
| 306 | min_pause_time_ms_ = 0; |
| 307 | } |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 308 | |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 309 | // StatsCounter sub-classes. |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 310 | AvgCounter::AvgCounter(Clock* clock, |
| 311 | StatsCounterObserver* observer, |
| 312 | bool include_empty_intervals) |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 313 | : StatsCounter(clock, |
| 314 | kDefaultProcessIntervalMs, |
| 315 | include_empty_intervals, |
| 316 | observer) {} |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 317 | |
| 318 | void AvgCounter::Add(int sample) { |
| 319 | StatsCounter::Add(sample); |
| 320 | } |
| 321 | |
| 322 | bool AvgCounter::GetMetric(int* metric) const { |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 323 | int64_t count = samples_->Count(); |
| 324 | if (count == 0) |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 325 | return false; |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 326 | |
| 327 | *metric = (samples_->Sum() + count / 2) / count; |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 328 | return true; |
| 329 | } |
| 330 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 331 | int AvgCounter::GetValueForEmptyInterval() const { |
| 332 | return aggregated_counter_->last_sample(); |
| 333 | } |
| 334 | |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 335 | MaxCounter::MaxCounter(Clock* clock, |
| 336 | StatsCounterObserver* observer, |
| 337 | int64_t process_intervals_ms) |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 338 | : StatsCounter(clock, |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 339 | process_intervals_ms, |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 340 | false, // |include_empty_intervals| |
| 341 | observer) {} |
| 342 | |
| 343 | void MaxCounter::Add(int sample) { |
| 344 | StatsCounter::Add(sample); |
| 345 | } |
| 346 | |
| 347 | bool MaxCounter::GetMetric(int* metric) const { |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 348 | if (samples_->Empty()) |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 349 | return false; |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 350 | |
| 351 | *metric = samples_->Max(); |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 355 | int MaxCounter::GetValueForEmptyInterval() const { |
| 356 | RTC_NOTREACHED(); |
| 357 | return 0; |
| 358 | } |
| 359 | |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 360 | PercentCounter::PercentCounter(Clock* clock, StatsCounterObserver* observer) |
| 361 | : StatsCounter(clock, |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 362 | kDefaultProcessIntervalMs, |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 363 | false, // |include_empty_intervals| |
| 364 | observer) {} |
| 365 | |
| 366 | void PercentCounter::Add(bool sample) { |
| 367 | StatsCounter::Add(sample ? 1 : 0); |
| 368 | } |
| 369 | |
| 370 | bool PercentCounter::GetMetric(int* metric) const { |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 371 | int64_t count = samples_->Count(); |
| 372 | if (count == 0) |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 373 | return false; |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 374 | |
| 375 | *metric = (samples_->Sum() * 100 + count / 2) / count; |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 376 | return true; |
| 377 | } |
| 378 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 379 | int PercentCounter::GetValueForEmptyInterval() const { |
| 380 | RTC_NOTREACHED(); |
| 381 | return 0; |
| 382 | } |
| 383 | |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 384 | PermilleCounter::PermilleCounter(Clock* clock, StatsCounterObserver* observer) |
| 385 | : StatsCounter(clock, |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 386 | kDefaultProcessIntervalMs, |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 387 | false, // |include_empty_intervals| |
| 388 | observer) {} |
| 389 | |
| 390 | void PermilleCounter::Add(bool sample) { |
| 391 | StatsCounter::Add(sample ? 1 : 0); |
| 392 | } |
| 393 | |
| 394 | bool PermilleCounter::GetMetric(int* metric) const { |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 395 | int64_t count = samples_->Count(); |
| 396 | if (count == 0) |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 397 | return false; |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 398 | |
| 399 | *metric = (samples_->Sum() * 1000 + count / 2) / count; |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 400 | return true; |
| 401 | } |
| 402 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 403 | int PermilleCounter::GetValueForEmptyInterval() const { |
| 404 | RTC_NOTREACHED(); |
| 405 | return 0; |
| 406 | } |
| 407 | |
asapersson | 5093b38 | 2016-08-15 01:20:30 -0700 | [diff] [blame] | 408 | RateCounter::RateCounter(Clock* clock, |
| 409 | StatsCounterObserver* observer, |
| 410 | bool include_empty_intervals) |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 411 | : StatsCounter(clock, |
| 412 | kDefaultProcessIntervalMs, |
| 413 | include_empty_intervals, |
| 414 | observer) {} |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 415 | |
| 416 | void RateCounter::Add(int sample) { |
| 417 | StatsCounter::Add(sample); |
| 418 | } |
| 419 | |
| 420 | bool RateCounter::GetMetric(int* metric) const { |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 421 | if (samples_->Empty()) |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 422 | return false; |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 423 | |
| 424 | *metric = (samples_->Sum() * 1000 + process_intervals_ms_ / 2) / |
| 425 | process_intervals_ms_; |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 426 | return true; |
| 427 | } |
| 428 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 429 | int RateCounter::GetValueForEmptyInterval() const { |
| 430 | return 0; |
| 431 | } |
| 432 | |
asapersson | 5093b38 | 2016-08-15 01:20:30 -0700 | [diff] [blame] | 433 | RateAccCounter::RateAccCounter(Clock* clock, |
| 434 | StatsCounterObserver* observer, |
| 435 | bool include_empty_intervals) |
asapersson | e402a14 | 2016-10-06 23:39:15 -0700 | [diff] [blame] | 436 | : StatsCounter(clock, |
| 437 | kDefaultProcessIntervalMs, |
| 438 | include_empty_intervals, |
| 439 | observer) {} |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 440 | |
asapersson | 93e1e23 | 2017-02-06 05:18:35 -0800 | [diff] [blame] | 441 | void RateAccCounter::Set(int64_t sample, uint32_t stream_id) { |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 442 | StatsCounter::Set(sample, stream_id); |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 443 | } |
| 444 | |
asapersson | 93e1e23 | 2017-02-06 05:18:35 -0800 | [diff] [blame] | 445 | void RateAccCounter::SetLast(int64_t sample, uint32_t stream_id) { |
| 446 | StatsCounter::SetLast(sample, stream_id); |
| 447 | } |
| 448 | |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 449 | bool RateAccCounter::GetMetric(int* metric) const { |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 450 | int64_t diff = samples_->Diff(); |
| 451 | if (diff < 0 || (!include_empty_intervals_ && diff == 0)) |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 452 | return false; |
asapersson | fe647f4 | 2016-11-01 00:21:34 -0700 | [diff] [blame] | 453 | |
| 454 | *metric = (diff * 1000 + process_intervals_ms_ / 2) / process_intervals_ms_; |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 455 | return true; |
| 456 | } |
| 457 | |
asapersson | ce2e136 | 2016-09-09 00:13:35 -0700 | [diff] [blame] | 458 | int RateAccCounter::GetValueForEmptyInterval() const { |
| 459 | return 0; |
| 460 | } |
| 461 | |
asapersson | 0e9d6d9 | 2016-05-23 06:07:55 -0700 | [diff] [blame] | 462 | } // namespace webrtc |