| 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" | |
| 18 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
| 19 #include "base/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
| 20 #include "base/macros.h" | 19 #include "base/macros.h" |
| 21 #include "base/metrics/bucket_ranges.h" | |
| 22 #include "base/metrics/histogram_base.h" | 20 #include "base/metrics/histogram_base.h" |
| 23 #include "base/metrics/histogram_samples.h" | 21 #include "base/metrics/histogram_samples.h" |
| 24 #include "base/metrics/persistent_memory_allocator.h" | |
| 25 | 22 |
| 26 namespace base { | 23 namespace base { |
| 27 | 24 |
| 28 class BucketRanges; | 25 class BucketRanges; |
| 29 | 26 |
| 30 class BASE_EXPORT SampleVectorBase : public HistogramSamples { | 27 class BASE_EXPORT SampleVector : public HistogramSamples { |
| 31 public: | 28 public: |
| 32 SampleVectorBase(uint64_t id, const BucketRanges* bucket_ranges); | 29 explicit SampleVector(const BucketRanges* bucket_ranges); |
| 33 SampleVectorBase(uint64_t id, | 30 SampleVector(uint64_t id, const BucketRanges* bucket_ranges); |
| 34 Metadata* meta, | 31 SampleVector(uint64_t id, |
| 35 const BucketRanges* bucket_ranges); | 32 HistogramBase::AtomicCount* counts, |
| 36 ~SampleVectorBase() override; | 33 size_t counts_size, |
| 34 Metadata* meta, |
| 35 const BucketRanges* bucket_ranges); |
| 36 ~SampleVector() override; |
| 37 | 37 |
| 38 // HistogramSamples: | 38 // HistogramSamples implementation: |
| 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 | |
| 88 private: | 55 private: |
| 89 friend class SampleVectorTest; | |
| 90 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts); | 56 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts); |
| 91 FRIEND_TEST_ALL_PREFIXES(SharedHistogramTest, CorruptSampleCounts); | 57 FRIEND_TEST_ALL_PREFIXES(SharedHistogramTest, CorruptSampleCounts); |
| 92 | 58 |
| 93 // |counts_| is actually a pointer to a HistogramBase::AtomicCount array but | 59 // In the case where this class manages the memory, here it is. |
| 94 // is held as an AtomicWord for concurrency reasons. When combined with the | 60 std::vector<HistogramBase::AtomicCount> local_counts_; |
| 95 // single_sample held in the metadata, there are four possible states: | 61 |
| 96 // 1) single_sample == zero, counts_ == null | 62 // These are raw pointers rather than objects for flexibility. The actual |
| 97 // 2) single_sample != zero, counts_ == null | 63 // memory is either managed by local_counts_ above or by an external object |
| 98 // 3) single_sample != zero, counts_ != null BUT IS EMPTY | 64 // and passed in directly. |
| 99 // 4) single_sample == zero, counts_ != null and may have data | 65 HistogramBase::AtomicCount* counts_; |
| 100 // Once |counts_| is set, it can never revert and any existing single-sample | 66 size_t counts_size_; |
| 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; | |
| 105 | 67 |
| 106 // Shares the same BucketRanges with Histogram object. | 68 // Shares the same BucketRanges with Histogram object. |
| 107 const BucketRanges* const bucket_ranges_; | 69 const BucketRanges* const bucket_ranges_; |
| 108 | 70 |
| 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 | |
| 127 DISALLOW_COPY_AND_ASSIGN(SampleVector); | 71 DISALLOW_COPY_AND_ASSIGN(SampleVector); |
| 128 }; | 72 }; |
| 129 | 73 |
| 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. | |
| 152 class BASE_EXPORT SampleVectorIterator : public SampleCountIterator { | 74 class BASE_EXPORT SampleVectorIterator : public SampleCountIterator { |
| 153 public: | 75 public: |
| 154 SampleVectorIterator(const std::vector<HistogramBase::AtomicCount>* counts, | 76 SampleVectorIterator(const std::vector<HistogramBase::AtomicCount>* counts, |
| 155 const BucketRanges* bucket_ranges); | 77 const BucketRanges* bucket_ranges); |
| 156 SampleVectorIterator(const HistogramBase::AtomicCount* counts, | 78 SampleVectorIterator(const HistogramBase::AtomicCount* counts, |
| 157 size_t counts_size, | 79 size_t counts_size, |
| 158 const BucketRanges* bucket_ranges); | 80 const BucketRanges* bucket_ranges); |
| 159 ~SampleVectorIterator() override; | 81 ~SampleVectorIterator() override; |
| 160 | 82 |
| 161 // SampleCountIterator implementation: | 83 // SampleCountIterator implementation: |
| (...skipping 12 matching lines...) Expand all Loading... |
| 174 const HistogramBase::AtomicCount* counts_; | 96 const HistogramBase::AtomicCount* counts_; |
| 175 size_t counts_size_; | 97 size_t counts_size_; |
| 176 const BucketRanges* bucket_ranges_; | 98 const BucketRanges* bucket_ranges_; |
| 177 | 99 |
| 178 size_t index_; | 100 size_t index_; |
| 179 }; | 101 }; |
| 180 | 102 |
| 181 } // namespace base | 103 } // namespace base |
| 182 | 104 |
| 183 #endif // BASE_METRICS_SAMPLE_VECTOR_H_ | 105 #endif // BASE_METRICS_SAMPLE_VECTOR_H_ |
| OLD | NEW |