| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/metrics/histogram_samples.h" | |
| 6 | |
| 7 #include "base/compiler_specific.h" | |
| 8 #include "base/pickle.h" | |
| 9 | |
| 10 namespace base { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 class SampleCountPickleIterator : public SampleCountIterator { | |
| 15 public: | |
| 16 explicit SampleCountPickleIterator(PickleIterator* iter); | |
| 17 | |
| 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; | |
| 23 | |
| 24 private: | |
| 25 PickleIterator* const iter_; | |
| 26 | |
| 27 HistogramBase::Sample min_; | |
| 28 HistogramBase::Sample max_; | |
| 29 HistogramBase::Count count_; | |
| 30 bool is_done_; | |
| 31 }; | |
| 32 | |
| 33 SampleCountPickleIterator::SampleCountPickleIterator(PickleIterator* iter) | |
| 34 : iter_(iter), | |
| 35 is_done_(false) { | |
| 36 Next(); | |
| 37 } | |
| 38 | |
| 39 bool SampleCountPickleIterator::Done() const { | |
| 40 return is_done_; | |
| 41 } | |
| 42 | |
| 43 void SampleCountPickleIterator::Next() { | |
| 44 DCHECK(!Done()); | |
| 45 if (!iter_->ReadInt(&min_) || | |
| 46 !iter_->ReadInt(&max_) || | |
| 47 !iter_->ReadInt(&count_)) | |
| 48 is_done_ = true; | |
| 49 } | |
| 50 | |
| 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_; | |
| 58 } | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 HistogramSamples::HistogramSamples() : sum_(0), redundant_count_(0) {} | |
| 63 | |
| 64 HistogramSamples::~HistogramSamples() {} | |
| 65 | |
| 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); | |
| 74 } | |
| 75 | |
| 76 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { | |
| 77 int64 sum; | |
| 78 HistogramBase::Count redundant_count; | |
| 79 | |
| 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); | |
| 87 | |
| 88 SampleCountPickleIterator pickle_iter(iter); | |
| 89 return AddSubtractImpl(&pickle_iter, ADD); | |
| 90 } | |
| 91 | |
| 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); | |
| 100 } | |
| 101 | |
| 102 bool HistogramSamples::Serialize(Pickle* pickle) const { | |
| 103 if (!pickle->WriteInt64(sum_) || | |
| 104 !pickle->WriteInt(subtle::NoBarrier_Load(&redundant_count_))) | |
| 105 return false; | |
| 106 | |
| 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; | |
| 118 } | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 void HistogramSamples::IncreaseSum(int64 diff) { | |
| 123 sum_ += diff; | |
| 124 } | |
| 125 | |
| 126 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { | |
| 127 subtle::NoBarrier_Store(&redundant_count_, | |
| 128 subtle::NoBarrier_Load(&redundant_count_) + diff); | |
| 129 } | |
| 130 | |
| 131 SampleCountIterator::~SampleCountIterator() {} | |
| 132 | |
| 133 bool SampleCountIterator::GetBucketIndex(size_t* index) const { | |
| 134 DCHECK(!Done()); | |
| 135 return false; | |
| 136 } | |
| 137 | |
| 138 } // namespace base | |
| OLD | NEW |