Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 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" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 16 | #include "base/metrics/histogram_base.h" |
nikunjb | ddaa36a | 2016-10-19 04:12:55 | [diff] [blame] | 17 | #include "base/metrics/histogram_samples.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 18 | #include "base/synchronization/lock.h" |
Jun Kokatsu | 505af9f | 2020-05-05 11:59:47 | [diff] [blame] | 19 | #include "base/values.h" |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 20 | |
| 21 | namespace base { |
| 22 | |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 23 | class HistogramSamples; |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 24 | class PersistentHistogramAllocator; |
nikunjb | ddaa36a | 2016-10-19 04:12:55 | [diff] [blame] | 25 | class Pickle; |
| 26 | class PickleIterator; |
[email protected] | 877ef56 | 2012-10-20 02:56:18 | [diff] [blame] | 27 | |
xhwang | 3e9ca56 | 2015-11-06 18:50:36 | [diff] [blame] | 28 | class BASE_EXPORT SparseHistogram : public HistogramBase { |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 29 | public: |
| 30 | // If there's one with same name, return the existing one. If not, create a |
| 31 | // new one. |
Alexei Svitkine | 784ce69 | 2024-05-24 16:19:24 | [diff] [blame^] | 32 | static HistogramBase* FactoryGet(std::string_view name, int32_t flags); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 33 | |
bcwhite | 27f5f34e | 2016-04-19 21:49:16 | [diff] [blame] | 34 | // Create a histogram using data in persistent storage. The allocator must |
| 35 | // live longer than the created sparse histogram. |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 36 | static std::unique_ptr<HistogramBase> PersistentCreate( |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 37 | PersistentHistogramAllocator* allocator, |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 38 | const char* name, |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 39 | HistogramSamples::Metadata* meta, |
| 40 | HistogramSamples::Metadata* logged_meta); |
| 41 | |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 42 | SparseHistogram(const SparseHistogram&) = delete; |
| 43 | SparseHistogram& operator=(const SparseHistogram&) = delete; |
| 44 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 45 | ~SparseHistogram() override; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 46 | |
Alexei Svitkine | 784ce69 | 2024-05-24 16:19:24 | [diff] [blame^] | 47 | // HistogramBase: |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 48 | uint64_t name_hash() const override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 49 | HistogramType GetHistogramType() const override; |
| 50 | bool HasConstructionArguments(Sample expected_minimum, |
| 51 | Sample expected_maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 52 | size_t expected_bucket_count) const override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 53 | void Add(Sample value) override; |
amohammadkhan | 6779b5c3 | 2015-08-05 20:31:11 | [diff] [blame] | 54 | void AddCount(Sample value, int count) override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 55 | void AddSamples(const HistogramSamples& samples) override; |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 56 | bool AddSamplesFromPickle(base::PickleIterator* iter) override; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 57 | std::unique_ptr<HistogramSamples> SnapshotSamples() const override; |
Luc Nguyen | e01c675 | 2022-12-01 18:40:15 | [diff] [blame] | 58 | std::unique_ptr<HistogramSamples> SnapshotUnloggedSamples() const override; |
| 59 | void MarkSamplesAsLogged(const HistogramSamples& samples) override; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 60 | std::unique_ptr<HistogramSamples> SnapshotDelta() override; |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 61 | std::unique_ptr<HistogramSamples> SnapshotFinalDelta() const override; |
Nathan Memmott | c034fa4e | 2022-07-15 23:59:43 | [diff] [blame] | 62 | base::Value::Dict ToGraphDict() const override; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 63 | |
| 64 | protected: |
Alexei Svitkine | 784ce69 | 2024-05-24 16:19:24 | [diff] [blame^] | 65 | // HistogramBase: |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 66 | void SerializeInfoImpl(base::Pickle* pickle) const override; |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 67 | |
| 68 | private: |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 69 | // Clients should always use FactoryGet to create SparseHistogram. |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 70 | explicit SparseHistogram(const char* name); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 71 | |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 72 | SparseHistogram(PersistentHistogramAllocator* allocator, |
Brian White | d1c9108 | 2017-11-03 14:46:42 | [diff] [blame] | 73 | const char* name, |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 74 | HistogramSamples::Metadata* meta, |
| 75 | HistogramSamples::Metadata* logged_meta); |
| 76 | |
xhwang | 3e9ca56 | 2015-11-06 18:50:36 | [diff] [blame] | 77 | friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 78 | base::PickleIterator* iter); |
| 79 | static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 80 | |
Weilun Shi | f07fe53 | 2020-06-11 03:21:54 | [diff] [blame] | 81 | // Writes the type of the sparse histogram in the |params|. |
Nathan Memmott | c034fa4e | 2022-07-15 23:59:43 | [diff] [blame] | 82 | Value::Dict GetParameters() const override; |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 83 | |
Weilun Shi | f07fe53 | 2020-06-11 03:21:54 | [diff] [blame] | 84 | // For constructor calling. |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 85 | friend class SparseHistogramTest; |
Luc Nguyen | 6458a5c | 2023-03-31 20:59:17 | [diff] [blame] | 86 | friend class HistogramThreadsafeTest; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 87 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 88 | // Protects access to |samples_|. |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 89 | mutable base::Lock lock_; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 90 | |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 91 | // Flag to indicate if PrepareFinalDelta has been previously called. |
| 92 | mutable bool final_delta_created_ = false; |
| 93 | |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 94 | std::unique_ptr<HistogramSamples> unlogged_samples_; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 95 | std::unique_ptr<HistogramSamples> logged_samples_; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // namespace base |
| 99 | |
| 100 | #endif // BASE_METRICS_SPARSE_HISTOGRAM_H_ |