| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/metrics/histogram_samples.h" | 5 #include "base/metrics/histogram_samples.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 HistogramBase::Sample* max, | 52 HistogramBase::Sample* max, |
| 53 HistogramBase::Count* count) const { | 53 HistogramBase::Count* count) const { |
| 54 DCHECK(!Done()); | 54 DCHECK(!Done()); |
| 55 *min = min_; | 55 *min = min_; |
| 56 *max = max_; | 56 *max = max_; |
| 57 *count = count_; | 57 *count = count_; |
| 58 } | 58 } |
| 59 | 59 |
| 60 } // namespace | 60 } // namespace |
| 61 | 61 |
| 62 HistogramSamples::HistogramSamples() : sum_(0), redundant_count_(0) {} | 62 HistogramSamples::HistogramSamples(uint64_t id) |
| 63 : HistogramSamples(id, &local_meta_) {} |
| 64 |
| 65 HistogramSamples::HistogramSamples(uint64_t id, Metadata* meta) |
| 66 : meta_(meta) { |
| 67 DCHECK(meta_->id == 0 || meta_->id == id); |
| 68 meta_->id = id; |
| 69 } |
| 63 | 70 |
| 64 HistogramSamples::~HistogramSamples() {} | 71 HistogramSamples::~HistogramSamples() {} |
| 65 | 72 |
| 73 // Despite using atomic operations, the increment/add actions below are *not* |
| 74 // atomic! Race conditions may cause loss of samples or even completely corrupt |
| 75 // the 64-bit sum on 32-bit machines. This is done intentionally to reduce the |
| 76 // cost of these operations that could be executed in performance-significant |
| 77 // points of the code. |
| 78 // |
| 79 // TODO(bcwhite): Gather quantitative information as to the cost of using |
| 80 // proper atomic increments and improve either globally or for those histograms |
| 81 // that really need it. |
| 82 |
| 66 void HistogramSamples::Add(const HistogramSamples& other) { | 83 void HistogramSamples::Add(const HistogramSamples& other) { |
| 67 sum_ += other.sum(); | 84 meta_->sum += other.sum(); |
| 85 |
| 68 HistogramBase::Count old_redundant_count = | 86 HistogramBase::Count old_redundant_count = |
| 69 subtle::NoBarrier_Load(&redundant_count_); | 87 subtle::NoBarrier_Load(&meta_->redundant_count); |
| 70 subtle::NoBarrier_Store(&redundant_count_, | 88 subtle::NoBarrier_Store(&meta_->redundant_count, |
| 71 old_redundant_count + other.redundant_count()); | 89 old_redundant_count + other.redundant_count()); |
| 72 bool success = AddSubtractImpl(other.Iterator().get(), ADD); | 90 bool success = AddSubtractImpl(other.Iterator().get(), ADD); |
| 73 DCHECK(success); | 91 DCHECK(success); |
| 74 } | 92 } |
| 75 | 93 |
| 76 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { | 94 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { |
| 77 int64 sum; | 95 int64_t sum; |
| 78 HistogramBase::Count redundant_count; | 96 HistogramBase::Count redundant_count; |
| 79 | 97 |
| 80 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count)) | 98 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count)) |
| 81 return false; | 99 return false; |
| 82 sum_ += sum; | 100 |
| 101 meta_->sum += sum; |
| 102 |
| 83 HistogramBase::Count old_redundant_count = | 103 HistogramBase::Count old_redundant_count = |
| 84 subtle::NoBarrier_Load(&redundant_count_); | 104 subtle::NoBarrier_Load(&meta_->redundant_count); |
| 85 subtle::NoBarrier_Store(&redundant_count_, | 105 subtle::NoBarrier_Store(&meta_->redundant_count, |
| 86 old_redundant_count + redundant_count); | 106 old_redundant_count + redundant_count); |
| 87 | 107 |
| 88 SampleCountPickleIterator pickle_iter(iter); | 108 SampleCountPickleIterator pickle_iter(iter); |
| 89 return AddSubtractImpl(&pickle_iter, ADD); | 109 return AddSubtractImpl(&pickle_iter, ADD); |
| 90 } | 110 } |
| 91 | 111 |
| 92 void HistogramSamples::Subtract(const HistogramSamples& other) { | 112 void HistogramSamples::Subtract(const HistogramSamples& other) { |
| 93 sum_ -= other.sum(); | 113 meta_->sum -= other.sum(); |
| 114 |
| 94 HistogramBase::Count old_redundant_count = | 115 HistogramBase::Count old_redundant_count = |
| 95 subtle::NoBarrier_Load(&redundant_count_); | 116 subtle::NoBarrier_Load(&meta_->redundant_count); |
| 96 subtle::NoBarrier_Store(&redundant_count_, | 117 subtle::NoBarrier_Store(&meta_->redundant_count, |
| 97 old_redundant_count - other.redundant_count()); | 118 old_redundant_count - other.redundant_count()); |
| 98 bool success = AddSubtractImpl(other.Iterator().get(), SUBTRACT); | 119 bool success = AddSubtractImpl(other.Iterator().get(), SUBTRACT); |
| 99 DCHECK(success); | 120 DCHECK(success); |
| 100 } | 121 } |
| 101 | 122 |
| 102 bool HistogramSamples::Serialize(Pickle* pickle) const { | 123 bool HistogramSamples::Serialize(Pickle* pickle) const { |
| 103 if (!pickle->WriteInt64(sum_) || | 124 if (!pickle->WriteInt64(meta_->sum)) |
| 104 !pickle->WriteInt(subtle::NoBarrier_Load(&redundant_count_))) | 125 return false; |
| 126 if (!pickle->WriteInt(subtle::NoBarrier_Load(&meta_->redundant_count))) |
| 105 return false; | 127 return false; |
| 106 | 128 |
| 107 HistogramBase::Sample min; | 129 HistogramBase::Sample min; |
| 108 HistogramBase::Sample max; | 130 HistogramBase::Sample max; |
| 109 HistogramBase::Count count; | 131 HistogramBase::Count count; |
| 110 for (scoped_ptr<SampleCountIterator> it = Iterator(); | 132 for (scoped_ptr<SampleCountIterator> it = Iterator(); |
| 111 !it->Done(); | 133 !it->Done(); |
| 112 it->Next()) { | 134 it->Next()) { |
| 113 it->Get(&min, &max, &count); | 135 it->Get(&min, &max, &count); |
| 114 if (!pickle->WriteInt(min) || | 136 if (!pickle->WriteInt(min) || |
| 115 !pickle->WriteInt(max) || | 137 !pickle->WriteInt(max) || |
| 116 !pickle->WriteInt(count)) | 138 !pickle->WriteInt(count)) |
| 117 return false; | 139 return false; |
| 118 } | 140 } |
| 119 return true; | 141 return true; |
| 120 } | 142 } |
| 121 | 143 |
| 122 void HistogramSamples::IncreaseSum(int64 diff) { | 144 void HistogramSamples::IncreaseSum(int64_t diff) { |
| 123 sum_ += diff; | 145 meta_->sum += diff; |
| 124 } | 146 } |
| 125 | 147 |
| 126 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { | 148 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { |
| 127 subtle::NoBarrier_Store(&redundant_count_, | 149 subtle::NoBarrier_Store(&meta_->redundant_count, |
| 128 subtle::NoBarrier_Load(&redundant_count_) + diff); | 150 subtle::NoBarrier_Load(&meta_->redundant_count) + diff); |
| 129 } | 151 } |
| 130 | 152 |
| 131 SampleCountIterator::~SampleCountIterator() {} | 153 SampleCountIterator::~SampleCountIterator() {} |
| 132 | 154 |
| 133 bool SampleCountIterator::GetBucketIndex(size_t* index) const { | 155 bool SampleCountIterator::GetBucketIndex(size_t* index) const { |
| 134 DCHECK(!Done()); | 156 DCHECK(!Done()); |
| 135 return false; | 157 return false; |
| 136 } | 158 } |
| 137 | 159 |
| 138 } // namespace base | 160 } // namespace base |
| OLD | NEW |