You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "rtc_base/rate_statistics.h"
  11. #include <algorithm>
  12. #include <memory>
  13. #include "rtc_base/checks.h"
  14. namespace webrtc {
  15. RateStatistics::RateStatistics(int64_t window_size_ms, float scale)
  16. : buckets_(new Bucket[window_size_ms]()),
  17. accumulated_count_(0),
  18. num_samples_(0),
  19. oldest_time_(-window_size_ms),
  20. oldest_index_(0),
  21. scale_(scale),
  22. max_window_size_ms_(window_size_ms),
  23. current_window_size_ms_(max_window_size_ms_) {}
  24. RateStatistics::RateStatistics(const RateStatistics& other)
  25. : accumulated_count_(other.accumulated_count_),
  26. num_samples_(other.num_samples_),
  27. oldest_time_(other.oldest_time_),
  28. oldest_index_(other.oldest_index_),
  29. scale_(other.scale_),
  30. max_window_size_ms_(other.max_window_size_ms_),
  31. current_window_size_ms_(other.current_window_size_ms_) {
  32. buckets_ = std::make_unique<Bucket[]>(other.max_window_size_ms_);
  33. std::copy(other.buckets_.get(),
  34. other.buckets_.get() + other.max_window_size_ms_, buckets_.get());
  35. }
  36. RateStatistics::RateStatistics(RateStatistics&& other) = default;
  37. RateStatistics::~RateStatistics() {}
  38. void RateStatistics::Reset() {
  39. accumulated_count_ = 0;
  40. num_samples_ = 0;
  41. oldest_time_ = -max_window_size_ms_;
  42. oldest_index_ = 0;
  43. current_window_size_ms_ = max_window_size_ms_;
  44. for (int64_t i = 0; i < max_window_size_ms_; i++)
  45. buckets_[i] = Bucket();
  46. }
  47. void RateStatistics::Update(size_t count, int64_t now_ms) {
  48. if (now_ms < oldest_time_) {
  49. // Too old data is ignored.
  50. return;
  51. }
  52. EraseOld(now_ms);
  53. // First ever sample, reset window to start now.
  54. if (!IsInitialized())
  55. oldest_time_ = now_ms;
  56. uint32_t now_offset = static_cast<uint32_t>(now_ms - oldest_time_);
  57. RTC_DCHECK_LT(now_offset, max_window_size_ms_);
  58. uint32_t index = oldest_index_ + now_offset;
  59. if (index >= max_window_size_ms_)
  60. index -= max_window_size_ms_;
  61. buckets_[index].sum += count;
  62. ++buckets_[index].samples;
  63. accumulated_count_ += count;
  64. ++num_samples_;
  65. }
  66. absl::optional<uint32_t> RateStatistics::Rate(int64_t now_ms) const {
  67. // Yeah, this const_cast ain't pretty, but the alternative is to declare most
  68. // of the members as mutable...
  69. const_cast<RateStatistics*>(this)->EraseOld(now_ms);
  70. // If window is a single bucket or there is only one sample in a data set that
  71. // has not grown to the full window size, treat this as rate unavailable.
  72. int64_t active_window_size = now_ms - oldest_time_ + 1;
  73. if (num_samples_ == 0 || active_window_size <= 1 ||
  74. (num_samples_ <= 1 && active_window_size < current_window_size_ms_)) {
  75. return absl::nullopt;
  76. }
  77. float scale = scale_ / active_window_size;
  78. return static_cast<uint32_t>(accumulated_count_ * scale + 0.5f);
  79. }
  80. void RateStatistics::EraseOld(int64_t now_ms) {
  81. if (!IsInitialized())
  82. return;
  83. // New oldest time that is included in data set.
  84. int64_t new_oldest_time = now_ms - current_window_size_ms_ + 1;
  85. // New oldest time is older than the current one, no need to cull data.
  86. if (new_oldest_time <= oldest_time_)
  87. return;
  88. // Loop over buckets and remove too old data points.
  89. while (num_samples_ > 0 && oldest_time_ < new_oldest_time) {
  90. const Bucket& oldest_bucket = buckets_[oldest_index_];
  91. RTC_DCHECK_GE(accumulated_count_, oldest_bucket.sum);
  92. RTC_DCHECK_GE(num_samples_, oldest_bucket.samples);
  93. accumulated_count_ -= oldest_bucket.sum;
  94. num_samples_ -= oldest_bucket.samples;
  95. buckets_[oldest_index_] = Bucket();
  96. if (++oldest_index_ >= max_window_size_ms_)
  97. oldest_index_ = 0;
  98. ++oldest_time_;
  99. }
  100. oldest_time_ = new_oldest_time;
  101. }
  102. bool RateStatistics::SetWindowSize(int64_t window_size_ms, int64_t now_ms) {
  103. if (window_size_ms <= 0 || window_size_ms > max_window_size_ms_)
  104. return false;
  105. current_window_size_ms_ = window_size_ms;
  106. EraseOld(now_ms);
  107. return true;
  108. }
  109. bool RateStatistics::IsInitialized() const {
  110. return oldest_time_ != -max_window_size_ms_;
  111. }
  112. } // namespace webrtc