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