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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // atomic! Race conditions may cause loss of samples or even completely corrupt | 80 // atomic! Race conditions may cause loss of samples or even completely corrupt |
81 // the 64-bit sum on 32-bit machines. This is done intentionally to reduce the | 81 // the 64-bit sum on 32-bit machines. This is done intentionally to reduce the |
82 // cost of these operations that could be executed in performance-significant | 82 // cost of these operations that could be executed in performance-significant |
83 // points of the code. | 83 // points of the code. |
84 // | 84 // |
85 // TODO(bcwhite): Gather quantitative information as to the cost of using | 85 // TODO(bcwhite): Gather quantitative information as to the cost of using |
86 // proper atomic increments and improve either globally or for those histograms | 86 // proper atomic increments and improve either globally or for those histograms |
87 // that really need it. | 87 // that really need it. |
88 | 88 |
89 void HistogramSamples::Add(const HistogramSamples& other) { | 89 void HistogramSamples::Add(const HistogramSamples& other) { |
90 #ifdef ARCH_CPU_64_BITS | |
91 subtle::NoBarrier_Store( | |
92 &meta_->sum, | |
93 subtle::NoBarrier_Load(&meta_->sum) + other.sum()); | |
94 #else // No Atomic64 on 32-bit platforms. | |
95 meta_->sum += other.sum(); | 90 meta_->sum += other.sum(); |
96 #endif | |
97 | 91 |
98 HistogramBase::Count old_redundant_count = | 92 HistogramBase::Count old_redundant_count = |
99 subtle::NoBarrier_Load(&meta_->redundant_count); | 93 subtle::NoBarrier_Load(&meta_->redundant_count); |
100 subtle::NoBarrier_Store(&meta_->redundant_count, | 94 subtle::NoBarrier_Store(&meta_->redundant_count, |
101 old_redundant_count + other.redundant_count()); | 95 old_redundant_count + other.redundant_count()); |
102 bool success = AddSubtractImpl(other.Iterator().get(), ADD); | 96 bool success = AddSubtractImpl(other.Iterator().get(), ADD); |
103 DCHECK(success); | 97 DCHECK(success); |
104 } | 98 } |
105 | 99 |
106 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { | 100 bool HistogramSamples::AddFromPickle(PickleIterator* iter) { |
107 int64 sum; | 101 int64 sum; |
108 HistogramBase::Count redundant_count; | 102 HistogramBase::Count redundant_count; |
109 | 103 |
110 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count)) | 104 if (!iter->ReadInt64(&sum) || !iter->ReadInt(&redundant_count)) |
111 return false; | 105 return false; |
112 | 106 |
113 #ifdef ARCH_CPU_64_BITS | |
114 subtle::NoBarrier_Store( | |
115 &meta_->sum, | |
116 subtle::NoBarrier_Load(&meta_->sum) + sum); | |
117 #else // No Atomic64 on 32-bit platforms. | |
118 meta_->sum += sum; | 107 meta_->sum += sum; |
119 #endif | |
120 | 108 |
121 HistogramBase::Count old_redundant_count = | 109 HistogramBase::Count old_redundant_count = |
122 subtle::NoBarrier_Load(&meta_->redundant_count); | 110 subtle::NoBarrier_Load(&meta_->redundant_count); |
123 subtle::NoBarrier_Store(&meta_->redundant_count, | 111 subtle::NoBarrier_Store(&meta_->redundant_count, |
124 old_redundant_count + redundant_count); | 112 old_redundant_count + redundant_count); |
125 | 113 |
126 SampleCountPickleIterator pickle_iter(iter); | 114 SampleCountPickleIterator pickle_iter(iter); |
127 return AddSubtractImpl(&pickle_iter, ADD); | 115 return AddSubtractImpl(&pickle_iter, ADD); |
128 } | 116 } |
129 | 117 |
130 void HistogramSamples::Subtract(const HistogramSamples& other) { | 118 void HistogramSamples::Subtract(const HistogramSamples& other) { |
131 #ifdef ARCH_CPU_64_BITS | |
132 subtle::NoBarrier_Store( | |
133 &meta_->sum, | |
134 subtle::NoBarrier_Load(&meta_->sum) - other.sum()); | |
135 #else // No Atomic64 on 32-bit platforms. | |
136 meta_->sum -= other.sum(); | 119 meta_->sum -= other.sum(); |
137 #endif | |
138 | 120 |
139 HistogramBase::Count old_redundant_count = | 121 HistogramBase::Count old_redundant_count = |
140 subtle::NoBarrier_Load(&meta_->redundant_count); | 122 subtle::NoBarrier_Load(&meta_->redundant_count); |
141 subtle::NoBarrier_Store(&meta_->redundant_count, | 123 subtle::NoBarrier_Store(&meta_->redundant_count, |
142 old_redundant_count - other.redundant_count()); | 124 old_redundant_count - other.redundant_count()); |
143 bool success = AddSubtractImpl(other.Iterator().get(), SUBTRACT); | 125 bool success = AddSubtractImpl(other.Iterator().get(), SUBTRACT); |
144 DCHECK(success); | 126 DCHECK(success); |
145 } | 127 } |
146 | 128 |
147 bool HistogramSamples::Serialize(Pickle* pickle) const { | 129 bool HistogramSamples::Serialize(Pickle* pickle) const { |
148 #ifdef ARCH_CPU_64_BITS | |
149 if (!pickle->WriteInt64(subtle::NoBarrier_Load(&meta_->sum))) | |
150 return false; | |
151 #else // No Atomic64 on 32-bit platforms. | |
152 if (!pickle->WriteInt64(meta_->sum)) | 130 if (!pickle->WriteInt64(meta_->sum)) |
153 return false; | 131 return false; |
154 #endif | |
155 if (!pickle->WriteInt(subtle::NoBarrier_Load(&meta_->redundant_count))) | 132 if (!pickle->WriteInt(subtle::NoBarrier_Load(&meta_->redundant_count))) |
156 return false; | 133 return false; |
157 | 134 |
158 HistogramBase::Sample min; | 135 HistogramBase::Sample min; |
159 HistogramBase::Sample max; | 136 HistogramBase::Sample max; |
160 HistogramBase::Count count; | 137 HistogramBase::Count count; |
161 for (scoped_ptr<SampleCountIterator> it = Iterator(); | 138 for (scoped_ptr<SampleCountIterator> it = Iterator(); |
162 !it->Done(); | 139 !it->Done(); |
163 it->Next()) { | 140 it->Next()) { |
164 it->Get(&min, &max, &count); | 141 it->Get(&min, &max, &count); |
165 if (!pickle->WriteInt(min) || | 142 if (!pickle->WriteInt(min) || |
166 !pickle->WriteInt(max) || | 143 !pickle->WriteInt(max) || |
167 !pickle->WriteInt(count)) | 144 !pickle->WriteInt(count)) |
168 return false; | 145 return false; |
169 } | 146 } |
170 return true; | 147 return true; |
171 } | 148 } |
172 | 149 |
173 void HistogramSamples::IncreaseSum(int64 diff) { | 150 void HistogramSamples::IncreaseSum(int64 diff) { |
174 #ifdef ARCH_CPU_64_BITS | |
175 subtle::NoBarrier_Store( | |
176 &meta_->sum, | |
177 subtle::NoBarrier_Load(&meta_->sum) + diff); | |
178 #else // No Atomic64 on 32-bit platforms. | |
179 meta_->sum += diff; | 151 meta_->sum += diff; |
180 #endif | |
181 } | 152 } |
182 | 153 |
183 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { | 154 void HistogramSamples::IncreaseRedundantCount(HistogramBase::Count diff) { |
184 subtle::NoBarrier_Store(&meta_->redundant_count, | 155 subtle::NoBarrier_Store(&meta_->redundant_count, |
185 subtle::NoBarrier_Load(&meta_->redundant_count) + diff); | 156 subtle::NoBarrier_Load(&meta_->redundant_count) + diff); |
186 } | 157 } |
187 | 158 |
188 SampleCountIterator::~SampleCountIterator() {} | 159 SampleCountIterator::~SampleCountIterator() {} |
189 | 160 |
190 bool SampleCountIterator::GetBucketIndex(size_t* index) const { | 161 bool SampleCountIterator::GetBucketIndex(size_t* index) const { |
191 DCHECK(!Done()); | 162 DCHECK(!Done()); |
192 return false; | 163 return false; |
193 } | 164 } |
194 | 165 |
195 } // namespace base | 166 } // namespace base |
OLD | NEW |