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