Chromium Code Reviews| 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 name_hash = histogram->name_hash(); | 78 const uint64_t name_hash = histogram->name_hash(); |
| 79 DCHECK_NE(0, name_hash); | |
| 63 HistogramMap::iterator it = histograms_->find(name_hash); | 80 HistogramMap::iterator it = histograms_->find(name_hash); |
| 64 if (histograms_->end() == it) { | 81 if (histograms_->end() == it) { |
| 65 (*histograms_)[name_hash] = histogram; | 82 (*histograms_)[name_hash] = histogram; |
| 66 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 | 83 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 |
| 67 // If there are callbacks for this histogram, we set the kCallbackExists | 84 // If there are callbacks for this histogram, we set the kCallbackExists |
| 68 // flag. | 85 // flag. |
| 69 auto callback_iterator = callbacks_->find(name); | 86 auto callback_iterator = callbacks_->find(name); |
| 70 if (callback_iterator != callbacks_->end()) { | 87 if (callback_iterator != callbacks_->end()) { |
| 71 if (!callback_iterator->second.is_null()) | 88 if (!callback_iterator->second.is_null()) |
| 72 histogram->SetFlags(HistogramBase::kCallbackExists); | 89 histogram->SetFlags(HistogramBase::kCallbackExists); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 248 if (lock_ == NULL) | 265 if (lock_ == NULL) |
| 249 return false; | 266 return false; |
| 250 base::AutoLock auto_lock(*lock_); | 267 base::AutoLock auto_lock(*lock_); |
| 251 if (histograms_ == NULL) | 268 if (histograms_ == NULL) |
| 252 return false; | 269 return false; |
| 253 | 270 |
| 254 if (ContainsKey(*callbacks_, name)) | 271 if (ContainsKey(*callbacks_, name)) |
| 255 return false; | 272 return false; |
| 256 callbacks_->insert(std::make_pair(name, cb)); | 273 callbacks_->insert(std::make_pair(name, cb)); |
| 257 | 274 |
| 258 HistogramMap::iterator it = histograms_->find(HashMetricName(name)); | 275 auto it = histograms_->find(HashMetricName(name)); |
| 259 if (it != histograms_->end()) { | 276 if (it != histograms_->end()) { |
| 260 DCHECK_EQ(name, it->second->histogram_name()) << "hash collision"; | 277 DCHECK_EQ(name, it->second->histogram_name()) << "hash collision"; |
| 261 it->second->SetFlags(HistogramBase::kCallbackExists); | 278 it->second->SetFlags(HistogramBase::kCallbackExists); |
| 262 } | 279 } |
| 263 | 280 |
| 264 return true; | 281 return true; |
| 265 } | 282 } |
| 266 | 283 |
| 267 // static | 284 // static |
| 268 void StatisticsRecorder::ClearCallback(const std::string& name) { | 285 void StatisticsRecorder::ClearCallback(const std::string& name) { |
| 269 if (lock_ == NULL) | 286 if (lock_ == NULL) |
| 270 return; | 287 return; |
| 271 base::AutoLock auto_lock(*lock_); | 288 base::AutoLock auto_lock(*lock_); |
| 272 if (histograms_ == NULL) | 289 if (histograms_ == NULL) |
| 273 return; | 290 return; |
| 274 | 291 |
| 275 callbacks_->erase(name); | 292 callbacks_->erase(name); |
| 276 | 293 |
| 277 // We also clear the flag from the histogram (if it exists). | 294 // We also clear the flag from the histogram (if it exists). |
| 278 HistogramMap::iterator it = histograms_->find(HashMetricName(name)); | 295 auto it = histograms_->find(HashMetricName(name)); |
| 279 if (it != histograms_->end()) { | 296 if (it != histograms_->end()) { |
| 280 DCHECK_EQ(name, it->second->histogram_name()) << "hash collision"; | 297 DCHECK_EQ(name, it->second->histogram_name()) << "hash collision"; |
| 281 it->second->ClearFlags(HistogramBase::kCallbackExists); | 298 it->second->ClearFlags(HistogramBase::kCallbackExists); |
| 282 } | 299 } |
| 283 } | 300 } |
| 284 | 301 |
| 285 // static | 302 // static |
| 286 StatisticsRecorder::OnSampleCallback StatisticsRecorder::FindCallback( | 303 StatisticsRecorder::OnSampleCallback StatisticsRecorder::FindCallback( |
| 287 const std::string& name) { | 304 const std::string& name) { |
| 288 if (lock_ == NULL) | 305 if (lock_ == NULL) |
| 289 return OnSampleCallback(); | 306 return OnSampleCallback(); |
| 290 base::AutoLock auto_lock(*lock_); | 307 base::AutoLock auto_lock(*lock_); |
| 291 if (histograms_ == NULL) | 308 if (histograms_ == NULL) |
| 292 return OnSampleCallback(); | 309 return OnSampleCallback(); |
| 293 | 310 |
| 294 auto callback_iterator = callbacks_->find(name); | 311 auto callback_iterator = callbacks_->find(name); |
| 295 return callback_iterator != callbacks_->end() ? callback_iterator->second | 312 return callback_iterator != callbacks_->end() ? callback_iterator->second |
| 296 : OnSampleCallback(); | 313 : OnSampleCallback(); |
| 297 } | 314 } |
| 298 | 315 |
| 299 // private static | 316 // static |
| 317 StatisticsRecorder::HistogramIterator StatisticsRecorder::begin( | |
| 318 bool include_persistent) { | |
|
Alexei Svitkine (slow)
2016/01/14 16:43:13
It would be better to make this an enum so that it
bcwhite
2016/01/14 19:20:25
Sure, though I don't see the use-case of that. Yo
| |
| 319 return HistogramIterator(histograms_->begin(), include_persistent); | |
| 320 } | |
| 321 | |
| 322 // static | |
| 323 StatisticsRecorder::HistogramIterator StatisticsRecorder::end() { | |
| 324 return HistogramIterator(histograms_->end(), true); | |
|
Alexei Svitkine (slow)
2016/01/14 16:43:14
Why is it correct to pass in true here, rather tha
bcwhite
2016/01/14 19:20:26
The second parameter defines what histograms are f
| |
| 325 } | |
| 326 | |
| 327 // static | |
| 300 void StatisticsRecorder::GetSnapshot(const std::string& query, | 328 void StatisticsRecorder::GetSnapshot(const std::string& query, |
| 301 Histograms* snapshot) { | 329 Histograms* snapshot) { |
| 302 if (lock_ == NULL) | 330 if (lock_ == NULL) |
| 303 return; | 331 return; |
| 304 base::AutoLock auto_lock(*lock_); | 332 base::AutoLock auto_lock(*lock_); |
| 305 if (histograms_ == NULL) | 333 if (histograms_ == NULL) |
| 306 return; | 334 return; |
| 307 | 335 |
| 308 for (const auto& entry : *histograms_) { | 336 for (const auto& entry : *histograms_) { |
| 309 if (entry.second->histogram_name().find(query) != std::string::npos) | 337 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 | 394 // static |
| 367 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; | 395 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
| 368 // static | 396 // static |
| 369 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; | 397 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; |
| 370 // static | 398 // static |
| 371 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; | 399 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; |
| 372 // static | 400 // static |
| 373 base::Lock* StatisticsRecorder::lock_ = NULL; | 401 base::Lock* StatisticsRecorder::lock_ = NULL; |
| 374 | 402 |
| 375 } // namespace base | 403 } // namespace base |
| OLD | NEW |