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 HistogramBase::Count old_redundant_count =
68 subtle::NoBarrier_Load(&redundant_count_);
69 subtle::NoBarrier_Store(&redundant_count_,
70 old_redundant_count + other.redundant_count());
71 bool success = AddSubtractImpl(other.Iterator().get(), ADD);
75 bool HistogramSamples::AddFromPickle(PickleIterator* iter) {
77 HistogramBase::Count redundant_count;
79 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count))
82 HistogramBase::Count old_redundant_count =
83 subtle::NoBarrier_Load(&redundant_count_);
84 subtle::NoBarrier_Store(&redundant_count_,
85 old_redundant_count + redundant_count);
87 SampleCountPickleIterator pickle_iter(iter);
88 return AddSubtractImpl(&pickle_iter, ADD);
91 void HistogramSamples::Subtract(const HistogramSamples& other) {
93 HistogramBase::Count old_redundant_count =
94 subtle::NoBarrier_Load(&redundant_count_);
95 subtle::NoBarrier_Store(&redundant_count_,
96 old_redundant_count - other.redundant_count());
97 bool success = AddSubtractImpl(other.Iterator().get(), SUBTRACT);
101 bool HistogramSamples::Serialize(Pickle* pickle) const {
102 if (!pickle->WriteInt64(sum_) ||
103 !pickle->WriteInt(subtle::NoBarrier_Load(&redundant_count_)))
106 HistogramBase::Sample min;
107 HistogramBase::Sample max;
108 HistogramBase::Count count;
109 for (scoped_ptr<SampleCountIterator> it = Iterator();
112 it->Get(&min, &max, &count);
113 if (!pickle->WriteInt(min) ||
114 !pickle->WriteInt(max) ||
115 !pickle->WriteInt(count))
121 void HistogramSamples::IncreaseSum(int64 diff) {
125 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) {
126 subtle::NoBarrier_Store(&redundant_count_,
127 subtle::NoBarrier_Load(&redundant_count_) + diff);
130 SampleCountIterator::~SampleCountIterator() {}
132 bool SampleCountIterator::GetBucketIndex(size_t* index) const {