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.

152 lines
5.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2015 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_tracker.h"
  11. #include <algorithm>
  12. #include "rtc_base/checks.h"
  13. #include "rtc_base/time_utils.h"
  14. namespace rtc {
  15. static const int64_t kTimeUnset = -1;
  16. RateTracker::RateTracker(int64_t bucket_milliseconds, size_t bucket_count)
  17. : bucket_milliseconds_(bucket_milliseconds),
  18. bucket_count_(bucket_count),
  19. sample_buckets_(new size_t[bucket_count + 1]),
  20. total_sample_count_(0u),
  21. bucket_start_time_milliseconds_(kTimeUnset) {
  22. RTC_CHECK(bucket_milliseconds > 0);
  23. RTC_CHECK(bucket_count > 0);
  24. }
  25. RateTracker::~RateTracker() {
  26. delete[] sample_buckets_;
  27. }
  28. double RateTracker::ComputeRateForInterval(
  29. int64_t interval_milliseconds) const {
  30. if (bucket_start_time_milliseconds_ == kTimeUnset) {
  31. return 0.0;
  32. }
  33. int64_t current_time = Time();
  34. // Calculate which buckets to sum up given the current time. If the time
  35. // has passed to a new bucket then we have to skip some of the oldest buckets.
  36. int64_t available_interval_milliseconds =
  37. std::min(interval_milliseconds,
  38. bucket_milliseconds_ * static_cast<int64_t>(bucket_count_));
  39. // number of old buckets (i.e. after the current bucket in the ring buffer)
  40. // that are expired given our current time interval.
  41. size_t buckets_to_skip;
  42. // Number of milliseconds of the first bucket that are not a portion of the
  43. // current interval.
  44. int64_t milliseconds_to_skip;
  45. if (current_time >
  46. initialization_time_milliseconds_ + available_interval_milliseconds) {
  47. int64_t time_to_skip =
  48. current_time - bucket_start_time_milliseconds_ +
  49. static_cast<int64_t>(bucket_count_) * bucket_milliseconds_ -
  50. available_interval_milliseconds;
  51. buckets_to_skip = time_to_skip / bucket_milliseconds_;
  52. milliseconds_to_skip = time_to_skip % bucket_milliseconds_;
  53. } else {
  54. buckets_to_skip = bucket_count_ - current_bucket_;
  55. milliseconds_to_skip = 0;
  56. available_interval_milliseconds =
  57. TimeDiff(current_time, initialization_time_milliseconds_);
  58. // Let one bucket interval pass after initialization before reporting.
  59. if (available_interval_milliseconds < bucket_milliseconds_) {
  60. return 0.0;
  61. }
  62. }
  63. // If we're skipping all buckets that means that there have been no samples
  64. // within the sampling interval so report 0.
  65. if (buckets_to_skip > bucket_count_ || available_interval_milliseconds == 0) {
  66. return 0.0;
  67. }
  68. size_t start_bucket = NextBucketIndex(current_bucket_ + buckets_to_skip);
  69. // Only count a portion of the first bucket according to how much of the
  70. // first bucket is within the current interval.
  71. size_t total_samples = ((sample_buckets_[start_bucket] *
  72. (bucket_milliseconds_ - milliseconds_to_skip)) +
  73. (bucket_milliseconds_ >> 1)) /
  74. bucket_milliseconds_;
  75. // All other buckets in the interval are counted in their entirety.
  76. for (size_t i = NextBucketIndex(start_bucket);
  77. i != NextBucketIndex(current_bucket_); i = NextBucketIndex(i)) {
  78. total_samples += sample_buckets_[i];
  79. }
  80. // Convert to samples per second.
  81. return static_cast<double>(total_samples * 1000) /
  82. static_cast<double>(available_interval_milliseconds);
  83. }
  84. double RateTracker::ComputeTotalRate() const {
  85. if (bucket_start_time_milliseconds_ == kTimeUnset) {
  86. return 0.0;
  87. }
  88. int64_t current_time = Time();
  89. if (current_time <= initialization_time_milliseconds_) {
  90. return 0.0;
  91. }
  92. return static_cast<double>(total_sample_count_ * 1000) /
  93. static_cast<double>(
  94. TimeDiff(current_time, initialization_time_milliseconds_));
  95. }
  96. size_t RateTracker::TotalSampleCount() const {
  97. return total_sample_count_;
  98. }
  99. void RateTracker::AddSamples(size_t sample_count) {
  100. EnsureInitialized();
  101. int64_t current_time = Time();
  102. // Advance the current bucket as needed for the current time, and reset
  103. // bucket counts as we advance.
  104. for (size_t i = 0;
  105. i <= bucket_count_ &&
  106. current_time >= bucket_start_time_milliseconds_ + bucket_milliseconds_;
  107. ++i) {
  108. bucket_start_time_milliseconds_ += bucket_milliseconds_;
  109. current_bucket_ = NextBucketIndex(current_bucket_);
  110. sample_buckets_[current_bucket_] = 0;
  111. }
  112. // Ensure that bucket_start_time_milliseconds_ is updated appropriately if
  113. // the entire buffer of samples has been expired.
  114. bucket_start_time_milliseconds_ +=
  115. bucket_milliseconds_ *
  116. ((current_time - bucket_start_time_milliseconds_) / bucket_milliseconds_);
  117. // Add all samples in the bucket that includes the current time.
  118. sample_buckets_[current_bucket_] += sample_count;
  119. total_sample_count_ += sample_count;
  120. }
  121. int64_t RateTracker::Time() const {
  122. return rtc::TimeMillis();
  123. }
  124. void RateTracker::EnsureInitialized() {
  125. if (bucket_start_time_milliseconds_ == kTimeUnset) {
  126. initialization_time_milliseconds_ = Time();
  127. bucket_start_time_milliseconds_ = initialization_time_milliseconds_;
  128. current_bucket_ = 0;
  129. // We only need to initialize the first bucket because we reset buckets when
  130. // current_bucket_ increments.
  131. sample_buckets_[current_bucket_] = 0;
  132. }
  133. }
  134. size_t RateTracker::NextBucketIndex(size_t bucket_index) const {
  135. return (bucket_index + 1u) % (bucket_count_ + 1u);
  136. }
  137. } // namespace rtc