| 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 |
| 20 // Initialize histogram statistics gathering system. | 21 // Initialize histogram statistics gathering system. |
| 21 base::LazyInstance<base::StatisticsRecorder>::Leaky g_statistics_recorder_ = | 22 base::LazyInstance<base::StatisticsRecorder>::Leaky g_statistics_recorder_ = |
| 22 LAZY_INSTANCE_INITIALIZER; | 23 LAZY_INSTANCE_INITIALIZER; |
| 24 |
| 25 bool HistogramNameLesser(const base::HistogramBase* a, |
| 26 const base::HistogramBase* b) { |
| 27 return a->histogram_name() < b->histogram_name(); |
| 28 } |
| 29 |
| 23 } // namespace | 30 } // namespace |
| 24 | 31 |
| 25 namespace base { | 32 namespace base { |
| 26 | 33 |
| 27 // static | 34 // static |
| 28 void StatisticsRecorder::Initialize() { | 35 void StatisticsRecorder::Initialize() { |
| 29 // Ensure that an instance of the StatisticsRecorder object is created. | 36 // Ensure that an instance of the StatisticsRecorder object is created. |
| 30 g_statistics_recorder_.Get(); | 37 g_statistics_recorder_.Get(); |
| 31 } | 38 } |
| 32 | 39 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 } | 142 } |
| 136 | 143 |
| 137 // static | 144 // static |
| 138 void StatisticsRecorder::WriteHTMLGraph(const std::string& query, | 145 void StatisticsRecorder::WriteHTMLGraph(const std::string& query, |
| 139 std::string* output) { | 146 std::string* output) { |
| 140 if (!IsActive()) | 147 if (!IsActive()) |
| 141 return; | 148 return; |
| 142 | 149 |
| 143 Histograms snapshot; | 150 Histograms snapshot; |
| 144 GetSnapshot(query, &snapshot); | 151 GetSnapshot(query, &snapshot); |
| 152 std::sort(snapshot.begin(), snapshot.end(), &HistogramNameLesser); |
| 145 for (const HistogramBase* histogram : snapshot) { | 153 for (const HistogramBase* histogram : snapshot) { |
| 146 histogram->WriteHTMLGraph(output); | 154 histogram->WriteHTMLGraph(output); |
| 147 output->append("<br><hr><br>"); | 155 output->append("<br><hr><br>"); |
| 148 } | 156 } |
| 149 } | 157 } |
| 150 | 158 |
| 151 // static | 159 // static |
| 152 void StatisticsRecorder::WriteGraph(const std::string& query, | 160 void StatisticsRecorder::WriteGraph(const std::string& query, |
| 153 std::string* output) { | 161 std::string* output) { |
| 154 if (!IsActive()) | 162 if (!IsActive()) |
| 155 return; | 163 return; |
| 156 if (query.length()) | 164 if (query.length()) |
| 157 StringAppendF(output, "Collections of histograms for %s\n", query.c_str()); | 165 StringAppendF(output, "Collections of histograms for %s\n", query.c_str()); |
| 158 else | 166 else |
| 159 output->append("Collections of all histograms\n"); | 167 output->append("Collections of all histograms\n"); |
| 160 | 168 |
| 161 Histograms snapshot; | 169 Histograms snapshot; |
| 162 GetSnapshot(query, &snapshot); | 170 GetSnapshot(query, &snapshot); |
| 171 std::sort(snapshot.begin(), snapshot.end(), &HistogramNameLesser); |
| 163 for (const HistogramBase* histogram : snapshot) { | 172 for (const HistogramBase* histogram : snapshot) { |
| 164 histogram->WriteAscii(output); | 173 histogram->WriteAscii(output); |
| 165 output->append("\n"); | 174 output->append("\n"); |
| 166 } | 175 } |
| 167 } | 176 } |
| 168 | 177 |
| 169 // static | 178 // static |
| 170 std::string StatisticsRecorder::ToJSON(const std::string& query) { | 179 std::string StatisticsRecorder::ToJSON(const std::string& query) { |
| 171 if (!IsActive()) | 180 if (!IsActive()) |
| 172 return std::string(); | 181 return std::string(); |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 // static | 375 // static |
| 367 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; | 376 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
| 368 // static | 377 // static |
| 369 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; | 378 StatisticsRecorder::CallbackMap* StatisticsRecorder::callbacks_ = NULL; |
| 370 // static | 379 // static |
| 371 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; | 380 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; |
| 372 // static | 381 // static |
| 373 base::Lock* StatisticsRecorder::lock_ = NULL; | 382 base::Lock* StatisticsRecorder::lock_ = NULL; |
| 374 | 383 |
| 375 } // namespace base | 384 } // namespace base |
| OLD | NEW |