OLD | NEW |
---|---|
(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; | |
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 { | |
jar (doing other things)
2012/10/02 17:41:11
This is a surprising API. I would assume that you
| |
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 { | |
jar (doing other things)
2012/10/02 17:41:11
Note that this approach, of gathering the total co
| |
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 { | |
jar (doing other things)
2012/10/02 17:41:11
This is the scary interface. We're giving away an
| |
48 return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_counts_)); | |
49 } | |
50 | |
51 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, | |
52 HistogramSamples::Instruction instruction) { | |
53 Sample min; | |
54 Sample max; | |
55 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(Sample* min, Sample* max, Count* count) const { | |
84 DCHECK(!Done()); | |
85 if (min != NULL) | |
86 *min = iter_->first; | |
87 if (max != NULL) | |
88 *max = iter_->first + 1; | |
89 if (count != NULL) | |
90 *count = iter_->second; | |
91 } | |
92 | |
93 } // namespace base | |
OLD | NEW |