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_count_[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_count_.find(value); | |
30 if (it == sample_count_.end()) | |
31 return 0; | |
32 return it->second; | |
33 } | |
34 | |
35 Count SampleMap::TotalCount() const { | |
36 Count count = 0; | |
37 map<Sample, Count>::const_iterator it; | |
Ilya Sherman
2012/10/01 21:24:50
nit: Please define this within the for loop's vari
kaiwang
2012/10/01 22:38:52
Done.
| |
38 | |
39 base::AutoLock auto_lock(lock_); | |
40 for (it = sample_count_.begin(); it != sample_count_.end(); ++it) { | |
41 count += it->second; | |
42 } | |
43 return count; | |
44 } | |
45 | |
46 scoped_ptr<SampleCountIterator> SampleMap::Iterator() const { | |
47 return scoped_ptr<SampleCountIterator>(new SampleMapIterator(sample_count_)); | |
48 } | |
49 | |
50 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, | |
51 HistogramSamples::Instruction instruction) { | |
52 HistogramBase::Sample min; | |
53 HistogramBase::Sample max; | |
54 HistogramBase::Count count; | |
55 | |
56 base::AutoLock auto_lock(lock_); | |
57 for (; !iter->Done(); iter->Next()) { | |
58 iter->Get(&min, &max, &count); | |
59 if (min + 1 != max) | |
60 return false; // SparseHistogram only supports bucket with size 1. | |
Ilya Sherman
2012/10/01 21:24:50
nit: Should this be a DCHECK?
kaiwang
2012/10/01 22:38:52
As in SampleVector, this function can be called in
| |
61 sample_count_[min] += | |
62 (instruction == HistogramSamples::ADD) ? count : -count; | |
Ilya Sherman
2012/10/01 21:24:50
nit: I don't think you need the "HistogramSamples:
kaiwang
2012/10/01 22:38:52
Right, but I think this is a bit easier for code r
| |
63 } | |
64 return true; | |
65 } | |
66 | |
67 SampleMapIterator::SampleMapIterator(const map<Sample, Count>& sample_count) | |
68 : iter_(sample_count.begin()), | |
69 end_(sample_count.end()) {} | |
70 | |
71 SampleMapIterator::~SampleMapIterator() {} | |
72 | |
73 bool SampleMapIterator::Done() const { | |
74 return iter_ == end_; | |
75 } | |
76 | |
77 void SampleMapIterator::Next() { | |
78 DCHECK(!Done()); | |
79 iter_++; | |
80 } | |
81 | |
82 void SampleMapIterator::Get(HistogramBase::Sample* min, | |
83 HistogramBase::Sample* max, | |
84 HistogramBase::Count* count) const { | |
85 DCHECK(!Done()); | |
86 if (min != NULL) | |
87 *min = iter_->first; | |
88 if (max != NULL) | |
89 *max = iter_->first + 1; | |
90 if (count != NULL) | |
91 *count = iter_->second; | |
92 } | |
93 | |
94 } // namespace base | |
OLD | NEW |