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 #ifndef MODULES_AUDIO_CODING_NETEQ_EXPAND_UMA_LOGGER_H_
11 #define MODULES_AUDIO_CODING_NETEQ_EXPAND_UMA_LOGGER_H_
12 
13 #include <stdint.h>
14 
15 #include <memory>
16 #include <string>
17 
18 #include "absl/types/optional.h"
19 #include "api/neteq/tick_timer.h"
20 #include "rtc_base/constructor_magic.h"
21 
22 namespace webrtc {
23 
24 // This class is used to periodically log values to a UMA histogram. The caller
25 // is expected to update this class with an incremental sample counter which
26 // counts expand samples. At the end of each logging period, the class will
27 // calculate the fraction of samples that were expand samples during that period
28 // and report that in percent. The logging period must be strictly positive.
29 // Does not take ownership of tick_timer and the pointer must refer to a valid
30 // object that outlives the one constructed.
31 class ExpandUmaLogger {
32  public:
33   ExpandUmaLogger(std::string uma_name,
34                   int logging_period_s,
35                   const TickTimer* tick_timer);
36 
37   ~ExpandUmaLogger();
38 
39   // In this call, value should be an incremental sample counter. The sample
40   // rate must be strictly positive.
41   void UpdateSampleCounter(uint64_t value, int sample_rate_hz);
42 
43  private:
44   const std::string uma_name_;
45   const int logging_period_s_;
46   const TickTimer& tick_timer_;
47   std::unique_ptr<TickTimer::Countdown> timer_;
48   absl::optional<uint64_t> last_logged_value_;
49   uint64_t last_value_ = 0;
50   int sample_rate_hz_ = 0;
51 
52   RTC_DISALLOW_COPY_AND_ASSIGN(ExpandUmaLogger);
53 };
54 
55 }  // namespace webrtc
56 #endif  // MODULES_AUDIO_CODING_NETEQ_EXPAND_UMA_LOGGER_H_
57