OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // SampleVector implements HistogramSamples interface. It is used by all | 5 // SampleVector implements HistogramSamples interface. It is used by all |
6 // Histogram based classes to store samples. | 6 // Histogram based classes to store samples. |
7 | 7 |
8 #ifndef BASE_METRICS_SAMPLE_VECTOR_H_ | 8 #ifndef BASE_METRICS_SAMPLE_VECTOR_H_ |
9 #define BASE_METRICS_SAMPLE_VECTOR_H_ | 9 #define BASE_METRICS_SAMPLE_VECTOR_H_ |
10 | 10 |
11 #include <stddef.h> | 11 #include <stddef.h> |
12 #include <stdint.h> | 12 #include <stdint.h> |
13 | 13 |
14 #include <memory> | 14 #include <memory> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
| 17 #include "base/atomicops.h" |
17 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
18 #include "base/gtest_prod_util.h" | 19 #include "base/gtest_prod_util.h" |
19 #include "base/macros.h" | 20 #include "base/macros.h" |
| 21 #include "base/metrics/bucket_ranges.h" |
20 #include "base/metrics/histogram_base.h" | 22 #include "base/metrics/histogram_base.h" |
21 #include "base/metrics/histogram_samples.h" | 23 #include "base/metrics/histogram_samples.h" |
| 24 #include "base/metrics/persistent_memory_allocator.h" |
22 | 25 |
23 namespace base { | 26 namespace base { |
24 | 27 |
25 class BucketRanges; | 28 class BucketRanges; |
26 | 29 |
27 class BASE_EXPORT SampleVector : public HistogramSamples { | 30 class BASE_EXPORT SampleVectorBase : public HistogramSamples { |
28 public: | 31 public: |
29 explicit SampleVector(const BucketRanges* bucket_ranges); | 32 SampleVectorBase(uint64_t id, const BucketRanges* bucket_ranges); |
30 SampleVector(uint64_t id, const BucketRanges* bucket_ranges); | 33 SampleVectorBase(uint64_t id, |
31 SampleVector(uint64_t id, | 34 Metadata* meta, |
32 HistogramBase::AtomicCount* counts, | 35 const BucketRanges* bucket_ranges); |
33 size_t counts_size, | 36 ~SampleVectorBase() override; |
34 Metadata* meta, | |
35 const BucketRanges* bucket_ranges); | |
36 ~SampleVector() override; | |
37 | 37 |
38 // HistogramSamples implementation: | 38 // HistogramSamples: |
39 void Accumulate(HistogramBase::Sample value, | 39 void Accumulate(HistogramBase::Sample value, |
40 HistogramBase::Count count) override; | 40 HistogramBase::Count count) override; |
41 HistogramBase::Count GetCount(HistogramBase::Sample value) const override; | 41 HistogramBase::Count GetCount(HistogramBase::Sample value) const override; |
42 HistogramBase::Count TotalCount() const override; | 42 HistogramBase::Count TotalCount() const override; |
43 std::unique_ptr<SampleCountIterator> Iterator() const override; | 43 std::unique_ptr<SampleCountIterator> Iterator() const override; |
44 | 44 |
45 // Get count of a specific bucket. | 45 // Get count of a specific bucket. |
46 HistogramBase::Count GetCountAtIndex(size_t bucket_index) const; | 46 HistogramBase::Count GetCountAtIndex(size_t bucket_index) const; |
47 | 47 |
48 protected: | 48 protected: |
49 bool AddSubtractImpl( | 49 bool AddSubtractImpl( |
50 SampleCountIterator* iter, | 50 SampleCountIterator* iter, |
51 HistogramSamples::Operator op) override; // |op| is ADD or SUBTRACT. | 51 HistogramSamples::Operator op) override; // |op| is ADD or SUBTRACT. |
52 | 52 |
53 virtual size_t GetBucketIndex(HistogramBase::Sample value) const; | 53 virtual size_t GetBucketIndex(HistogramBase::Sample value) const; |
54 | 54 |
| 55 // Moves the single-sample value to a mounted "counts" array. |
| 56 void MoveSingleSampleToCounts(); |
| 57 |
| 58 // Mounts (creating if necessary) an array of "counts" for multi-value |
| 59 // storage. |
| 60 void MountCountsStorageAndMoveSingleSample(); |
| 61 |
| 62 // Mounts "counts" storage that already exists. This does not attempt to move |
| 63 // any single-sample information to that storage as that would violate the |
| 64 // "const" restriction that is often used to indicate read-only memory. |
| 65 virtual bool MountExistingCountsStorage() const = 0; |
| 66 |
| 67 // Creates "counts" storage and returns a pointer to it. Ownership of the |
| 68 // array remains with the called method but will never change. This must be |
| 69 // called while some sort of lock is held to prevent reentry. |
| 70 virtual HistogramBase::Count* CreateCountsStorageWhileLocked() = 0; |
| 71 |
| 72 HistogramBase::AtomicCount* counts() { |
| 73 return reinterpret_cast<HistogramBase::AtomicCount*>( |
| 74 subtle::Acquire_Load(&counts_)); |
| 75 } |
| 76 |
| 77 const HistogramBase::AtomicCount* counts() const { |
| 78 return reinterpret_cast<HistogramBase::AtomicCount*>( |
| 79 subtle::Acquire_Load(&counts_)); |
| 80 } |
| 81 |
| 82 void set_counts(const HistogramBase::AtomicCount* counts) const { |
| 83 subtle::Release_Store(&counts_, reinterpret_cast<uintptr_t>(counts)); |
| 84 } |
| 85 |
| 86 size_t counts_size() const { return bucket_ranges_->bucket_count(); } |
| 87 |
55 private: | 88 private: |
| 89 friend class SampleVectorTest; |
56 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts); | 90 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts); |
57 FRIEND_TEST_ALL_PREFIXES(SharedHistogramTest, CorruptSampleCounts); | 91 FRIEND_TEST_ALL_PREFIXES(SharedHistogramTest, CorruptSampleCounts); |
58 | 92 |
59 // In the case where this class manages the memory, here it is. | 93 // |counts_| is actually a pointer to a HistogramBase::AtomicCount array but |
60 std::vector<HistogramBase::AtomicCount> local_counts_; | 94 // is held as an AtomicWord for concurrency reasons. When combined with the |
61 | 95 // single_sample held in the metadata, there are four possible states: |
62 // These are raw pointers rather than objects for flexibility. The actual | 96 // 1) single_sample == zero, counts_ == null |
63 // memory is either managed by local_counts_ above or by an external object | 97 // 2) single_sample != zero, counts_ == null |
64 // and passed in directly. | 98 // 3) single_sample != zero, counts_ != null BUT IS EMPTY |
65 HistogramBase::AtomicCount* counts_; | 99 // 4) single_sample == zero, counts_ != null and may have data |
66 size_t counts_size_; | 100 // Once |counts_| is set, it can never revert and any existing single-sample |
| 101 // must be moved to this storage. It is mutable because changing it doesn't |
| 102 // change the (const) data but must adapt if a non-const object causes the |
| 103 // storage to be allocated and updated. |
| 104 mutable subtle::AtomicWord counts_ = 0; |
67 | 105 |
68 // Shares the same BucketRanges with Histogram object. | 106 // Shares the same BucketRanges with Histogram object. |
69 const BucketRanges* const bucket_ranges_; | 107 const BucketRanges* const bucket_ranges_; |
70 | 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(SampleVectorBase); |
| 110 }; |
| 111 |
| 112 // A sample vector that uses local memory for the counts array. |
| 113 class BASE_EXPORT SampleVector : public SampleVectorBase { |
| 114 public: |
| 115 explicit SampleVector(const BucketRanges* bucket_ranges); |
| 116 SampleVector(uint64_t id, const BucketRanges* bucket_ranges); |
| 117 ~SampleVector() override; |
| 118 |
| 119 private: |
| 120 // SampleVectorBase: |
| 121 bool MountExistingCountsStorage() const override; |
| 122 HistogramBase::Count* CreateCountsStorageWhileLocked() override; |
| 123 |
| 124 // Simple local storage for counts. |
| 125 mutable std::vector<HistogramBase::AtomicCount> local_counts_; |
| 126 |
71 DISALLOW_COPY_AND_ASSIGN(SampleVector); | 127 DISALLOW_COPY_AND_ASSIGN(SampleVector); |
72 }; | 128 }; |
73 | 129 |
| 130 // A sample vector that uses persistent memory for the counts array. |
| 131 class BASE_EXPORT PersistentSampleVector : public SampleVectorBase { |
| 132 public: |
| 133 PersistentSampleVector(uint64_t id, |
| 134 const BucketRanges* bucket_ranges, |
| 135 Metadata* meta, |
| 136 const DelayedPersistentAllocation& counts); |
| 137 ~PersistentSampleVector() override; |
| 138 |
| 139 private: |
| 140 // SampleVectorBase: |
| 141 bool MountExistingCountsStorage() const override; |
| 142 HistogramBase::Count* CreateCountsStorageWhileLocked() override; |
| 143 |
| 144 // Persistent storage for counts. |
| 145 DelayedPersistentAllocation persistent_counts_; |
| 146 |
| 147 DISALLOW_COPY_AND_ASSIGN(PersistentSampleVector); |
| 148 }; |
| 149 |
| 150 // An iterator for sample vectors. This could be defined privately in the .cc |
| 151 // file but is here for easy testing. |
74 class BASE_EXPORT SampleVectorIterator : public SampleCountIterator { | 152 class BASE_EXPORT SampleVectorIterator : public SampleCountIterator { |
75 public: | 153 public: |
76 SampleVectorIterator(const std::vector<HistogramBase::AtomicCount>* counts, | 154 SampleVectorIterator(const std::vector<HistogramBase::AtomicCount>* counts, |
77 const BucketRanges* bucket_ranges); | 155 const BucketRanges* bucket_ranges); |
78 SampleVectorIterator(const HistogramBase::AtomicCount* counts, | 156 SampleVectorIterator(const HistogramBase::AtomicCount* counts, |
79 size_t counts_size, | 157 size_t counts_size, |
80 const BucketRanges* bucket_ranges); | 158 const BucketRanges* bucket_ranges); |
81 ~SampleVectorIterator() override; | 159 ~SampleVectorIterator() override; |
82 | 160 |
83 // SampleCountIterator implementation: | 161 // SampleCountIterator implementation: |
(...skipping 12 matching lines...) Expand all Loading... |
96 const HistogramBase::AtomicCount* counts_; | 174 const HistogramBase::AtomicCount* counts_; |
97 size_t counts_size_; | 175 size_t counts_size_; |
98 const BucketRanges* bucket_ranges_; | 176 const BucketRanges* bucket_ranges_; |
99 | 177 |
100 size_t index_; | 178 size_t index_; |
101 }; | 179 }; |
102 | 180 |
103 } // namespace base | 181 } // namespace base |
104 | 182 |
105 #endif // BASE_METRICS_SAMPLE_VECTOR_H_ | 183 #endif // BASE_METRICS_SAMPLE_VECTOR_H_ |
OLD | NEW |