[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 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 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 11 | #include <map> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 12 | #include <memory> |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 13 | #include <string> |
| 14 | |
| 15 | #include "base/base_export.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 17 | #include "base/metrics/histogram_base.h" |
nikunjb | ddaa36a | 2016-10-19 04:12:55 | [diff] [blame] | 18 | #include "base/metrics/histogram_samples.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
Jun Kokatsu | 505af9f | 2020-05-05 11:59:47 | [diff] [blame^] | 20 | #include "base/values.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 21 | |
| 22 | namespace base { |
| 23 | |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 24 | class HistogramSamples; |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 25 | class PersistentHistogramAllocator; |
nikunjb | ddaa36a | 2016-10-19 04:12:55 | [diff] [blame] | 26 | class Pickle; |
| 27 | class PickleIterator; |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 28 | |
xhwang | 3e9ca56 | 2015-11-06 18:50:36 | [diff] [blame] | 29 | class BASE_EXPORT SparseHistogram : public HistogramBase { |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 30 | public: |
| 31 | // If there's one with same name, return the existing one. If not, create a |
| 32 | // new one. |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 33 | static HistogramBase* FactoryGet(const std::string& name, int32_t flags); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 34 | |
bcwhite | 27f5f34e | 2016-04-19 21:49:16 | [diff] [blame] | 35 | // Create a histogram using data in persistent storage. The allocator must |
| 36 | // live longer than the created sparse histogram. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 37 | static std::unique_ptr<HistogramBase> PersistentCreate( |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 38 | PersistentHistogramAllocator* allocator, |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 39 | const char* name, |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 40 | HistogramSamples::Metadata* meta, |
| 41 | HistogramSamples::Metadata* logged_meta); |
| 42 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 43 | ~SparseHistogram() override; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 44 | |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 45 | // HistogramBase implementation: |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 46 | uint64_t name_hash() const override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 47 | HistogramType GetHistogramType() const override; |
| 48 | bool HasConstructionArguments(Sample expected_minimum, |
| 49 | Sample expected_maximum, |
jam | 1eacd7e | 2016-02-08 22:48:16 | [diff] [blame] | 50 | uint32_t expected_bucket_count) const override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 51 | void Add(Sample value) override; |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 52 | void AddCount(Sample value, int count) override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 53 | void AddSamples(const HistogramSamples& samples) override; |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 54 | bool AddSamplesFromPickle(base::PickleIterator* iter) override; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 55 | std::unique_ptr<HistogramSamples> SnapshotSamples() const override; |
| 56 | std::unique_ptr<HistogramSamples> SnapshotDelta() override; |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 57 | std::unique_ptr<HistogramSamples> SnapshotFinalDelta() const override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 58 | void WriteAscii(std::string* output) const override; |
Jun Kokatsu | 505af9f | 2020-05-05 11:59:47 | [diff] [blame^] | 59 | base::DictionaryValue ToGraphDict() const override; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 60 | |
| 61 | protected: |
| 62 | // HistogramBase implementation: |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 63 | void SerializeInfoImpl(base::Pickle* pickle) const override; |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 64 | |
| 65 | private: |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 66 | // Clients should always use FactoryGet to create SparseHistogram. |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 67 | explicit SparseHistogram(const char* name); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 68 | |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 69 | SparseHistogram(PersistentHistogramAllocator* allocator, |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 70 | const char* name, |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 71 | HistogramSamples::Metadata* meta, |
| 72 | HistogramSamples::Metadata* logged_meta); |
| 73 | |
xhwang | 3e9ca56 | 2015-11-06 18:50:36 | [diff] [blame] | 74 | friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 75 | base::PickleIterator* iter); |
| 76 | static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 77 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 78 | void GetParameters(DictionaryValue* params) const override; |
| 79 | void GetCountAndBucketData(Count* count, |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 80 | int64_t* sum, |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 81 | ListValue* buckets) const override; |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 82 | |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 83 | // Helpers for emitting Ascii graphic. Each method appends data to output. |
Andrew Solovey | 4b8ee3d | 2020-01-27 14:22:42 | [diff] [blame] | 84 | void WriteAsciiBody(const HistogramSamples& snapshot, |
| 85 | bool graph_it, |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 86 | const std::string& newline, |
| 87 | std::string* output) const; |
| 88 | |
| 89 | // Write a common header message describing this histogram. |
Andrew Solovey | 4b8ee3d | 2020-01-27 14:22:42 | [diff] [blame] | 90 | void WriteAsciiHeader(const HistogramSamples& snapshot, |
[email protected] | f2bb320 | 2013-04-05 21:21:54 | [diff] [blame] | 91 | std::string* output) const; |
| 92 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 93 | // For constuctor calling. |
| 94 | friend class SparseHistogramTest; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 95 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 96 | // Protects access to |samples_|. |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 97 | mutable base::Lock lock_; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 98 | |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 99 | // Flag to indicate if PrepareFinalDelta has been previously called. |
| 100 | mutable bool final_delta_created_ = false; |
| 101 | |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 102 | std::unique_ptr<HistogramSamples> unlogged_samples_; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 103 | std::unique_ptr<HistogramSamples> logged_samples_; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 104 | |
| 105 | DISALLOW_COPY_AND_ASSIGN(SparseHistogram); |
| 106 | }; |
| 107 | |
| 108 | } // namespace base |
| 109 | |
| 110 | #endif // BASE_METRICS_SPARSE_HISTOGRAM_H_ |