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