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, |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 38 | DurableStringView name, |
Ramon Cano Aparicio | eb4e315 | 2025-05-09 14:26:57 | [diff] [blame] | 39 | uint64_t name_hash, |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 40 | HistogramSamples::Metadata* meta, |
| 41 | HistogramSamples::Metadata* logged_meta); |
| 42 | |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 43 | SparseHistogram(const SparseHistogram&) = delete; |
| 44 | SparseHistogram& operator=(const SparseHistogram&) = delete; |
| 45 | |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 46 | ~SparseHistogram() override; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 47 | |
Alexei Svitkine | 784ce69 | 2024-05-24 16:19:24 | [diff] [blame] | 48 | // HistogramBase: |
bcwhite | b036e432 | 2015-12-10 18:36:34 | [diff] [blame] | 49 | uint64_t name_hash() const override; |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 50 | HistogramType GetHistogramType() const override; |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 51 | bool HasConstructionArguments(Sample32 expected_minimum, |
| 52 | Sample32 expected_maximum, |
Peter Kasting | fc94f506 | 2022-06-08 16:41:45 | [diff] [blame] | 53 | size_t expected_bucket_count) const override; |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 54 | void Add(Sample32 value) override; |
| 55 | void AddCount(Sample32 value, int count) override; |
Alexei Svitkine | 1e86b0c89 | 2024-10-02 21:02:03 | [diff] [blame] | 56 | bool AddSamples(const HistogramSamples& samples) override; |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 57 | bool AddSamplesFromPickle(base::PickleIterator* iter) override; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 58 | std::unique_ptr<HistogramSamples> SnapshotSamples() const override; |
Luc Nguyen | e01c675 | 2022-12-01 18:40:15 | [diff] [blame] | 59 | std::unique_ptr<HistogramSamples> SnapshotUnloggedSamples() const override; |
| 60 | void MarkSamplesAsLogged(const HistogramSamples& samples) override; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 61 | std::unique_ptr<HistogramSamples> SnapshotDelta() override; |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 62 | std::unique_ptr<HistogramSamples> SnapshotFinalDelta() const override; |
Nathan Memmott | c034fa4e | 2022-07-15 23:59:43 | [diff] [blame] | 63 | base::Value::Dict ToGraphDict() const override; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 64 | |
| 65 | protected: |
Alexei Svitkine | 784ce69 | 2024-05-24 16:19:24 | [diff] [blame] | 66 | // HistogramBase: |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 67 | void SerializeInfoImpl(base::Pickle* pickle) const override; |
[email protected] | b4af2ec | 2012-10-05 21:29:44 | [diff] [blame] | 68 | |
| 69 | private: |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 70 | // Clients should always use FactoryGet to create SparseHistogram. |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 71 | explicit SparseHistogram(DurableStringView name); |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 72 | |
Ramon Cano Aparicio | eb4e315 | 2025-05-09 14:26:57 | [diff] [blame] | 73 | // Same as above, but takes a pre-computed `name_hash`. This function is more |
| 74 | // efficient as it avoids recomputing the hash if it's already known. The |
| 75 | // `name_hash` must be the hash of `name`, this is enforced with a DCHECK. |
| 76 | SparseHistogram(DurableStringView name, uint64_t name_hash); |
| 77 | |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 78 | SparseHistogram(PersistentHistogramAllocator* allocator, |
Roger McFarlane | 8952859 | 2025-02-20 20:50:27 | [diff] [blame] | 79 | DurableStringView name, |
Ramon Cano Aparicio | eb4e315 | 2025-05-09 14:26:57 | [diff] [blame] | 80 | uint64_t name_hash, |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 81 | HistogramSamples::Metadata* meta, |
| 82 | HistogramSamples::Metadata* logged_meta); |
| 83 | |
xhwang | 3e9ca56 | 2015-11-06 18:50:36 | [diff] [blame] | 84 | friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 85 | base::PickleIterator* iter); |
| 86 | static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 87 | |
Weilun Shi | f07fe53 | 2020-06-11 03:21:54 | [diff] [blame] | 88 | // Writes the type of the sparse histogram in the |params|. |
Nathan Memmott | c034fa4e | 2022-07-15 23:59:43 | [diff] [blame] | 89 | Value::Dict GetParameters() const override; |
[email protected] | 24a7ec5 | 2012-10-08 10:31:50 | [diff] [blame] | 90 | |
Weilun Shi | f07fe53 | 2020-06-11 03:21:54 | [diff] [blame] | 91 | // For constructor calling. |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 92 | friend class SparseHistogramTest; |
Luc Nguyen | 6458a5c | 2023-03-31 20:59:17 | [diff] [blame] | 93 | friend class HistogramThreadsafeTest; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 94 | |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 95 | // Protects access to |samples_|. |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 96 | mutable base::Lock lock_; |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 97 | |
bcwhite | 65e57d0 | 2016-05-13 14:39:40 | [diff] [blame] | 98 | // Flag to indicate if PrepareFinalDelta has been previously called. |
| 99 | mutable bool final_delta_created_ = false; |
| 100 | |
altimin | 498c838 | 2017-05-12 17:49:18 | [diff] [blame] | 101 | std::unique_ptr<HistogramSamples> unlogged_samples_; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 102 | std::unique_ptr<HistogramSamples> logged_samples_; |
[email protected] | 7c7a4275 | 2012-08-09 05:14:15 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | } // namespace base |
| 106 | |
| 107 | #endif // BASE_METRICS_SPARSE_HISTOGRAM_H_ |