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

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

Powered by Google App Engine
This is Rietveld 408576698