Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [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 | // PersistentSampleMap implements HistogramSamples interface. It is used |
| 6 | // by the SparseHistogram class to store samples in persistent memory which |
| 7 | // allows it to be shared between processes or live across restarts. |
| 8 | |
| 9 | #ifndef BASE_METRICS_PERSISTENT_SAMPLE_MAP_H_ |
| 10 | #define BASE_METRICS_PERSISTENT_SAMPLE_MAP_H_ |
| 11 | |
| 12 | #include <stdint.h> |
| 13 | |
Peter Kasting | 85296747 | 2025-01-06 16:42:20 | [diff] [blame] | 14 | #include <atomic> |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 15 | #include <map> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 16 | #include <memory> |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 17 | #include <optional> |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 18 | |
David Sanders | 6e70994 | 2022-04-05 06:49:26 | [diff] [blame] | 19 | #include "base/base_export.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 20 | #include "base/memory/raw_ptr.h" |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 21 | #include "base/metrics/histogram_base.h" |
| 22 | #include "base/metrics/histogram_samples.h" |
Victor Hugo Vianna Silva | 858706f | 2025-01-07 15:01:07 | [diff] [blame] | 23 | #include "base/metrics/persistent_histogram_allocator.h" |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 24 | #include "base/metrics/persistent_memory_allocator.h" |
| 25 | |
| 26 | namespace base { |
| 27 | |
| 28 | // The logic here is similar to that of SampleMap but with different data |
| 29 | // structures. Changes here likely need to be duplicated there. |
| 30 | class BASE_EXPORT PersistentSampleMap : public HistogramSamples { |
| 31 | public: |
Peter Kasting | c4639bb | 2025-01-07 18:34:13 | [diff] [blame] | 32 | using SampleToCountMap = |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 33 | std::map<HistogramBase::Sample32, |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 34 | raw_ptr<std::atomic<HistogramBase::Count32>, CtnExperimental>>; |
Peter Kasting | c4639bb | 2025-01-07 18:34:13 | [diff] [blame] | 35 | |
bcwhite | 1d02f43 | 2016-04-28 18:59:53 | [diff] [blame] | 36 | // Constructs a persistent sample map using a PersistentHistogramAllocator |
| 37 | // as the data source for persistent records. |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 38 | PersistentSampleMap(uint64_t id, |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 39 | PersistentHistogramAllocator* allocator, |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 40 | Metadata* meta); |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 41 | |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 42 | PersistentSampleMap(const PersistentSampleMap&) = delete; |
| 43 | PersistentSampleMap& operator=(const PersistentSampleMap&) = delete; |
| 44 | |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 45 | ~PersistentSampleMap() override; |
| 46 | |
| 47 | // HistogramSamples: |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 48 | void Accumulate(HistogramBase::Sample32 value, |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 49 | HistogramBase::Count32 count) override; |
| 50 | HistogramBase::Count32 GetCount(HistogramBase::Sample32 value) const override; |
| 51 | HistogramBase::Count32 TotalCount() const override; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 52 | std::unique_ptr<SampleCountIterator> Iterator() const override; |
Luc Nguyen | 6458a5c | 2023-03-31 20:59:17 | [diff] [blame] | 53 | std::unique_ptr<SampleCountIterator> ExtractingIterator() override; |
Luc Nguyen | bbaab29 | 2023-09-28 23:30:57 | [diff] [blame] | 54 | bool IsDefinitelyEmpty() const override; |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 55 | |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 56 | // Uses a persistent-memory |iterator| to locate and return information about |
Luc Nguyen | f56cc82 | 2023-09-11 21:19:35 | [diff] [blame] | 57 | // the next record holding information for a PersistentSampleMap (in |
| 58 | // particular, the reference and the sample |value| it holds). The record |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 59 | // could be for any Map so return the |sample_map_id| as well. |
| 60 | static PersistentMemoryAllocator::Reference GetNextPersistentRecord( |
| 61 | PersistentMemoryAllocator::Iterator& iterator, |
Luc Nguyen | f56cc82 | 2023-09-11 21:19:35 | [diff] [blame] | 62 | uint64_t* sample_map_id, |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 63 | HistogramBase::Sample32* value); |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 64 | |
| 65 | // Creates a new record in an |allocator| storing count information for a |
| 66 | // specific sample |value| of a histogram with the given |sample_map_id|. |
| 67 | static PersistentMemoryAllocator::Reference CreatePersistentRecord( |
| 68 | PersistentMemoryAllocator* allocator, |
| 69 | uint64_t sample_map_id, |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 70 | HistogramBase::Sample32 value); |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 71 | |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 72 | protected: |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 73 | // Performs arithmetic. |op| is ADD or SUBTRACT. |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 74 | bool AddSubtractImpl(SampleCountIterator* iter, Operator op) override; |
| 75 | |
| 76 | // Gets a pointer to a "count" corresponding to a given |value|. Returns NULL |
| 77 | // if sample does not exist. |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 78 | std::atomic<HistogramBase::Count32>* GetSampleCountStorage( |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 79 | HistogramBase::Sample32 value) const; |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 80 | |
| 81 | // Gets a pointer to a "count" corresponding to a given |value|, creating |
Peter Kasting | c4639bb | 2025-01-07 18:34:13 | [diff] [blame] | 82 | // the sample (initialized to zero) if it does not already exist. |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 83 | std::atomic<HistogramBase::Count32>* GetOrCreateSampleCountStorage( |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 84 | HistogramBase::Sample32 value); |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 85 | |
| 86 | private: |
bcwhite | 1d02f43 | 2016-04-28 18:59:53 | [diff] [blame] | 87 | // Gets the object that manages persistent records. This returns the |
| 88 | // |records_| member after first initializing it if necessary. |
Peter Kasting | c4639bb | 2025-01-07 18:34:13 | [diff] [blame] | 89 | PersistentSampleMapRecords* GetRecords() const; |
bcwhite | 1d02f43 | 2016-04-28 18:59:53 | [diff] [blame] | 90 | |
Luc Nguyen | 146f34f | 2023-08-31 18:07:13 | [diff] [blame] | 91 | // Imports samples from persistent memory by iterating over all sample records |
| 92 | // found therein, adding them to the sample_counts_ map. If a count for the |
| 93 | // sample |until_value| is found, stop the import and return a pointer to that |
| 94 | // counter. If that value is not found, null will be returned after all |
| 95 | // currently available samples have been loaded. Pass a nullopt for |
| 96 | // |until_value| to force the importing of all available samples (null will |
| 97 | // always be returned in this case). |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 98 | std::atomic<HistogramBase::Count32>* ImportSamples( |
Ramon Cano Aparicio | 79e0664 | 2025-01-09 19:10:22 | [diff] [blame] | 99 | std::optional<HistogramBase::Sample32> until_value = std::nullopt) const; |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 100 | |
| 101 | // All created/loaded sample values and their associated counts. The storage |
bcwhite | b0bb919 | 2016-04-18 01:33:10 | [diff] [blame] | 102 | // for the actual Count numbers is owned by the |records_| object and its |
| 103 | // underlying allocator. |
Peter Kasting | c4639bb | 2025-01-07 18:34:13 | [diff] [blame] | 104 | mutable SampleToCountMap sample_counts_; |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 105 | |
bcwhite | 1d02f43 | 2016-04-28 18:59:53 | [diff] [blame] | 106 | // The allocator that manages histograms inside persistent memory. This is |
| 107 | // owned externally and is expected to live beyond the life of this object. |
Peter Kasting | c4639bb | 2025-01-07 18:34:13 | [diff] [blame] | 108 | mutable raw_ptr<PersistentHistogramAllocator> allocator_; |
bcwhite | 1d02f43 | 2016-04-28 18:59:53 | [diff] [blame] | 109 | |
Luc Nguyen | f56cc82 | 2023-09-11 21:19:35 | [diff] [blame] | 110 | // The object that manages sample records inside persistent memory. The |
| 111 | // underlying data used is owned by the |allocator_| object (above). This |
| 112 | // value is lazily-initialized on first use via the GetRecords() accessor |
| 113 | // method. |
Peter Kasting | c4639bb | 2025-01-07 18:34:13 | [diff] [blame] | 114 | mutable std::unique_ptr<PersistentSampleMapRecords> records_ = nullptr; |
bcwhite | 3dd85c4f | 2016-03-17 13:21:56 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | } // namespace base |
| 118 | |
| 119 | #endif // BASE_METRICS_PERSISTENT_SAMPLE_MAP_H_ |