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

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

Issue 2853853002: Fix overflow when logging MaxInt32 to a sparse histogram. (Closed)
Patch Set: 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/persistent_sample_map.h" 5 #include "base/metrics/persistent_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/metrics/histogram_macros.h" 9 #include "base/metrics/histogram_macros.h"
10 #include "base/metrics/persistent_histogram_allocator.h" 10 #include "base/metrics/persistent_histogram_allocator.h"
(...skipping 23 matching lines...) Expand all
34 typedef std::map<HistogramBase::Sample, HistogramBase::Count*> 34 typedef std::map<HistogramBase::Sample, HistogramBase::Count*>
35 SampleToCountMap; 35 SampleToCountMap;
36 36
37 explicit PersistentSampleMapIterator(const SampleToCountMap& sample_counts); 37 explicit PersistentSampleMapIterator(const SampleToCountMap& sample_counts);
38 ~PersistentSampleMapIterator() override; 38 ~PersistentSampleMapIterator() override;
39 39
40 // SampleCountIterator: 40 // SampleCountIterator:
41 bool Done() const override; 41 bool Done() const override;
42 void Next() override; 42 void Next() override;
43 void Get(HistogramBase::Sample* min, 43 void Get(HistogramBase::Sample* min,
44 HistogramBase::Sample* max, 44 int64_t* max,
45 HistogramBase::Count* count) const override; 45 HistogramBase::Count* count) const override;
46 46
47 private: 47 private:
48 void SkipEmptyBuckets(); 48 void SkipEmptyBuckets();
49 49
50 SampleToCountMap::const_iterator iter_; 50 SampleToCountMap::const_iterator iter_;
51 const SampleToCountMap::const_iterator end_; 51 const SampleToCountMap::const_iterator end_;
52 }; 52 };
53 53
54 PersistentSampleMapIterator::PersistentSampleMapIterator( 54 PersistentSampleMapIterator::PersistentSampleMapIterator(
55 const SampleToCountMap& sample_counts) 55 const SampleToCountMap& sample_counts)
56 : iter_(sample_counts.begin()), 56 : iter_(sample_counts.begin()),
57 end_(sample_counts.end()) { 57 end_(sample_counts.end()) {
58 SkipEmptyBuckets(); 58 SkipEmptyBuckets();
59 } 59 }
60 60
61 PersistentSampleMapIterator::~PersistentSampleMapIterator() {} 61 PersistentSampleMapIterator::~PersistentSampleMapIterator() {}
62 62
63 bool PersistentSampleMapIterator::Done() const { 63 bool PersistentSampleMapIterator::Done() const {
64 return iter_ == end_; 64 return iter_ == end_;
65 } 65 }
66 66
67 void PersistentSampleMapIterator::Next() { 67 void PersistentSampleMapIterator::Next() {
68 DCHECK(!Done()); 68 DCHECK(!Done());
69 ++iter_; 69 ++iter_;
70 SkipEmptyBuckets(); 70 SkipEmptyBuckets();
71 } 71 }
72 72
73 void PersistentSampleMapIterator::Get(Sample* min, 73 void PersistentSampleMapIterator::Get(Sample* min,
74 Sample* max, 74 int64_t* max,
75 Count* count) const { 75 Count* count) const {
76 DCHECK(!Done()); 76 DCHECK(!Done());
77 if (min) 77 if (min)
78 *min = iter_->first; 78 *min = iter_->first;
79 if (max) 79 if (max)
80 *max = iter_->first + 1; 80 *max = static_cast<int64_t>(iter_->first) + 1;
Ilya Sherman 2017/05/02 21:37:49 nit: Here and everywhere else that you add a stati
Alexei Svitkine (slow) 2017/05/03 15:21:53 Done.
81 if (count) 81 if (count)
82 *count = *iter_->second; 82 *count = *iter_->second;
83 } 83 }
84 84
85 void PersistentSampleMapIterator::SkipEmptyBuckets() { 85 void PersistentSampleMapIterator::SkipEmptyBuckets() {
86 while (!Done() && *iter_->second == 0) { 86 while (!Done() && *iter_->second == 0) {
87 ++iter_; 87 ++iter_;
88 } 88 }
89 } 89 }
90 90
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 record->count = 0; 179 record->count = 0;
180 180
181 PersistentMemoryAllocator::Reference ref = allocator->GetAsReference(record); 181 PersistentMemoryAllocator::Reference ref = allocator->GetAsReference(record);
182 allocator->MakeIterable(ref); 182 allocator->MakeIterable(ref);
183 return ref; 183 return ref;
184 } 184 }
185 185
186 bool PersistentSampleMap::AddSubtractImpl(SampleCountIterator* iter, 186 bool PersistentSampleMap::AddSubtractImpl(SampleCountIterator* iter,
187 Operator op) { 187 Operator op) {
188 Sample min; 188 Sample min;
189 Sample max; 189 int64_t max;
190 Count count; 190 Count count;
191 for (; !iter->Done(); iter->Next()) { 191 for (; !iter->Done(); iter->Next()) {
192 iter->Get(&min, &max, &count); 192 iter->Get(&min, &max, &count);
193 if (count == 0) 193 if (count == 0)
194 continue; 194 continue;
195 if (min + 1 != max) 195 if (static_cast<int64_t>(min) + 1 != max)
196 return false; // SparseHistogram only supports bucket with size 1. 196 return false; // SparseHistogram only supports bucket with size 1.
197 197
198 #if 0 // TODO(bcwhite) Re-enable efficient version after crbug.com/682680. 198 #if 0 // TODO(bcwhite) Re-enable efficient version after crbug.com/682680.
199 *GetOrCreateSampleCountStorage(min) += 199 *GetOrCreateSampleCountStorage(min) +=
200 (op == HistogramSamples::ADD) ? count : -count; 200 (op == HistogramSamples::ADD) ? count : -count;
201 #else 201 #else
202 NegativeSampleReason reason = MAX_NEGATIVE_SAMPLE_REASONS; 202 NegativeSampleReason reason = MAX_NEGATIVE_SAMPLE_REASONS;
203 if (op == HistogramSamples::ADD) { 203 if (op == HistogramSamples::ADD) {
204 // Add should generally be adding only positive values. 204 // Add should generally be adding only positive values.
205 Count* local_count_ptr = GetOrCreateSampleCountStorage(min); 205 Count* local_count_ptr = GetOrCreateSampleCountStorage(min);
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 found_count = &record->count; 327 found_count = &record->count;
328 if (!import_everything) 328 if (!import_everything)
329 break; 329 break;
330 } 330 }
331 } 331 }
332 332
333 return found_count; 333 return found_count;
334 } 334 }
335 335
336 } // namespace base 336 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698