single_sample_metrics.h 3.7 KB

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