sparse_histogram.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/macros.h"
  13. #include "base/metrics/histogram_base.h"
  14. #include "base/metrics/histogram_samples.h"
  15. #include "base/synchronization/lock.h"
  16. #include "base/values.h"
  17. namespace base {
  18. class HistogramSamples;
  19. class PersistentHistogramAllocator;
  20. class Pickle;
  21. class PickleIterator;
  22. class BASE_EXPORT SparseHistogram : public HistogramBase {
  23. public:
  24. // If there's one with same name, return the existing one. If not, create a
  25. // new one.
  26. static HistogramBase* FactoryGet(const std::string& name, int32_t flags);
  27. // Create a histogram using data in persistent storage. The allocator must
  28. // live longer than the created sparse histogram.
  29. static std::unique_ptr<HistogramBase> PersistentCreate(
  30. PersistentHistogramAllocator* allocator,
  31. const char* name,
  32. HistogramSamples::Metadata* meta,
  33. HistogramSamples::Metadata* logged_meta);
  34. ~SparseHistogram() override;
  35. // HistogramBase implementation:
  36. uint64_t name_hash() const override;
  37. HistogramType GetHistogramType() const override;
  38. bool HasConstructionArguments(Sample expected_minimum,
  39. Sample expected_maximum,
  40. uint32_t expected_bucket_count) const override;
  41. void Add(Sample value) override;
  42. void AddCount(Sample value, int count) override;
  43. void AddSamples(const HistogramSamples& samples) override;
  44. bool AddSamplesFromPickle(base::PickleIterator* iter) override;
  45. std::unique_ptr<HistogramSamples> SnapshotSamples() const override;
  46. std::unique_ptr<HistogramSamples> SnapshotDelta() override;
  47. std::unique_ptr<HistogramSamples> SnapshotFinalDelta() const override;
  48. void WriteAscii(std::string* output) const override;
  49. base::DictionaryValue 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. void GetParameters(DictionaryValue* params) const override;
  65. // Helpers for emitting Ascii graphic. Each method appends data to output.
  66. void WriteAsciiBody(const HistogramSamples& snapshot,
  67. bool graph_it,
  68. const std::string& newline,
  69. std::string* output) const;
  70. // Write a common header message describing this histogram.
  71. void WriteAsciiHeader(const HistogramSamples& snapshot,
  72. std::string* output) const;
  73. // For constructor calling.
  74. friend class SparseHistogramTest;
  75. // Protects access to |samples_|.
  76. mutable base::Lock lock_;
  77. // Flag to indicate if PrepareFinalDelta has been previously called.
  78. mutable bool final_delta_created_ = false;
  79. std::unique_ptr<HistogramSamples> unlogged_samples_;
  80. std::unique_ptr<HistogramSamples> logged_samples_;
  81. DISALLOW_COPY_AND_ASSIGN(SparseHistogram);
  82. };
  83. } // namespace base
  84. #endif // BASE_METRICS_SPARSE_HISTOGRAM_H_