blob: a91358b489adf4288cda9d982c8ce3e79bc728a5 [file] [log] [blame]
Henrik Lundin3ef3bfc2018-04-10 15:10:26 +02001/* 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"
Jonas Olssona4d87372019-07-05 19:08:33 +020011
Ali Tofigh714e3cb2022-07-20 12:53:07 +020012#include "absl/strings/string_view.h"
Henrik Lundin3ef3bfc2018-04-10 15:10:26 +020013#include "rtc_base/checks.h"
14#include "system_wrappers/include/metrics.h"
15
16namespace webrtc {
17namespace {
18std::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
Ali Tofigh714e3cb2022-07-20 12:53:07 +020026ExpandUmaLogger::ExpandUmaLogger(absl::string_view uma_name,
Henrik Lundin3ef3bfc2018-04-10 15:10:26 +020027 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
37ExpandUmaLogger::~ExpandUmaLogger() = default;
38
39void 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_) {
Danil Chapovalovb6021232018-06-19 13:26:36 +020050 last_logged_value_ = absl::optional<uint64_t>(samples);
Henrik Lundin3ef3bfc2018-04-10 15:10:26 +020051 }
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_;
Danil Chapovalovb6021232018-06-19 13:26:36 +020061 last_logged_value_ = absl::optional<uint64_t>(last_value_);
Henrik Lundin3ef3bfc2018-04-10 15:10:26 +020062 // 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