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 uint64_t 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 // TODO(bcwhite): Change this to std::atomic<int64_t>. |
| 35 int64_t sum; |
| 36 |
| 37 // A "redundant" count helps identify memory corruption. It redundantly |
| 38 // stores the total number of samples accumulated in the histogram. We |
| 39 // can compare this count to the sum of the counts (TotalCount() function), |
| 40 // and detect problems. Note, depending on the implementation of different |
| 41 // histogram types, there might be races during histogram accumulation |
| 42 // and snapshotting that we choose to accept. In this case, the tallies |
| 43 // might mismatch even when no memory corruption has happened. |
| 44 HistogramBase::AtomicCount redundant_count; |
| 45 |
| 46 Metadata() : id(0), sum(0), redundant_count(0) {} |
| 47 }; |
| 48 |
| 49 explicit HistogramSamples(uint64_t id); |
| 50 HistogramSamples(uint64_t id, Metadata* meta); |
22 virtual ~HistogramSamples(); | 51 virtual ~HistogramSamples(); |
23 | 52 |
24 virtual void Accumulate(HistogramBase::Sample value, | 53 virtual void Accumulate(HistogramBase::Sample value, |
25 HistogramBase::Count count) = 0; | 54 HistogramBase::Count count) = 0; |
26 virtual HistogramBase::Count GetCount(HistogramBase::Sample value) const = 0; | 55 virtual HistogramBase::Count GetCount(HistogramBase::Sample value) const = 0; |
27 virtual HistogramBase::Count TotalCount() const = 0; | 56 virtual HistogramBase::Count TotalCount() const = 0; |
28 | 57 |
29 virtual void Add(const HistogramSamples& other); | 58 virtual void Add(const HistogramSamples& other); |
30 | 59 |
31 // Add from serialized samples. | 60 // Add from serialized samples. |
32 virtual bool AddFromPickle(PickleIterator* iter); | 61 virtual bool AddFromPickle(PickleIterator* iter); |
33 | 62 |
34 virtual void Subtract(const HistogramSamples& other); | 63 virtual void Subtract(const HistogramSamples& other); |
35 | 64 |
36 virtual scoped_ptr<SampleCountIterator> Iterator() const = 0; | 65 virtual scoped_ptr<SampleCountIterator> Iterator() const = 0; |
37 virtual bool Serialize(Pickle* pickle) const; | 66 virtual bool Serialize(Pickle* pickle) const; |
38 | 67 |
39 // Accessor fuctions. | 68 // Accessor fuctions. |
40 int64 sum() const { return sum_; } | 69 uint64_t id() const { return meta_->id; } |
| 70 int64_t sum() const { return meta_->sum; } |
41 HistogramBase::Count redundant_count() const { | 71 HistogramBase::Count redundant_count() const { |
42 return subtle::NoBarrier_Load(&redundant_count_); | 72 return subtle::NoBarrier_Load(&meta_->redundant_count); |
43 } | 73 } |
44 | 74 |
45 protected: | 75 protected: |
46 // Based on |op| type, add or subtract sample counts data from the iterator. | 76 // Based on |op| type, add or subtract sample counts data from the iterator. |
47 enum Operator { ADD, SUBTRACT }; | 77 enum Operator { ADD, SUBTRACT }; |
48 virtual bool AddSubtractImpl(SampleCountIterator* iter, Operator op) = 0; | 78 virtual bool AddSubtractImpl(SampleCountIterator* iter, Operator op) = 0; |
49 | 79 |
50 void IncreaseSum(int64 diff); | 80 void IncreaseSum(int64_t diff); |
51 void IncreaseRedundantCount(HistogramBase::Count diff); | 81 void IncreaseRedundantCount(HistogramBase::Count diff); |
52 | 82 |
53 private: | 83 private: |
54 int64 sum_; | 84 // In order to support histograms shared through an external memory segment, |
| 85 // meta values may be the local storage or external storage depending on the |
| 86 // wishes of the derived class. |
| 87 Metadata local_meta_; |
| 88 Metadata* meta_; |
55 | 89 |
56 // |redundant_count_| helps identify memory corruption. It redundantly stores | 90 DISALLOW_COPY_AND_ASSIGN(HistogramSamples); |
57 // the total number of samples accumulated in the histogram. We can compare | |
58 // this count to the sum of the counts (TotalCount() function), and detect | |
59 // problems. Note, depending on the implementation of different histogram | |
60 // types, there might be races during histogram accumulation and snapshotting | |
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 }; | 91 }; |
65 | 92 |
66 class BASE_EXPORT SampleCountIterator { | 93 class BASE_EXPORT SampleCountIterator { |
67 public: | 94 public: |
68 virtual ~SampleCountIterator(); | 95 virtual ~SampleCountIterator(); |
69 | 96 |
70 virtual bool Done() const = 0; | 97 virtual bool Done() const = 0; |
71 virtual void Next() = 0; | 98 virtual void Next() = 0; |
72 | 99 |
73 // Get the sample and count at current position. | 100 // Get the sample and count at current position. |
74 // |min| |max| and |count| can be NULL if the value is not of interest. | 101 // |min| |max| and |count| can be NULL if the value is not of interest. |
75 // Requires: !Done(); | 102 // Requires: !Done(); |
76 virtual void Get(HistogramBase::Sample* min, | 103 virtual void Get(HistogramBase::Sample* min, |
77 HistogramBase::Sample* max, | 104 HistogramBase::Sample* max, |
78 HistogramBase::Count* count) const = 0; | 105 HistogramBase::Count* count) const = 0; |
79 | 106 |
80 // Get the index of current histogram bucket. | 107 // Get the index of current histogram bucket. |
81 // For histograms that don't use predefined buckets, it returns false. | 108 // For histograms that don't use predefined buckets, it returns false. |
82 // Requires: !Done(); | 109 // Requires: !Done(); |
83 virtual bool GetBucketIndex(size_t* index) const; | 110 virtual bool GetBucketIndex(size_t* index) const; |
84 }; | 111 }; |
85 | 112 |
86 } // namespace base | 113 } // namespace base |
87 | 114 |
88 #endif // BASE_METRICS_HISTOGRAM_SAMPLES_H_ | 115 #endif // BASE_METRICS_HISTOGRAM_SAMPLES_H_ |
OLD | NEW |