blob: d706569ecd15a020b0905988bef455b6dcedb255 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
Tim Psiaki63046262015-09-14 17:38:082 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
henrike@webrtc.orgf0488722014-05-13 18:00:263 *
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 Anton10542f22019-01-11 17:11:0011#ifndef RTC_BASE_RATE_TRACKER_H_
12#define RTC_BASE_RATE_TRACKER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Niels Möller65ec0fc2018-05-21 09:46:2014#include <stdint.h>
Henrik Kjellanderec78f1c2017-06-29 05:52:5015#include <stdlib.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2616
Henrik Kjellanderec78f1c2017-06-29 05:52:5017namespace 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.
22class 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 Alvestranda846cef2020-01-15 13:02:1244 int64_t TotalSampleCount() const;
Henrik Kjellanderec78f1c2017-06-29 05:52:5045
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 Alvestranda846cef2020-01-15 13:02:1248 void AddSamples(int64_t sample_count);
Henrik Kjellanderec78f1c2017-06-29 05:52:5049
Artem Titov96e3b992021-07-26 14:03:1450 // Increment count for bucket at `current_time_ms`.
Jonas Oreland3c5d5822020-12-14 11:45:0551 void AddSamplesAtTime(int64_t current_time_ms, int64_t sample_count);
52
Henrik Kjellanderec78f1c2017-06-29 05:52:5053 protected:
54 // overrideable for tests
55 virtual int64_t Time() const;
56
57 private:
58 void EnsureInitialized();
59 size_t NextBucketIndex(size_t bucket_index) const;
60
61 const int64_t bucket_milliseconds_;
62 const size_t bucket_count_;
Harald Alvestranda846cef2020-01-15 13:02:1263 int64_t* sample_buckets_;
Henrik Kjellanderec78f1c2017-06-29 05:52:5064 size_t total_sample_count_;
65 size_t current_bucket_;
66 int64_t bucket_start_time_milliseconds_;
67 int64_t initialization_time_milliseconds_;
68};
69
70} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:2671
Steve Anton10542f22019-01-11 17:11:0072#endif // RTC_BASE_RATE_TRACKER_H_