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 #include "base/metrics/sample_map.h" | 5 #include "base/metrics/sample_map.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" |
8 | 9 |
9 namespace base { | 10 namespace base { |
10 | 11 |
11 typedef HistogramBase::Count Count; | 12 typedef HistogramBase::Count Count; |
12 typedef HistogramBase::Sample Sample; | 13 typedef HistogramBase::Sample Sample; |
13 | 14 |
| 15 namespace { |
| 16 |
| 17 // An iterator for going through a SampleMap. The logic here is identical |
| 18 // to that of PersistentSampleMapIterator but with different data structures. |
| 19 // Changes here likely need to be duplicated there. |
| 20 class SampleMapIterator : public SampleCountIterator { |
| 21 public: |
| 22 typedef std::map<HistogramBase::Sample, HistogramBase::Count> |
| 23 SampleToCountMap; |
| 24 |
| 25 explicit SampleMapIterator(const SampleToCountMap& sample_counts); |
| 26 ~SampleMapIterator() override; |
| 27 |
| 28 // SampleCountIterator: |
| 29 bool Done() const override; |
| 30 void Next() override; |
| 31 void Get(HistogramBase::Sample* min, |
| 32 HistogramBase::Sample* max, |
| 33 HistogramBase::Count* count) const override; |
| 34 |
| 35 private: |
| 36 void SkipEmptyBuckets(); |
| 37 |
| 38 SampleToCountMap::const_iterator iter_; |
| 39 const SampleToCountMap::const_iterator end_; |
| 40 }; |
| 41 |
| 42 SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts) |
| 43 : iter_(sample_counts.begin()), |
| 44 end_(sample_counts.end()) { |
| 45 SkipEmptyBuckets(); |
| 46 } |
| 47 |
| 48 SampleMapIterator::~SampleMapIterator() {} |
| 49 |
| 50 bool SampleMapIterator::Done() const { |
| 51 return iter_ == end_; |
| 52 } |
| 53 |
| 54 void SampleMapIterator::Next() { |
| 55 DCHECK(!Done()); |
| 56 ++iter_; |
| 57 SkipEmptyBuckets(); |
| 58 } |
| 59 |
| 60 void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const { |
| 61 DCHECK(!Done()); |
| 62 if (min) |
| 63 *min = iter_->first; |
| 64 if (max) |
| 65 *max = iter_->first + 1; |
| 66 if (count) |
| 67 *count = iter_->second; |
| 68 } |
| 69 |
| 70 void SampleMapIterator::SkipEmptyBuckets() { |
| 71 while (!Done() && iter_->second == 0) { |
| 72 ++iter_; |
| 73 } |
| 74 } |
| 75 |
| 76 } // namespace |
| 77 |
14 SampleMap::SampleMap() : SampleMap(0) {} | 78 SampleMap::SampleMap() : SampleMap(0) {} |
15 | 79 |
16 SampleMap::SampleMap(uint64_t id) : HistogramSamples(id) {} | 80 SampleMap::SampleMap(uint64_t id) : HistogramSamples(id) {} |
17 | 81 |
18 SampleMap::~SampleMap() {} | 82 SampleMap::~SampleMap() {} |
19 | 83 |
20 void SampleMap::Accumulate(Sample value, Count count) { | 84 void SampleMap::Accumulate(Sample value, Count count) { |
21 sample_counts_[value] += count; | 85 sample_counts_[value] += count; |
22 IncreaseSum(static_cast<int64_t>(count) * value); | 86 IncreaseSum(static_cast<int64_t>(count) * value); |
23 IncreaseRedundantCount(count); | 87 IncreaseRedundantCount(count); |
24 } | 88 } |
25 | 89 |
26 Count SampleMap::GetCount(Sample value) const { | 90 Count SampleMap::GetCount(Sample value) const { |
27 std::map<Sample, Count>::const_iterator it = sample_counts_.find(value); | 91 std::map<Sample, Count>::const_iterator it = sample_counts_.find(value); |
28 if (it == sample_counts_.end()) | 92 if (it == sample_counts_.end()) |
29 return 0; | 93 return 0; |
30 return it->second; | 94 return it->second; |
31 } | 95 } |
32 | 96 |
33 Count SampleMap::TotalCount() const { | 97 Count SampleMap::TotalCount() const { |
34 Count count = 0; | 98 Count count = 0; |
35 for (const auto& entry : sample_counts_) { | 99 for (const auto& entry : sample_counts_) { |
36 count += entry.second; | 100 count += entry.second; |
37 } | 101 } |
38 return count; | 102 return count; |
39 } | 103 } |
40 | 104 |
41 scoped_ptr<SampleCountIterator> SampleMap::Iterator() const { | 105 scoped_ptr<SampleCountIterator> SampleMap::Iterator() const { |
42 return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_counts_)); | 106 return make_scoped_ptr(new SampleMapIterator(sample_counts_)); |
43 } | 107 } |
44 | 108 |
45 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, | 109 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, Operator op) { |
46 HistogramSamples::Operator op) { | |
47 Sample min; | 110 Sample min; |
48 Sample max; | 111 Sample max; |
49 Count count; | 112 Count count; |
50 for (; !iter->Done(); iter->Next()) { | 113 for (; !iter->Done(); iter->Next()) { |
51 iter->Get(&min, &max, &count); | 114 iter->Get(&min, &max, &count); |
52 if (min + 1 != max) | 115 if (min + 1 != max) |
53 return false; // SparseHistogram only supports bucket with size 1. | 116 return false; // SparseHistogram only supports bucket with size 1. |
54 | 117 |
55 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count; | 118 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count; |
56 } | 119 } |
57 return true; | 120 return true; |
58 } | 121 } |
59 | 122 |
60 SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts) | |
61 : iter_(sample_counts.begin()), | |
62 end_(sample_counts.end()) { | |
63 SkipEmptyBuckets(); | |
64 } | |
65 | |
66 SampleMapIterator::~SampleMapIterator() {} | |
67 | |
68 bool SampleMapIterator::Done() const { | |
69 return iter_ == end_; | |
70 } | |
71 | |
72 void SampleMapIterator::Next() { | |
73 DCHECK(!Done()); | |
74 ++iter_; | |
75 SkipEmptyBuckets(); | |
76 } | |
77 | |
78 void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const { | |
79 DCHECK(!Done()); | |
80 if (min != NULL) | |
81 *min = iter_->first; | |
82 if (max != NULL) | |
83 *max = iter_->first + 1; | |
84 if (count != NULL) | |
85 *count = iter_->second; | |
86 } | |
87 | |
88 void SampleMapIterator::SkipEmptyBuckets() { | |
89 while (!Done() && iter_->second == 0) { | |
90 ++iter_; | |
91 } | |
92 } | |
93 | |
94 } // namespace base | 123 } // namespace base |
OLD | NEW |