mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-16 23:30:48 +01:00

WebRTC is now using C++14 so there is no need to use the Abseil version of std::make_unique. This CL has been created with the following steps: git grep -l absl::make_unique | sort | uniq > /tmp/make_unique.txt git grep -l absl::WrapUnique | sort | uniq > /tmp/wrap_unique.txt git grep -l "#include <memory>" | sort | uniq > /tmp/memory.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/make_unique.txt /tmp/wrap_unique.txt | sort | \ uniq > /tmp/only_make_unique.txt diff --new-line-format="" --unchanged-line-format="" \ /tmp/only_make_unique.txt /tmp/memory.txt | \ xargs grep -l "absl/memory" > /tmp/add-memory.txt git grep -l "\babsl::make_unique\b" | \ xargs sed -i "s/\babsl::make_unique\b/std::make_unique/g" git checkout PRESUBMIT.py abseil-in-webrtc.md cat /tmp/add-memory.txt | \ xargs sed -i \ 's/#include "absl\/memory\/memory.h"/#include <memory>/g' git cl format # Manual fix order of the new inserted #include <memory> cat /tmp/only_make_unique | xargs grep -l "#include <memory>" | \ xargs sed -i '/#include "absl\/memory\/memory.h"/d' git ls-files | grep BUILD.gn | \ xargs sed -i '/\/\/third_party\/abseil-cpp\/absl\/memory/d' python tools_webrtc/gn_check_autofix.py \ -m tryserver.webrtc -b linux_rel # Repead the gn_check_autofix step for other platforms git ls-files | grep BUILD.gn | \ xargs sed -i 's/absl\/memory:memory/absl\/memory/g' git cl format Bug: webrtc:10945 Change-Id: I3fe28ea80f4dd3ba3cf28effd151d5e1f19aff89 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/153221 Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Alessio Bazzica <alessiob@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#29209}
136 lines
4.3 KiB
C++
136 lines
4.3 KiB
C++
/*
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "rtc_base/rate_statistics.h"
|
|
|
|
#include <algorithm>
|
|
#include <memory>
|
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
namespace webrtc {
|
|
|
|
RateStatistics::RateStatistics(int64_t window_size_ms, float scale)
|
|
: buckets_(new Bucket[window_size_ms]()),
|
|
accumulated_count_(0),
|
|
num_samples_(0),
|
|
oldest_time_(-window_size_ms),
|
|
oldest_index_(0),
|
|
scale_(scale),
|
|
max_window_size_ms_(window_size_ms),
|
|
current_window_size_ms_(max_window_size_ms_) {}
|
|
|
|
RateStatistics::RateStatistics(const RateStatistics& other)
|
|
: accumulated_count_(other.accumulated_count_),
|
|
num_samples_(other.num_samples_),
|
|
oldest_time_(other.oldest_time_),
|
|
oldest_index_(other.oldest_index_),
|
|
scale_(other.scale_),
|
|
max_window_size_ms_(other.max_window_size_ms_),
|
|
current_window_size_ms_(other.current_window_size_ms_) {
|
|
buckets_ = std::make_unique<Bucket[]>(other.max_window_size_ms_);
|
|
std::copy(other.buckets_.get(),
|
|
other.buckets_.get() + other.max_window_size_ms_, buckets_.get());
|
|
}
|
|
|
|
RateStatistics::RateStatistics(RateStatistics&& other) = default;
|
|
|
|
RateStatistics::~RateStatistics() {}
|
|
|
|
void RateStatistics::Reset() {
|
|
accumulated_count_ = 0;
|
|
num_samples_ = 0;
|
|
oldest_time_ = -max_window_size_ms_;
|
|
oldest_index_ = 0;
|
|
current_window_size_ms_ = max_window_size_ms_;
|
|
for (int64_t i = 0; i < max_window_size_ms_; i++)
|
|
buckets_[i] = Bucket();
|
|
}
|
|
|
|
void RateStatistics::Update(size_t count, int64_t now_ms) {
|
|
if (now_ms < oldest_time_) {
|
|
// Too old data is ignored.
|
|
return;
|
|
}
|
|
|
|
EraseOld(now_ms);
|
|
|
|
// First ever sample, reset window to start now.
|
|
if (!IsInitialized())
|
|
oldest_time_ = now_ms;
|
|
|
|
uint32_t now_offset = static_cast<uint32_t>(now_ms - oldest_time_);
|
|
RTC_DCHECK_LT(now_offset, max_window_size_ms_);
|
|
uint32_t index = oldest_index_ + now_offset;
|
|
if (index >= max_window_size_ms_)
|
|
index -= max_window_size_ms_;
|
|
buckets_[index].sum += count;
|
|
++buckets_[index].samples;
|
|
accumulated_count_ += count;
|
|
++num_samples_;
|
|
}
|
|
|
|
absl::optional<uint32_t> RateStatistics::Rate(int64_t now_ms) const {
|
|
// Yeah, this const_cast ain't pretty, but the alternative is to declare most
|
|
// of the members as mutable...
|
|
const_cast<RateStatistics*>(this)->EraseOld(now_ms);
|
|
|
|
// If window is a single bucket or there is only one sample in a data set that
|
|
// has not grown to the full window size, treat this as rate unavailable.
|
|
int64_t active_window_size = now_ms - oldest_time_ + 1;
|
|
if (num_samples_ == 0 || active_window_size <= 1 ||
|
|
(num_samples_ <= 1 && active_window_size < current_window_size_ms_)) {
|
|
return absl::nullopt;
|
|
}
|
|
|
|
float scale = scale_ / active_window_size;
|
|
return static_cast<uint32_t>(accumulated_count_ * scale + 0.5f);
|
|
}
|
|
|
|
void RateStatistics::EraseOld(int64_t now_ms) {
|
|
if (!IsInitialized())
|
|
return;
|
|
|
|
// New oldest time that is included in data set.
|
|
int64_t new_oldest_time = now_ms - current_window_size_ms_ + 1;
|
|
|
|
// New oldest time is older than the current one, no need to cull data.
|
|
if (new_oldest_time <= oldest_time_)
|
|
return;
|
|
|
|
// Loop over buckets and remove too old data points.
|
|
while (num_samples_ > 0 && oldest_time_ < new_oldest_time) {
|
|
const Bucket& oldest_bucket = buckets_[oldest_index_];
|
|
RTC_DCHECK_GE(accumulated_count_, oldest_bucket.sum);
|
|
RTC_DCHECK_GE(num_samples_, oldest_bucket.samples);
|
|
accumulated_count_ -= oldest_bucket.sum;
|
|
num_samples_ -= oldest_bucket.samples;
|
|
buckets_[oldest_index_] = Bucket();
|
|
if (++oldest_index_ >= max_window_size_ms_)
|
|
oldest_index_ = 0;
|
|
++oldest_time_;
|
|
}
|
|
oldest_time_ = new_oldest_time;
|
|
}
|
|
|
|
bool RateStatistics::SetWindowSize(int64_t window_size_ms, int64_t now_ms) {
|
|
if (window_size_ms <= 0 || window_size_ms > max_window_size_ms_)
|
|
return false;
|
|
|
|
current_window_size_ms_ = window_size_ms;
|
|
EraseOld(now_ms);
|
|
return true;
|
|
}
|
|
|
|
bool RateStatistics::IsInitialized() const {
|
|
return oldest_time_ != -max_window_size_ms_;
|
|
}
|
|
|
|
} // namespace webrtc
|