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/debug/leak_annotations.h" | 7 #include "base/debug/leak_annotations.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
10 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
11 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
12 | 13 |
| 14 using std::list; |
| 15 using std::string; |
| 16 |
13 namespace { | 17 namespace { |
14 // Initialize histogram statistics gathering system. | 18 // Initialize histogram statistics gathering system. |
15 base::LazyInstance<base::StatisticsRecorder>::Leaky | 19 base::LazyInstance<base::StatisticsRecorder>::Leaky g_statistics_recorder_ = |
16 g_statistics_recorder_ = LAZY_INSTANCE_INITIALIZER; | 20 LAZY_INSTANCE_INITIALIZER; |
17 } // namespace | 21 } // namespace |
18 | 22 |
19 namespace base { | 23 namespace base { |
20 | 24 |
21 // Collect the number of histograms created. | 25 // Collect the number of histograms created. |
22 static uint32 number_of_histograms_ = 0; | 26 static uint32 number_of_histograms_ = 0; |
23 // Collect the number of vectors saved because of caching ranges. | 27 // Collect the number of vectors saved because of caching ranges. |
24 static uint32 number_of_vectors_saved_ = 0; | 28 static uint32 number_of_vectors_saved_ = 0; |
25 // Collect the number of ranges_ elements saved because of caching ranges. | 29 // Collect the number of ranges_ elements saved because of caching ranges. |
26 static size_t saved_ranges_size_ = 0; | 30 static size_t saved_ranges_size_ = 0; |
27 | 31 |
28 // This singleton instance should be started during the single threaded portion | |
29 // of main(), and hence it is not thread safe. It initializes globals to | |
30 // provide support for all future calls. | |
31 StatisticsRecorder::StatisticsRecorder() { | |
32 DCHECK(!histograms_); | |
33 if (lock_ == NULL) { | |
34 // This will leak on purpose. It's the only way to make sure we won't race | |
35 // against the static uninitialization of the module while one of our | |
36 // static methods relying on the lock get called at an inappropriate time | |
37 // during the termination phase. Since it's a static data member, we will | |
38 // leak one per process, which would be similar to the instance allocated | |
39 // during static initialization and released only on process termination. | |
40 lock_ = new base::Lock; | |
41 } | |
42 base::AutoLock auto_lock(*lock_); | |
43 histograms_ = new HistogramMap; | |
44 ranges_ = new RangesMap; | |
45 } | |
46 | |
47 StatisticsRecorder::~StatisticsRecorder() { | |
48 DCHECK(histograms_ && lock_); | |
49 | |
50 if (dump_on_exit_) { | |
51 std::string output; | |
52 WriteGraph("", &output); | |
53 DLOG(INFO) << output; | |
54 } | |
55 // Clean up. | |
56 HistogramMap* histograms = NULL; | |
57 { | |
58 base::AutoLock auto_lock(*lock_); | |
59 histograms = histograms_; | |
60 histograms_ = NULL; | |
61 } | |
62 RangesMap* ranges = NULL; | |
63 { | |
64 base::AutoLock auto_lock(*lock_); | |
65 ranges = ranges_; | |
66 ranges_ = NULL; | |
67 } | |
68 // We are going to leak the histograms and the ranges. | |
69 delete histograms; | |
70 delete ranges; | |
71 // We don't delete lock_ on purpose to avoid having to properly protect | |
72 // against it going away after we checked for NULL in the static methods. | |
73 } | |
74 | |
75 // static | 32 // static |
76 void StatisticsRecorder::Initialize() { | 33 void StatisticsRecorder::Initialize() { |
77 // Ensure that an instance of the StatisticsRecorder object is created. | 34 // Ensure that an instance of the StatisticsRecorder object is created. |
78 g_statistics_recorder_.Get(); | 35 g_statistics_recorder_.Get(); |
79 } | 36 } |
80 | 37 |
81 | 38 |
82 // static | 39 // static |
83 bool StatisticsRecorder::IsActive() { | 40 bool StatisticsRecorder::IsActive() { |
84 if (lock_ == NULL) | 41 if (lock_ == NULL) |
85 return false; | 42 return false; |
86 base::AutoLock auto_lock(*lock_); | 43 base::AutoLock auto_lock(*lock_); |
87 return NULL != histograms_; | 44 return NULL != histograms_; |
88 } | 45 } |
89 | 46 |
| 47 // static |
90 Histogram* StatisticsRecorder::RegisterOrDeleteDuplicate(Histogram* histogram) { | 48 Histogram* StatisticsRecorder::RegisterOrDeleteDuplicate(Histogram* histogram) { |
91 // As per crbug.com/79322 the histograms are intentionally leaked, so we need | 49 // As per crbug.com/79322 the histograms are intentionally leaked, so we need |
92 // to annotate them. Because ANNOTATE_LEAKING_OBJECT_PTR may be used only once | 50 // to annotate them. Because ANNOTATE_LEAKING_OBJECT_PTR may be used only once |
93 // for an object, the duplicates should not be annotated. | 51 // for an object, the duplicates should not be annotated. |
94 // Callers are responsible for not calling RegisterOrDeleteDuplicate(ptr) | 52 // Callers are responsible for not calling RegisterOrDeleteDuplicate(ptr) |
95 // twice if (lock_ == NULL) || (!histograms_). | 53 // twice if (lock_ == NULL) || (!histograms_). |
96 DCHECK(histogram->HasValidRangeChecksum()); | |
97 if (lock_ == NULL) { | 54 if (lock_ == NULL) { |
98 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 | 55 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 |
99 return histogram; | 56 return histogram; |
100 } | 57 } |
101 base::AutoLock auto_lock(*lock_); | 58 |
102 if (!histograms_) { | 59 Histogram* histogram_to_delete = NULL; |
103 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 | 60 Histogram* histogram_to_return = NULL; |
104 return histogram; | 61 { |
| 62 base::AutoLock auto_lock(*lock_); |
| 63 if (histograms_ == NULL) { |
| 64 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 |
| 65 histogram_to_return = histogram; |
| 66 } else { |
| 67 const string& name = histogram->histogram_name(); |
| 68 HistogramMap::iterator it = histograms_->find(name); |
| 69 if (histograms_->end() == it) { |
| 70 (*histograms_)[name] = histogram; |
| 71 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 |
| 72 ++number_of_histograms_; |
| 73 histogram_to_return = histogram; |
| 74 } else if (histogram == it->second) { |
| 75 // The histogram was registered before. |
| 76 histogram_to_return = histogram; |
| 77 } else { |
| 78 // We already have one histogram with this name. |
| 79 histogram_to_return = it->second; |
| 80 histogram_to_delete = histogram; |
| 81 } |
| 82 } |
105 } | 83 } |
106 const std::string name = histogram->histogram_name(); | 84 delete histogram_to_delete; |
107 HistogramMap::iterator it = histograms_->find(name); | 85 return histogram_to_return; |
108 // Avoid overwriting a previous registration. | |
109 if (histograms_->end() == it) { | |
110 (*histograms_)[name] = histogram; | |
111 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322 | |
112 RegisterOrDeleteDuplicateRanges(histogram); | |
113 ++number_of_histograms_; | |
114 } else { | |
115 delete histogram; // We already have one by this name. | |
116 histogram = it->second; | |
117 } | |
118 return histogram; | |
119 } | 86 } |
120 | 87 |
121 // static | 88 // static |
122 void StatisticsRecorder::RegisterOrDeleteDuplicateRanges(Histogram* histogram) { | 89 const BucketRanges* StatisticsRecorder::RegisterOrDeleteDuplicateRanges( |
123 DCHECK(histogram); | 90 const BucketRanges* ranges) { |
124 BucketRanges* histogram_ranges = histogram->bucket_ranges(); | 91 DCHECK(ranges->HasValidChecksum()); |
125 DCHECK(histogram_ranges); | 92 scoped_ptr<const BucketRanges> ranges_deleter; |
126 uint32 checksum = histogram->range_checksum(); | |
127 histogram_ranges->set_checksum(checksum); | |
128 | 93 |
129 RangesMap::iterator ranges_it = ranges_->find(checksum); | 94 if (lock_ == NULL) { |
130 if (ranges_->end() == ranges_it) { | 95 ANNOTATE_LEAKING_OBJECT_PTR(ranges); |
131 // Register the new BucketRanges. | 96 return ranges; |
132 std::list<BucketRanges*>* checksum_matching_list( | |
133 new std::list<BucketRanges*>()); | |
134 checksum_matching_list->push_front(histogram_ranges); | |
135 (*ranges_)[checksum] = checksum_matching_list; | |
136 return; | |
137 } | 97 } |
138 | 98 |
139 // Use the registered BucketRanges if the registered BucketRanges has same | 99 base::AutoLock auto_lock(*lock_); |
140 // ranges_ as |histogram|'s BucketRanges. | 100 if (ranges_ == NULL) { |
141 std::list<BucketRanges*>* checksum_matching_list = ranges_it->second; | 101 ANNOTATE_LEAKING_OBJECT_PTR(ranges); |
142 std::list<BucketRanges*>::iterator checksum_matching_list_it; | 102 return ranges; |
| 103 } |
| 104 |
| 105 list<const BucketRanges*>* checksum_matching_list; |
| 106 RangesMap::iterator ranges_it = ranges_->find(ranges->checksum()); |
| 107 if (ranges_->end() == ranges_it) { |
| 108 // Add a new matching list to map. |
| 109 checksum_matching_list = new list<const BucketRanges*>(); |
| 110 ANNOTATE_LEAKING_OBJECT_PTR(checksum_matching_list); |
| 111 (*ranges_)[ranges->checksum()] = checksum_matching_list; |
| 112 } else { |
| 113 checksum_matching_list = ranges_it->second; |
| 114 } |
| 115 |
| 116 list<const BucketRanges*>::iterator checksum_matching_list_it; |
143 for (checksum_matching_list_it = checksum_matching_list->begin(); | 117 for (checksum_matching_list_it = checksum_matching_list->begin(); |
144 checksum_matching_list_it != checksum_matching_list->end(); | 118 checksum_matching_list_it != checksum_matching_list->end(); |
145 ++checksum_matching_list_it) { | 119 ++checksum_matching_list_it) { |
146 BucketRanges* existing_histogram_ranges = *checksum_matching_list_it; | 120 const BucketRanges* existing_ranges = *checksum_matching_list_it; |
147 DCHECK(existing_histogram_ranges); | 121 if (existing_ranges->Equals(ranges)) { |
148 if (existing_histogram_ranges->Equals(histogram_ranges)) { | 122 if (existing_ranges == ranges) { |
149 histogram->set_bucket_ranges(existing_histogram_ranges); | 123 return ranges; |
150 ++number_of_vectors_saved_; | 124 } else { |
151 saved_ranges_size_ += histogram_ranges->size(); | 125 ++number_of_vectors_saved_; |
152 delete histogram_ranges; | 126 saved_ranges_size_ += ranges->size(); |
153 return; | 127 ranges_deleter.reset(ranges); |
| 128 return existing_ranges; |
| 129 } |
154 } | 130 } |
155 } | 131 } |
156 | |
157 // We haven't found a BucketRanges which has the same ranges. Register the | 132 // We haven't found a BucketRanges which has the same ranges. Register the |
158 // new BucketRanges. | 133 // new BucketRanges. |
159 DCHECK(checksum_matching_list_it == checksum_matching_list->end()); | 134 checksum_matching_list->push_front(ranges); |
160 checksum_matching_list->push_front(histogram_ranges); | 135 ANNOTATE_LEAKING_OBJECT_PTR(ranges); |
| 136 return ranges; |
161 } | 137 } |
162 | 138 |
163 // static | 139 // static |
164 void StatisticsRecorder::CollectHistogramStats(const std::string& suffix) { | 140 void StatisticsRecorder::CollectHistogramStats(const std::string& suffix) { |
165 static int uma_upload_attempt = 0; | 141 static int uma_upload_attempt = 0; |
166 ++uma_upload_attempt; | 142 ++uma_upload_attempt; |
167 if (uma_upload_attempt == 1) { | 143 if (uma_upload_attempt == 1) { |
168 UMA_HISTOGRAM_COUNTS_10000( | 144 UMA_HISTOGRAM_COUNTS_10000( |
169 "Histogram.SharedRange.Count.FirstUpload." + suffix, | 145 "Histogram.SharedRange.Count.FirstUpload." + suffix, |
170 number_of_histograms_); | 146 number_of_histograms_); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 (*it)->WriteAscii(output); | 215 (*it)->WriteAscii(output); |
240 output->append("\n"); | 216 output->append("\n"); |
241 } | 217 } |
242 } | 218 } |
243 | 219 |
244 // static | 220 // static |
245 void StatisticsRecorder::GetHistograms(Histograms* output) { | 221 void StatisticsRecorder::GetHistograms(Histograms* output) { |
246 if (lock_ == NULL) | 222 if (lock_ == NULL) |
247 return; | 223 return; |
248 base::AutoLock auto_lock(*lock_); | 224 base::AutoLock auto_lock(*lock_); |
249 if (!histograms_) | 225 if (histograms_ == NULL) |
250 return; | 226 return; |
| 227 |
251 for (HistogramMap::iterator it = histograms_->begin(); | 228 for (HistogramMap::iterator it = histograms_->begin(); |
252 histograms_->end() != it; | 229 histograms_->end() != it; |
253 ++it) { | 230 ++it) { |
254 DCHECK_EQ(it->first, it->second->histogram_name()); | 231 DCHECK_EQ(it->first, it->second->histogram_name()); |
255 output->push_back(it->second); | 232 output->push_back(it->second); |
256 } | 233 } |
257 } | 234 } |
258 | 235 |
259 // static | 236 // static |
| 237 void StatisticsRecorder::GetBucketRanges( |
| 238 std::vector<const BucketRanges*>* output) { |
| 239 if (lock_ == NULL) |
| 240 return; |
| 241 base::AutoLock auto_lock(*lock_); |
| 242 if (ranges_ == NULL) |
| 243 return; |
| 244 |
| 245 for (RangesMap::iterator it = ranges_->begin(); |
| 246 ranges_->end() != it; |
| 247 ++it) { |
| 248 list<const BucketRanges*>* ranges_list = it->second; |
| 249 list<const BucketRanges*>::iterator ranges_list_it; |
| 250 for (ranges_list_it = ranges_list->begin(); |
| 251 ranges_list_it != ranges_list->end(); |
| 252 ++ranges_list_it) { |
| 253 output->push_back(*ranges_list_it); |
| 254 } |
| 255 } |
| 256 } |
| 257 |
| 258 // static |
260 Histogram* StatisticsRecorder::FindHistogram(const std::string& name) { | 259 Histogram* StatisticsRecorder::FindHistogram(const std::string& name) { |
261 if (lock_ == NULL) | 260 if (lock_ == NULL) |
262 return NULL; | 261 return NULL; |
263 base::AutoLock auto_lock(*lock_); | 262 base::AutoLock auto_lock(*lock_); |
264 if (!histograms_) | 263 if (histograms_ == NULL) |
265 return NULL; | 264 return NULL; |
| 265 |
266 HistogramMap::iterator it = histograms_->find(name); | 266 HistogramMap::iterator it = histograms_->find(name); |
267 if (histograms_->end() == it) | 267 if (histograms_->end() == it) |
268 return NULL; | 268 return NULL; |
269 return it->second; | 269 return it->second; |
270 } | 270 } |
271 | 271 |
272 // private static | 272 // private static |
273 void StatisticsRecorder::GetSnapshot(const std::string& query, | 273 void StatisticsRecorder::GetSnapshot(const std::string& query, |
274 Histograms* snapshot) { | 274 Histograms* snapshot) { |
275 if (lock_ == NULL) | 275 if (lock_ == NULL) |
276 return; | 276 return; |
277 base::AutoLock auto_lock(*lock_); | 277 base::AutoLock auto_lock(*lock_); |
278 if (!histograms_) | 278 if (histograms_ == NULL) |
279 return; | 279 return; |
| 280 |
280 for (HistogramMap::iterator it = histograms_->begin(); | 281 for (HistogramMap::iterator it = histograms_->begin(); |
281 histograms_->end() != it; | 282 histograms_->end() != it; |
282 ++it) { | 283 ++it) { |
283 if (it->first.find(query) != std::string::npos) | 284 if (it->first.find(query) != std::string::npos) |
284 snapshot->push_back(it->second); | 285 snapshot->push_back(it->second); |
285 } | 286 } |
286 } | 287 } |
287 | 288 |
| 289 // This singleton instance should be started during the single threaded portion |
| 290 // of main(), and hence it is not thread safe. It initializes globals to |
| 291 // provide support for all future calls. |
| 292 StatisticsRecorder::StatisticsRecorder() { |
| 293 DCHECK(!histograms_); |
| 294 if (lock_ == NULL) { |
| 295 // This will leak on purpose. It's the only way to make sure we won't race |
| 296 // against the static uninitialization of the module while one of our |
| 297 // static methods relying on the lock get called at an inappropriate time |
| 298 // during the termination phase. Since it's a static data member, we will |
| 299 // leak one per process, which would be similar to the instance allocated |
| 300 // during static initialization and released only on process termination. |
| 301 lock_ = new base::Lock; |
| 302 } |
| 303 base::AutoLock auto_lock(*lock_); |
| 304 histograms_ = new HistogramMap; |
| 305 ranges_ = new RangesMap; |
| 306 } |
| 307 |
| 308 StatisticsRecorder::~StatisticsRecorder() { |
| 309 DCHECK(histograms_ && ranges_ && lock_); |
| 310 if (dump_on_exit_) { |
| 311 string output; |
| 312 WriteGraph("", &output); |
| 313 DLOG(INFO) << output; |
| 314 } |
| 315 |
| 316 // Clean up. |
| 317 scoped_ptr<HistogramMap> histograms_deleter; |
| 318 scoped_ptr<RangesMap> ranges_deleter; |
| 319 // We don't delete lock_ on purpose to avoid having to properly protect |
| 320 // against it going away after we checked for NULL in the static methods. |
| 321 { |
| 322 base::AutoLock auto_lock(*lock_); |
| 323 histograms_deleter.reset(histograms_); |
| 324 ranges_deleter.reset(ranges_); |
| 325 histograms_ = NULL; |
| 326 ranges_ = NULL; |
| 327 } |
| 328 // We are going to leak the histograms and the ranges. |
| 329 } |
| 330 |
| 331 |
288 // static | 332 // static |
289 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; | 333 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
290 // static | 334 // static |
291 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; | 335 StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL; |
292 // static | 336 // static |
293 base::Lock* StatisticsRecorder::lock_ = NULL; | 337 base::Lock* StatisticsRecorder::lock_ = NULL; |
294 // static | 338 // static |
295 bool StatisticsRecorder::dump_on_exit_ = false; | 339 bool StatisticsRecorder::dump_on_exit_ = false; |
296 | 340 |
297 } // namespace base | 341 } // namespace base |
OLD | NEW |