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 // Histogram is an object that aggregates statistics, and can summarize them in | 5 // Histogram is an object that aggregates statistics, and can summarize them in |
6 // various forms, including ASCII graphical, HTML, and numerically (as a | 6 // various forms, including ASCII graphical, HTML, and numerically (as a |
7 // vector of numbers corresponding to each of the aggregating buckets). | 7 // vector of numbers corresponding to each of the aggregating buckets). |
8 // See header file for details and examples. | 8 // See header file for details and examples. |
9 | 9 |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 }; | 147 }; |
148 | 148 |
149 HistogramBase* Histogram::Factory::Build() { | 149 HistogramBase* Histogram::Factory::Build() { |
150 // Import histograms from known persistent storage. Histograms could have | 150 // Import histograms from known persistent storage. Histograms could have |
151 // been added by other processes and they must be fetched and recognized | 151 // been added by other processes and they must be fetched and recognized |
152 // locally in order to be found by FindHistograms() below. If the persistent | 152 // locally in order to be found by FindHistograms() below. If the persistent |
153 // memory segment is not shared between processes, this call does nothing. | 153 // memory segment is not shared between processes, this call does nothing. |
154 ImportPersistentHistograms(); | 154 ImportPersistentHistograms(); |
155 | 155 |
156 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name_); | 156 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name_); |
157 | |
158 // crbug.com/588946 debugging. See comment at end of function. | |
159 const BucketRanges* created_ranges = | |
160 reinterpret_cast<const BucketRanges*>(0xDEADBEEF); | |
161 const BucketRanges* registered_ranges = | |
162 reinterpret_cast<const BucketRanges*>(0xDEADBEEF); | |
163 HistogramBase* tentative_histogram = | |
164 reinterpret_cast<HistogramBase*>(0xDEADBEEF); | |
165 PersistentMemoryAllocator* allocator = | |
166 reinterpret_cast<PersistentMemoryAllocator*>(0xDEADBEEF); | |
167 PersistentMemoryAllocator::Reference histogram_ref = 0xDEADBEEF; | |
168 bool bad_args = false; | |
169 | |
170 if (!histogram) { | 157 if (!histogram) { |
171 // To avoid racy destruction at shutdown, the following will be leaked. | 158 // To avoid racy destruction at shutdown, the following will be leaked. |
172 created_ranges = CreateRanges(); | 159 const BucketRanges* created_ranges = CreateRanges(); |
173 registered_ranges = | 160 const BucketRanges* registered_ranges = |
174 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(created_ranges); | 161 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(created_ranges); |
175 | 162 |
176 // In most cases, the bucket-count, minimum, and maximum values are known | 163 // In most cases, the bucket-count, minimum, and maximum values are known |
177 // when the code is written and so are passed in explicitly. In other | 164 // when the code is written and so are passed in explicitly. In other |
178 // cases (such as with a CustomHistogram), they are calculated dynamically | 165 // cases (such as with a CustomHistogram), they are calculated dynamically |
179 // at run-time. In the latter case, those ctor parameters are zero and | 166 // at run-time. In the latter case, those ctor parameters are zero and |
180 // the results extracted from the result of CreateRanges(). | 167 // the results extracted from the result of CreateRanges(). |
181 if (bucket_count_ == 0) { | 168 if (bucket_count_ == 0) { |
182 bucket_count_ = static_cast<uint32_t>(registered_ranges->bucket_count()); | 169 bucket_count_ = static_cast<uint32_t>(registered_ranges->bucket_count()); |
183 minimum_ = registered_ranges->range(1); | 170 minimum_ = registered_ranges->range(1); |
184 maximum_ = registered_ranges->range(bucket_count_ - 1); | 171 maximum_ = registered_ranges->range(bucket_count_ - 1); |
185 } | 172 } |
186 | 173 |
187 // Try to create the histogram using a "persistent" allocator. As of | 174 // Try to create the histogram using a "persistent" allocator. As of |
188 // 2015-01-14, the availability of such is controlled by a base::Feature | 175 // 2015-01-14, the availability of such is controlled by a base::Feature |
189 // that is off by default. If the allocator doesn't exist or if | 176 // that is off by default. If the allocator doesn't exist or if |
190 // allocating from it fails, code below will allocate the histogram from | 177 // allocating from it fails, code below will allocate the histogram from |
191 // the process heap. | 178 // the process heap. |
192 histogram_ref = 0; | 179 PersistentMemoryAllocator::Reference histogram_ref = 0; |
193 tentative_histogram = nullptr; | 180 HistogramBase* tentative_histogram = nullptr; |
194 allocator = | 181 PersistentMemoryAllocator* allocator = |
195 GetPersistentHistogramMemoryAllocator(); | 182 GetPersistentHistogramMemoryAllocator(); |
196 if (allocator) { | 183 if (allocator) { |
197 flags_ |= HistogramBase::kIsPersistent; | 184 flags_ |= HistogramBase::kIsPersistent; |
198 tentative_histogram = AllocatePersistentHistogram( | 185 tentative_histogram = AllocatePersistentHistogram( |
199 allocator, | 186 allocator, |
200 histogram_type_, | 187 histogram_type_, |
201 name_, | 188 name_, |
202 minimum_, | 189 minimum_, |
203 maximum_, | 190 maximum_, |
204 registered_ranges, | 191 registered_ranges, |
(...skipping 24 matching lines...) Expand all Loading... |
229 DCHECK_EQ(histogram_type_, histogram->GetHistogramType()); | 216 DCHECK_EQ(histogram_type_, histogram->GetHistogramType()); |
230 if (bucket_count_ != 0 && | 217 if (bucket_count_ != 0 && |
231 !histogram->HasConstructionArguments(minimum_, maximum_, bucket_count_)) { | 218 !histogram->HasConstructionArguments(minimum_, maximum_, bucket_count_)) { |
232 // The construction arguments do not match the existing histogram. This can | 219 // The construction arguments do not match the existing histogram. This can |
233 // come about if an extension updates in the middle of a chrome run and has | 220 // come about if an extension updates in the middle of a chrome run and has |
234 // changed one of them, or simply by bad code within Chrome itself. We | 221 // changed one of them, or simply by bad code within Chrome itself. We |
235 // return NULL here with the expectation that bad code in Chrome will crash | 222 // return NULL here with the expectation that bad code in Chrome will crash |
236 // on dereference, but extension/Pepper APIs will guard against NULL and not | 223 // on dereference, but extension/Pepper APIs will guard against NULL and not |
237 // crash. | 224 // crash. |
238 DLOG(ERROR) << "Histogram " << name_ << " has bad construction arguments"; | 225 DLOG(ERROR) << "Histogram " << name_ << " has bad construction arguments"; |
239 bad_args = true; | 226 return nullptr; |
240 histogram = nullptr; | |
241 } | 227 } |
242 | |
243 #if !DCHECK_IS_ON() // Don't affect tests, only release builds. | |
244 // For the moment, crash here so that collected crash reports have access | |
245 // to the construction values in order to figure out why this is failing. | |
246 // TODO(bcwhite): Remove this once crbug.com/588946 is resolved. Also remove | |
247 // from beta-branch because we don't want crashes due to misbehaving | |
248 // extensions (see comment above). | |
249 if (!histogram) { | |
250 HistogramType histogram_type = histogram_type_; | |
251 HistogramBase::Sample minimum = minimum_; | |
252 HistogramBase::Sample maximum = maximum_; | |
253 uint32_t bucket_count = bucket_count_; | |
254 int32_t flags = flags_; | |
255 CHECK(histogram) << name_ << ": bad-args=" << bad_args; | |
256 base::debug::Alias(&histogram_type); | |
257 base::debug::Alias(&minimum); | |
258 base::debug::Alias(&maximum); | |
259 base::debug::Alias(&bucket_count); | |
260 base::debug::Alias(&flags); | |
261 base::debug::Alias(&created_ranges); | |
262 base::debug::Alias(®istered_ranges); | |
263 base::debug::Alias(&histogram_ref); | |
264 base::debug::Alias(&tentative_histogram); | |
265 base::debug::Alias(&allocator); | |
266 base::debug::Alias(&tentative_histogram); | |
267 } | |
268 #endif | |
269 | |
270 base::debug::Alias(&bad_args); // Down here so var is always "used". | |
271 return histogram; | 228 return histogram; |
272 } | 229 } |
273 | 230 |
274 HistogramBase* Histogram::FactoryGet(const std::string& name, | 231 HistogramBase* Histogram::FactoryGet(const std::string& name, |
275 Sample minimum, | 232 Sample minimum, |
276 Sample maximum, | 233 Sample maximum, |
277 uint32_t bucket_count, | 234 uint32_t bucket_count, |
278 int32_t flags) { | 235 int32_t flags) { |
279 bool valid_arguments = | 236 bool valid_arguments = |
280 InspectConstructionArguments(name, &minimum, &maximum, &bucket_count); | 237 InspectConstructionArguments(name, &minimum, &maximum, &bucket_count); |
(...skipping 917 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1198 Sample sample = custom_ranges[i]; | 1155 Sample sample = custom_ranges[i]; |
1199 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1) | 1156 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1) |
1200 return false; | 1157 return false; |
1201 if (sample != 0) | 1158 if (sample != 0) |
1202 has_valid_range = true; | 1159 has_valid_range = true; |
1203 } | 1160 } |
1204 return has_valid_range; | 1161 return has_valid_range; |
1205 } | 1162 } |
1206 | 1163 |
1207 } // namespace base | 1164 } // namespace base |
OLD | NEW |