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