sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_RATE_STATISTICS_H_ |
| 12 | #define RTC_BASE_RATE_STATISTICS_H_ |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
Erik Språng | 03d9e52 | 2020-05-25 12:58:03 +0200 | [diff] [blame] | 17 | #include <deque> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | #include <memory> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 19 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 20 | #include "absl/types/optional.h" |
Mirko Bonadei | c66e004 | 2019-10-18 09:52:22 +0200 | [diff] [blame] | 21 | #include "rtc_base/system/rtc_export.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
Harald Alvestrand | a846cef | 2020-01-15 14:02:12 +0100 | [diff] [blame] | 25 | // Class to estimate rates based on counts in a sequence of 1-millisecond |
| 26 | // intervals. |
| 27 | |
| 28 | // This class uses int64 for all its numbers because some rates can be very |
| 29 | // high; for instance, a 20 Mbit/sec video stream can wrap a 32-bit byte |
| 30 | // counter in 14 minutes. |
| 31 | |
Erik Språng | 03d9e52 | 2020-05-25 12:58:03 +0200 | [diff] [blame] | 32 | // Note that timestamps used in Update(), Rate() and SetWindowSize() must never |
| 33 | // decrease for two consecutive calls. |
| 34 | // TODO(bugs.webrtc.org/11600): Migrate from int64_t to Timestamp. |
| 35 | |
Mirko Bonadei | c66e004 | 2019-10-18 09:52:22 +0200 | [diff] [blame] | 36 | class RTC_EXPORT RateStatistics { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 37 | public: |
| 38 | static constexpr float kBpsScale = 8000.0f; |
| 39 | |
| 40 | // max_window_size_ms = Maximum window size in ms for the rate estimation. |
| 41 | // Initial window size is set to this, but may be changed |
| 42 | // to something lower by calling SetWindowSize(). |
| 43 | // scale = coefficient to convert counts/ms to desired unit |
| 44 | // ex: kBpsScale (8000) for bits/s if count represents bytes. |
| 45 | RateStatistics(int64_t max_window_size_ms, float scale); |
Sergey Silkin | 40b7050 | 2018-08-27 10:55:07 +0200 | [diff] [blame] | 46 | |
| 47 | RateStatistics(const RateStatistics& other); |
| 48 | |
| 49 | RateStatistics(RateStatistics&& other); |
| 50 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 51 | ~RateStatistics(); |
| 52 | |
| 53 | // Reset instance to original state. |
| 54 | void Reset(); |
| 55 | |
| 56 | // Update rate with a new data point, moving averaging window as needed. |
Harald Alvestrand | a846cef | 2020-01-15 14:02:12 +0100 | [diff] [blame] | 57 | void Update(int64_t count, int64_t now_ms); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 58 | |
| 59 | // Note that despite this being a const method, it still updates the internal |
| 60 | // state (moves averaging window), but it doesn't make any alterations that |
| 61 | // are observable from the other methods, as long as supplied timestamps are |
| 62 | // from a monotonic clock. Ie, it doesn't matter if this call moves the |
| 63 | // window, since any subsequent call to Update or Rate would still have moved |
| 64 | // the window as much or more. |
Harald Alvestrand | a846cef | 2020-01-15 14:02:12 +0100 | [diff] [blame] | 65 | absl::optional<int64_t> Rate(int64_t now_ms) const; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 66 | |
| 67 | // Update the size of the averaging window. The maximum allowed value for |
| 68 | // window_size_ms is max_window_size_ms as supplied in the constructor. |
| 69 | bool SetWindowSize(int64_t window_size_ms, int64_t now_ms); |
| 70 | |
| 71 | private: |
| 72 | void EraseOld(int64_t now_ms); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 73 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 74 | struct Bucket { |
Erik Språng | 03d9e52 | 2020-05-25 12:58:03 +0200 | [diff] [blame] | 75 | explicit Bucket(int64_t timestamp); |
Harald Alvestrand | a846cef | 2020-01-15 14:02:12 +0100 | [diff] [blame] | 76 | int64_t sum; // Sum of all samples in this bucket. |
Erik Språng | 03d9e52 | 2020-05-25 12:58:03 +0200 | [diff] [blame] | 77 | int num_samples; // Number of samples in this bucket. |
| 78 | const int64_t timestamp; // Timestamp this bucket corresponds to. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 79 | }; |
Erik Språng | 03d9e52 | 2020-05-25 12:58:03 +0200 | [diff] [blame] | 80 | // All buckets within the time window, ordered by time. |
| 81 | std::deque<Bucket> buckets_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 82 | |
Erik Språng | 03d9e52 | 2020-05-25 12:58:03 +0200 | [diff] [blame] | 83 | // Total count recorded in all buckets. |
Harald Alvestrand | a846cef | 2020-01-15 14:02:12 +0100 | [diff] [blame] | 84 | int64_t accumulated_count_; |
| 85 | |
Erik Språng | 03d9e52 | 2020-05-25 12:58:03 +0200 | [diff] [blame] | 86 | // Timestamp of the first data point seen, or -1 of none seen. |
| 87 | int64_t first_timestamp_; |
| 88 | |
Harald Alvestrand | a846cef | 2020-01-15 14:02:12 +0100 | [diff] [blame] | 89 | // True if accumulated_count_ has ever grown too large to be |
| 90 | // contained in its integer type. |
| 91 | bool overflow_ = false; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 92 | |
| 93 | // The total number of samples in the buckets. |
Harald Alvestrand | a846cef | 2020-01-15 14:02:12 +0100 | [diff] [blame] | 94 | int num_samples_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 95 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 96 | // To convert counts/ms to desired units |
| 97 | const float scale_; |
| 98 | |
| 99 | // The window sizes, in ms, over which the rate is calculated. |
| 100 | const int64_t max_window_size_ms_; |
| 101 | int64_t current_window_size_ms_; |
| 102 | }; |
| 103 | } // namespace webrtc |
sprang@webrtc.org | 37968a9 | 2013-12-03 10:31:59 +0000 | [diff] [blame] | 104 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 105 | #endif // RTC_BASE_RATE_STATISTICS_H_ |