blob: 5c83796471e52862eb5e57c1bca0a99e5ce1b6a1 [file] [log] [blame]
sprang@webrtc.org37968a92013-12-03 10:31:591/*
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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "rtc_base/rate_statistics.h"
sprang@webrtc.org37968a92013-12-03 10:31:5912
Stefan Holmerfb8fc532016-04-22 13:48:2313#include <algorithm>
Yves Gereya688d112019-12-31 15:16:5114#include <limits>
Mirko Bonadei317a1f02019-09-17 15:06:1815#include <memory>
Stefan Holmerfb8fc532016-04-22 13:48:2316
Mirko Bonadei92ea95e2017-09-15 04:47:3117#include "rtc_base/checks.h"
Harald Alvestranda846cef2020-01-15 13:02:1218#include "rtc_base/logging.h"
19#include "rtc_base/numerics/safe_conversions.h"
henrike@webrtc.orgf2aafe42014-04-29 17:54:1720
sprang@webrtc.org37968a92013-12-03 10:31:5921namespace webrtc {
22
Erik Språng03d9e522020-05-25 10:58:0323RateStatistics::Bucket::Bucket(int64_t timestamp)
24 : sum(0), num_samples(0), timestamp(timestamp) {}
25
Erik Språng51e60302016-06-10 20:13:2126RateStatistics::RateStatistics(int64_t window_size_ms, float scale)
Erik Språng03d9e522020-05-25 10:58:0327 : accumulated_count_(0),
28 first_timestamp_(-1),
Erik Språng51e60302016-06-10 20:13:2129 num_samples_(0),
Erik Språng51e60302016-06-10 20:13:2130 scale_(scale),
31 max_window_size_ms_(window_size_ms),
32 current_window_size_ms_(max_window_size_ms_) {}
sprang@webrtc.org37968a92013-12-03 10:31:5933
Sergey Silkin40b70502018-08-27 08:55:0734RateStatistics::RateStatistics(const RateStatistics& other)
Erik Språng03d9e522020-05-25 10:58:0335 : buckets_(other.buckets_),
36 accumulated_count_(other.accumulated_count_),
37 first_timestamp_(other.first_timestamp_),
Harald Alvestranda846cef2020-01-15 13:02:1238 overflow_(other.overflow_),
Sergey Silkin40b70502018-08-27 08:55:0739 num_samples_(other.num_samples_),
Sergey Silkin40b70502018-08-27 08:55:0740 scale_(other.scale_),
41 max_window_size_ms_(other.max_window_size_ms_),
Erik Språng03d9e522020-05-25 10:58:0342 current_window_size_ms_(other.current_window_size_ms_) {}
Sergey Silkin40b70502018-08-27 08:55:0743
44RateStatistics::RateStatistics(RateStatistics&& other) = default;
45
tkchinf75d0082016-02-24 06:49:4246RateStatistics::~RateStatistics() {}
sprang@webrtc.org37968a92013-12-03 10:31:5947
48void RateStatistics::Reset() {
49 accumulated_count_ = 0;
Harald Alvestranda846cef2020-01-15 13:02:1250 overflow_ = false;
Erik Språng51e60302016-06-10 20:13:2151 num_samples_ = 0;
Erik Språng03d9e522020-05-25 10:58:0352 first_timestamp_ = -1;
Erik Språng51e60302016-06-10 20:13:2153 current_window_size_ms_ = max_window_size_ms_;
Erik Språng03d9e522020-05-25 10:58:0354 buckets_.clear();
sprang@webrtc.org37968a92013-12-03 10:31:5955}
56
Harald Alvestranda846cef2020-01-15 13:02:1257void RateStatistics::Update(int64_t count, int64_t now_ms) {
Erik Språng03d9e522020-05-25 10:58:0358 RTC_DCHECK_GE(count, 0);
sprang@webrtc.org37968a92013-12-03 10:31:5959
60 EraseOld(now_ms);
Yun Zhang4774a9f2021-11-23 09:11:2061 if (first_timestamp_ == -1 || num_samples_ == 0) {
Erik Språng03d9e522020-05-25 10:58:0362 first_timestamp_ = now_ms;
63 }
sprang@webrtc.org37968a92013-12-03 10:31:5964
Erik Språng03d9e522020-05-25 10:58:0365 if (buckets_.empty() || now_ms != buckets_.back().timestamp) {
66 if (!buckets_.empty() && now_ms < buckets_.back().timestamp) {
67 RTC_LOG(LS_WARNING) << "Timestamp " << now_ms
68 << " is before the last added "
69 "timestamp in the rate window: "
70 << buckets_.back().timestamp << ", aligning to that.";
71 now_ms = buckets_.back().timestamp;
72 }
73 buckets_.emplace_back(now_ms);
74 }
75 Bucket& last_bucket = buckets_.back();
76 last_bucket.sum += count;
77 ++last_bucket.num_samples;
Erik Språng51e60302016-06-10 20:13:2178
Harald Alvestranda846cef2020-01-15 13:02:1279 if (std::numeric_limits<int64_t>::max() - accumulated_count_ > count) {
80 accumulated_count_ += count;
81 } else {
82 overflow_ = true;
83 }
Erik Språng51e60302016-06-10 20:13:2184 ++num_samples_;
sprang@webrtc.org37968a92013-12-03 10:31:5985}
86
Harald Alvestranda846cef2020-01-15 13:02:1287absl::optional<int64_t> RateStatistics::Rate(int64_t now_ms) const {
sprangcd349d92016-07-13 16:11:2888 // Yeah, this const_cast ain't pretty, but the alternative is to declare most
89 // of the members as mutable...
90 const_cast<RateStatistics*>(this)->EraseOld(now_ms);
Erik Språng51e60302016-06-10 20:13:2191
Erik Språng03d9e522020-05-25 10:58:0392 int active_window_size = 0;
93 if (first_timestamp_ != -1) {
94 if (first_timestamp_ <= now_ms - current_window_size_ms_) {
95 // Count window as full even if no data points currently in view, if the
96 // data stream started before the window.
97 active_window_size = current_window_size_ms_;
98 } else {
99 // Size of a single bucket is 1ms, so even if now_ms == first_timestmap_
100 // the window size should be 1.
101 active_window_size = now_ms - first_timestamp_ + 1;
102 }
103 }
104
Erik Språng51e60302016-06-10 20:13:21105 // If window is a single bucket or there is only one sample in a data set that
Harald Alvestranda846cef2020-01-15 13:02:12106 // has not grown to the full window size, or if the accumulator has
107 // overflowed, treat this as rate unavailable.
Erik Språng51e60302016-06-10 20:13:21108 if (num_samples_ == 0 || active_window_size <= 1 ||
Harald Alvestranda846cef2020-01-15 13:02:12109 (num_samples_ <= 1 &&
110 rtc::SafeLt(active_window_size, current_window_size_ms_)) ||
111 overflow_) {
Danil Chapovalov0a1d1892018-06-21 09:48:25112 return absl::nullopt;
Erik Språng51e60302016-06-10 20:13:21113 }
114
Harald Alvestranda846cef2020-01-15 13:02:12115 float scale = static_cast<float>(scale_) / active_window_size;
Yves Gereya688d112019-12-31 15:16:51116 float result = accumulated_count_ * scale + 0.5f;
117
118 // Better return unavailable rate than garbage value (undefined behavior).
Harald Alvestranda846cef2020-01-15 13:02:12119 if (result > static_cast<float>(std::numeric_limits<int64_t>::max())) {
Yves Gereya688d112019-12-31 15:16:51120 return absl::nullopt;
121 }
Harald Alvestranda846cef2020-01-15 13:02:12122 return rtc::dchecked_cast<int64_t>(result);
sprang@webrtc.org37968a92013-12-03 10:31:59123}
124
125void RateStatistics::EraseOld(int64_t now_ms) {
Erik Språng51e60302016-06-10 20:13:21126 // New oldest time that is included in data set.
Erik Språng03d9e522020-05-25 10:58:03127 const int64_t new_oldest_time = now_ms - current_window_size_ms_ + 1;
Erik Språng51e60302016-06-10 20:13:21128
129 // Loop over buckets and remove too old data points.
Erik Språng03d9e522020-05-25 10:58:03130 while (!buckets_.empty() && buckets_.front().timestamp < new_oldest_time) {
131 const Bucket& oldest_bucket = buckets_.front();
Erik Språng51e60302016-06-10 20:13:21132 RTC_DCHECK_GE(accumulated_count_, oldest_bucket.sum);
Erik Språng03d9e522020-05-25 10:58:03133 RTC_DCHECK_GE(num_samples_, oldest_bucket.num_samples);
Erik Språng51e60302016-06-10 20:13:21134 accumulated_count_ -= oldest_bucket.sum;
Erik Språng03d9e522020-05-25 10:58:03135 num_samples_ -= oldest_bucket.num_samples;
136 buckets_.pop_front();
Harald Alvestranda846cef2020-01-15 13:02:12137 // This does not clear overflow_ even when counter is empty.
138 // TODO(https://bugs.webrtc.org/11247): Consider if overflow_ can be reset.
sprang@webrtc.org37968a92013-12-03 10:31:59139 }
sprang@webrtc.org37968a92013-12-03 10:31:59140}
141
Erik Språng51e60302016-06-10 20:13:21142bool RateStatistics::SetWindowSize(int64_t window_size_ms, int64_t now_ms) {
143 if (window_size_ms <= 0 || window_size_ms > max_window_size_ms_)
144 return false;
Erik Språng03d9e522020-05-25 10:58:03145 if (first_timestamp_ != -1) {
146 // If the window changes (e.g. decreases - removing data point, then
147 // increases again) we need to update the first timestamp mark as
148 // otherwise it indicates the window coveres a region of zeros, suddenly
149 // under-estimating the rate.
150 first_timestamp_ = std::max(first_timestamp_, now_ms - window_size_ms + 1);
151 }
Erik Språng51e60302016-06-10 20:13:21152 current_window_size_ms_ = window_size_ms;
153 EraseOld(now_ms);
154 return true;
155}
156
sprang@webrtc.org37968a92013-12-03 10:31:59157} // namespace webrtc