henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
Tim Psiaki | 6304626 | 2015-09-14 10:38:08 -0700 | [diff] [blame] | 2 | * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_RATE_TRACKER_H_ |
| 12 | #define RTC_BASE_RATE_TRACKER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Niels Möller | 65ec0fc | 2018-05-21 11:46:20 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 15 | #include <stdlib.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 17 | namespace rtc { |
| 18 | |
| 19 | // Computes units per second over a given interval by tracking the units over |
| 20 | // each bucket of a given size and calculating the instantaneous rate assuming |
| 21 | // that over each bucket the rate was constant. |
| 22 | class RateTracker { |
| 23 | public: |
| 24 | RateTracker(int64_t bucket_milliseconds, size_t bucket_count); |
| 25 | virtual ~RateTracker(); |
| 26 | |
| 27 | // Computes the average rate over the most recent interval_milliseconds, |
| 28 | // or if the first sample was added within this period, computes the rate |
| 29 | // since the first sample was added. |
| 30 | double ComputeRateForInterval(int64_t interval_milliseconds) const; |
| 31 | |
| 32 | // Computes the average rate over the rate tracker's recording interval |
| 33 | // of bucket_milliseconds * bucket_count. |
| 34 | double ComputeRate() const { |
| 35 | return ComputeRateForInterval(bucket_milliseconds_ * |
| 36 | static_cast<int64_t>(bucket_count_)); |
| 37 | } |
| 38 | |
| 39 | // Computes the average rate since the first sample was added to the |
| 40 | // rate tracker. |
| 41 | double ComputeTotalRate() const; |
| 42 | |
| 43 | // The total number of samples added. |
Harald Alvestrand | a846cef | 2020-01-15 14:02:12 +0100 | [diff] [blame^] | 44 | int64_t TotalSampleCount() const; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 45 | |
| 46 | // Reads the current time in order to determine the appropriate bucket for |
| 47 | // these samples, and increments the count for that bucket by sample_count. |
Harald Alvestrand | a846cef | 2020-01-15 14:02:12 +0100 | [diff] [blame^] | 48 | void AddSamples(int64_t sample_count); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 49 | |
| 50 | protected: |
| 51 | // overrideable for tests |
| 52 | virtual int64_t Time() const; |
| 53 | |
| 54 | private: |
| 55 | void EnsureInitialized(); |
| 56 | size_t NextBucketIndex(size_t bucket_index) const; |
| 57 | |
| 58 | const int64_t bucket_milliseconds_; |
| 59 | const size_t bucket_count_; |
Harald Alvestrand | a846cef | 2020-01-15 14:02:12 +0100 | [diff] [blame^] | 60 | int64_t* sample_buckets_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 61 | size_t total_sample_count_; |
| 62 | size_t current_bucket_; |
| 63 | int64_t bucket_start_time_milliseconds_; |
| 64 | int64_t initialization_time_milliseconds_; |
| 65 | }; |
| 66 | |
| 67 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 69 | #endif // RTC_BASE_RATE_TRACKER_H_ |