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

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

Issue 11022002: Add SampleMap and use it in SparseHistogram (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 8 years, 2 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/metrics/sample_map.h"
6
7 #include "base/logging.h"
8
9 using std::map;
10
11 namespace base {
12
13 typedef HistogramBase::Count Count;
jar (doing other things) 2012/10/01 23:10:38 nit: would using work here?
kaiwang 2012/10/02 02:57:56 nope... using only works for namespace
14 typedef HistogramBase::Sample Sample;
15
16 SampleMap::SampleMap() {}
17
18 SampleMap::~SampleMap() {}
19
20 void SampleMap::Accumulate(Sample value, Count count) {
21 base::AutoLock auto_lock(lock_);
22 sample_counts_[value] += count;
23 IncreaseSum(count * value);
24 IncreaseRedundantCount(count);
25 }
26
27 Count SampleMap::GetCount(Sample value) const {
28 base::AutoLock auto_lock(lock_);
29 map<Sample, Count>::const_iterator it = sample_counts_.find(value);
30 if (it == sample_counts_.end())
31 return 0;
32 return it->second;
33 }
34
35 Count SampleMap::TotalCount() const {
36 Count count = 0;
37
38 base::AutoLock auto_lock(lock_);
39 for (map<Sample, Count>::const_iterator it = sample_counts_.begin();
40 it != sample_counts_.end();
41 ++it) {
42 count += it->second;
43 }
44 return count;
45 }
46
47 scoped_ptr<SampleCountIterator> SampleMap::Iterator() const {
48 return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_counts_));
49 }
50
51 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter,
52 HistogramSamples::Instruction instruction) {
53 HistogramBase::Sample min;
54 HistogramBase::Sample max;
55 HistogramBase::Count count;
56
57 base::AutoLock auto_lock(lock_);
58 for (; !iter->Done(); iter->Next()) {
59 iter->Get(&min, &max, &count);
60 if (min + 1 != max)
61 return false; // SparseHistogram only supports bucket with size 1.
62 sample_counts_[min] +=
63 (instruction == HistogramSamples::ADD) ? count : -count;
64 }
65 return true;
66 }
67
68 SampleMapIterator::SampleMapIterator(const map<Sample, Count>& sample_counts)
69 : iter_(sample_counts.begin()),
70 end_(sample_counts.end()) {}
71
72 SampleMapIterator::~SampleMapIterator() {}
73
74 bool SampleMapIterator::Done() const {
75 return iter_ == end_;
76 }
77
78 void SampleMapIterator::Next() {
79 DCHECK(!Done());
80 iter_++;
81 }
82
83 void SampleMapIterator::Get(HistogramBase::Sample* min,
84 HistogramBase::Sample* max,
85 HistogramBase::Count* count) const {
86 DCHECK(!Done());
jar (doing other things) 2012/10/01 23:10:38 I'm surprised we're not holding a lock when we acc
kaiwang 2012/10/02 02:57:56 As discussed locked in SnapshotSamples function
87 if (min != NULL)
88 *min = iter_->first;
89 if (max != NULL)
90 *max = iter_->first + 1;
91 if (count != NULL)
92 *count = iter_->second;
93 }
94
95 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698