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::HistogramIterator( |
| 28 const HistogramMap::iterator& iter, bool include_persistent) |
| 29 : iter_(iter), |
| 30 include_persistent_(include_persistent) { |
| 31 } |
| 32 |
| 33 StatisticsRecorder::HistogramIterator::HistogramIterator( |
| 34 const HistogramIterator& rhs) |
| 35 : iter_(rhs.iter_), |
| 36 include_persistent_(rhs.include_persistent_) { |
| 37 } |
| 38 |
| 39 StatisticsRecorder::HistogramIterator& |
| 40 StatisticsRecorder::HistogramIterator::operator++() { |
| 41 const HistogramMap::iterator histograms_end = histograms_->end(); |
| 42 while (iter_ != histograms_end) { |
| 43 ++iter_; |
| 44 if (iter_ == histograms_end) |
| 45 break; |
| 46 if (!include_persistent_ && (iter_->second->flags() & |
| 47 HistogramBase::kIsPersistent)) { |
| 48 continue; |
| 49 } |
| 50 break; |
| 51 } |
| 52 return *this; |
| 53 } |
| 54 |
27 // static | 55 // static |
28 void StatisticsRecorder::Initialize() { | 56 void StatisticsRecorder::Initialize() { |
29 // Ensure that an instance of the StatisticsRecorder object is created. | 57 // Ensure that an instance of the StatisticsRecorder object is created. |
30 g_statistics_recorder_.Get(); | 58 g_statistics_recorder_.Get(); |
31 } | 59 } |
32 | 60 |
33 // static | 61 // static |
34 bool StatisticsRecorder::IsActive() { | 62 bool StatisticsRecorder::IsActive() { |
35 if (lock_ == NULL) | 63 if (lock_ == NULL) |
36 return false; | 64 return false; |
(...skipping 15 matching lines...) Expand all Loading... |
52 } | 80 } |
53 | 81 |
54 HistogramBase* histogram_to_delete = NULL; | 82 HistogramBase* histogram_to_delete = NULL; |
55 HistogramBase* histogram_to_return = NULL; | 83 HistogramBase* histogram_to_return = NULL; |
56 { | 84 { |
57 base::AutoLock auto_lock(*lock_); | 85 base::AutoLock auto_lock(*lock_); |
58 if (histograms_ == NULL) { | 86 if (histograms_ == NULL) { |
59 histogram_to_return = histogram; | 87 histogram_to_return = histogram; |
60 } else { | 88 } else { |
61 const std::string& name = histogram->histogram_name(); | 89 const std::string& name = histogram->histogram_name(); |
62 uint64_t name_hash = histogram->name_hash(); | 90 const uint64_t name_hash = histogram->name_hash(); |
| 91 DCHECK_NE(0U, name_hash); |
63 HistogramMap::iterator it = histograms_->find(name_hash); | 92 HistogramMap::iterator it = histograms_->find(name_hash); |
64 if (histograms_->end() == it) { | 93 if (histograms_->end() == it) { |
65 (*histograms_)[name_hash] = histogram; | 94 (*histograms_)[name_hash] = histogram; |
66 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 | 95 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 |
67 // If there are callbacks for this histogram, we set the kCallbackExists | 96 // If there are callbacks for this histogram, we set the kCallbackExists |
68 // flag. | 97 // flag. |
69 auto callback_iterator = callbacks_->find(name); | 98 auto callback_iterator = callbacks_->find(name); |
70 if (callback_iterator != callbacks_->end()) { | 99 if (callback_iterator != callbacks_->end()) { |
71 if (!callback_iterator->second.is_null()) | 100 if (!callback_iterator->second.is_null()) |
72 histogram->SetFlags(HistogramBase::kCallbackExists); | 101 histogram->SetFlags(HistogramBase::kCallbackExists); |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 if (lock_ == NULL) | 277 if (lock_ == NULL) |
249 return false; | 278 return false; |
250 base::AutoLock auto_lock(*lock_); | 279 base::AutoLock auto_lock(*lock_); |
251 if (histograms_ == NULL) | 280 if (histograms_ == NULL) |
252 return false; | 281 return false; |
253 | 282 |
254 if (ContainsKey(*callbacks_, name)) | 283 if (ContainsKey(*callbacks_, name)) |
255 return false; | 284 return false; |
256 callbacks_->insert(std::make_pair(name, cb)); | 285 callbacks_->insert(std::make_pair(name, cb)); |
257 | 286 |
258 HistogramMap::iterator it = histograms_->find(HashMetricName(name)); | 287 auto it = histograms_->find(HashMetricName(name)); |
259 if (it != histograms_->end()) { | 288 if (it != histograms_->end()) { |
260 DCHECK_EQ(name, it->second->histogram_name()) << "hash collision"; | 289 DCHECK_EQ(name, it->second->histogram_name()) << "hash collision"; |
261 it->second->SetFlags(HistogramBase::kCallbackExists); | 290 it->second->SetFlags(HistogramBase::kCallbackExists); |
262 } | 291 } |
263 | 292 |
264 return true; | 293 return true; |
265 } | 294 } |
266 | 295 |
267 // static | 296 // static |
268 void StatisticsRecorder::ClearCallback(const std::string& name) { | 297 void StatisticsRecorder::ClearCallback(const std::string& name) { |
269 if (lock_ == NULL) | 298 if (lock_ == NULL) |
270 return; | 299 return; |
271 base::AutoLock auto_lock(*lock_); | 300 base::AutoLock auto_lock(*lock_); |
272 if (histograms_ == NULL) | 301 if (histograms_ == NULL) |
273 return; | 302 return; |
274 | 303 |
275 callbacks_->erase(name); | 304 callbacks_->erase(name); |
276 | 305 |
277 // We also clear the flag from the histogram (if it exists). | 306 // We also clear the flag from the histogram (if it exists). |
278 HistogramMap::iterator it = histograms_->find(HashMetricName(name)); | 307 auto it = histograms_->find(HashMetricName(name)); |
279 if (it != histograms_->end()) { | 308 if (it != histograms_->end()) { |
280 DCHECK_EQ(name, it->second->histogram_name()) << "hash collision"; | 309 DCHECK_EQ(name, it->second->histogram_name()) << "hash collision"; |
281 it->second->ClearFlags(HistogramBase::kCallbackExists); | 310 it->second->ClearFlags(HistogramBase::kCallbackExists); |
282 } | 311 } |
283 } | 312 } |
284 | 313 |
285 // static | 314 // static |
286 StatisticsRecorder::OnSampleCallback StatisticsRecorder::FindCallback( | 315 StatisticsRecorder::OnSampleCallback StatisticsRecorder::FindCallback( |
287 const std::string& name) { | 316 const std::string& name) { |
288 if (lock_ == NULL) | 317 if (lock_ == NULL) |
289 return OnSampleCallback(); | 318 return OnSampleCallback(); |
290 base::AutoLock auto_lock(*lock_); | 319 base::AutoLock auto_lock(*lock_); |
291 if (histograms_ == NULL) | 320 if (histograms_ == NULL) |
292 return OnSampleCallback(); | 321 return OnSampleCallback(); |
293 | 322 |
294 auto callback_iterator = callbacks_->find(name); | 323 auto callback_iterator = callbacks_->find(name); |
295 return callback_iterator != callbacks_->end() ? callback_iterator->second | 324 return callback_iterator != callbacks_->end() ? callback_iterator->second |
296 : OnSampleCallback(); | 325 : OnSampleCallback(); |
297 } | 326 } |
298 | 327 |
299 // private static | 328 // static |
| 329 StatisticsRecorder::HistogramIterator StatisticsRecorder::begin( |
| 330 bool include_persistent) { |
| 331 return HistogramIterator(histograms_->begin(), include_persistent); |
| 332 } |
| 333 |
| 334 // static |
| 335 StatisticsRecorder::HistogramIterator StatisticsRecorder::end() { |
| 336 return HistogramIterator(histograms_->end(), true); |
| 337 } |
| 338 |
| 339 // static |
300 void StatisticsRecorder::GetSnapshot(const std::string& query, | 340 void StatisticsRecorder::GetSnapshot(const std::string& query, |
301 Histograms* snapshot) { | 341 Histograms* snapshot) { |
302 if (lock_ == NULL) | 342 if (lock_ == NULL) |
303 return; | 343 return; |
304 base::AutoLock auto_lock(*lock_); | 344 base::AutoLock auto_lock(*lock_); |
305 if (histograms_ == NULL) | 345 if (histograms_ == NULL) |
306 return; | 346 return; |
307 | 347 |
308 for (const auto& entry : *histograms_) { | 348 for (const auto& entry : *histograms_) { |
309 if (entry.second->histogram_name().find(query) != std::string::npos) | 349 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 | 406 // static |
367 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; | 407 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
368 // static | 408 // static |
369 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; | 409 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; |
370 // static | 410 // static |
371 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; | 411 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; |
372 // static | 412 // static |
373 base::Lock* StatisticsRecorder::lock_ = NULL; | 413 base::Lock* StatisticsRecorder::lock_ = NULL; |
374 | 414 |
375 } // namespace base | 415 } // namespace base |
OLD | NEW |