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/statistics_recorder.h" | 5 #include "base/metrics/statistics_recorder.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/debug/leak_annotations.h" | 10 #include "base/debug/leak_annotations.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 } | 106 } |
107 | 107 |
108 HistogramBase* histogram_to_delete = NULL; | 108 HistogramBase* histogram_to_delete = NULL; |
109 HistogramBase* histogram_to_return = NULL; | 109 HistogramBase* histogram_to_return = NULL; |
110 { | 110 { |
111 base::AutoLock auto_lock(*lock_); | 111 base::AutoLock auto_lock(*lock_); |
112 if (histograms_ == NULL) { | 112 if (histograms_ == NULL) { |
113 histogram_to_return = histogram; | 113 histogram_to_return = histogram; |
114 } else { | 114 } else { |
115 const std::string& name = histogram->histogram_name(); | 115 const std::string& name = histogram->histogram_name(); |
116 const uint64_t name_hash = histogram->name_hash(); | 116 HistogramMap::iterator it = histograms_->find(name); |
117 DCHECK_NE(0U, name_hash); | |
118 HistogramMap::iterator it = histograms_->find(name_hash); | |
119 if (histograms_->end() == it) { | 117 if (histograms_->end() == it) { |
120 (*histograms_)[name_hash] = histogram; | 118 // The StringKey references the name within |histogram| rather than |
| 119 // making a copy. |
| 120 (*histograms_)[name] = histogram; |
121 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 | 121 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 |
122 // If there are callbacks for this histogram, we set the kCallbackExists | 122 // If there are callbacks for this histogram, we set the kCallbackExists |
123 // flag. | 123 // flag. |
124 auto callback_iterator = callbacks_->find(name); | 124 auto callback_iterator = callbacks_->find(name); |
125 if (callback_iterator != callbacks_->end()) { | 125 if (callback_iterator != callbacks_->end()) { |
126 if (!callback_iterator->second.is_null()) | 126 if (!callback_iterator->second.is_null()) |
127 histogram->SetFlags(HistogramBase::kCallbackExists); | 127 histogram->SetFlags(HistogramBase::kCallbackExists); |
128 else | 128 else |
129 histogram->ClearFlags(HistogramBase::kCallbackExists); | 129 histogram->ClearFlags(HistogramBase::kCallbackExists); |
130 } | 130 } |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 | 254 |
255 // static | 255 // static |
256 void StatisticsRecorder::GetHistograms(Histograms* output) { | 256 void StatisticsRecorder::GetHistograms(Histograms* output) { |
257 if (lock_ == NULL) | 257 if (lock_ == NULL) |
258 return; | 258 return; |
259 base::AutoLock auto_lock(*lock_); | 259 base::AutoLock auto_lock(*lock_); |
260 if (histograms_ == NULL) | 260 if (histograms_ == NULL) |
261 return; | 261 return; |
262 | 262 |
263 for (const auto& entry : *histograms_) { | 263 for (const auto& entry : *histograms_) { |
264 DCHECK_EQ(entry.first, entry.second->name_hash()); | |
265 output->push_back(entry.second); | 264 output->push_back(entry.second); |
266 } | 265 } |
267 } | 266 } |
268 | 267 |
269 // static | 268 // static |
270 void StatisticsRecorder::GetBucketRanges( | 269 void StatisticsRecorder::GetBucketRanges( |
271 std::vector<const BucketRanges*>* output) { | 270 std::vector<const BucketRanges*>* output) { |
272 if (lock_ == NULL) | 271 if (lock_ == NULL) |
273 return; | 272 return; |
274 base::AutoLock auto_lock(*lock_); | 273 base::AutoLock auto_lock(*lock_); |
(...skipping 16 matching lines...) Expand all Loading... |
291 return NULL; | 290 return NULL; |
292 | 291 |
293 // Import histograms from known persistent storage. Histograms could have | 292 // Import histograms from known persistent storage. Histograms could have |
294 // been added by other processes and they must be fetched and recognized | 293 // been added by other processes and they must be fetched and recognized |
295 // locally. If the persistent memory segment is not shared between processes, | 294 // locally. If the persistent memory segment is not shared between processes, |
296 // this call does nothing. | 295 // this call does nothing. |
297 GlobalHistogramAllocator* allocator = GlobalHistogramAllocator::Get(); | 296 GlobalHistogramAllocator* allocator = GlobalHistogramAllocator::Get(); |
298 if (allocator) | 297 if (allocator) |
299 allocator->ImportHistogramsToStatisticsRecorder(); | 298 allocator->ImportHistogramsToStatisticsRecorder(); |
300 | 299 |
301 HistogramMap::iterator it = histograms_->find(HashMetricName(name)); | 300 HistogramMap::iterator it = histograms_->find(name); |
302 if (histograms_->end() == it) | 301 if (histograms_->end() == it) |
303 return NULL; | 302 return NULL; |
304 DCHECK_EQ(name, it->second->histogram_name()) << "hash collision"; | |
305 return it->second; | 303 return it->second; |
306 } | 304 } |
307 | 305 |
308 // static | 306 // static |
309 StatisticsRecorder::HistogramIterator StatisticsRecorder::begin( | 307 StatisticsRecorder::HistogramIterator StatisticsRecorder::begin( |
310 bool include_persistent) { | 308 bool include_persistent) { |
311 return HistogramIterator(histograms_->begin(), include_persistent); | 309 return HistogramIterator(histograms_->begin(), include_persistent); |
312 } | 310 } |
313 | 311 |
314 // static | 312 // static |
(...skipping 24 matching lines...) Expand all Loading... |
339 if (lock_ == NULL) | 337 if (lock_ == NULL) |
340 return false; | 338 return false; |
341 base::AutoLock auto_lock(*lock_); | 339 base::AutoLock auto_lock(*lock_); |
342 if (histograms_ == NULL) | 340 if (histograms_ == NULL) |
343 return false; | 341 return false; |
344 | 342 |
345 if (ContainsKey(*callbacks_, name)) | 343 if (ContainsKey(*callbacks_, name)) |
346 return false; | 344 return false; |
347 callbacks_->insert(std::make_pair(name, cb)); | 345 callbacks_->insert(std::make_pair(name, cb)); |
348 | 346 |
349 auto it = histograms_->find(HashMetricName(name)); | 347 auto it = histograms_->find(name); |
350 if (it != histograms_->end()) { | 348 if (it != histograms_->end()) |
351 DCHECK_EQ(name, it->second->histogram_name()) << "hash collision"; | |
352 it->second->SetFlags(HistogramBase::kCallbackExists); | 349 it->second->SetFlags(HistogramBase::kCallbackExists); |
353 } | |
354 | 350 |
355 return true; | 351 return true; |
356 } | 352 } |
357 | 353 |
358 // static | 354 // static |
359 void StatisticsRecorder::ClearCallback(const std::string& name) { | 355 void StatisticsRecorder::ClearCallback(const std::string& name) { |
360 if (lock_ == NULL) | 356 if (lock_ == NULL) |
361 return; | 357 return; |
362 base::AutoLock auto_lock(*lock_); | 358 base::AutoLock auto_lock(*lock_); |
363 if (histograms_ == NULL) | 359 if (histograms_ == NULL) |
364 return; | 360 return; |
365 | 361 |
366 callbacks_->erase(name); | 362 callbacks_->erase(name); |
367 | 363 |
368 // We also clear the flag from the histogram (if it exists). | 364 // We also clear the flag from the histogram (if it exists). |
369 auto it = histograms_->find(HashMetricName(name)); | 365 auto it = histograms_->find(name); |
370 if (it != histograms_->end()) { | 366 if (it != histograms_->end()) |
371 DCHECK_EQ(name, it->second->histogram_name()) << "hash collision"; | |
372 it->second->ClearFlags(HistogramBase::kCallbackExists); | 367 it->second->ClearFlags(HistogramBase::kCallbackExists); |
373 } | |
374 } | 368 } |
375 | 369 |
376 // static | 370 // static |
377 StatisticsRecorder::OnSampleCallback StatisticsRecorder::FindCallback( | 371 StatisticsRecorder::OnSampleCallback StatisticsRecorder::FindCallback( |
378 const std::string& name) { | 372 const std::string& name) { |
379 if (lock_ == NULL) | 373 if (lock_ == NULL) |
380 return OnSampleCallback(); | 374 return OnSampleCallback(); |
381 base::AutoLock auto_lock(*lock_); | 375 base::AutoLock auto_lock(*lock_); |
382 if (histograms_ == NULL) | 376 if (histograms_ == NULL) |
383 return OnSampleCallback(); | 377 return OnSampleCallback(); |
(...skipping 16 matching lines...) Expand all Loading... |
400 | 394 |
401 // static | 395 // static |
402 void StatisticsRecorder::ResetForTesting() { | 396 void StatisticsRecorder::ResetForTesting() { |
403 // Just call the private version that is used also by the destructor. | 397 // Just call the private version that is used also by the destructor. |
404 Reset(); | 398 Reset(); |
405 } | 399 } |
406 | 400 |
407 // static | 401 // static |
408 void StatisticsRecorder::ForgetHistogramForTesting(base::StringPiece name) { | 402 void StatisticsRecorder::ForgetHistogramForTesting(base::StringPiece name) { |
409 if (histograms_) | 403 if (histograms_) |
410 histograms_->erase(HashMetricName(name.as_string())); | 404 histograms_->erase(name); |
411 } | 405 } |
412 | 406 |
413 // This singleton instance should be started during the single threaded portion | 407 // This singleton instance should be started during the single threaded portion |
414 // of main(), and hence it is not thread safe. It initializes globals to | 408 // of main(), and hence it is not thread safe. It initializes globals to |
415 // provide support for all future calls. | 409 // provide support for all future calls. |
416 StatisticsRecorder::StatisticsRecorder() { | 410 StatisticsRecorder::StatisticsRecorder() { |
417 DCHECK(!histograms_); | 411 DCHECK(!histograms_); |
418 if (lock_ == NULL) { | 412 if (lock_ == NULL) { |
419 // This will leak on purpose. It's the only way to make sure we won't race | 413 // This will leak on purpose. It's the only way to make sure we won't race |
420 // against the static uninitialization of the module while one of our | 414 // against the static uninitialization of the module while one of our |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 // static | 461 // static |
468 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; | 462 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
469 // static | 463 // static |
470 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; | 464 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; |
471 // static | 465 // static |
472 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; | 466 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; |
473 // static | 467 // static |
474 base::Lock* StatisticsRecorder::lock_ = NULL; | 468 base::Lock* StatisticsRecorder::lock_ = NULL; |
475 | 469 |
476 } // namespace base | 470 } // namespace base |
OLD | NEW |