| 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 #include <set> |
| 8 | 9 |
| 9 #include "base/at_exit.h" | 10 #include "base/at_exit.h" |
| 10 #include "base/debug/leak_annotations.h" | 11 #include "base/debug/leak_annotations.h" |
| 11 #include "base/json/string_escape.h" | 12 #include "base/json/string_escape.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 14 #include "base/metrics/metrics_hashes.h" | 15 #include "base/metrics/metrics_hashes.h" |
| 15 #include "base/metrics/persistent_histogram_allocator.h" | 16 #include "base/metrics/persistent_histogram_allocator.h" |
| 16 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 17 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 18 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 19 #include "base/values.h" | 20 #include "base/values.h" |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // Initialize histogram statistics gathering system. | 24 // Initialize histogram statistics gathering system. |
| 24 base::LazyInstance<base::StatisticsRecorder>::Leaky g_statistics_recorder_ = | 25 base::LazyInstance<base::StatisticsRecorder>::Leaky g_statistics_recorder_ = |
| 25 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
| 26 | 27 |
| 27 bool HistogramNameLesser(const base::HistogramBase* a, | 28 bool HistogramNameLesser(const base::HistogramBase* a, |
| 28 const base::HistogramBase* b) { | 29 const base::HistogramBase* b) { |
| 29 return a->histogram_name() < b->histogram_name(); | 30 return a->histogram_name() < b->histogram_name(); |
| 30 } | 31 } |
| 31 | 32 |
| 33 base::LazyInstance<std::set<base::StatisticsRecorder::MetricsDisplay*>>::Leaky |
| 34 g_metrics_displays_ = LAZY_INSTANCE_INITIALIZER; |
| 35 |
| 32 } // namespace | 36 } // namespace |
| 33 | 37 |
| 34 namespace base { | 38 namespace base { |
| 35 | 39 |
| 36 StatisticsRecorder::HistogramIterator::HistogramIterator( | 40 StatisticsRecorder::HistogramIterator::HistogramIterator( |
| 37 const HistogramMap::iterator& iter, bool include_persistent) | 41 const HistogramMap::iterator& iter, bool include_persistent) |
| 38 : iter_(iter), | 42 : iter_(iter), |
| 39 include_persistent_(include_persistent) { | 43 include_persistent_(include_persistent) { |
| 40 } | 44 } |
| 41 | 45 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 // static | 200 // static |
| 197 void StatisticsRecorder::WriteHTMLGraph(const std::string& query, | 201 void StatisticsRecorder::WriteHTMLGraph(const std::string& query, |
| 198 std::string* output) { | 202 std::string* output) { |
| 199 if (!IsActive()) | 203 if (!IsActive()) |
| 200 return; | 204 return; |
| 201 | 205 |
| 202 Histograms snapshot; | 206 Histograms snapshot; |
| 203 GetSnapshot(query, &snapshot); | 207 GetSnapshot(query, &snapshot); |
| 204 std::sort(snapshot.begin(), snapshot.end(), &HistogramNameLesser); | 208 std::sort(snapshot.begin(), snapshot.end(), &HistogramNameLesser); |
| 205 for (const HistogramBase* histogram : snapshot) { | 209 for (const HistogramBase* histogram : snapshot) { |
| 206 histogram->WriteHTMLGraph(output); | 210 histogram->WriteHTMLGraph(nullptr, output); |
| 207 output->append("<br><hr><br>"); | 211 output->append("<br><hr><br>"); |
| 208 } | 212 } |
| 213 |
| 214 for (auto display : g_metrics_displays_.Get()) { |
| 215 output->append("<hr size=10 noshade><br><hr><br>\n<p>"); |
| 216 display->WriteTitleString(output); |
| 217 output->append("</p>\n"); |
| 218 display->WriteGraphs(query, true, output); |
| 219 } |
| 209 } | 220 } |
| 210 | 221 |
| 211 // static | 222 // static |
| 212 void StatisticsRecorder::WriteGraph(const std::string& query, | 223 void StatisticsRecorder::WriteGraph(const std::string& query, |
| 213 std::string* output) { | 224 std::string* output) { |
| 214 if (!IsActive()) | 225 if (!IsActive()) |
| 215 return; | 226 return; |
| 216 if (query.length()) | 227 if (query.length()) |
| 217 StringAppendF(output, "Collections of histograms for %s\n", query.c_str()); | 228 StringAppendF(output, "Collections of histograms for %s\n", query.c_str()); |
| 218 else | 229 else |
| 219 output->append("Collections of all histograms\n"); | 230 output->append("Collections of all histograms\n"); |
| 220 | 231 |
| 221 Histograms snapshot; | 232 Histograms snapshot; |
| 222 GetSnapshot(query, &snapshot); | 233 GetSnapshot(query, &snapshot); |
| 223 std::sort(snapshot.begin(), snapshot.end(), &HistogramNameLesser); | 234 std::sort(snapshot.begin(), snapshot.end(), &HistogramNameLesser); |
| 224 for (const HistogramBase* histogram : snapshot) { | 235 for (const HistogramBase* histogram : snapshot) { |
| 225 histogram->WriteAscii(output); | 236 histogram->WriteAscii(nullptr, output); |
| 226 output->append("\n"); | 237 output->append("\n"); |
| 227 } | 238 } |
| 239 |
| 240 for (auto display : g_metrics_displays_.Get()) { |
| 241 output->append( |
| 242 "########################################" |
| 243 "########################################\n\n"); |
| 244 display->WriteTitleString(output); |
| 245 output->append("\n\n"); |
| 246 display->WriteGraphs(query, false, output); |
| 247 } |
| 228 } | 248 } |
| 229 | 249 |
| 230 // static | 250 // static |
| 231 std::string StatisticsRecorder::ToJSON(const std::string& query) { | 251 std::string StatisticsRecorder::ToJSON(const std::string& query) { |
| 232 if (!IsActive()) | 252 if (!IsActive()) |
| 233 return std::string(); | 253 return std::string(); |
| 234 | 254 |
| 235 std::string output("{"); | 255 std::string output("{"); |
| 236 if (!query.empty()) { | 256 if (!query.empty()) { |
| 237 output += "\"query\":"; | 257 output += "\"query\":"; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 if (!lock_) | 414 if (!lock_) |
| 395 return 0; | 415 return 0; |
| 396 | 416 |
| 397 base::AutoLock auto_lock(*lock_); | 417 base::AutoLock auto_lock(*lock_); |
| 398 if (!histograms_) | 418 if (!histograms_) |
| 399 return 0; | 419 return 0; |
| 400 return histograms_->size(); | 420 return histograms_->size(); |
| 401 } | 421 } |
| 402 | 422 |
| 403 // static | 423 // static |
| 424 void StatisticsRecorder::RegisterMetricsDisplay(MetricsDisplay* display) { |
| 425 DCHECK(!ContainsKey(g_metrics_displays_.Get(), display)); |
| 426 g_metrics_displays_.Get().insert(display); |
| 427 } |
| 428 |
| 429 // static |
| 430 void StatisticsRecorder::DeregisterMetricsDisplay(MetricsDisplay* display) { |
| 431 DCHECK(ContainsKey(g_metrics_displays_.Get(), display)); |
| 432 g_metrics_displays_.Get().erase(display); |
| 433 } |
| 434 |
| 435 // static |
| 404 void StatisticsRecorder::ForgetHistogramForTesting(base::StringPiece name) { | 436 void StatisticsRecorder::ForgetHistogramForTesting(base::StringPiece name) { |
| 405 if (histograms_) | 437 if (histograms_) |
| 406 histograms_->erase(name); | 438 histograms_->erase(name); |
| 407 } | 439 } |
| 408 | 440 |
| 409 // static | 441 // static |
| 410 void StatisticsRecorder::UninitializeForTesting() { | 442 void StatisticsRecorder::UninitializeForTesting() { |
| 411 // Stop now if it's never been initialized. | 443 // Stop now if it's never been initialized. |
| 412 if (lock_ == NULL || histograms_ == NULL) | 444 if (lock_ == NULL || histograms_ == NULL) |
| 413 return; | 445 return; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 // static | 517 // static |
| 486 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; | 518 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
| 487 // static | 519 // static |
| 488 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; | 520 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; |
| 489 // static | 521 // static |
| 490 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; | 522 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; |
| 491 // static | 523 // static |
| 492 base::Lock* StatisticsRecorder::lock_ = NULL; | 524 base::Lock* StatisticsRecorder::lock_ = NULL; |
| 493 | 525 |
| 494 } // namespace base | 526 } // namespace base |
| OLD | NEW |