rate_statistics.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #ifndef RTC_BASE_RATE_STATISTICS_H_
  11. #define RTC_BASE_RATE_STATISTICS_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <deque>
  15. #include <memory>
  16. #include "absl/types/optional.h"
  17. #include "rtc_base/system/rtc_export.h"
  18. namespace webrtc {
  19. // Class to estimate rates based on counts in a sequence of 1-millisecond
  20. // intervals.
  21. // This class uses int64 for all its numbers because some rates can be very
  22. // high; for instance, a 20 Mbit/sec video stream can wrap a 32-bit byte
  23. // counter in 14 minutes.
  24. // Note that timestamps used in Update(), Rate() and SetWindowSize() must never
  25. // decrease for two consecutive calls.
  26. // TODO(bugs.webrtc.org/11600): Migrate from int64_t to Timestamp.
  27. class RTC_EXPORT RateStatistics {
  28. public:
  29. static constexpr float kBpsScale = 8000.0f;
  30. // max_window_size_ms = Maximum window size in ms for the rate estimation.
  31. // Initial window size is set to this, but may be changed
  32. // to something lower by calling SetWindowSize().
  33. // scale = coefficient to convert counts/ms to desired unit
  34. // ex: kBpsScale (8000) for bits/s if count represents bytes.
  35. RateStatistics(int64_t max_window_size_ms, float scale);
  36. RateStatistics(const RateStatistics& other);
  37. RateStatistics(RateStatistics&& other);
  38. ~RateStatistics();
  39. // Reset instance to original state.
  40. void Reset();
  41. // Update rate with a new data point, moving averaging window as needed.
  42. void Update(int64_t count, int64_t now_ms);
  43. // Note that despite this being a const method, it still updates the internal
  44. // state (moves averaging window), but it doesn't make any alterations that
  45. // are observable from the other methods, as long as supplied timestamps are
  46. // from a monotonic clock. Ie, it doesn't matter if this call moves the
  47. // window, since any subsequent call to Update or Rate would still have moved
  48. // the window as much or more.
  49. absl::optional<int64_t> Rate(int64_t now_ms) const;
  50. // Update the size of the averaging window. The maximum allowed value for
  51. // window_size_ms is max_window_size_ms as supplied in the constructor.
  52. bool SetWindowSize(int64_t window_size_ms, int64_t now_ms);
  53. private:
  54. void EraseOld(int64_t now_ms);
  55. struct Bucket {
  56. explicit Bucket(int64_t timestamp);
  57. int64_t sum; // Sum of all samples in this bucket.
  58. int num_samples; // Number of samples in this bucket.
  59. const int64_t timestamp; // Timestamp this bucket corresponds to.
  60. };
  61. // All buckets within the time window, ordered by time.
  62. std::deque<Bucket> buckets_;
  63. // Total count recorded in all buckets.
  64. int64_t accumulated_count_;
  65. // Timestamp of the first data point seen, or -1 of none seen.
  66. int64_t first_timestamp_;
  67. // True if accumulated_count_ has ever grown too large to be
  68. // contained in its integer type.
  69. bool overflow_ = false;
  70. // The total number of samples in the buckets.
  71. int num_samples_;
  72. // To convert counts/ms to desired units
  73. const float scale_;
  74. // The window sizes, in ms, over which the rate is calculated.
  75. const int64_t max_window_size_ms_;
  76. int64_t current_window_size_ms_;
  77. };
  78. } // namespace webrtc
  79. #endif // RTC_BASE_RATE_STATISTICS_H_