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 // Histogram is an object that aggregates statistics, and can summarize them in | 5 // Histogram is an object that aggregates statistics, and can summarize them in |
6 // various forms, including ASCII graphical, HTML, and numerically (as a | 6 // various forms, including ASCII graphical, HTML, and numerically (as a |
7 // vector of numbers corresponding to each of the aggregating buckets). | 7 // vector of numbers corresponding to each of the aggregating buckets). |
8 // See header file for details and examples. | 8 // See header file for details and examples. |
9 | 9 |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 return ranges; | 120 return ranges; |
121 } | 121 } |
122 | 122 |
123 // Allocate the correct Histogram object off the heap (in case persistent | 123 // Allocate the correct Histogram object off the heap (in case persistent |
124 // memory is not available). | 124 // memory is not available). |
125 virtual scoped_ptr<HistogramBase> HeapAlloc(const BucketRanges* ranges) { | 125 virtual scoped_ptr<HistogramBase> HeapAlloc(const BucketRanges* ranges) { |
126 return make_scoped_ptr(new Histogram(name_, minimum_, maximum_, ranges)); | 126 return make_scoped_ptr(new Histogram(name_, minimum_, maximum_, ranges)); |
127 } | 127 } |
128 | 128 |
129 // Perform any required datafill on the just-created histogram. If | 129 // Perform any required datafill on the just-created histogram. If |
130 // overridden, be sure to call the "super" version. | 130 // overridden, be sure to call the "super" version -- this method may not |
131 virtual void FillHistogram(HistogramBase* histogram) { | 131 // always remain empty. |
132 histogram->SetFlags(flags_); | 132 virtual void FillHistogram(HistogramBase* histogram) {} |
133 } | |
134 | 133 |
135 // These values are protected (instead of private) because they need to | 134 // These values are protected (instead of private) because they need to |
136 // be accessible to methods of sub-classes in order to avoid passing | 135 // be accessible to methods of sub-classes in order to avoid passing |
137 // unnecessary parameters everywhere. | 136 // unnecessary parameters everywhere. |
138 const std::string& name_; | 137 const std::string& name_; |
139 const HistogramType histogram_type_; | 138 const HistogramType histogram_type_; |
140 HistogramBase::Sample minimum_; | 139 HistogramBase::Sample minimum_; |
141 HistogramBase::Sample maximum_; | 140 HistogramBase::Sample maximum_; |
142 uint32_t bucket_count_; | 141 uint32_t bucket_count_; |
143 int32_t flags_; | 142 int32_t flags_; |
(...skipping 21 matching lines...) Expand all Loading... |
165 // cases (such as with a CustomHistogram), they are calculated dynamically | 164 // cases (such as with a CustomHistogram), they are calculated dynamically |
166 // at run-time. In the latter case, those ctor parameters are zero and | 165 // at run-time. In the latter case, those ctor parameters are zero and |
167 // the results extracted from the result of CreateRanges(). | 166 // the results extracted from the result of CreateRanges(). |
168 if (bucket_count_ == 0) { | 167 if (bucket_count_ == 0) { |
169 bucket_count_ = static_cast<uint32_t>(registered_ranges->bucket_count()); | 168 bucket_count_ = static_cast<uint32_t>(registered_ranges->bucket_count()); |
170 minimum_ = registered_ranges->range(1); | 169 minimum_ = registered_ranges->range(1); |
171 maximum_ = registered_ranges->range(bucket_count_ - 1); | 170 maximum_ = registered_ranges->range(bucket_count_ - 1); |
172 } | 171 } |
173 | 172 |
174 // Try to create the histogram using a "persistent" allocator. As of | 173 // Try to create the histogram using a "persistent" allocator. As of |
175 // 2015-01-14, the availability of such is controlled by a base::Feature | 174 // 2016-02-25, the availability of such is controlled by a base::Feature |
176 // that is off by default. If the allocator doesn't exist or if | 175 // that is off by default. If the allocator doesn't exist or if |
177 // allocating from it fails, code below will allocate the histogram from | 176 // allocating from it fails, code below will allocate the histogram from |
178 // the process heap. | 177 // the process heap. |
179 PersistentHistogramAllocator::Reference histogram_ref = 0; | 178 PersistentHistogramAllocator::Reference histogram_ref = 0; |
180 scoped_ptr<HistogramBase> tentative_histogram; | 179 scoped_ptr<HistogramBase> tentative_histogram; |
181 PersistentHistogramAllocator* allocator = | 180 PersistentHistogramAllocator* allocator = |
182 PersistentHistogramAllocator::GetGlobalAllocator(); | 181 PersistentHistogramAllocator::GetGlobalAllocator(); |
183 if (allocator) { | 182 if (allocator) { |
184 flags_ |= HistogramBase::kIsPersistent; | |
185 tentative_histogram = allocator->AllocateHistogram( | 183 tentative_histogram = allocator->AllocateHistogram( |
186 histogram_type_, | 184 histogram_type_, |
187 name_, | 185 name_, |
188 minimum_, | 186 minimum_, |
189 maximum_, | 187 maximum_, |
190 registered_ranges, | 188 registered_ranges, |
191 flags_, | 189 flags_, |
192 &histogram_ref); | 190 &histogram_ref); |
193 } | 191 } |
194 | 192 |
195 // Handle the case where no persistent allocator is present or the | 193 // Handle the case where no persistent allocator is present or the |
196 // persistent allocation fails (perhaps because it is full). | 194 // persistent allocation fails (perhaps because it is full). |
197 if (!tentative_histogram) { | 195 if (!tentative_histogram) { |
198 DCHECK(!histogram_ref); // Should never have been set. | 196 DCHECK(!histogram_ref); // Should never have been set. |
199 DCHECK(!allocator); // Shouldn't have failed. | 197 DCHECK(!allocator); // Shouldn't have failed. |
200 flags_ &= ~HistogramBase::kIsPersistent; | 198 flags_ &= ~HistogramBase::kIsPersistent; |
201 tentative_histogram = HeapAlloc(registered_ranges); | 199 tentative_histogram = HeapAlloc(registered_ranges); |
| 200 tentative_histogram->SetFlags(flags_); |
202 } | 201 } |
203 | 202 |
204 FillHistogram(tentative_histogram.get()); | 203 FillHistogram(tentative_histogram.get()); |
205 | 204 |
206 // Register this histogram with the StatisticsRecorder. Keep a copy of | 205 // Register this histogram with the StatisticsRecorder. Keep a copy of |
207 // the pointer value to tell later whether the locally created histogram | 206 // the pointer value to tell later whether the locally created histogram |
208 // was registered or deleted. The type is "void" because it could point | 207 // was registered or deleted. The type is "void" because it could point |
209 // to released memory after the following line. | 208 // to released memory after the following line. |
210 const void* tentative_histogram_ptr = tentative_histogram.get(); | 209 const void* tentative_histogram_ptr = tentative_histogram.get(); |
211 histogram = StatisticsRecorder::RegisterOrDeleteDuplicate( | 210 histogram = StatisticsRecorder::RegisterOrDeleteDuplicate( |
(...skipping 957 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1169 Sample sample = custom_ranges[i]; | 1168 Sample sample = custom_ranges[i]; |
1170 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1) | 1169 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1) |
1171 return false; | 1170 return false; |
1172 if (sample != 0) | 1171 if (sample != 0) |
1173 has_valid_range = true; | 1172 has_valid_range = true; |
1174 } | 1173 } |
1175 return has_valid_range; | 1174 return has_valid_range; |
1176 } | 1175 } |
1177 | 1176 |
1178 } // namespace base | 1177 } // namespace base |
OLD | NEW |