|
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/histogram_samples.h" | |
6 | |
7 #include "base/compiler_specific.h" | |
8 #include "base/pickle.h" | |
9 | |
10 namespace base { | |
11 | |
12 namespace { | |
13 | |
14 class SampleCountPickleIterator : public SampleCountIterator { | |
15 public: | |
16 SampleCountPickleIterator(PickleIterator* iter); | |
17 | |
18 virtual bool Done() const OVERRIDE; | |
19 virtual void Next() OVERRIDE; | |
20 virtual void Get(HistogramBase::Sample* min, | |
21 HistogramBase::Sample* max, | |
22 HistogramBase::Count* count) const OVERRIDE; | |
23 private: | |
24 PickleIterator* const iter_; | |
25 | |
26 HistogramBase::Sample min_; | |
27 HistogramBase::Sample max_; | |
28 HistogramBase::Count count_; | |
29 bool is_done_; | |
30 }; | |
31 | |
32 SampleCountPickleIterator::SampleCountPickleIterator(PickleIterator* iter) | |
33 : iter_(iter), | |
34 is_done_(false) { | |
35 Next(); | |
36 } | |
37 | |
38 bool SampleCountPickleIterator::Done() const { | |
39 return is_done_; | |
40 } | |
41 | |
42 void SampleCountPickleIterator::Next() { | |
43 CHECK(!Done()); | |
Ilya Sherman
2012/09/21 08:52:12
nit: DCHECK
kaiwang
2012/09/21 19:51:18
Done.
| |
44 if (!iter_->ReadInt(&min_) || | |
45 !iter_->ReadInt(&max_) || | |
46 !iter_->ReadInt(&count_)) | |
47 is_done_ = true; | |
48 } | |
49 | |
50 void SampleCountPickleIterator::Get(HistogramBase::Sample* min, | |
51 HistogramBase::Sample* max, | |
52 HistogramBase::Count* count) const { | |
53 CHECK(!Done()); | |
Ilya Sherman
2012/09/21 08:52:12
nit: DCHECK
kaiwang
2012/09/21 19:51:18
Done.
| |
54 *min = min_; | |
55 *max = max_; | |
56 *count = count_; | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 HistogramSamples::HistogramSamples() : sum_(0), redundant_count_(0) {} | |
62 | |
63 HistogramSamples::~HistogramSamples() {} | |
64 | |
65 void HistogramSamples::Add(const HistogramSamples& other) { | |
66 sum_ += other.sum(); | |
67 redundant_count_ += other.redundant_count(); | |
68 CHECK(AddSubtractImpl(other.Iterator().get(), ADD)); | |
Ilya Sherman
2012/09/21 08:52:12
nit: DCHECK (which also means you'll need to move
kaiwang
2012/09/21 19:51:18
Done.
| |
69 } | |
70 | |
71 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { | |
72 int64 sum; | |
73 HistogramBase::Count redundant_count; | |
74 | |
75 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count)) | |
76 return false; | |
77 sum_ += sum; | |
78 redundant_count_ += redundant_count; | |
79 | |
80 SampleCountPickleIterator pickle_iter(iter); | |
81 return AddSubtractImpl(&pickle_iter, ADD); | |
82 } | |
83 | |
84 void HistogramSamples::Subtract(const HistogramSamples& other) { | |
85 sum_ -= other.sum(); | |
86 redundant_count_ -= other.redundant_count(); | |
87 CHECK(AddSubtractImpl(other.Iterator().get(), SUBTRACT)); | |
Ilya Sherman
2012/09/21 08:52:12
nit: DCHECK (which also means you'll need to move
kaiwang
2012/09/21 19:51:18
Done.
| |
88 } | |
89 | |
90 bool HistogramSamples::Serialize(Pickle* pickle) const { | |
91 if (!pickle->WriteInt64(sum_) || !pickle->WriteInt(redundant_count_)) | |
92 return false; | |
93 | |
94 HistogramBase::Sample min; | |
95 HistogramBase::Sample max; | |
96 HistogramBase::Count count; | |
97 for (scoped_ptr<SampleCountIterator> it = Iterator(); | |
98 !it->Done(); | |
99 it->Next()) { | |
100 it->Get(&min, &max, &count); | |
101 if (!pickle->WriteInt(min) || | |
102 !pickle->WriteInt(max) || | |
103 !pickle->WriteInt(count)) | |
104 return false; | |
105 } | |
106 return true; | |
107 } | |
108 | |
109 void HistogramSamples::IncreaseSum(int64 diff) { | |
110 sum_ += diff; | |
111 } | |
112 | |
113 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { | |
114 redundant_count_ += diff; | |
115 } | |
116 | |
117 SampleCountIterator::~SampleCountIterator() {} | |
118 | |
119 bool SampleCountIterator::GetBucketIndex(size_t* index) const { | |
120 DCHECK(!Done()); | |
121 return false; | |
122 } | |
123 | |
124 } // namespace base | |
OLD | NEW |