Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: base/metrics/statistics_recorder.cc

Issue 1180693002: Update from https://crrev.com/333737 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/metrics/sparse_histogram_unittest.cc ('k') | base/metrics/statistics_recorder_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/synchronization/lock.h" 14 #include "base/synchronization/lock.h"
15 #include "base/values.h" 15 #include "base/values.h"
16 16
17 using std::list;
18 using std::string;
19
20 namespace { 17 namespace {
21 // Initialize histogram statistics gathering system. 18 // Initialize histogram statistics gathering system.
22 base::LazyInstance<base::StatisticsRecorder>::Leaky g_statistics_recorder_ = 19 base::LazyInstance<base::StatisticsRecorder>::Leaky g_statistics_recorder_ =
23 LAZY_INSTANCE_INITIALIZER; 20 LAZY_INSTANCE_INITIALIZER;
24 } // namespace 21 } // namespace
25 22
26 namespace base { 23 namespace base {
27 24
28 // static 25 // static
29 void StatisticsRecorder::Initialize() { 26 void StatisticsRecorder::Initialize() {
(...skipping 22 matching lines...) Expand all
52 return histogram; 49 return histogram;
53 } 50 }
54 51
55 HistogramBase* histogram_to_delete = NULL; 52 HistogramBase* histogram_to_delete = NULL;
56 HistogramBase* histogram_to_return = NULL; 53 HistogramBase* histogram_to_return = NULL;
57 { 54 {
58 base::AutoLock auto_lock(*lock_); 55 base::AutoLock auto_lock(*lock_);
59 if (histograms_ == NULL) { 56 if (histograms_ == NULL) {
60 histogram_to_return = histogram; 57 histogram_to_return = histogram;
61 } else { 58 } else {
62 const string& name = histogram->histogram_name(); 59 const std::string& name = histogram->histogram_name();
63 HistogramMap::iterator it = histograms_->find(name); 60 HistogramMap::iterator it = histograms_->find(name);
64 if (histograms_->end() == it) { 61 if (histograms_->end() == it) {
65 (*histograms_)[name] = histogram; 62 (*histograms_)[name] = histogram;
66 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 63 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322
67 histogram_to_return = histogram; 64 histogram_to_return = histogram;
68 } else if (histogram == it->second) { 65 } else if (histogram == it->second) {
69 // The histogram was registered before. 66 // The histogram was registered before.
70 histogram_to_return = histogram; 67 histogram_to_return = histogram;
71 } else { 68 } else {
72 // We already have one histogram with this name. 69 // We already have one histogram with this name.
(...skipping 16 matching lines...) Expand all
89 ANNOTATE_LEAKING_OBJECT_PTR(ranges); 86 ANNOTATE_LEAKING_OBJECT_PTR(ranges);
90 return ranges; 87 return ranges;
91 } 88 }
92 89
93 base::AutoLock auto_lock(*lock_); 90 base::AutoLock auto_lock(*lock_);
94 if (ranges_ == NULL) { 91 if (ranges_ == NULL) {
95 ANNOTATE_LEAKING_OBJECT_PTR(ranges); 92 ANNOTATE_LEAKING_OBJECT_PTR(ranges);
96 return ranges; 93 return ranges;
97 } 94 }
98 95
99 list<const BucketRanges*>* checksum_matching_list; 96 std::list<const BucketRanges*>* checksum_matching_list;
100 RangesMap::iterator ranges_it = ranges_->find(ranges->checksum()); 97 RangesMap::iterator ranges_it = ranges_->find(ranges->checksum());
101 if (ranges_->end() == ranges_it) { 98 if (ranges_->end() == ranges_it) {
102 // Add a new matching list to map. 99 // Add a new matching list to map.
103 checksum_matching_list = new list<const BucketRanges*>(); 100 checksum_matching_list = new std::list<const BucketRanges*>();
104 ANNOTATE_LEAKING_OBJECT_PTR(checksum_matching_list); 101 ANNOTATE_LEAKING_OBJECT_PTR(checksum_matching_list);
105 (*ranges_)[ranges->checksum()] = checksum_matching_list; 102 (*ranges_)[ranges->checksum()] = checksum_matching_list;
106 } else { 103 } else {
107 checksum_matching_list = ranges_it->second; 104 checksum_matching_list = ranges_it->second;
108 } 105 }
109 106
110 list<const BucketRanges*>::iterator checksum_matching_list_it; 107 for (const BucketRanges* existing_ranges : *checksum_matching_list) {
111 for (checksum_matching_list_it = checksum_matching_list->begin();
112 checksum_matching_list_it != checksum_matching_list->end();
113 ++checksum_matching_list_it) {
114 const BucketRanges* existing_ranges = *checksum_matching_list_it;
115 if (existing_ranges->Equals(ranges)) { 108 if (existing_ranges->Equals(ranges)) {
116 if (existing_ranges == ranges) { 109 if (existing_ranges == ranges) {
117 return ranges; 110 return ranges;
118 } else { 111 } else {
119 ranges_deleter.reset(ranges); 112 ranges_deleter.reset(ranges);
120 return existing_ranges; 113 return existing_ranges;
121 } 114 }
122 } 115 }
123 } 116 }
124 // We haven't found a BucketRanges which has the same ranges. Register the 117 // We haven't found a BucketRanges which has the same ranges. Register the
125 // new BucketRanges. 118 // new BucketRanges.
126 checksum_matching_list->push_front(ranges); 119 checksum_matching_list->push_front(ranges);
127 return ranges; 120 return ranges;
128 } 121 }
129 122
130 // static 123 // static
131 void StatisticsRecorder::WriteHTMLGraph(const std::string& query, 124 void StatisticsRecorder::WriteHTMLGraph(const std::string& query,
132 std::string* output) { 125 std::string* output) {
133 if (!IsActive()) 126 if (!IsActive())
134 return; 127 return;
135 128
136 Histograms snapshot; 129 Histograms snapshot;
137 GetSnapshot(query, &snapshot); 130 GetSnapshot(query, &snapshot);
138 for (Histograms::iterator it = snapshot.begin(); 131 for (const HistogramBase* histogram : snapshot) {
139 it != snapshot.end(); 132 histogram->WriteHTMLGraph(output);
140 ++it) {
141 (*it)->WriteHTMLGraph(output);
142 output->append("<br><hr><br>"); 133 output->append("<br><hr><br>");
143 } 134 }
144 } 135 }
145 136
146 // static 137 // static
147 void StatisticsRecorder::WriteGraph(const std::string& query, 138 void StatisticsRecorder::WriteGraph(const std::string& query,
148 std::string* output) { 139 std::string* output) {
149 if (!IsActive()) 140 if (!IsActive())
150 return; 141 return;
151 if (query.length()) 142 if (query.length())
152 StringAppendF(output, "Collections of histograms for %s\n", query.c_str()); 143 StringAppendF(output, "Collections of histograms for %s\n", query.c_str());
153 else 144 else
154 output->append("Collections of all histograms\n"); 145 output->append("Collections of all histograms\n");
155 146
156 Histograms snapshot; 147 Histograms snapshot;
157 GetSnapshot(query, &snapshot); 148 GetSnapshot(query, &snapshot);
158 for (Histograms::iterator it = snapshot.begin(); 149 for (const HistogramBase* histogram : snapshot) {
159 it != snapshot.end(); 150 histogram->WriteAscii(output);
160 ++it) {
161 (*it)->WriteAscii(output);
162 output->append("\n"); 151 output->append("\n");
163 } 152 }
164 } 153 }
165 154
166 // static 155 // static
167 std::string StatisticsRecorder::ToJSON(const std::string& query) { 156 std::string StatisticsRecorder::ToJSON(const std::string& query) {
168 if (!IsActive()) 157 if (!IsActive())
169 return std::string(); 158 return std::string();
170 159
171 std::string output("{"); 160 std::string output("{");
172 if (!query.empty()) { 161 if (!query.empty()) {
173 output += "\"query\":"; 162 output += "\"query\":";
174 EscapeJSONString(query, true, &output); 163 EscapeJSONString(query, true, &output);
175 output += ","; 164 output += ",";
176 } 165 }
177 166
178 Histograms snapshot; 167 Histograms snapshot;
179 GetSnapshot(query, &snapshot); 168 GetSnapshot(query, &snapshot);
180 output += "\"histograms\":["; 169 output += "\"histograms\":[";
181 bool first_histogram = true; 170 bool first_histogram = true;
182 for (Histograms::const_iterator it = snapshot.begin(); it != snapshot.end(); 171 for (const HistogramBase* histogram : snapshot) {
183 ++it) {
184 if (first_histogram) 172 if (first_histogram)
185 first_histogram = false; 173 first_histogram = false;
186 else 174 else
187 output += ","; 175 output += ",";
188 std::string json; 176 std::string json;
189 (*it)->WriteJSON(&json); 177 histogram->WriteJSON(&json);
190 output += json; 178 output += json;
191 } 179 }
192 output += "]}"; 180 output += "]}";
193 return output; 181 return output;
194 } 182 }
195 183
196 // static 184 // static
197 void StatisticsRecorder::GetHistograms(Histograms* output) { 185 void StatisticsRecorder::GetHistograms(Histograms* output) {
198 if (lock_ == NULL) 186 if (lock_ == NULL)
199 return; 187 return;
200 base::AutoLock auto_lock(*lock_); 188 base::AutoLock auto_lock(*lock_);
201 if (histograms_ == NULL) 189 if (histograms_ == NULL)
202 return; 190 return;
203 191
204 for (HistogramMap::iterator it = histograms_->begin(); 192 for (const auto& entry : *histograms_) {
205 histograms_->end() != it; 193 DCHECK_EQ(entry.first, entry.second->histogram_name());
206 ++it) { 194 output->push_back(entry.second);
207 DCHECK_EQ(it->first, it->second->histogram_name());
208 output->push_back(it->second);
209 } 195 }
210 } 196 }
211 197
212 // static 198 // static
213 void StatisticsRecorder::GetBucketRanges( 199 void StatisticsRecorder::GetBucketRanges(
214 std::vector<const BucketRanges*>* output) { 200 std::vector<const BucketRanges*>* output) {
215 if (lock_ == NULL) 201 if (lock_ == NULL)
216 return; 202 return;
217 base::AutoLock auto_lock(*lock_); 203 base::AutoLock auto_lock(*lock_);
218 if (ranges_ == NULL) 204 if (ranges_ == NULL)
219 return; 205 return;
220 206
221 for (RangesMap::iterator it = ranges_->begin(); 207 for (const auto& entry : *ranges_) {
222 ranges_->end() != it; 208 for (const auto& range_entry : *entry.second) {
223 ++it) { 209 output->push_back(range_entry);
224 list<const BucketRanges*>* ranges_list = it->second;
225 list<const BucketRanges*>::iterator ranges_list_it;
226 for (ranges_list_it = ranges_list->begin();
227 ranges_list_it != ranges_list->end();
228 ++ranges_list_it) {
229 output->push_back(*ranges_list_it);
230 } 210 }
231 } 211 }
232 } 212 }
233 213
234 // static 214 // static
235 HistogramBase* StatisticsRecorder::FindHistogram(const std::string& name) { 215 HistogramBase* StatisticsRecorder::FindHistogram(const std::string& name) {
236 if (lock_ == NULL) 216 if (lock_ == NULL)
237 return NULL; 217 return NULL;
238 base::AutoLock auto_lock(*lock_); 218 base::AutoLock auto_lock(*lock_);
239 if (histograms_ == NULL) 219 if (histograms_ == NULL)
240 return NULL; 220 return NULL;
241 221
242 HistogramMap::iterator it = histograms_->find(name); 222 HistogramMap::iterator it = histograms_->find(name);
243 if (histograms_->end() == it) 223 if (histograms_->end() == it)
244 return NULL; 224 return NULL;
245 return it->second; 225 return it->second;
246 } 226 }
247 227
248 // private static 228 // private static
249 void StatisticsRecorder::GetSnapshot(const std::string& query, 229 void StatisticsRecorder::GetSnapshot(const std::string& query,
250 Histograms* snapshot) { 230 Histograms* snapshot) {
251 if (lock_ == NULL) 231 if (lock_ == NULL)
252 return; 232 return;
253 base::AutoLock auto_lock(*lock_); 233 base::AutoLock auto_lock(*lock_);
254 if (histograms_ == NULL) 234 if (histograms_ == NULL)
255 return; 235 return;
256 236
257 for (HistogramMap::iterator it = histograms_->begin(); 237 for (const auto& entry : *histograms_) {
258 histograms_->end() != it; 238 if (entry.first.find(query) != std::string::npos)
259 ++it) { 239 snapshot->push_back(entry.second);
260 if (it->first.find(query) != std::string::npos)
261 snapshot->push_back(it->second);
262 } 240 }
263 } 241 }
264 242
265 // This singleton instance should be started during the single threaded portion 243 // This singleton instance should be started during the single threaded portion
266 // of main(), and hence it is not thread safe. It initializes globals to 244 // of main(), and hence it is not thread safe. It initializes globals to
267 // provide support for all future calls. 245 // provide support for all future calls.
268 StatisticsRecorder::StatisticsRecorder() { 246 StatisticsRecorder::StatisticsRecorder() {
269 DCHECK(!histograms_); 247 DCHECK(!histograms_);
270 if (lock_ == NULL) { 248 if (lock_ == NULL) {
271 // This will leak on purpose. It's the only way to make sure we won't race 249 // This will leak on purpose. It's the only way to make sure we won't race
272 // against the static uninitialization of the module while one of our 250 // against the static uninitialization of the module while one of our
273 // static methods relying on the lock get called at an inappropriate time 251 // static methods relying on the lock get called at an inappropriate time
274 // during the termination phase. Since it's a static data member, we will 252 // during the termination phase. Since it's a static data member, we will
275 // leak one per process, which would be similar to the instance allocated 253 // leak one per process, which would be similar to the instance allocated
276 // during static initialization and released only on process termination. 254 // during static initialization and released only on process termination.
277 lock_ = new base::Lock; 255 lock_ = new base::Lock;
278 } 256 }
279 base::AutoLock auto_lock(*lock_); 257 base::AutoLock auto_lock(*lock_);
280 histograms_ = new HistogramMap; 258 histograms_ = new HistogramMap;
281 ranges_ = new RangesMap; 259 ranges_ = new RangesMap;
282 260
283 if (VLOG_IS_ON(1)) 261 if (VLOG_IS_ON(1))
284 AtExitManager::RegisterCallback(&DumpHistogramsToVlog, this); 262 AtExitManager::RegisterCallback(&DumpHistogramsToVlog, this);
285 } 263 }
286 264
287 // static 265 // static
288 void StatisticsRecorder::DumpHistogramsToVlog(void* instance) { 266 void StatisticsRecorder::DumpHistogramsToVlog(void* instance) {
289 string output; 267 std::string output;
290 StatisticsRecorder::WriteGraph(std::string(), &output); 268 StatisticsRecorder::WriteGraph(std::string(), &output);
291 VLOG(1) << output; 269 VLOG(1) << output;
292 } 270 }
293 271
294 StatisticsRecorder::~StatisticsRecorder() { 272 StatisticsRecorder::~StatisticsRecorder() {
295 DCHECK(histograms_ && ranges_ && lock_); 273 DCHECK(histograms_ && ranges_ && lock_);
296 274
297 // Clean up. 275 // Clean up.
298 scoped_ptr<HistogramMap> histograms_deleter; 276 scoped_ptr<HistogramMap> histograms_deleter;
299 scoped_ptr<RangesMap> ranges_deleter; 277 scoped_ptr<RangesMap> ranges_deleter;
(...skipping 11 matching lines...) Expand all
311 289
312 290
313 // static 291 // static
314 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; 292 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL;
315 // static 293 // static
316 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; 294 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL;
317 // static 295 // static
318 base::Lock* StatisticsRecorder::lock_ = NULL; 296 base::Lock* StatisticsRecorder::lock_ = NULL;
319 297
320 } // namespace base 298 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/sparse_histogram_unittest.cc ('k') | base/metrics/statistics_recorder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698