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.
5 #include "base/metrics/histogram_samples.h"
7 #include "base/compiler_specific.h"
8 #include "base/pickle.h"
14 class SampleCountPickleIterator
: public SampleCountIterator
{
16 explicit SampleCountPickleIterator(PickleIterator
* iter
);
18 bool Done() const override
;
20 void Get(HistogramBase::Sample
* min
,
21 HistogramBase::Sample
* max
,
22 HistogramBase::Count
* count
) const override
;
25 PickleIterator
* const iter_
;
27 HistogramBase::Sample min_
;
28 HistogramBase::Sample max_
;
29 HistogramBase::Count count_
;
33 SampleCountPickleIterator::SampleCountPickleIterator(PickleIterator
* iter
)
39 bool SampleCountPickleIterator::Done() const {
43 void SampleCountPickleIterator::Next() {
45 if (!iter_
->ReadInt(&min_
) ||
46 !iter_
->ReadInt(&max_
) ||
47 !iter_
->ReadInt(&count_
))
51 void SampleCountPickleIterator::Get(HistogramBase::Sample
* min
,
52 HistogramBase::Sample
* max
,
53 HistogramBase::Count
* count
) const {
62 HistogramSamples::HistogramSamples() : sum_(0), redundant_count_(0) {}
64 HistogramSamples::~HistogramSamples() {}
66 void HistogramSamples::Add(const HistogramSamples
& other
) {
68 HistogramBase::Count old_redundant_count
=
69 subtle::NoBarrier_Load(&redundant_count_
);
70 subtle::NoBarrier_Store(&redundant_count_
,
71 old_redundant_count
+ other
.redundant_count());
72 bool success
= AddSubtractImpl(other
.Iterator().get(), ADD
);
76 bool HistogramSamples::AddFromPickle(PickleIterator
* iter
) {
78 HistogramBase::Count redundant_count
;
80 if (!iter
->ReadInt64(&sum
) || !iter
->ReadInt(&redundant_count
))
83 HistogramBase::Count old_redundant_count
=
84 subtle::NoBarrier_Load(&redundant_count_
);
85 subtle::NoBarrier_Store(&redundant_count_
,
86 old_redundant_count
+ redundant_count
);
88 SampleCountPickleIterator
pickle_iter(iter
);
89 return AddSubtractImpl(&pickle_iter
, ADD
);
92 void HistogramSamples::Subtract(const HistogramSamples
& other
) {
94 HistogramBase::Count old_redundant_count
=
95 subtle::NoBarrier_Load(&redundant_count_
);
96 subtle::NoBarrier_Store(&redundant_count_
,
97 old_redundant_count
- other
.redundant_count());
98 bool success
= AddSubtractImpl(other
.Iterator().get(), SUBTRACT
);
102 bool HistogramSamples::Serialize(Pickle
* pickle
) const {
103 if (!pickle
->WriteInt64(sum_
) ||
104 !pickle
->WriteInt(subtle::NoBarrier_Load(&redundant_count_
)))
107 HistogramBase::Sample min
;
108 HistogramBase::Sample max
;
109 HistogramBase::Count count
;
110 for (scoped_ptr
<SampleCountIterator
> it
= Iterator();
113 it
->Get(&min
, &max
, &count
);
114 if (!pickle
->WriteInt(min
) ||
115 !pickle
->WriteInt(max
) ||
116 !pickle
->WriteInt(count
))
122 void HistogramSamples::IncreaseSum(int64 diff
) {
126 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff
) {
127 subtle::NoBarrier_Store(&redundant_count_
,
128 subtle::NoBarrier_Load(&redundant_count_
) + diff
);
131 SampleCountIterator::~SampleCountIterator() {}
133 bool SampleCountIterator::GetBucketIndex(size_t* index
) const {