| 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 char* 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 | void SerializeInfoImpl(base::Pickle* pickle) const override; |
| 63 |
| 64 | private: |
| 65 | // Clients should always use FactoryGet to create SparseHistogram. |
| 66 | explicit SparseHistogram(const char* name); |
| 67 |
| 68 | SparseHistogram(PersistentHistogramAllocator* allocator, |
| 69 | const char* 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 WriteAsciiBody(const HistogramSamples& snapshot, |
| 84 | bool graph_it, |
| 85 | const std::string& newline, |
| 86 | std::string* output) const; |
| 87 |
| 88 | // Write a common header message describing this histogram. |
| 89 | void WriteAsciiHeader(const HistogramSamples& snapshot, |
| 90 | std::string* output) const; |
| 91 |
| 92 | // For constuctor calling. |
| 93 | friend class SparseHistogramTest; |
| 94 |
| 95 | // Protects access to |samples_|. |
| 96 | mutable base::Lock lock_; |
| 97 |
| 98 | // Flag to indicate if PrepareFinalDelta has been previously called. |
| 99 | mutable bool final_delta_created_ = false; |
| 100 |
| 101 | std::unique_ptr<HistogramSamples> unlogged_samples_; |
| 102 | std::unique_ptr<HistogramSamples> logged_samples_; |
| 103 |
| 104 | DISALLOW_COPY_AND_ASSIGN(SparseHistogram); |
| 105 | }; |
| 106 |
| 107 | } // namespace base |
| 108 |
| 109 | #endif // BASE_METRICS_SPARSE_HISTOGRAM_H_ |
| 110 |