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::MetricsDisplayer*>>::Leaky |
| 34 g_metrics_displayers_ = 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 // The starting location could point to a persistent histogram when such | 44 // The starting location could point to a persistent histogram when such |
41 // is not wanted. If so, skip it. | 45 // is not wanted. If so, skip it. |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 // static | 208 // static |
205 void StatisticsRecorder::WriteHTMLGraph(const std::string& query, | 209 void StatisticsRecorder::WriteHTMLGraph(const std::string& query, |
206 std::string* output) { | 210 std::string* output) { |
207 if (!IsActive()) | 211 if (!IsActive()) |
208 return; | 212 return; |
209 | 213 |
210 Histograms snapshot; | 214 Histograms snapshot; |
211 GetSnapshot(query, &snapshot); | 215 GetSnapshot(query, &snapshot); |
212 std::sort(snapshot.begin(), snapshot.end(), &HistogramNameLesser); | 216 std::sort(snapshot.begin(), snapshot.end(), &HistogramNameLesser); |
213 for (const HistogramBase* histogram : snapshot) { | 217 for (const HistogramBase* histogram : snapshot) { |
214 histogram->WriteHTMLGraph(output); | 218 histogram->WriteHTMLGraph(nullptr, output); |
215 output->append("<br><hr><br>"); | 219 output->append("<br><hr><br>"); |
216 } | 220 } |
| 221 |
| 222 for (auto display : g_metrics_displayers_.Get()) { |
| 223 output->append("<hr size=10 noshade><br><hr><br>\n<p>"); |
| 224 display->WriteTitleString(output); |
| 225 output->append("</p>\n"); |
| 226 display->WriteGraphs(query, MetricsDisplayer::DISPLAY_TEXT_HTML, output); |
| 227 } |
217 } | 228 } |
218 | 229 |
219 // static | 230 // static |
220 void StatisticsRecorder::WriteGraph(const std::string& query, | 231 void StatisticsRecorder::WriteGraph(const std::string& query, |
221 std::string* output) { | 232 std::string* output) { |
222 if (!IsActive()) | 233 if (!IsActive()) |
223 return; | 234 return; |
224 if (query.length()) | 235 if (query.length()) |
225 StringAppendF(output, "Collections of histograms for %s\n", query.c_str()); | 236 StringAppendF(output, "Collections of histograms for %s\n", query.c_str()); |
226 else | 237 else |
227 output->append("Collections of all histograms\n"); | 238 output->append("Collections of all histograms\n"); |
228 | 239 |
229 Histograms snapshot; | 240 Histograms snapshot; |
230 GetSnapshot(query, &snapshot); | 241 GetSnapshot(query, &snapshot); |
231 std::sort(snapshot.begin(), snapshot.end(), &HistogramNameLesser); | 242 std::sort(snapshot.begin(), snapshot.end(), &HistogramNameLesser); |
232 for (const HistogramBase* histogram : snapshot) { | 243 for (const HistogramBase* histogram : snapshot) { |
233 histogram->WriteAscii(output); | 244 histogram->WriteAscii(nullptr, output); |
234 output->append("\n"); | 245 output->append("\n"); |
235 } | 246 } |
| 247 |
| 248 for (auto display : g_metrics_displayers_.Get()) { |
| 249 output->append( |
| 250 "########################################" |
| 251 "########################################\n\n"); |
| 252 display->WriteTitleString(output); |
| 253 output->append("\n\n"); |
| 254 display->WriteGraphs(query, MetricsDisplayer::DISPLAY_TEXT_PLAIN, output); |
| 255 } |
236 } | 256 } |
237 | 257 |
238 // static | 258 // static |
239 std::string StatisticsRecorder::ToJSON(const std::string& query) { | 259 std::string StatisticsRecorder::ToJSON(const std::string& query) { |
240 if (!IsActive()) | 260 if (!IsActive()) |
241 return std::string(); | 261 return std::string(); |
242 | 262 |
243 std::string output("{"); | 263 std::string output("{"); |
244 if (!query.empty()) { | 264 if (!query.empty()) { |
245 output += "\"query\":"; | 265 output += "\"query\":"; |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 if (!lock_) | 428 if (!lock_) |
409 return 0; | 429 return 0; |
410 | 430 |
411 base::AutoLock auto_lock(*lock_); | 431 base::AutoLock auto_lock(*lock_); |
412 if (!histograms_) | 432 if (!histograms_) |
413 return 0; | 433 return 0; |
414 return histograms_->size(); | 434 return histograms_->size(); |
415 } | 435 } |
416 | 436 |
417 // static | 437 // static |
| 438 void StatisticsRecorder::RegisterMetricsDisplayer(MetricsDisplayer* display) { |
| 439 DCHECK(!ContainsKey(g_metrics_displayers_.Get(), display)); |
| 440 g_metrics_displayers_.Get().insert(display); |
| 441 } |
| 442 |
| 443 // static |
| 444 void StatisticsRecorder::DeregisterMetricsDisplayer(MetricsDisplayer* display) { |
| 445 DCHECK(ContainsKey(g_metrics_displayers_.Get(), display)); |
| 446 g_metrics_displayers_.Get().erase(display); |
| 447 } |
| 448 |
| 449 // static |
418 void StatisticsRecorder::ForgetHistogramForTesting(base::StringPiece name) { | 450 void StatisticsRecorder::ForgetHistogramForTesting(base::StringPiece name) { |
419 if (histograms_) | 451 if (histograms_) |
420 histograms_->erase(name); | 452 histograms_->erase(name); |
421 } | 453 } |
422 | 454 |
423 // static | 455 // static |
424 void StatisticsRecorder::UninitializeForTesting() { | 456 void StatisticsRecorder::UninitializeForTesting() { |
425 // Stop now if it's never been initialized. | 457 // Stop now if it's never been initialized. |
426 if (lock_ == NULL || histograms_ == NULL) | 458 if (lock_ == NULL || histograms_ == NULL) |
427 return; | 459 return; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 // static | 545 // static |
514 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; | 546 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
515 // static | 547 // static |
516 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; | 548 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; |
517 // static | 549 // static |
518 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; | 550 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; |
519 // static | 551 // static |
520 base::Lock* StatisticsRecorder::lock_ = NULL; | 552 base::Lock* StatisticsRecorder::lock_ = NULL; |
521 | 553 |
522 } // namespace base | 554 } // namespace base |
OLD | NEW |