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 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_; 25 26 HistogramBase::Sample min_; 27 HistogramBase::Sample max_; 28 HistogramBase::Count count_; 29 bool is_done_; 30 }; 31 32 SampleCountPickleIterator::SampleCountPickleIterator(PickleIterator* iter) 33 : iter_(iter), 34 is_done_(false) { 35 Next(); 36 } 37 38 bool SampleCountPickleIterator::Done() const { 39 return is_done_; 40 } 41 42 void SampleCountPickleIterator::Next() { 43 DCHECK(!Done()); 44 if (!iter_->ReadInt(&min_) || 45 !iter_->ReadInt(&max_) || 46 !iter_->ReadInt(&count_)) 47 is_done_ = true; 48 } 49 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_; 57 } 58 59 } // namespace 60 61 HistogramSamples::HistogramSamples() : sum_(0), redundant_count_(0) {} 62 63 HistogramSamples::~HistogramSamples() {} 64 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); 70 } 71 72 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { 73 int64 sum; 74 HistogramBase::Count redundant_count; 75 76 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count)) 77 return false; 78 sum_ += sum; 79 redundant_count_ += redundant_count; 80 81 SampleCountPickleIterator pickle_iter(iter); 82 return AddSubtractImpl(&pickle_iter, ADD); 83 } 84 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); 90 } 91 92 bool HistogramSamples::Serialize(Pickle* pickle) const { 93 if (!pickle->WriteInt64(sum_) || !pickle->WriteInt(redundant_count_)) 94 return false; 95 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; 107 } 108 return true; 109 } 110 111 void HistogramSamples::IncreaseSum(int64 diff) { 112 sum_ += diff; 113 } 114 115 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { 116 redundant_count_ += diff; 117 } 118 119 SampleCountIterator::~SampleCountIterator() {} 120 121 bool SampleCountIterator::GetBucketIndex(size_t* index) const { 122 DCHECK(!Done()); 123 return false; 124 } 125 126 } // namespace base 127