Chromium Code Reviews
|
| 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 SampleCountPickleIterator(PickleIterator* iter); | |
| 17 | |
| 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 bool ReadNext(); | |
| 25 | |
| 26 PickleIterator* const iter_; | |
| 27 | |
| 28 HistogramBase::Sample min_; | |
| 29 HistogramBase::Sample max_; | |
| 30 HistogramBase::Count count_; | |
| 31 bool is_done_; | |
| 32 }; | |
| 33 | |
| 34 SampleCountPickleIterator::SampleCountPickleIterator(PickleIterator* iter) | |
| 35 : iter_(iter), | |
| 36 is_done_(false) { | |
| 37 Next(); | |
| 38 } | |
| 39 | |
| 40 bool SampleCountPickleIterator::Done() const { | |
| 41 return is_done_; | |
| 42 } | |
| 43 | |
| 44 void SampleCountPickleIterator::Next() { | |
| 45 CHECK(!Done()); | |
| 46 if (!ReadNext()) | |
| 47 is_done_ = true; | |
| 48 } | |
| 49 | |
| 50 void SampleCountPickleIterator::Get(HistogramBase::Sample* min, | |
| 51 HistogramBase::Sample* max, | |
| 52 HistogramBase::Count* count) const { | |
| 53 CHECK(!Done()); | |
| 54 *min = min_; | |
| 55 *max = max_; | |
| 56 *count = count_; | |
| 57 } | |
| 58 | |
| 59 bool SampleCountPickleIterator::ReadNext() { | |
|
Ilya Sherman
2012/09/12 03:20:58
Optional nit: This method is only used in one plac
kaiwang
2012/09/20 22:54:59
Done.
| |
| 60 return iter_->ReadInt(&min_) && | |
| 61 iter_->ReadInt(&max_) && | |
| 62 iter_->ReadInt(&count_); | |
| 63 } | |
| 64 | |
| 65 } // namespace | |
| 66 | |
| 67 HistogramSamples::HistogramSamples() : sum_(0), redundant_count_(0) {} | |
| 68 | |
| 69 HistogramSamples::~HistogramSamples() {} | |
| 70 | |
| 71 void HistogramSamples::Add(const HistogramSamples& other) { | |
| 72 sum_ += other.sum(); | |
| 73 redundant_count_ += other.redundant_count(); | |
| 74 CHECK(AddSubtractImpl(other.Iterator().get(), ADD)); | |
| 75 } | |
| 76 | |
| 77 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { | |
| 78 int64 sum; | |
| 79 HistogramBase::Count redundant_count; | |
| 80 | |
| 81 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count)) | |
| 82 return false; | |
| 83 sum_ += sum; | |
| 84 redundant_count_ += redundant_count; | |
| 85 | |
| 86 SampleCountPickleIterator pickle_iter(iter); | |
| 87 return AddSubtractImpl(&pickle_iter, ADD); | |
| 88 } | |
| 89 | |
| 90 void HistogramSamples::Subtract(const HistogramSamples& other) { | |
| 91 sum_ -= other.sum(); | |
| 92 redundant_count_ -= other.redundant_count(); | |
| 93 CHECK(AddSubtractImpl(other.Iterator().get(), SUBTRACT)); | |
| 94 } | |
| 95 | |
| 96 bool HistogramSamples::Serialize(Pickle* pickle) const { | |
| 97 if (!pickle->WriteInt64(sum_) || !pickle->WriteInt(redundant_count_)) | |
| 98 return false; | |
| 99 | |
| 100 HistogramBase::Sample min; | |
| 101 HistogramBase::Sample max; | |
| 102 HistogramBase::Count count; | |
| 103 for (scoped_ptr<SampleCountIterator> it = Iterator(); | |
| 104 !it->Done(); | |
| 105 it->Next()) { | |
| 106 it->Get(&min, &max, &count); | |
| 107 if (!pickle->WriteInt(min) || | |
| 108 !pickle->WriteInt(max) || | |
| 109 !pickle->WriteInt(count)) | |
| 110 return false; | |
| 111 } | |
| 112 return true; | |
| 113 } | |
| 114 | |
| 115 void HistogramSamples::IncreaseSum(int64 diff) { | |
| 116 sum_ += diff; | |
| 117 } | |
| 118 | |
| 119 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { | |
| 120 redundant_count_ += diff; | |
| 121 } | |
| 122 | |
| 123 SampleCountIterator::~SampleCountIterator() {} | |
| 124 | |
| 125 bool SampleCountIterator::GetBucketIndex(size_t* index) const { | |
| 126 CHECK(!Done()); | |
|
Ilya Sherman
2012/09/12 03:20:58
nit: DCHECK
| |
| 127 return false; | |
| 128 } | |
| 129 | |
| 130 } // namespace base | |
| OLD | NEW |