Chromium Code Reviews| 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 #ifndef BASE_METRICS_HISTOGRAM_SAMPLES_H_ | 5 #ifndef BASE_METRICS_HISTOGRAM_SAMPLES_H_ |
| 6 #define BASE_METRICS_HISTOGRAM_SAMPLES_H_ | 6 #define BASE_METRICS_HISTOGRAM_SAMPLES_H_ |
| 7 | 7 |
| 8 #include "base/atomicops.h" | |
| 8 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 9 #include "base/metrics/histogram_base.h" | 10 #include "base/metrics/histogram_base.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 | 12 |
| 12 namespace base { | 13 namespace base { |
| 13 | 14 |
| 14 class Pickle; | 15 class Pickle; |
| 15 class PickleIterator; | 16 class PickleIterator; |
| 16 class SampleCountIterator; | 17 class SampleCountIterator; |
| 17 | 18 |
| 18 // HistogramSamples is a container storing all samples of a histogram. | 19 // HistogramSamples is a container storing all samples of a histogram. |
| 19 class BASE_EXPORT HistogramSamples { | 20 class BASE_EXPORT HistogramSamples { |
| 20 public: | 21 public: |
| 21 HistogramSamples(); | 22 struct Metadata { |
| 23 // Initialized when the sample-set is first created with a value provided | |
| 24 // by the caller. It is generally used to identify the sample-set across | |
| 25 // threads and processes, though not necessarily uniquely as it is possible | |
| 26 // to have multiple sample-sets representing subsets of the data. | |
| 27 const uint64 id; | |
| 28 | |
| 29 // The sum of all the entries, effectivly the sum(sample * count) for | |
| 30 // all samples. Despite being atomic, no guarantees are made on the | |
| 31 // accuracy of this value; there may be races during histogram | |
| 32 // accumulation and snapshotting that we choose to accept. It should | |
| 33 // be treated as approximate. | |
| 34 #ifdef ARCH_CPU_64_BITS | |
| 35 subtle::Atomic64 sum; | |
| 36 #else // No Atomic64 on 32-bit platforms. | |
| 37 int64 sum; | |
| 38 #endif | |
| 39 | |
| 40 // A "redundant" count helps identify memory corruption. It redundantly | |
| 41 // stores the total number of samples accumulated in the histogram. We | |
| 42 // can compare this count to the sum of the counts (TotalCount() function), | |
| 43 // and detect problems. Note, depending on the implementation of different | |
| 44 // histogram types, there might be races during histogram accumulation | |
| 45 // and snapshotting that we choose to accept. In this case, the tallies | |
| 46 // might mismatch even when no memory corruption has happened. | |
| 47 HistogramBase::AtomicCount redundant_count; | |
| 48 | |
| 49 Metadata() : id(0), sum(0), redundant_count(0) {} | |
| 50 }; | |
| 51 | |
| 52 explicit HistogramSamples(uint64 id); | |
|
Alexei Svitkine (slow)
2015/11/25 18:05:27
Why does HistogramSamples need an id, as opposed t
bcwhite
2015/11/25 21:54:17
The id is shared. Histograms use shared sample-se
| |
| 53 HistogramSamples(uint64 id, Metadata* meta); | |
| 22 virtual ~HistogramSamples(); | 54 virtual ~HistogramSamples(); |
| 23 | 55 |
| 24 virtual void Accumulate(HistogramBase::Sample value, | 56 virtual void Accumulate(HistogramBase::Sample value, |
| 25 HistogramBase::Count count) = 0; | 57 HistogramBase::Count count) = 0; |
| 26 virtual HistogramBase::Count GetCount(HistogramBase::Sample value) const = 0; | 58 virtual HistogramBase::Count GetCount(HistogramBase::Sample value) const = 0; |
| 27 virtual HistogramBase::Count TotalCount() const = 0; | 59 virtual HistogramBase::Count TotalCount() const = 0; |
| 28 | 60 |
| 29 virtual void Add(const HistogramSamples& other); | 61 virtual void Add(const HistogramSamples& other); |
| 30 | 62 |
| 31 // Add from serialized samples. | 63 // Add from serialized samples. |
| 32 virtual bool AddFromPickle(PickleIterator* iter); | 64 virtual bool AddFromPickle(PickleIterator* iter); |
| 33 | 65 |
| 34 virtual void Subtract(const HistogramSamples& other); | 66 virtual void Subtract(const HistogramSamples& other); |
| 35 | 67 |
| 36 virtual scoped_ptr<SampleCountIterator> Iterator() const = 0; | 68 virtual scoped_ptr<SampleCountIterator> Iterator() const = 0; |
| 37 virtual bool Serialize(Pickle* pickle) const; | 69 virtual bool Serialize(Pickle* pickle) const; |
| 38 | 70 |
| 39 // Accessor fuctions. | 71 // Accessor fuctions. |
| 40 int64 sum() const { return sum_; } | 72 uint64 id() const { return meta_->id; } |
| 73 int64 sum() const { return meta_->sum; } | |
| 41 HistogramBase::Count redundant_count() const { | 74 HistogramBase::Count redundant_count() const { |
| 42 return subtle::NoBarrier_Load(&redundant_count_); | 75 return subtle::NoBarrier_Load(&meta_->redundant_count); |
| 43 } | 76 } |
| 44 | 77 |
| 45 protected: | 78 protected: |
| 46 // Based on |op| type, add or subtract sample counts data from the iterator. | 79 // Based on |op| type, add or subtract sample counts data from the iterator. |
| 47 enum Operator { ADD, SUBTRACT }; | 80 enum Operator { ADD, SUBTRACT }; |
| 48 virtual bool AddSubtractImpl(SampleCountIterator* iter, Operator op) = 0; | 81 virtual bool AddSubtractImpl(SampleCountIterator* iter, Operator op) = 0; |
| 49 | 82 |
| 50 void IncreaseSum(int64 diff); | 83 void IncreaseSum(int64 diff); |
| 51 void IncreaseRedundantCount(HistogramBase::Count diff); | 84 void IncreaseRedundantCount(HistogramBase::Count diff); |
| 52 | 85 |
| 53 private: | 86 private: |
| 54 int64 sum_; | 87 void SetMetadataId(uint64 id); |
| 55 | 88 |
| 56 // |redundant_count_| helps identify memory corruption. It redundantly stores | 89 // In order to support histograms shared through an external memory segment, |
| 57 // the total number of samples accumulated in the histogram. We can compare | 90 // meta values may be the local storage or external storage depending on the |
| 58 // this count to the sum of the counts (TotalCount() function), and detect | 91 // wishes of the derived class. |
|
Alexei Svitkine (slow)
2015/11/25 18:05:27
HistogramSamples is meant to just be an interface.
bcwhite
2015/11/25 21:54:17
Yes, but then it has to be repeated. It's never b
| |
| 59 // problems. Note, depending on the implementation of different histogram | 92 Metadata local_meta_; |
| 60 // types, there might be races during histogram accumulation and snapshotting | 93 Metadata* meta_; |
| 61 // that we choose to accept. In this case, the tallies might mismatch even | |
| 62 // when no memory corruption has happened. | |
| 63 HistogramBase::AtomicCount redundant_count_; | |
| 64 }; | 94 }; |
| 65 | 95 |
| 66 class BASE_EXPORT SampleCountIterator { | 96 class BASE_EXPORT SampleCountIterator { |
| 67 public: | 97 public: |
| 68 virtual ~SampleCountIterator(); | 98 virtual ~SampleCountIterator(); |
| 69 | 99 |
| 70 virtual bool Done() const = 0; | 100 virtual bool Done() const = 0; |
| 71 virtual void Next() = 0; | 101 virtual void Next() = 0; |
| 72 | 102 |
| 73 // Get the sample and count at current position. | 103 // Get the sample and count at current position. |
| 74 // |min| |max| and |count| can be NULL if the value is not of interest. | 104 // |min| |max| and |count| can be NULL if the value is not of interest. |
| 75 // Requires: !Done(); | 105 // Requires: !Done(); |
| 76 virtual void Get(HistogramBase::Sample* min, | 106 virtual void Get(HistogramBase::Sample* min, |
| 77 HistogramBase::Sample* max, | 107 HistogramBase::Sample* max, |
| 78 HistogramBase::Count* count) const = 0; | 108 HistogramBase::Count* count) const = 0; |
| 79 | 109 |
| 80 // Get the index of current histogram bucket. | 110 // Get the index of current histogram bucket. |
| 81 // For histograms that don't use predefined buckets, it returns false. | 111 // For histograms that don't use predefined buckets, it returns false. |
| 82 // Requires: !Done(); | 112 // Requires: !Done(); |
| 83 virtual bool GetBucketIndex(size_t* index) const; | 113 virtual bool GetBucketIndex(size_t* index) const; |
| 84 }; | 114 }; |
| 85 | 115 |
| 86 } // namespace base | 116 } // namespace base |
| 87 | 117 |
| 88 #endif // BASE_METRICS_HISTOGRAM_SAMPLES_H_ | 118 #endif // BASE_METRICS_HISTOGRAM_SAMPLES_H_ |
| OLD | NEW |