xref: /aosp_15_r20/external/webrtc/modules/audio_coding/neteq/expand_uma_logger.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "absl/strings/string_view.h"
13 #include "rtc_base/checks.h"
14 #include "system_wrappers/include/metrics.h"
15 
16 namespace webrtc {
17 namespace {
GetNewCountdown(const TickTimer & tick_timer,int logging_period_s)18 std::unique_ptr<TickTimer::Countdown> GetNewCountdown(
19     const TickTimer& tick_timer,
20     int logging_period_s) {
21   return tick_timer.GetNewCountdown((logging_period_s * 1000) /
22                                     tick_timer.ms_per_tick());
23 }
24 }  // namespace
25 
ExpandUmaLogger(absl::string_view uma_name,int logging_period_s,const TickTimer * tick_timer)26 ExpandUmaLogger::ExpandUmaLogger(absl::string_view uma_name,
27                                  int logging_period_s,
28                                  const TickTimer* tick_timer)
29     : uma_name_(uma_name),
30       logging_period_s_(logging_period_s),
31       tick_timer_(*tick_timer),
32       timer_(GetNewCountdown(tick_timer_, logging_period_s_)) {
33   RTC_DCHECK(tick_timer);
34   RTC_DCHECK_GT(logging_period_s_, 0);
35 }
36 
37 ExpandUmaLogger::~ExpandUmaLogger() = default;
38 
UpdateSampleCounter(uint64_t samples,int sample_rate_hz)39 void ExpandUmaLogger::UpdateSampleCounter(uint64_t samples,
40                                           int sample_rate_hz) {
41   if ((last_logged_value_ && *last_logged_value_ > samples) ||
42       sample_rate_hz_ != sample_rate_hz) {
43     // Sanity checks. The incremental counter moved backwards, or sample rate
44     // changed.
45     last_logged_value_.reset();
46   }
47   last_value_ = samples;
48   sample_rate_hz_ = sample_rate_hz;
49   if (!last_logged_value_) {
50     last_logged_value_ = absl::optional<uint64_t>(samples);
51   }
52 
53   if (!timer_->Finished()) {
54     // Not yet time to log.
55     return;
56   }
57 
58   RTC_DCHECK(last_logged_value_);
59   RTC_DCHECK_GE(last_value_, *last_logged_value_);
60   const uint64_t diff = last_value_ - *last_logged_value_;
61   last_logged_value_ = absl::optional<uint64_t>(last_value_);
62   // Calculate rate in percent.
63   RTC_DCHECK_GT(sample_rate_hz, 0);
64   const int rate = (100 * diff) / (sample_rate_hz * logging_period_s_);
65   RTC_DCHECK_GE(rate, 0);
66   RTC_DCHECK_LE(rate, 100);
67   RTC_HISTOGRAM_PERCENTAGE_SPARSE(uma_name_, rate);
68   timer_ = GetNewCountdown(tick_timer_, logging_period_s_);
69 }
70 
71 }  // namespace webrtc
72