HttpServer: don't crash in SetReceiveBufferSize/SetSendBufferSize if connection is...
[chromium-blink-merge.git] / base / metrics / histogram_samples.cc
blobf5e03b979e110cbf297e27ab278225965141a1eb
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 bool Done() const override;
19 void Next() override;
20 void Get(HistogramBase::Sample* min,
21 HistogramBase::Sample* max,
22 HistogramBase::Count* count) const override;
24 private:
25 PickleIterator* const iter_;
27 HistogramBase::Sample min_;
28 HistogramBase::Sample max_;
29 HistogramBase::Count count_;
30 bool is_done_;
33 SampleCountPickleIterator::SampleCountPickleIterator(PickleIterator* iter)
34 : iter_(iter),
35 is_done_(false) {
36 Next();
39 bool SampleCountPickleIterator::Done() const {
40 return is_done_;
43 void SampleCountPickleIterator::Next() {
44 DCHECK(!Done());
45 if (!iter_->ReadInt(&min_) ||
46 !iter_->ReadInt(&max_) ||
47 !iter_->ReadInt(&count_))
48 is_done_ = true;
51 void SampleCountPickleIterator::Get(HistogramBase::Sample* min,
52 HistogramBase::Sample* max,
53 HistogramBase::Count* count) const {
54 DCHECK(!Done());
55 *min = min_;
56 *max = max_;
57 *count = count_;
60 } // namespace
62 HistogramSamples::HistogramSamples() : sum_(0), redundant_count_(0) {}
64 HistogramSamples::~HistogramSamples() {}
66 void HistogramSamples::Add(const HistogramSamples& other) {
67 sum_ += other.sum();
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);
73 DCHECK(success);
76 bool HistogramSamples::AddFromPickle(PickleIterator* iter) {
77 int64 sum;
78 HistogramBase::Count redundant_count;
80 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count))
81 return false;
82 sum_ += sum;
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) {
93 sum_ -= other.sum();
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);
99 DCHECK(success);
102 bool HistogramSamples::Serialize(Pickle* pickle) const {
103 if (!pickle->WriteInt64(sum_) ||
104 !pickle->WriteInt(subtle::NoBarrier_Load(&redundant_count_)))
105 return false;
107 HistogramBase::Sample min;
108 HistogramBase::Sample max;
109 HistogramBase::Count count;
110 for (scoped_ptr<SampleCountIterator> it = Iterator();
111 !it->Done();
112 it->Next()) {
113 it->Get(&min, &max, &count);
114 if (!pickle->WriteInt(min) ||
115 !pickle->WriteInt(max) ||
116 !pickle->WriteInt(count))
117 return false;
119 return true;
122 void HistogramSamples::IncreaseSum(int64 diff) {
123 sum_ += 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 {
134 DCHECK(!Done());
135 return false;
138 } // namespace base