1 /* Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
2 *
3 * Use of this source code is governed by a BSD-style license
4 * that can be found in the LICENSE file in the root of the source
5 * tree. An additional intellectual property rights grant can be found
6 * in the file PATENTS. All contributing project authors may
7 * be found in the AUTHORS file in the root of the source tree.
8 */
9
10 #include "modules/audio_coding/neteq/expand_uma_logger.h"
11
12 #include "rtc_base/checks.h"
13 #include "system_wrappers/include/metrics.h"
14
15 namespace webrtc {
16 namespace {
GetNewCountdown(const TickTimer & tick_timer,int logging_period_s)17 std::unique_ptr<TickTimer::Countdown> GetNewCountdown(
18 const TickTimer& tick_timer,
19 int logging_period_s) {
20 return tick_timer.GetNewCountdown((logging_period_s * 1000) /
21 tick_timer.ms_per_tick());
22 }
23 } // namespace
24
ExpandUmaLogger(std::string uma_name,int logging_period_s,const TickTimer * tick_timer)25 ExpandUmaLogger::ExpandUmaLogger(std::string uma_name,
26 int logging_period_s,
27 const TickTimer* tick_timer)
28 : uma_name_(uma_name),
29 logging_period_s_(logging_period_s),
30 tick_timer_(*tick_timer),
31 timer_(GetNewCountdown(tick_timer_, logging_period_s_)) {
32 RTC_DCHECK(tick_timer);
33 RTC_DCHECK_GT(logging_period_s_, 0);
34 }
35
36 ExpandUmaLogger::~ExpandUmaLogger() = default;
37
UpdateSampleCounter(uint64_t samples,int sample_rate_hz)38 void ExpandUmaLogger::UpdateSampleCounter(uint64_t samples,
39 int sample_rate_hz) {
40 if ((last_logged_value_ && *last_logged_value_ > samples) ||
41 sample_rate_hz_ != sample_rate_hz) {
42 // Sanity checks. The incremental counter moved backwards, or sample rate
43 // changed.
44 last_logged_value_.reset();
45 }
46 last_value_ = samples;
47 sample_rate_hz_ = sample_rate_hz;
48 if (!last_logged_value_) {
49 last_logged_value_ = absl::optional<uint64_t>(samples);
50 }
51
52 if (!timer_->Finished()) {
53 // Not yet time to log.
54 return;
55 }
56
57 RTC_DCHECK(last_logged_value_);
58 RTC_DCHECK_GE(last_value_, *last_logged_value_);
59 const uint64_t diff = last_value_ - *last_logged_value_;
60 last_logged_value_ = absl::optional<uint64_t>(last_value_);
61 // Calculate rate in percent.
62 RTC_DCHECK_GT(sample_rate_hz, 0);
63 const int rate = (100 * diff) / (sample_rate_hz * logging_period_s_);
64 RTC_DCHECK_GE(rate, 0);
65 RTC_DCHECK_LE(rate, 100);
66 RTC_HISTOGRAM_PERCENTAGE_SPARSE(uma_name_, rate);
67 timer_ = GetNewCountdown(tick_timer_, logging_period_s_);
68 }
69
70 } // namespace webrtc
71