Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 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_SAMPLE_MAP_ITERATOR_H_ |
| 6 | #define BASE_METRICS_SAMPLE_MAP_ITERATOR_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
Peter Kasting | 85296747 | 2025-01-06 16:42:20 | [diff] [blame] | 10 | #include <atomic> |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 11 | #include <type_traits> |
| 12 | #include <utility> |
| 13 | |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 14 | #include "base/check.h" |
Peter Kasting | 85296747 | 2025-01-06 16:42:20 | [diff] [blame] | 15 | #include "base/memory/raw_ptr.h" |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 16 | #include "base/metrics/histogram_base.h" |
| 17 | #include "base/metrics/histogram_samples.h" |
Peter Kasting | 85296747 | 2025-01-06 16:42:20 | [diff] [blame] | 18 | #include "base/types/to_address.h" |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 19 | |
| 20 | namespace base { |
| 21 | |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 22 | // An iterator for going through a SampleMap. `MapT` is the underlying map type |
| 23 | // that stores the counts. `support_extraction` should be true iff the caller |
Peter Kasting | 85296747 | 2025-01-06 16:42:20 | [diff] [blame] | 24 | // wants this iterator to support extracting the values. If the counts are |
| 25 | // pointers, accesses to them will be atomic; see `kUseAtomicOps` below. |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 26 | template <typename MapT, bool support_extraction> |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 27 | class SampleMapIterator : public SampleCountIterator { |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 28 | private: |
| 29 | using T = std::conditional_t<support_extraction, MapT, const MapT>; |
| 30 | |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 31 | public: |
| 32 | explicit SampleMapIterator(T& sample_counts) |
| 33 | : iter_(sample_counts.begin()), end_(sample_counts.end()) { |
| 34 | SkipEmptyBuckets(); |
| 35 | } |
| 36 | |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 37 | ~SampleMapIterator() override { |
| 38 | if constexpr (support_extraction) { |
| 39 | // Ensure that the user has consumed all the samples in order to ensure no |
| 40 | // samples are lost. |
| 41 | DCHECK(Done()); |
| 42 | } |
| 43 | } |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 44 | |
| 45 | // SampleCountIterator: |
| 46 | bool Done() const override { return iter_ == end_; } |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 47 | |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 48 | void Next() override { |
| 49 | DCHECK(!Done()); |
| 50 | ++iter_; |
| 51 | SkipEmptyBuckets(); |
| 52 | } |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 53 | |
Ramon Cano Aparicio | 4a39d12 | 2025-01-20 13:00:17 | [diff] [blame] | 54 | void Get(HistogramBase::Sample32* min, |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 55 | int64_t* max, |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 56 | HistogramBase::Count32* count) override { |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 57 | DCHECK(!Done()); |
| 58 | *min = iter_->first; |
| 59 | *max = int64_t{iter_->first} + 1; |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 60 | if constexpr (support_extraction) { |
| 61 | *count = Exchange(); |
| 62 | } else { |
| 63 | *count = Load(); |
| 64 | } |
| 65 | } |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 66 | |
| 67 | private: |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 68 | using I = std::conditional_t<support_extraction, |
| 69 | typename T::iterator, |
| 70 | typename T::const_iterator>; |
| 71 | |
Peter Kasting | 85296747 | 2025-01-06 16:42:20 | [diff] [blame] | 72 | // If the counts are pointers, assume they may live in shared memory, which |
| 73 | // means accesses to them must be atomic, since other processes may attempt to |
| 74 | // concurrently modify their values. (Note that a lock wouldn't help here, |
| 75 | // since said other processes would not be aware of our lock.) If they are |
| 76 | // values, we don't bother with atomic ops; callers who want thread-safety can |
| 77 | // use locking. |
| 78 | static constexpr bool kUseAtomicOps = |
| 79 | IsPointerOrRawPtr<typename T::mapped_type>; |
| 80 | |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 81 | void SkipEmptyBuckets() { |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 82 | while (!Done() && Load() == 0) { |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 83 | ++iter_; |
| 84 | } |
| 85 | } |
| 86 | |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 87 | HistogramBase::Count32 Load() const { |
Peter Kasting | 85296747 | 2025-01-06 16:42:20 | [diff] [blame] | 88 | if constexpr (kUseAtomicOps) { |
| 89 | return iter_->second->load(std::memory_order_relaxed); |
| 90 | } else { |
| 91 | return iter_->second; |
| 92 | } |
| 93 | } |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 94 | |
Ramon Cano Aparicio | b2cba0f | 2025-01-22 21:26:10 | [diff] [blame] | 95 | HistogramBase::Count32 Exchange() const |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 96 | requires support_extraction |
| 97 | { |
Peter Kasting | 85296747 | 2025-01-06 16:42:20 | [diff] [blame] | 98 | if constexpr (kUseAtomicOps) { |
| 99 | return iter_->second->exchange(0, std::memory_order_relaxed); |
| 100 | } else { |
| 101 | return std::exchange(iter_->second, 0); |
| 102 | } |
Peter Kasting | 73cd82b | 2025-01-03 19:09:40 | [diff] [blame] | 103 | } |
| 104 | |
Peter Kasting | 858bf62ca | 2025-01-03 19:01:36 | [diff] [blame] | 105 | I iter_; |
| 106 | const I end_; |
| 107 | }; |
| 108 | |
| 109 | } // namespace base |
| 110 | |
| 111 | #endif // BASE_METRICS_SAMPLE_MAP_ITERATOR_H_ |