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 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
;
24 PickleIterator
* const iter_
;
26 HistogramBase::Sample min_
;
27 HistogramBase::Sample max_
;
28 HistogramBase::Count count_
;
32 SampleCountPickleIterator::SampleCountPickleIterator(PickleIterator
* iter
)
38 bool SampleCountPickleIterator::Done() const {
42 void SampleCountPickleIterator::Next() {
44 if (!iter_
->ReadInt(&min_
) ||
45 !iter_
->ReadInt(&max_
) ||
46 !iter_
->ReadInt(&count_
))
50 void SampleCountPickleIterator::Get(HistogramBase::Sample
* min
,
51 HistogramBase::Sample
* max
,
52 HistogramBase::Count
* count
) const {
61 HistogramSamples::HistogramSamples() : sum_(0), redundant_count_(0) {}
63 HistogramSamples::~HistogramSamples() {}
65 void HistogramSamples::Add(const HistogramSamples
& other
) {
67 redundant_count_
+= other
.redundant_count();
68 bool success
= AddSubtractImpl(other
.Iterator().get(), ADD
);
72 bool HistogramSamples::AddFromPickle(PickleIterator
* iter
) {
74 HistogramBase::Count redundant_count
;
76 if (!iter
->ReadInt64(&sum
) || !iter
->ReadInt(&redundant_count
))
79 redundant_count_
+= redundant_count
;
81 SampleCountPickleIterator
pickle_iter(iter
);
82 return AddSubtractImpl(&pickle_iter
, ADD
);
85 void HistogramSamples::Subtract(const HistogramSamples
& other
) {
87 redundant_count_
-= other
.redundant_count();
88 bool success
= AddSubtractImpl(other
.Iterator().get(), SUBTRACT
);
92 bool HistogramSamples::Serialize(Pickle
* pickle
) const {
93 if (!pickle
->WriteInt64(sum_
) || !pickle
->WriteInt(redundant_count_
))
96 HistogramBase::Sample min
;
97 HistogramBase::Sample max
;
98 HistogramBase::Count count
;
99 for (scoped_ptr
<SampleCountIterator
> it
= Iterator();
102 it
->Get(&min
, &max
, &count
);
103 if (!pickle
->WriteInt(min
) ||
104 !pickle
->WriteInt(max
) ||
105 !pickle
->WriteInt(count
))
111 void HistogramSamples::IncreaseSum(int64 diff
) {
115 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff
) {
116 base::subtle::NoBarrier_Store(&redundant_count_
,
117 base::subtle::NoBarrier_Load(&redundant_count_
) + diff
);
120 SampleCountIterator::~SampleCountIterator() {}
122 bool SampleCountIterator::GetBucketIndex(size_t* index
) const {