| 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 "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 8 #include "base/debug/leak_annotations.h" | 8 #include "base/debug/leak_annotations.h" |
| 9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/metrics/metrics_hashes.h" | 13 #include "base/metrics/metrics_hashes.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 // Initialize histogram statistics gathering system. | 20 // Initialize histogram statistics gathering system. |
| 21 base::LazyInstance<base::StatisticsRecorder>::Leaky g_statistics_recorder_ = | 21 base::LazyInstance<base::StatisticsRecorder>::Leaky g_statistics_recorder_ = |
| 22 LAZY_INSTANCE_INITIALIZER; | 22 LAZY_INSTANCE_INITIALIZER; |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 namespace base { | 25 namespace base { |
| 26 | 26 |
| 27 StatisticsRecorder::HistogramIterator& |
| 28 StatisticsRecorder::HistogramIterator::operator++() { |
| 29 const HistogramMap::iterator histograms_end = histograms_->end(); |
| 30 while (iter_ != histograms_end) { |
| 31 ++iter_; |
| 32 if (iter_ == histograms_end) |
| 33 break; |
| 34 if (!include_persistent_ && (iter_->second->flags() & |
| 35 HistogramBase::kIsPersistent)) { |
| 36 continue; |
| 37 } |
| 38 break; |
| 39 } |
| 40 return *this; |
| 41 } |
| 42 |
| 27 // static | 43 // static |
| 28 void StatisticsRecorder::Initialize() { | 44 void StatisticsRecorder::Initialize() { |
| 29 // Ensure that an instance of the StatisticsRecorder object is created. | 45 // Ensure that an instance of the StatisticsRecorder object is created. |
| 30 g_statistics_recorder_.Get(); | 46 g_statistics_recorder_.Get(); |
| 31 } | 47 } |
| 32 | 48 |
| 33 // static | 49 // static |
| 34 bool StatisticsRecorder::IsActive() { | 50 bool StatisticsRecorder::IsActive() { |
| 35 if (lock_ == NULL) | 51 if (lock_ == NULL) |
| 36 return false; | 52 return false; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 52 } | 68 } |
| 53 | 69 |
| 54 HistogramBase* histogram_to_delete = NULL; | 70 HistogramBase* histogram_to_delete = NULL; |
| 55 HistogramBase* histogram_to_return = NULL; | 71 HistogramBase* histogram_to_return = NULL; |
| 56 { | 72 { |
| 57 base::AutoLock auto_lock(*lock_); | 73 base::AutoLock auto_lock(*lock_); |
| 58 if (histograms_ == NULL) { | 74 if (histograms_ == NULL) { |
| 59 histogram_to_return = histogram; | 75 histogram_to_return = histogram; |
| 60 } else { | 76 } else { |
| 61 const std::string& name = histogram->histogram_name(); | 77 const std::string& name = histogram->histogram_name(); |
| 62 uint64_t id = histogram->name_hash(); | 78 uint64_t hash = histogram->name_hash(); |
| 63 HistogramMap::iterator it = histograms_->find(id); | 79 HistogramMap::iterator it = histograms_->find(hash); |
| 64 if (histograms_->end() == it) { | 80 if (histograms_->end() == it) { |
| 65 (*histograms_)[id] = histogram; | 81 (*histograms_)[hash] = histogram; |
| 66 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 | 82 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 |
| 67 // If there are callbacks for this histogram, we set the kCallbackExists | 83 // If there are callbacks for this histogram, we set the kCallbackExists |
| 68 // flag. | 84 // flag. |
| 69 auto callback_iterator = callbacks_->find(name); | 85 auto callback_iterator = callbacks_->find(name); |
| 70 if (callback_iterator != callbacks_->end()) { | 86 if (callback_iterator != callbacks_->end()) { |
| 71 if (!callback_iterator->second.is_null()) | 87 if (!callback_iterator->second.is_null()) |
| 72 histogram->SetFlags(HistogramBase::kCallbackExists); | 88 histogram->SetFlags(HistogramBase::kCallbackExists); |
| 73 else | 89 else |
| 74 histogram->ClearFlags(HistogramBase::kCallbackExists); | 90 histogram->ClearFlags(HistogramBase::kCallbackExists); |
| 75 } | 91 } |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 return OnSampleCallback(); | 305 return OnSampleCallback(); |
| 290 base::AutoLock auto_lock(*lock_); | 306 base::AutoLock auto_lock(*lock_); |
| 291 if (histograms_ == NULL) | 307 if (histograms_ == NULL) |
| 292 return OnSampleCallback(); | 308 return OnSampleCallback(); |
| 293 | 309 |
| 294 auto callback_iterator = callbacks_->find(name); | 310 auto callback_iterator = callbacks_->find(name); |
| 295 return callback_iterator != callbacks_->end() ? callback_iterator->second | 311 return callback_iterator != callbacks_->end() ? callback_iterator->second |
| 296 : OnSampleCallback(); | 312 : OnSampleCallback(); |
| 297 } | 313 } |
| 298 | 314 |
| 299 // private static | 315 // static |
| 316 StatisticsRecorder::HistogramIterator StatisticsRecorder::begin( |
| 317 bool include_persistent) { |
| 318 return HistogramIterator(histograms_->begin(), include_persistent); |
| 319 } |
| 320 |
| 321 // static |
| 322 StatisticsRecorder::HistogramIterator StatisticsRecorder::end() { |
| 323 return HistogramIterator(histograms_->end(), true); |
| 324 } |
| 325 |
| 326 // static |
| 300 void StatisticsRecorder::GetSnapshot(const std::string& query, | 327 void StatisticsRecorder::GetSnapshot(const std::string& query, |
| 301 Histograms* snapshot) { | 328 Histograms* snapshot) { |
| 302 if (lock_ == NULL) | 329 if (lock_ == NULL) |
| 303 return; | 330 return; |
| 304 base::AutoLock auto_lock(*lock_); | 331 base::AutoLock auto_lock(*lock_); |
| 305 if (histograms_ == NULL) | 332 if (histograms_ == NULL) |
| 306 return; | 333 return; |
| 307 | 334 |
| 308 for (const auto& entry : *histograms_) { | 335 for (const auto& entry : *histograms_) { |
| 309 if (entry.second->histogram_name().find(query) != std::string::npos) | 336 if (entry.second->histogram_name().find(query) != std::string::npos) |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 // static | 393 // static |
| 367 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; | 394 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
| 368 // static | 395 // static |
| 369 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; | 396 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; |
| 370 // static | 397 // static |
| 371 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; | 398 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; |
| 372 // static | 399 // static |
| 373 base::Lock* StatisticsRecorder::lock_ = NULL; | 400 base::Lock* StatisticsRecorder::lock_ = NULL; |
| 374 | 401 |
| 375 } // namespace base | 402 } // namespace base |
| OLD | NEW |