Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: base/metrics/sample_map.cc

Issue 2853853002: Fix overflow when logging MaxInt32 to a sparse histogram. (Closed)
Patch Set: Address comments. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/metrics/persistent_sample_map_unittest.cc ('k') | base/metrics/sample_map_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/numerics/safe_conversions.h"
9 #include "base/stl_util.h" 10 #include "base/stl_util.h"
10 11
11 namespace base { 12 namespace base {
12 13
13 typedef HistogramBase::Count Count; 14 typedef HistogramBase::Count Count;
14 typedef HistogramBase::Sample Sample; 15 typedef HistogramBase::Sample Sample;
15 16
16 namespace { 17 namespace {
17 18
18 // An iterator for going through a SampleMap. The logic here is identical 19 // An iterator for going through a SampleMap. The logic here is identical
19 // to that of PersistentSampleMapIterator but with different data structures. 20 // to that of PersistentSampleMapIterator but with different data structures.
20 // Changes here likely need to be duplicated there. 21 // Changes here likely need to be duplicated there.
21 class SampleMapIterator : public SampleCountIterator { 22 class SampleMapIterator : public SampleCountIterator {
22 public: 23 public:
23 typedef std::map<HistogramBase::Sample, HistogramBase::Count> 24 typedef std::map<HistogramBase::Sample, HistogramBase::Count>
24 SampleToCountMap; 25 SampleToCountMap;
25 26
26 explicit SampleMapIterator(const SampleToCountMap& sample_counts); 27 explicit SampleMapIterator(const SampleToCountMap& sample_counts);
27 ~SampleMapIterator() override; 28 ~SampleMapIterator() override;
28 29
29 // SampleCountIterator: 30 // SampleCountIterator:
30 bool Done() const override; 31 bool Done() const override;
31 void Next() override; 32 void Next() override;
32 void Get(HistogramBase::Sample* min, 33 void Get(HistogramBase::Sample* min,
33 HistogramBase::Sample* max, 34 int64_t* max,
34 HistogramBase::Count* count) const override; 35 HistogramBase::Count* count) const override;
35 36
36 private: 37 private:
37 void SkipEmptyBuckets(); 38 void SkipEmptyBuckets();
38 39
39 SampleToCountMap::const_iterator iter_; 40 SampleToCountMap::const_iterator iter_;
40 const SampleToCountMap::const_iterator end_; 41 const SampleToCountMap::const_iterator end_;
41 }; 42 };
42 43
43 SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts) 44 SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts)
44 : iter_(sample_counts.begin()), 45 : iter_(sample_counts.begin()),
45 end_(sample_counts.end()) { 46 end_(sample_counts.end()) {
46 SkipEmptyBuckets(); 47 SkipEmptyBuckets();
47 } 48 }
48 49
49 SampleMapIterator::~SampleMapIterator() {} 50 SampleMapIterator::~SampleMapIterator() {}
50 51
51 bool SampleMapIterator::Done() const { 52 bool SampleMapIterator::Done() const {
52 return iter_ == end_; 53 return iter_ == end_;
53 } 54 }
54 55
55 void SampleMapIterator::Next() { 56 void SampleMapIterator::Next() {
56 DCHECK(!Done()); 57 DCHECK(!Done());
57 ++iter_; 58 ++iter_;
58 SkipEmptyBuckets(); 59 SkipEmptyBuckets();
59 } 60 }
60 61
61 void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const { 62 void SampleMapIterator::Get(Sample* min, int64_t* max, Count* count) const {
62 DCHECK(!Done()); 63 DCHECK(!Done());
63 if (min) 64 if (min)
64 *min = iter_->first; 65 *min = iter_->first;
65 if (max) 66 if (max)
66 *max = iter_->first + 1; 67 *max = strict_cast<int64_t>(iter_->first) + 1;
67 if (count) 68 if (count)
68 *count = iter_->second; 69 *count = iter_->second;
69 } 70 }
70 71
71 void SampleMapIterator::SkipEmptyBuckets() { 72 void SampleMapIterator::SkipEmptyBuckets() {
72 while (!Done() && iter_->second == 0) { 73 while (!Done() && iter_->second == 0) {
73 ++iter_; 74 ++iter_;
74 } 75 }
75 } 76 }
76 77
77 } // namespace 78 } // namespace
78 79
79 SampleMap::SampleMap() : SampleMap(0) {} 80 SampleMap::SampleMap() : SampleMap(0) {}
80 81
81 SampleMap::SampleMap(uint64_t id) : HistogramSamples(id) {} 82 SampleMap::SampleMap(uint64_t id) : HistogramSamples(id) {}
82 83
83 SampleMap::~SampleMap() {} 84 SampleMap::~SampleMap() {}
84 85
85 void SampleMap::Accumulate(Sample value, Count count) { 86 void SampleMap::Accumulate(Sample value, Count count) {
86 sample_counts_[value] += count; 87 sample_counts_[value] += count;
87 IncreaseSumAndCount(static_cast<int64_t>(count) * value, count); 88 IncreaseSumAndCount(strict_cast<int64_t>(count) * value, count);
88 } 89 }
89 90
90 Count SampleMap::GetCount(Sample value) const { 91 Count SampleMap::GetCount(Sample value) const {
91 std::map<Sample, Count>::const_iterator it = sample_counts_.find(value); 92 std::map<Sample, Count>::const_iterator it = sample_counts_.find(value);
92 if (it == sample_counts_.end()) 93 if (it == sample_counts_.end())
93 return 0; 94 return 0;
94 return it->second; 95 return it->second;
95 } 96 }
96 97
97 Count SampleMap::TotalCount() const { 98 Count SampleMap::TotalCount() const {
98 Count count = 0; 99 Count count = 0;
99 for (const auto& entry : sample_counts_) { 100 for (const auto& entry : sample_counts_) {
100 count += entry.second; 101 count += entry.second;
101 } 102 }
102 return count; 103 return count;
103 } 104 }
104 105
105 std::unique_ptr<SampleCountIterator> SampleMap::Iterator() const { 106 std::unique_ptr<SampleCountIterator> SampleMap::Iterator() const {
106 return WrapUnique(new SampleMapIterator(sample_counts_)); 107 return WrapUnique(new SampleMapIterator(sample_counts_));
107 } 108 }
108 109
109 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, Operator op) { 110 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, Operator op) {
110 Sample min; 111 Sample min;
111 Sample max; 112 int64_t max;
112 Count count; 113 Count count;
113 for (; !iter->Done(); iter->Next()) { 114 for (; !iter->Done(); iter->Next()) {
114 iter->Get(&min, &max, &count); 115 iter->Get(&min, &max, &count);
115 if (min + 1 != max) 116 if (strict_cast<int64_t>(min) + 1 != max)
116 return false; // SparseHistogram only supports bucket with size 1. 117 return false; // SparseHistogram only supports bucket with size 1.
117 118
118 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count; 119 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count;
119 } 120 }
120 return true; 121 return true;
121 } 122 }
122 123
123 } // namespace base 124 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/persistent_sample_map_unittest.cc ('k') | base/metrics/sample_map_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698