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 3 // found in the LICENSE file. 4 5 // SampleVector implements HistogramSamples interface. It is used by all 6 // Histogram based classes to store samples. 7 8 #ifndef BASE_METRICS_SAMPLE_VECTOR_H_ 9 #define BASE_METRICS_SAMPLE_VECTOR_H_ 10 11 #include <stddef.h> 12 #include <stdint.h> 13 14 #include <memory> 15 #include <vector> 16 17 #include "base/compiler_specific.h" 18 #include "base/gtest_prod_util.h" 19 #include "base/macros.h" 20 #include "base/metrics/histogram_base.h" 21 #include "base/metrics/histogram_samples.h" 22 23 namespace base { 24 25 class BucketRanges; 26 27 class BASE_EXPORT SampleVector : public HistogramSamples { 28 public: 29 explicit SampleVector(const BucketRanges* bucket_ranges); 30 SampleVector(uint64_t id, const BucketRanges* bucket_ranges); 31 SampleVector(uint64_t id, 32 HistogramBase::AtomicCount* counts, 33 size_t counts_size, 34 Metadata* meta, 35 const BucketRanges* bucket_ranges); 36 ~SampleVector() override; 37 38 // HistogramSamples implementation: 39 void Accumulate(HistogramBase::Sample value, 40 HistogramBase::Count count) override; 41 HistogramBase::Count GetCount(HistogramBase::Sample value) const override; 42 HistogramBase::Count TotalCount() const override; 43 std::unique_ptr<SampleCountIterator> Iterator() const override; 44 45 // Get count of a specific bucket. 46 HistogramBase::Count GetCountAtIndex(size_t bucket_index) const; 47 48 protected: 49 bool AddSubtractImpl( 50 SampleCountIterator* iter, 51 HistogramSamples::Operator op) override; // |op| is ADD or SUBTRACT. 52 53 virtual size_t GetBucketIndex(HistogramBase::Sample value) const; 54 55 private: 56 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts); 57 FRIEND_TEST_ALL_PREFIXES(SharedHistogramTest, CorruptSampleCounts); 58 59 // In the case where this class manages the memory, here it is. 60 std::vector<HistogramBase::AtomicCount> local_counts_; 61 62 // These are raw pointers rather than objects for flexibility. The actual 63 // memory is either managed by local_counts_ above or by an external object 64 // and passed in directly. 65 HistogramBase::AtomicCount* counts_; 66 size_t counts_size_; 67 68 // Shares the same BucketRanges with Histogram object. 69 const BucketRanges* const bucket_ranges_; 70 71 DISALLOW_COPY_AND_ASSIGN(SampleVector); 72 }; 73 74 class BASE_EXPORT SampleVectorIterator : public SampleCountIterator { 75 public: 76 SampleVectorIterator(const std::vector<HistogramBase::AtomicCount>* counts, 77 const BucketRanges* bucket_ranges); 78 SampleVectorIterator(const HistogramBase::AtomicCount* counts, 79 size_t counts_size, 80 const BucketRanges* bucket_ranges); 81 ~SampleVectorIterator() override; 82 83 // SampleCountIterator implementation: 84 bool Done() const override; 85 void Next() override; 86 void Get(HistogramBase::Sample* min, 87 HistogramBase::Sample* max, 88 HistogramBase::Count* count) const override; 89 90 // SampleVector uses predefined buckets, so iterator can return bucket index. 91 bool GetBucketIndex(size_t* index) const override; 92 93 private: 94 void SkipEmptyBuckets(); 95 96 const HistogramBase::AtomicCount* counts_; 97 size_t counts_size_; 98 const BucketRanges* bucket_ranges_; 99 100 size_t index_; 101 }; 102 103 } // namespace base 104 105 #endif // BASE_METRICS_SAMPLE_VECTOR_H_ 106