sparse_histogram.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2012 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_SPARSE_HISTOGRAM_H_
  5. #define BASE_METRICS_SPARSE_HISTOGRAM_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <map>
  9. #include <memory>
  10. #include <string>
  11. #include "base/base_export.h"
  12. #include "base/metrics/histogram_base.h"
  13. #include "base/metrics/histogram_samples.h"
  14. #include "base/synchronization/lock.h"
  15. #include "base/values.h"
  16. namespace base {
  17. class HistogramSamples;
  18. class PersistentHistogramAllocator;
  19. class Pickle;
  20. class PickleIterator;
  21. class BASE_EXPORT SparseHistogram : public HistogramBase {
  22. public:
  23. // If there's one with same name, return the existing one. If not, create a
  24. // new one.
  25. static HistogramBase* FactoryGet(const std::string& name, int32_t flags);
  26. // Create a histogram using data in persistent storage. The allocator must
  27. // live longer than the created sparse histogram.
  28. static std::unique_ptr<HistogramBase> PersistentCreate(
  29. PersistentHistogramAllocator* allocator,
  30. const char* name,
  31. HistogramSamples::Metadata* meta,
  32. HistogramSamples::Metadata* logged_meta);
  33. SparseHistogram(const SparseHistogram&) = delete;
  34. SparseHistogram& operator=(const SparseHistogram&) = delete;
  35. ~SparseHistogram() override;
  36. // HistogramBase implementation:
  37. uint64_t name_hash() const override;
  38. HistogramType GetHistogramType() const override;
  39. bool HasConstructionArguments(Sample expected_minimum,
  40. Sample expected_maximum,
  41. size_t expected_bucket_count) const override;
  42. void Add(Sample value) override;
  43. void AddCount(Sample value, int count) override;
  44. void AddSamples(const HistogramSamples& samples) override;
  45. bool AddSamplesFromPickle(base::PickleIterator* iter) override;
  46. std::unique_ptr<HistogramSamples> SnapshotSamples() const override;
  47. std::unique_ptr<HistogramSamples> SnapshotDelta() override;
  48. std::unique_ptr<HistogramSamples> SnapshotFinalDelta() const override;
  49. base::Value::Dict ToGraphDict() const override;
  50. protected:
  51. // HistogramBase implementation:
  52. void SerializeInfoImpl(base::Pickle* pickle) const override;
  53. private:
  54. // Clients should always use FactoryGet to create SparseHistogram.
  55. explicit SparseHistogram(const char* name);
  56. SparseHistogram(PersistentHistogramAllocator* allocator,
  57. const char* name,
  58. HistogramSamples::Metadata* meta,
  59. HistogramSamples::Metadata* logged_meta);
  60. friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo(
  61. base::PickleIterator* iter);
  62. static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter);
  63. // Writes the type of the sparse histogram in the |params|.
  64. Value::Dict GetParameters() const override;
  65. // For constructor calling.
  66. friend class SparseHistogramTest;
  67. // Protects access to |samples_|.
  68. mutable base::Lock lock_;
  69. // Flag to indicate if PrepareFinalDelta has been previously called.
  70. mutable bool final_delta_created_ = false;
  71. std::unique_ptr<HistogramSamples> unlogged_samples_;
  72. std::unique_ptr<HistogramSamples> logged_samples_;
  73. };
  74. } // namespace base
  75. #endif // BASE_METRICS_SPARSE_HISTOGRAM_H_