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 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/macros.h" 17 #include "base/metrics/histogram_base.h" 18 #include "base/metrics/histogram_samples.h" 19 #include "base/synchronization/lock.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 std::string& name, 39 HistogramSamples::Metadata* meta, 40 HistogramSamples::Metadata* logged_meta); 41 42 ~SparseHistogram() override; 43 44 // HistogramBase implementation: 45 uint64_t name_hash() const override; 46 HistogramType GetHistogramType() const override; 47 bool HasConstructionArguments(Sample expected_minimum, 48 Sample expected_maximum, 49 uint32_t expected_bucket_count) const override; 50 void Add(Sample value) override; 51 void AddCount(Sample value, int count) override; 52 void AddSamples(const HistogramSamples& samples) override; 53 bool AddSamplesFromPickle(base::PickleIterator* iter) override; 54 std::unique_ptr<HistogramSamples> SnapshotSamples() const override; 55 std::unique_ptr<HistogramSamples> SnapshotDelta() override; 56 std::unique_ptr<HistogramSamples> SnapshotFinalDelta() const override; 57 void WriteHTMLGraph(std::string* output) const override; 58 void WriteAscii(std::string* output) const override; 59 60 protected: 61 // HistogramBase implementation: 62 bool SerializeInfoImpl(base::Pickle* pickle) const override; 63 64 private: 65 // Clients should always use FactoryGet to create SparseHistogram. 66 explicit SparseHistogram(const std::string& name); 67 68 SparseHistogram(PersistentHistogramAllocator* allocator, 69 const std::string& name, 70 HistogramSamples::Metadata* meta, 71 HistogramSamples::Metadata* logged_meta); 72 73 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( 74 base::PickleIterator* iter); 75 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); 76 77 void GetParameters(DictionaryValue* params) const override; 78 void GetCountAndBucketData(Count* count, 79 int64_t* sum, 80 ListValue* buckets) const override; 81 82 // Helpers for emitting Ascii graphic. Each method appends data to output. 83 void WriteAsciiImpl(bool graph_it, 84 const std::string& newline, 85 std::string* output) const; 86 87 // Write a common header message describing this histogram. 88 void WriteAsciiHeader(const Count total_count, 89 std::string* output) const; 90 91 // For constuctor calling. 92 friend class SparseHistogramTest; 93 94 // Protects access to |samples_|. 95 mutable base::Lock lock_; 96 97 // Flag to indicate if PrepareFinalDelta has been previously called. 98 mutable bool final_delta_created_ = false; 99 100 std::unique_ptr<HistogramSamples> samples_; 101 std::unique_ptr<HistogramSamples> logged_samples_; 102 103 DISALLOW_COPY_AND_ASSIGN(SparseHistogram); 104 }; 105 106 } // namespace base 107 108 #endif // BASE_METRICS_SPARSE_HISTOGRAM_H_ 109