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/logging.h" | 10 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "base/values.h" |
14 | 16 |
15 using std::list; | 17 using std::list; |
16 using std::string; | 18 using std::string; |
17 | 19 |
18 namespace { | 20 namespace { |
19 // Initialize histogram statistics gathering system. | 21 // Initialize histogram statistics gathering system. |
20 base::LazyInstance<base::StatisticsRecorder>::Leaky g_statistics_recorder_ = | 22 base::LazyInstance<base::StatisticsRecorder>::Leaky g_statistics_recorder_ = |
21 LAZY_INSTANCE_INITIALIZER; | 23 LAZY_INSTANCE_INITIALIZER; |
22 } // namespace | 24 } // namespace |
23 | 25 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 GetSnapshot(query, &snapshot); | 158 GetSnapshot(query, &snapshot); |
157 for (Histograms::iterator it = snapshot.begin(); | 159 for (Histograms::iterator it = snapshot.begin(); |
158 it != snapshot.end(); | 160 it != snapshot.end(); |
159 ++it) { | 161 ++it) { |
160 (*it)->WriteAscii(output); | 162 (*it)->WriteAscii(output); |
161 output->append("\n"); | 163 output->append("\n"); |
162 } | 164 } |
163 } | 165 } |
164 | 166 |
165 // static | 167 // static |
| 168 std::string StatisticsRecorder::ToJSON(const std::string& query) { |
| 169 if (!IsActive()) |
| 170 return std::string(); |
| 171 |
| 172 std::string output("{"); |
| 173 if (!query.empty()) { |
| 174 output += "\"query\":"; |
| 175 JsonDoubleQuote(query, true, &output); |
| 176 output += ","; |
| 177 } |
| 178 |
| 179 Histograms snapshot; |
| 180 GetSnapshot(query, &snapshot); |
| 181 output += "\"histograms\":["; |
| 182 std::string json; |
| 183 bool first_histogram = true; |
| 184 for (Histograms::const_iterator it = snapshot.begin(); it != snapshot.end(); |
| 185 ++it) { |
| 186 if (first_histogram) |
| 187 first_histogram = false; |
| 188 else |
| 189 output += ","; |
| 190 json.clear(); |
| 191 (*it)->WriteJSON(&json); |
| 192 output += json; |
| 193 } |
| 194 output += "]}"; |
| 195 return output; |
| 196 } |
| 197 |
| 198 // static |
166 void StatisticsRecorder::GetHistograms(Histograms* output) { | 199 void StatisticsRecorder::GetHistograms(Histograms* output) { |
167 if (lock_ == NULL) | 200 if (lock_ == NULL) |
168 return; | 201 return; |
169 base::AutoLock auto_lock(*lock_); | 202 base::AutoLock auto_lock(*lock_); |
170 if (histograms_ == NULL) | 203 if (histograms_ == NULL) |
171 return; | 204 return; |
172 | 205 |
173 for (HistogramMap::iterator it = histograms_->begin(); | 206 for (HistogramMap::iterator it = histograms_->begin(); |
174 histograms_->end() != it; | 207 histograms_->end() != it; |
175 ++it) { | 208 ++it) { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 | 316 |
284 | 317 |
285 // static | 318 // static |
286 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; | 319 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
287 // static | 320 // static |
288 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; | 321 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; |
289 // static | 322 // static |
290 base::Lock* StatisticsRecorder::lock_ = NULL; | 323 base::Lock* StatisticsRecorder::lock_ = NULL; |
291 | 324 |
292 } // namespace base | 325 } // namespace base |
OLD | NEW |