Webkit roll 143935:143980
[chromium-blink-merge.git] / base / metrics / histogram_samples.cc
blob0e0eeb4dd3dcb6bde0f7235a0872595a5b38791a
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"
10 namespace base {
12 namespace {
14 class SampleCountPickleIterator : public SampleCountIterator {
15 public:
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;
23 private:
24 PickleIterator* const iter_;
26 HistogramBase::Sample min_;
27 HistogramBase::Sample max_;
28 HistogramBase::Count count_;
29 bool is_done_;
32 SampleCountPickleIterator::SampleCountPickleIterator(PickleIterator* iter)
33 : iter_(iter),
34 is_done_(false) {
35 Next();
38 bool SampleCountPickleIterator::Done() const {
39 return is_done_;
42 void SampleCountPickleIterator::Next() {
43 DCHECK(!Done());
44 if (!iter_->ReadInt(&min_) ||
45 !iter_->ReadInt(&max_) ||
46 !iter_->ReadInt(&count_))
47 is_done_ = true;
50 void SampleCountPickleIterator::Get(HistogramBase::Sample* min,
51 HistogramBase::Sample* max,
52 HistogramBase::Count* count) const {
53 DCHECK(!Done());
54 *min = min_;
55 *max = max_;
56 *count = count_;
59 } // namespace
61 HistogramSamples::HistogramSamples() : sum_(0), redundant_count_(0) {}
63 HistogramSamples::~HistogramSamples() {}
65 void HistogramSamples::Add(const HistogramSamples& other) {
66 sum_ += other.sum();
67 redundant_count_ += other.redundant_count();
68 bool success = AddSubtractImpl(other.Iterator().get(), ADD);
69 DCHECK(success);
72 bool HistogramSamples::AddFromPickle(PickleIterator* iter) {
73 int64 sum;
74 HistogramBase::Count redundant_count;
76 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count))
77 return false;
78 sum_ += sum;
79 redundant_count_ += redundant_count;
81 SampleCountPickleIterator pickle_iter(iter);
82 return AddSubtractImpl(&pickle_iter, ADD);
85 void HistogramSamples::Subtract(const HistogramSamples& other) {
86 sum_ -= other.sum();
87 redundant_count_ -= other.redundant_count();
88 bool success = AddSubtractImpl(other.Iterator().get(), SUBTRACT);
89 DCHECK(success);
92 bool HistogramSamples::Serialize(Pickle* pickle) const {
93 if (!pickle->WriteInt64(sum_) || !pickle->WriteInt(redundant_count_))
94 return false;
96 HistogramBase::Sample min;
97 HistogramBase::Sample max;
98 HistogramBase::Count count;
99 for (scoped_ptr<SampleCountIterator> it = Iterator();
100 !it->Done();
101 it->Next()) {
102 it->Get(&min, &max, &count);
103 if (!pickle->WriteInt(min) ||
104 !pickle->WriteInt(max) ||
105 !pickle->WriteInt(count))
106 return false;
108 return true;
111 void HistogramSamples::IncreaseSum(int64 diff) {
112 sum_ += diff;
115 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) {
116 redundant_count_ += diff;
119 SampleCountIterator::~SampleCountIterator() {}
121 bool SampleCountIterator::GetBucketIndex(size_t* index) const {
122 DCHECK(!Done());
123 return false;
126 } // namespace base