• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_METRICS_SINGLE_SAMPLE_METRICS_H_
6 #define BASE_METRICS_SINGLE_SAMPLE_METRICS_H_
7 
8 #include <string>
9 
10 #include "base/base_export.h"
11 #include "base/macros.h"
12 #include "base/metrics/histogram_base.h"
13 
14 namespace base {
15 
16 // See base/metrics/histograms.h for parameter definitions. Must only be used
17 // and destroyed from the same thread as construction.
18 class BASE_EXPORT SingleSampleMetric {
19  public:
20   virtual ~SingleSampleMetric() = default;
21 
22   virtual void SetSample(HistogramBase::Sample sample) = 0;
23 };
24 
25 // Factory for creating single sample metrics. A single sample metric only
26 // reports its sample once at destruction time. The sample may be changed prior
27 // to destruction using the SetSample() method as many times as desired.
28 //
29 // The metric creation methods are safe to call from any thread, however the
30 // returned class must only be used and destroyed from the same thread as
31 // construction.
32 //
33 // See base/metrics/histogram_macros.h for usage recommendations and
34 // base/metrics/histogram.h for full parameter definitions.
35 class BASE_EXPORT SingleSampleMetricsFactory {
36  public:
37   virtual ~SingleSampleMetricsFactory() = default;
38 
39   // Returns the factory provided by SetFactory(), or if no factory has been set
40   // a default factory will be provided (future calls to SetFactory() will fail
41   // if the default factory is ever vended).
42   static SingleSampleMetricsFactory* Get();
43   static void SetFactory(std::unique_ptr<SingleSampleMetricsFactory> factory);
44 
45   // The factory normally persists until process shutdown, but in testing we
46   // should avoid leaking it since it sets a global.
47   static void DeleteFactoryForTesting();
48 
49   // The methods below return a single sample metric for counts histograms; see
50   // method comments for the corresponding histogram macro.
51 
52   // UMA_HISTOGRAM_CUSTOM_COUNTS()
53   virtual std::unique_ptr<SingleSampleMetric> CreateCustomCountsMetric(
54       const std::string& histogram_name,
55       HistogramBase::Sample min,
56       HistogramBase::Sample max,
57       uint32_t bucket_count) = 0;
58 };
59 
60 // Default implementation for when no factory has been provided to the process.
61 // Samples are only recorded within the current process in this case, so samples
62 // will be lost in the event of sudden process termination.
63 class BASE_EXPORT DefaultSingleSampleMetricsFactory
64     : public SingleSampleMetricsFactory {
65  public:
66   DefaultSingleSampleMetricsFactory() = default;
67   ~DefaultSingleSampleMetricsFactory() override = default;
68 
69   // SingleSampleMetricsFactory:
70   std::unique_ptr<SingleSampleMetric> CreateCustomCountsMetric(
71       const std::string& histogram_name,
72       HistogramBase::Sample min,
73       HistogramBase::Sample max,
74       uint32_t bucket_count) override;
75 
76  private:
77   DISALLOW_COPY_AND_ASSIGN(DefaultSingleSampleMetricsFactory);
78 };
79 
80 class BASE_EXPORT DefaultSingleSampleMetric : public SingleSampleMetric {
81  public:
82   DefaultSingleSampleMetric(const std::string& histogram_name,
83                             HistogramBase::Sample min,
84                             HistogramBase::Sample max,
85                             uint32_t bucket_count,
86                             int32_t flags);
87   ~DefaultSingleSampleMetric() override;
88 
89   // SingleSampleMetric:
90   void SetSample(HistogramBase::Sample sample) override;
91 
92  private:
93   HistogramBase* const histogram_;
94 
95   // The last sample provided to SetSample(). We use -1 as a sentinel value to
96   // indicate no sample has been set.
97   HistogramBase::Sample sample_ = -1;
98 
99   DISALLOW_COPY_AND_ASSIGN(DefaultSingleSampleMetric);
100 };
101 
102 }  // namespace base
103 
104 #endif  // BASE_METRICS_SINGLE_SAMPLE_METRICS_H_
105