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

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

Issue 1734033003: Add support for persistent sparse histograms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed more review comments by Alexei Created 4 years, 9 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 (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
14 SampleMap::SampleMap() : SampleMap(0) {} 15 SampleMap::SampleMap() : SampleMap(0) {}
15 16
16 SampleMap::SampleMap(uint64_t id) : HistogramSamples(id) {} 17 SampleMap::SampleMap(uint64_t id) : HistogramSamples(id) {}
17 18
18 SampleMap::~SampleMap() {} 19 SampleMap::~SampleMap() {}
19 20
20 void SampleMap::Accumulate(Sample value, Count count) { 21 void SampleMap::Accumulate(Sample value, Count count) {
21 sample_counts_[value] += count; 22 sample_counts_[value] += count;
22 IncreaseSum(static_cast<int64_t>(count) * value); 23 IncreaseSum(static_cast<int64_t>(count) * value);
23 IncreaseRedundantCount(count); 24 IncreaseRedundantCount(count);
24 } 25 }
25 26
26 Count SampleMap::GetCount(Sample value) const { 27 Count SampleMap::GetCount(Sample value) const {
27 std::map<Sample, Count>::const_iterator it = sample_counts_.find(value); 28 std::map<Sample, Count>::const_iterator it = sample_counts_.find(value);
28 if (it == sample_counts_.end()) 29 if (it == sample_counts_.end())
29 return 0; 30 return 0;
30 return it->second; 31 return it->second;
31 } 32 }
32 33
33 Count SampleMap::TotalCount() const { 34 Count SampleMap::TotalCount() const {
34 Count count = 0; 35 Count count = 0;
35 for (const auto& entry : sample_counts_) { 36 for (const auto& entry : sample_counts_)
36 count += entry.second; 37 count += entry.second;
37 }
38 return count; 38 return count;
39 } 39 }
40 40
41 scoped_ptr<SampleCountIterator> SampleMap::Iterator() const { 41 scoped_ptr<SampleCountIterator> SampleMap::Iterator() const {
42 return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_counts_)); 42 return make_scoped_ptr(new SampleMapIterator(sample_counts_));
43 } 43 }
44 44
45 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, 45 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, Operator op) {
46 HistogramSamples::Operator op) {
47 Sample min; 46 Sample min;
48 Sample max; 47 Sample max;
49 Count count; 48 Count count;
50 for (; !iter->Done(); iter->Next()) { 49 for (; !iter->Done(); iter->Next()) {
51 iter->Get(&min, &max, &count); 50 iter->Get(&min, &max, &count);
52 if (min + 1 != max) 51 if (min + 1 != max)
53 return false; // SparseHistogram only supports bucket with size 1. 52 return false; // SparseHistogram only supports bucket with size 1.
54 53
55 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count; 54 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count;
56 } 55 }
(...skipping 13 matching lines...) Expand all
70 } 69 }
71 70
72 void SampleMapIterator::Next() { 71 void SampleMapIterator::Next() {
73 DCHECK(!Done()); 72 DCHECK(!Done());
74 ++iter_; 73 ++iter_;
75 SkipEmptyBuckets(); 74 SkipEmptyBuckets();
76 } 75 }
77 76
78 void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const { 77 void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const {
79 DCHECK(!Done()); 78 DCHECK(!Done());
80 if (min != NULL) 79 if (min)
81 *min = iter_->first; 80 *min = iter_->first;
82 if (max != NULL) 81 if (max)
83 *max = iter_->first + 1; 82 *max = iter_->first + 1;
84 if (count != NULL) 83 if (count)
85 *count = iter_->second; 84 *count = iter_->second;
86 } 85 }
87 86
88 void SampleMapIterator::SkipEmptyBuckets() { 87 void SampleMapIterator::SkipEmptyBuckets() {
89 while (!Done() && iter_->second == 0) { 88 while (!Done() && iter_->second == 0)
90 ++iter_; 89 ++iter_;
91 }
92 } 90 }
93 91
94 } // namespace base 92 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698