|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 // Histogram is an object that aggregates statistics, and can summarize them in |
|
6 // various forms, including ASCII graphical, HTML, and numerically (as a |
|
7 // vector of numbers corresponding to each of the aggregating buckets). |
|
8 // See header file for details and examples. |
|
9 |
|
10 #include "base/histogram.h" |
|
11 |
|
12 #include <math.h> |
|
13 |
|
14 #include <algorithm> |
|
15 #include <string> |
|
16 |
|
17 #include "base/logging.h" |
|
18 #include "base/pickle.h" |
|
19 #include "base/string_util.h" |
|
20 #include "base/logging.h" |
|
21 |
|
22 namespace base { |
|
23 |
|
24 #define DVLOG(x) CHROMIUM_LOG(ERROR) |
|
25 #define CHECK_GT DCHECK_GT |
|
26 #define CHECK_LT DCHECK_LT |
|
27 typedef ::Lock Lock; |
|
28 typedef ::AutoLock AutoLock; |
|
29 |
|
30 // Static table of checksums for all possible 8 bit bytes. |
|
31 const uint32_t Histogram::kCrcTable[256] = {0x0, 0x77073096L, 0xee0e612cL, |
|
32 0x990951baL, 0x76dc419L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0xedb8832L, |
|
33 0x79dcb8a4L, 0xe0d5e91eL, 0x97d2d988L, 0x9b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, |
|
34 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, 0x1adad47dL, |
|
35 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, 0x646ba8c0L, 0xfd62f97aL, |
|
36 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, |
|
37 0x4c69105eL, 0xd56041e4L, 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, |
|
38 0xa50ab56bL, 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, |
|
39 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, 0xc8d75180L, |
|
40 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, 0xb8bda50fL, 0x2802b89eL, |
|
41 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, |
|
42 0xb6662d3dL, 0x76dc4190L, 0x1db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, |
|
43 0x6b6b51fL, 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0xf00f934L, 0x9609a88eL, |
|
44 0xe10e9818L, 0x7f6a0dbbL, 0x86d3d2dL, 0x91646c97L, 0xe6635c01L, 0x6b6b51f4L, |
|
45 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, 0x1b01a57bL, 0x8208f4c1L, |
|
46 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, |
|
47 0x15da2d49L, 0x8cd37cf3L, 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, |
|
48 0xd4bb30e2L, 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL, |
|
49 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, 0xaa0a4c5fL, |
|
50 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, 0xc90c2086L, 0x5768b525L, |
|
51 0x206f85b3L, 0xb966d409L, 0xce61e49fL, 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, |
|
52 0xc7d7a8b4L, 0x59b33d17L, 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, |
|
53 0x9abfb3b6L, 0x3b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x4db2615L, |
|
54 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0xd6d6a3eL, 0x7a6a5aa8L, 0xe40ecf0bL, |
|
55 0x9309ff9dL, 0xa00ae27L, 0x7d079eb1L, 0xf00f9344L, 0x8708a3d2L, 0x1e01f268L, |
|
56 0x6906c2feL, 0xf762575dL, 0x806567cbL, 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, |
|
57 0x89d32be0L, 0x10da7a5aL, 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, |
|
58 0x60b08ed5L, 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, |
|
59 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, 0x36034af6L, |
|
60 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, 0x4669be79L, 0xcb61b38cL, |
|
61 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, |
|
62 0x5505262fL, 0xc5ba3bbeL, 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, |
|
63 0xb5d0cf31L, 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, |
|
64 0x26d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x5005713L, 0x95bf4a82L, |
|
65 0xe2b87a14L, 0x7bb12baeL, 0xcb61b38L, 0x92d28e9bL, 0xe5d5be0dL, 0x7cdcefb7L, |
|
66 0xbdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, |
|
67 0xf6b9265bL, 0x6fb077e1L, 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, |
|
68 0x11010b5cL, 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L, |
|
69 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, 0x4969474dL, |
|
70 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, 0x37d83bf0L, 0xa9bcae53L, |
|
71 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, |
|
72 0x24b4a3a6L, 0xbad03605L, 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, |
|
73 0xc4614ab8L, 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, |
|
74 0x2d02ef8dL, |
|
75 }; |
|
76 |
|
77 typedef Histogram::Count Count; |
|
78 |
|
79 // static |
|
80 const size_t Histogram::kBucketCount_MAX = 16384u; |
|
81 |
|
82 Histogram* Histogram::FactoryGet(const std::string& name, |
|
83 Sample minimum, |
|
84 Sample maximum, |
|
85 size_t bucket_count, |
|
86 Flags flags) { |
|
87 Histogram* histogram(NULL); |
|
88 |
|
89 // Defensive code. |
|
90 if (minimum < 1) |
|
91 minimum = 1; |
|
92 if (maximum > kSampleType_MAX - 1) |
|
93 maximum = kSampleType_MAX - 1; |
|
94 |
|
95 if (!StatisticsRecorder::FindHistogram(name, &histogram)) { |
|
96 // Extra variable is not needed... but this keeps this section basically |
|
97 // identical to other derived classes in this file (and compiler will |
|
98 // optimize away the extra variable. |
|
99 Histogram* tentative_histogram = |
|
100 new Histogram(name, minimum, maximum, bucket_count); |
|
101 tentative_histogram->InitializeBucketRange(); |
|
102 tentative_histogram->SetFlags(flags); |
|
103 histogram = |
|
104 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
|
105 } |
|
106 |
|
107 DCHECK_EQ(HISTOGRAM, histogram->histogram_type()); |
|
108 DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count)); |
|
109 return histogram; |
|
110 } |
|
111 |
|
112 Histogram* Histogram::FactoryTimeGet(const std::string& name, |
|
113 TimeDelta minimum, |
|
114 TimeDelta maximum, |
|
115 size_t bucket_count, |
|
116 Flags flags) { |
|
117 return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(), |
|
118 bucket_count, flags); |
|
119 } |
|
120 |
|
121 void Histogram::Add(int value) { |
|
122 if (value > kSampleType_MAX - 1) |
|
123 value = kSampleType_MAX - 1; |
|
124 if (value < 0) |
|
125 value = 0; |
|
126 size_t index = BucketIndex(value); |
|
127 DCHECK_GE(value, ranges(index)); |
|
128 DCHECK_LT(value, ranges(index + 1)); |
|
129 Accumulate(value, 1, index); |
|
130 } |
|
131 |
|
132 void Histogram::Subtract(int value) { |
|
133 if (value > kSampleType_MAX - 1) |
|
134 value = kSampleType_MAX - 1; |
|
135 if (value < 0) |
|
136 value = 0; |
|
137 size_t index = BucketIndex(value); |
|
138 DCHECK_GE(value, ranges(index)); |
|
139 DCHECK_LT(value, ranges(index + 1)); |
|
140 Accumulate(value, -1, index); |
|
141 } |
|
142 |
|
143 void Histogram::AddBoolean(bool value) { |
|
144 DCHECK(false); |
|
145 } |
|
146 |
|
147 void Histogram::AddSampleSet(const SampleSet& sample) { |
|
148 sample_.Add(sample); |
|
149 } |
|
150 |
|
151 void Histogram::Clear() { |
|
152 SampleSet ss; |
|
153 ss.Resize(*this); |
|
154 sample_ = ss; |
|
155 } |
|
156 |
|
157 void Histogram::SetRangeDescriptions(const DescriptionPair descriptions[]) { |
|
158 DCHECK(false); |
|
159 } |
|
160 |
|
161 // The following methods provide a graphical histogram display. |
|
162 void Histogram::WriteHTMLGraph(std::string* output) const { |
|
163 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc. |
|
164 output->append("<PRE>"); |
|
165 WriteAscii(true, "<br>", output); |
|
166 output->append("</PRE>"); |
|
167 } |
|
168 |
|
169 void Histogram::WriteAscii(bool graph_it, const std::string& newline, |
|
170 std::string* output) const { |
|
171 // Get local (stack) copies of all effectively volatile class data so that we |
|
172 // are consistent across our output activities. |
|
173 SampleSet snapshot; |
|
174 SnapshotSample(&snapshot); |
|
175 Count sample_count = snapshot.TotalCount(); |
|
176 |
|
177 WriteAsciiHeader(snapshot, sample_count, output); |
|
178 output->append(newline); |
|
179 |
|
180 // Prepare to normalize graphical rendering of bucket contents. |
|
181 double max_size = 0; |
|
182 if (graph_it) |
|
183 max_size = GetPeakBucketSize(snapshot); |
|
184 |
|
185 // Calculate space needed to print bucket range numbers. Leave room to print |
|
186 // nearly the largest bucket range without sliding over the histogram. |
|
187 size_t largest_non_empty_bucket = bucket_count() - 1; |
|
188 while (0 == snapshot.counts(largest_non_empty_bucket)) { |
|
189 if (0 == largest_non_empty_bucket) |
|
190 break; // All buckets are empty. |
|
191 --largest_non_empty_bucket; |
|
192 } |
|
193 |
|
194 // Calculate largest print width needed for any of our bucket range displays. |
|
195 size_t print_width = 1; |
|
196 for (size_t i = 0; i < bucket_count(); ++i) { |
|
197 if (snapshot.counts(i)) { |
|
198 size_t width = GetAsciiBucketRange(i).size() + 1; |
|
199 if (width > print_width) |
|
200 print_width = width; |
|
201 } |
|
202 } |
|
203 |
|
204 int64_t remaining = sample_count; |
|
205 int64_t past = 0; |
|
206 // Output the actual histogram graph. |
|
207 for (size_t i = 0; i < bucket_count(); ++i) { |
|
208 Count current = snapshot.counts(i); |
|
209 if (!current && !PrintEmptyBucket(i)) |
|
210 continue; |
|
211 remaining -= current; |
|
212 std::string range = GetAsciiBucketRange(i); |
|
213 output->append(range); |
|
214 for (size_t j = 0; range.size() + j < print_width + 1; ++j) |
|
215 output->push_back(' '); |
|
216 if (0 == current && i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) { |
|
217 while (i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) |
|
218 ++i; |
|
219 output->append("... "); |
|
220 output->append(newline); |
|
221 continue; // No reason to plot emptiness. |
|
222 } |
|
223 double current_size = GetBucketSize(current, i); |
|
224 if (graph_it) |
|
225 WriteAsciiBucketGraph(current_size, max_size, output); |
|
226 WriteAsciiBucketContext(past, current, remaining, i, output); |
|
227 output->append(newline); |
|
228 past += current; |
|
229 } |
|
230 DCHECK_EQ(sample_count, past); |
|
231 } |
|
232 |
|
233 // static |
|
234 std::string Histogram::SerializeHistogramInfo(const Histogram& histogram, |
|
235 const SampleSet& snapshot) { |
|
236 DCHECK_NE(NOT_VALID_IN_RENDERER, histogram.histogram_type()); |
|
237 |
|
238 Pickle pickle; |
|
239 pickle.WriteString(histogram.histogram_name()); |
|
240 pickle.WriteInt(histogram.declared_min()); |
|
241 pickle.WriteInt(histogram.declared_max()); |
|
242 pickle.WriteSize(histogram.bucket_count()); |
|
243 pickle.WriteUInt32(histogram.range_checksum()); |
|
244 pickle.WriteInt(histogram.histogram_type()); |
|
245 pickle.WriteInt(histogram.flags()); |
|
246 |
|
247 snapshot.Serialize(&pickle); |
|
248 return std::string(static_cast<const char*>(pickle.data()), pickle.size()); |
|
249 } |
|
250 |
|
251 // static |
|
252 bool Histogram::DeserializeHistogramInfo(const std::string& histogram_info) { |
|
253 if (histogram_info.empty()) { |
|
254 return false; |
|
255 } |
|
256 |
|
257 Pickle pickle(histogram_info.data(), |
|
258 static_cast<int>(histogram_info.size())); |
|
259 std::string histogram_name; |
|
260 int declared_min; |
|
261 int declared_max; |
|
262 size_t bucket_count; |
|
263 uint32_t range_checksum; |
|
264 int histogram_type; |
|
265 int pickle_flags; |
|
266 SampleSet sample; |
|
267 |
|
268 void* iter = NULL; |
|
269 if (!pickle.ReadString(&iter, &histogram_name) || |
|
270 !pickle.ReadInt(&iter, &declared_min) || |
|
271 !pickle.ReadInt(&iter, &declared_max) || |
|
272 !pickle.ReadSize(&iter, &bucket_count) || |
|
273 !pickle.ReadUInt32(&iter, &range_checksum) || |
|
274 !pickle.ReadInt(&iter, &histogram_type) || |
|
275 !pickle.ReadInt(&iter, &pickle_flags) || |
|
276 !sample.Histogram::SampleSet::Deserialize(&iter, pickle)) { |
|
277 CHROMIUM_LOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name; |
|
278 return false; |
|
279 } |
|
280 DCHECK(pickle_flags & kIPCSerializationSourceFlag); |
|
281 // Since these fields may have come from an untrusted renderer, do additional |
|
282 // checks above and beyond those in Histogram::Initialize() |
|
283 if (declared_max <= 0 || declared_min <= 0 || declared_max < declared_min || |
|
284 INT_MAX / sizeof(Count) <= bucket_count || bucket_count < 2) { |
|
285 CHROMIUM_LOG(ERROR) << "Values error decoding Histogram: " << histogram_name; |
|
286 return false; |
|
287 } |
|
288 |
|
289 Flags flags = static_cast<Flags>(pickle_flags & ~kIPCSerializationSourceFlag); |
|
290 |
|
291 DCHECK_NE(NOT_VALID_IN_RENDERER, histogram_type); |
|
292 |
|
293 Histogram* render_histogram(NULL); |
|
294 |
|
295 if (histogram_type == HISTOGRAM) { |
|
296 render_histogram = Histogram::FactoryGet( |
|
297 histogram_name, declared_min, declared_max, bucket_count, flags); |
|
298 } else if (histogram_type == LINEAR_HISTOGRAM) { |
|
299 render_histogram = LinearHistogram::FactoryGet( |
|
300 histogram_name, declared_min, declared_max, bucket_count, flags); |
|
301 } else if (histogram_type == BOOLEAN_HISTOGRAM) { |
|
302 render_histogram = BooleanHistogram::FactoryGet(histogram_name, flags); |
|
303 } else { |
|
304 CHROMIUM_LOG(ERROR) << "Error Deserializing Histogram Unknown histogram_type: " |
|
305 << histogram_type; |
|
306 return false; |
|
307 } |
|
308 |
|
309 DCHECK_EQ(render_histogram->declared_min(), declared_min); |
|
310 DCHECK_EQ(render_histogram->declared_max(), declared_max); |
|
311 DCHECK_EQ(render_histogram->bucket_count(), bucket_count); |
|
312 DCHECK_EQ(render_histogram->range_checksum(), range_checksum); |
|
313 DCHECK_EQ(render_histogram->histogram_type(), histogram_type); |
|
314 |
|
315 if (render_histogram->flags() & kIPCSerializationSourceFlag) { |
|
316 DVLOG(1) << "Single process mode, histogram observed and not copied: " |
|
317 << histogram_name; |
|
318 } else { |
|
319 DCHECK_EQ(flags & render_histogram->flags(), flags); |
|
320 render_histogram->AddSampleSet(sample); |
|
321 } |
|
322 |
|
323 return true; |
|
324 } |
|
325 |
|
326 //------------------------------------------------------------------------------ |
|
327 // Methods for the validating a sample and a related histogram. |
|
328 //------------------------------------------------------------------------------ |
|
329 |
|
330 Histogram::Inconsistencies Histogram::FindCorruption( |
|
331 const SampleSet& snapshot) const { |
|
332 int inconsistencies = NO_INCONSISTENCIES; |
|
333 Sample previous_range = -1; // Bottom range is always 0. |
|
334 int64_t count = 0; |
|
335 for (size_t index = 0; index < bucket_count(); ++index) { |
|
336 count += snapshot.counts(index); |
|
337 int new_range = ranges(index); |
|
338 if (previous_range >= new_range) |
|
339 inconsistencies |= BUCKET_ORDER_ERROR; |
|
340 previous_range = new_range; |
|
341 } |
|
342 |
|
343 if (!HasValidRangeChecksum()) |
|
344 inconsistencies |= RANGE_CHECKSUM_ERROR; |
|
345 |
|
346 int64_t delta64 = snapshot.redundant_count() - count; |
|
347 if (delta64 != 0) { |
|
348 int delta = static_cast<int>(delta64); |
|
349 if (delta != delta64) |
|
350 delta = INT_MAX; // Flag all giant errors as INT_MAX. |
|
351 // Since snapshots of histograms are taken asynchronously relative to |
|
352 // sampling (and snapped from different threads), it is pretty likely that |
|
353 // we'll catch a redundant count that doesn't match the sample count. We |
|
354 // allow for a certain amount of slop before flagging this as an |
|
355 // inconsistency. Even with an inconsistency, we'll snapshot it again (for |
|
356 // UMA in about a half hour, so we'll eventually get the data, if it was |
|
357 // not the result of a corruption. If histograms show that 1 is "too tight" |
|
358 // then we may try to use 2 or 3 for this slop value. |
|
359 const int kCommonRaceBasedCountMismatch = 1; |
|
360 if (delta > 0) { |
|
361 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta); |
|
362 if (delta > kCommonRaceBasedCountMismatch) |
|
363 inconsistencies |= COUNT_HIGH_ERROR; |
|
364 } else { |
|
365 DCHECK_GT(0, delta); |
|
366 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta); |
|
367 if (-delta > kCommonRaceBasedCountMismatch) |
|
368 inconsistencies |= COUNT_LOW_ERROR; |
|
369 } |
|
370 } |
|
371 return static_cast<Inconsistencies>(inconsistencies); |
|
372 } |
|
373 |
|
374 Histogram::ClassType Histogram::histogram_type() const { |
|
375 return HISTOGRAM; |
|
376 } |
|
377 |
|
378 Histogram::Sample Histogram::ranges(size_t i) const { |
|
379 return ranges_[i]; |
|
380 } |
|
381 |
|
382 size_t Histogram::bucket_count() const { |
|
383 return bucket_count_; |
|
384 } |
|
385 |
|
386 // Do a safe atomic snapshot of sample data. |
|
387 // This implementation assumes we are on a safe single thread. |
|
388 void Histogram::SnapshotSample(SampleSet* sample) const { |
|
389 // Note locking not done in this version!!! |
|
390 *sample = sample_; |
|
391 } |
|
392 |
|
393 bool Histogram::HasConstructorArguments(Sample minimum, |
|
394 Sample maximum, |
|
395 size_t bucket_count) { |
|
396 return ((minimum == declared_min_) && (maximum == declared_max_) && |
|
397 (bucket_count == bucket_count_)); |
|
398 } |
|
399 |
|
400 bool Histogram::HasConstructorTimeDeltaArguments(TimeDelta minimum, |
|
401 TimeDelta maximum, |
|
402 size_t bucket_count) { |
|
403 return ((minimum.InMilliseconds() == declared_min_) && |
|
404 (maximum.InMilliseconds() == declared_max_) && |
|
405 (bucket_count == bucket_count_)); |
|
406 } |
|
407 |
|
408 bool Histogram::HasValidRangeChecksum() const { |
|
409 return CalculateRangeChecksum() == range_checksum_; |
|
410 } |
|
411 |
|
412 size_t Histogram::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) |
|
413 { |
|
414 size_t n = 0; |
|
415 n += aMallocSizeOf(this); |
|
416 // We're not allowed to do deep dives into STL data structures. This |
|
417 // is as close as we can get to measuring this array. |
|
418 n += aMallocSizeOf(&ranges_[0]); |
|
419 n += sample_.SizeOfExcludingThis(aMallocSizeOf); |
|
420 return n; |
|
421 } |
|
422 |
|
423 size_t Histogram::SampleSet::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) |
|
424 { |
|
425 // We're not allowed to do deep dives into STL data structures. This |
|
426 // is as close as we can get to measuring this array. |
|
427 return aMallocSizeOf(&counts_[0]); |
|
428 } |
|
429 |
|
430 Histogram::Histogram(const std::string& name, Sample minimum, |
|
431 Sample maximum, size_t bucket_count) |
|
432 : sample_(), |
|
433 histogram_name_(name), |
|
434 declared_min_(minimum), |
|
435 declared_max_(maximum), |
|
436 bucket_count_(bucket_count), |
|
437 flags_(kNoFlags), |
|
438 ranges_(bucket_count + 1, 0), |
|
439 range_checksum_(0) { |
|
440 Initialize(); |
|
441 } |
|
442 |
|
443 Histogram::Histogram(const std::string& name, TimeDelta minimum, |
|
444 TimeDelta maximum, size_t bucket_count) |
|
445 : sample_(), |
|
446 histogram_name_(name), |
|
447 declared_min_(static_cast<int> (minimum.InMilliseconds())), |
|
448 declared_max_(static_cast<int> (maximum.InMilliseconds())), |
|
449 bucket_count_(bucket_count), |
|
450 flags_(kNoFlags), |
|
451 ranges_(bucket_count + 1, 0), |
|
452 range_checksum_(0) { |
|
453 Initialize(); |
|
454 } |
|
455 |
|
456 Histogram::~Histogram() { |
|
457 if (StatisticsRecorder::dump_on_exit()) { |
|
458 std::string output; |
|
459 WriteAscii(true, "\n", &output); |
|
460 CHROMIUM_LOG(INFO) << output; |
|
461 } |
|
462 |
|
463 // Just to make sure most derived class did this properly... |
|
464 DCHECK(ValidateBucketRanges()); |
|
465 } |
|
466 |
|
467 // Calculate what range of values are held in each bucket. |
|
468 // We have to be careful that we don't pick a ratio between starting points in |
|
469 // consecutive buckets that is sooo small, that the integer bounds are the same |
|
470 // (effectively making one bucket get no values). We need to avoid: |
|
471 // ranges_[i] == ranges_[i + 1] |
|
472 // To avoid that, we just do a fine-grained bucket width as far as we need to |
|
473 // until we get a ratio that moves us along at least 2 units at a time. From |
|
474 // that bucket onward we do use the exponential growth of buckets. |
|
475 void Histogram::InitializeBucketRange() { |
|
476 double log_max = log(static_cast<double>(declared_max())); |
|
477 double log_ratio; |
|
478 double log_next; |
|
479 size_t bucket_index = 1; |
|
480 Sample current = declared_min(); |
|
481 SetBucketRange(bucket_index, current); |
|
482 while (bucket_count() > ++bucket_index) { |
|
483 double log_current; |
|
484 log_current = log(static_cast<double>(current)); |
|
485 // Calculate the count'th root of the range. |
|
486 log_ratio = (log_max - log_current) / (bucket_count() - bucket_index); |
|
487 // See where the next bucket would start. |
|
488 log_next = log_current + log_ratio; |
|
489 int next; |
|
490 next = static_cast<int>(floor(exp(log_next) + 0.5)); |
|
491 if (next > current) |
|
492 current = next; |
|
493 else |
|
494 ++current; // Just do a narrow bucket, and keep trying. |
|
495 SetBucketRange(bucket_index, current); |
|
496 } |
|
497 ResetRangeChecksum(); |
|
498 |
|
499 DCHECK_EQ(bucket_count(), bucket_index); |
|
500 } |
|
501 |
|
502 bool Histogram::PrintEmptyBucket(size_t index) const { |
|
503 return true; |
|
504 } |
|
505 |
|
506 size_t Histogram::BucketIndex(Sample value) const { |
|
507 // Use simple binary search. This is very general, but there are better |
|
508 // approaches if we knew that the buckets were linearly distributed. |
|
509 DCHECK_LE(ranges(0), value); |
|
510 DCHECK_GT(ranges(bucket_count()), value); |
|
511 size_t under = 0; |
|
512 size_t over = bucket_count(); |
|
513 size_t mid; |
|
514 |
|
515 do { |
|
516 DCHECK_GE(over, under); |
|
517 mid = under + (over - under)/2; |
|
518 if (mid == under) |
|
519 break; |
|
520 if (ranges(mid) <= value) |
|
521 under = mid; |
|
522 else |
|
523 over = mid; |
|
524 } while (true); |
|
525 |
|
526 DCHECK_LE(ranges(mid), value); |
|
527 CHECK_GT(ranges(mid+1), value); |
|
528 return mid; |
|
529 } |
|
530 |
|
531 // Use the actual bucket widths (like a linear histogram) until the widths get |
|
532 // over some transition value, and then use that transition width. Exponentials |
|
533 // get so big so fast (and we don't expect to see a lot of entries in the large |
|
534 // buckets), so we need this to make it possible to see what is going on and |
|
535 // not have 0-graphical-height buckets. |
|
536 double Histogram::GetBucketSize(Count current, size_t i) const { |
|
537 DCHECK_GT(ranges(i + 1), ranges(i)); |
|
538 static const double kTransitionWidth = 5; |
|
539 double denominator = ranges(i + 1) - ranges(i); |
|
540 if (denominator > kTransitionWidth) |
|
541 denominator = kTransitionWidth; // Stop trying to normalize. |
|
542 return current/denominator; |
|
543 } |
|
544 |
|
545 void Histogram::ResetRangeChecksum() { |
|
546 range_checksum_ = CalculateRangeChecksum(); |
|
547 } |
|
548 |
|
549 const std::string Histogram::GetAsciiBucketRange(size_t i) const { |
|
550 std::string result; |
|
551 if (kHexRangePrintingFlag & flags_) |
|
552 StringAppendF(&result, "%#x", ranges(i)); |
|
553 else |
|
554 StringAppendF(&result, "%d", ranges(i)); |
|
555 return result; |
|
556 } |
|
557 |
|
558 // Update histogram data with new sample. |
|
559 void Histogram::Accumulate(Sample value, Count count, size_t index) { |
|
560 // Note locking not done in this version!!! |
|
561 sample_.AccumulateWithExponentialStats(value, count, index, |
|
562 flags_ & kExtendedStatisticsFlag); |
|
563 } |
|
564 |
|
565 void Histogram::SetBucketRange(size_t i, Sample value) { |
|
566 DCHECK_GT(bucket_count_, i); |
|
567 ranges_[i] = value; |
|
568 } |
|
569 |
|
570 bool Histogram::ValidateBucketRanges() const { |
|
571 // Standard assertions that all bucket ranges should satisfy. |
|
572 DCHECK_EQ(bucket_count_ + 1, ranges_.size()); |
|
573 DCHECK_EQ(0, ranges_[0]); |
|
574 DCHECK_EQ(declared_min(), ranges_[1]); |
|
575 DCHECK_EQ(declared_max(), ranges_[bucket_count_ - 1]); |
|
576 DCHECK_EQ(kSampleType_MAX, ranges_[bucket_count_]); |
|
577 return true; |
|
578 } |
|
579 |
|
580 uint32_t Histogram::CalculateRangeChecksum() const { |
|
581 DCHECK_EQ(ranges_.size(), bucket_count() + 1); |
|
582 uint32_t checksum = static_cast<uint32_t>(ranges_.size()); // Seed checksum. |
|
583 for (size_t index = 0; index < bucket_count(); ++index) |
|
584 checksum = Crc32(checksum, ranges(index)); |
|
585 return checksum; |
|
586 } |
|
587 |
|
588 void Histogram::Initialize() { |
|
589 sample_.Resize(*this); |
|
590 if (declared_min_ < 1) |
|
591 declared_min_ = 1; |
|
592 if (declared_max_ > kSampleType_MAX - 1) |
|
593 declared_max_ = kSampleType_MAX - 1; |
|
594 DCHECK_LE(declared_min_, declared_max_); |
|
595 DCHECK_GT(bucket_count_, 1u); |
|
596 CHECK_LT(bucket_count_, kBucketCount_MAX); |
|
597 size_t maximal_bucket_count = declared_max_ - declared_min_ + 2; |
|
598 DCHECK_LE(bucket_count_, maximal_bucket_count); |
|
599 DCHECK_EQ(0, ranges_[0]); |
|
600 ranges_[bucket_count_] = kSampleType_MAX; |
|
601 } |
|
602 |
|
603 // We generate the CRC-32 using the low order bits to select whether to XOR in |
|
604 // the reversed polynomial 0xedb88320L. This is nice and simple, and allows us |
|
605 // to keep the quotient in a uint32_t. Since we're not concerned about the nature |
|
606 // of corruptions (i.e., we don't care about bit sequencing, since we are |
|
607 // handling memory changes, which are more grotesque) so we don't bother to |
|
608 // get the CRC correct for big-endian vs little-ending calculations. All we |
|
609 // need is a nice hash, that tends to depend on all the bits of the sample, with |
|
610 // very little chance of changes in one place impacting changes in another |
|
611 // place. |
|
612 uint32_t Histogram::Crc32(uint32_t sum, Histogram::Sample range) { |
|
613 const bool kUseRealCrc = true; // TODO(jar): Switch to false and watch stats. |
|
614 if (kUseRealCrc) { |
|
615 union { |
|
616 Histogram::Sample range; |
|
617 unsigned char bytes[sizeof(Histogram::Sample)]; |
|
618 } converter; |
|
619 converter.range = range; |
|
620 for (size_t i = 0; i < sizeof(converter); ++i) |
|
621 sum = kCrcTable[(sum & 0xff) ^ converter.bytes[i]] ^ (sum >> 8); |
|
622 } else { |
|
623 // Use hash techniques provided in ReallyFastHash, except we don't care |
|
624 // about "avalanching" (which would worsten the hash, and add collisions), |
|
625 // and we don't care about edge cases since we have an even number of bytes. |
|
626 union { |
|
627 Histogram::Sample range; |
|
628 uint16_t ints[sizeof(Histogram::Sample) / 2]; |
|
629 } converter; |
|
630 DCHECK_EQ(sizeof(Histogram::Sample), sizeof(converter)); |
|
631 converter.range = range; |
|
632 sum += converter.ints[0]; |
|
633 sum = (sum << 16) ^ sum ^ (static_cast<uint32_t>(converter.ints[1]) << 11); |
|
634 sum += sum >> 11; |
|
635 } |
|
636 return sum; |
|
637 } |
|
638 |
|
639 //------------------------------------------------------------------------------ |
|
640 // Private methods |
|
641 |
|
642 double Histogram::GetPeakBucketSize(const SampleSet& snapshot) const { |
|
643 double max = 0; |
|
644 for (size_t i = 0; i < bucket_count() ; ++i) { |
|
645 double current_size = GetBucketSize(snapshot.counts(i), i); |
|
646 if (current_size > max) |
|
647 max = current_size; |
|
648 } |
|
649 return max; |
|
650 } |
|
651 |
|
652 void Histogram::WriteAsciiHeader(const SampleSet& snapshot, |
|
653 Count sample_count, |
|
654 std::string* output) const { |
|
655 StringAppendF(output, |
|
656 "Histogram: %s recorded %d samples", |
|
657 histogram_name().c_str(), |
|
658 sample_count); |
|
659 if (0 == sample_count) { |
|
660 DCHECK_EQ(snapshot.sum(), 0); |
|
661 } else { |
|
662 double average = static_cast<float>(snapshot.sum()) / sample_count; |
|
663 |
|
664 StringAppendF(output, ", average = %.1f", average); |
|
665 } |
|
666 if (flags_ & ~kHexRangePrintingFlag) |
|
667 StringAppendF(output, " (flags = 0x%x)", flags_ & ~kHexRangePrintingFlag); |
|
668 } |
|
669 |
|
670 void Histogram::WriteAsciiBucketContext(const int64_t past, |
|
671 const Count current, |
|
672 const int64_t remaining, |
|
673 const size_t i, |
|
674 std::string* output) const { |
|
675 double scaled_sum = (past + current + remaining) / 100.0; |
|
676 WriteAsciiBucketValue(current, scaled_sum, output); |
|
677 if (0 < i) { |
|
678 double percentage = past / scaled_sum; |
|
679 StringAppendF(output, " {%3.1f%%}", percentage); |
|
680 } |
|
681 } |
|
682 |
|
683 void Histogram::WriteAsciiBucketValue(Count current, double scaled_sum, |
|
684 std::string* output) const { |
|
685 StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum); |
|
686 } |
|
687 |
|
688 void Histogram::WriteAsciiBucketGraph(double current_size, double max_size, |
|
689 std::string* output) const { |
|
690 const int k_line_length = 72; // Maximal horizontal width of graph. |
|
691 int x_count = static_cast<int>(k_line_length * (current_size / max_size) |
|
692 + 0.5); |
|
693 int x_remainder = k_line_length - x_count; |
|
694 |
|
695 while (0 < x_count--) |
|
696 output->append("-"); |
|
697 output->append("O"); |
|
698 while (0 < x_remainder--) |
|
699 output->append(" "); |
|
700 } |
|
701 |
|
702 //------------------------------------------------------------------------------ |
|
703 // Methods for the Histogram::SampleSet class |
|
704 //------------------------------------------------------------------------------ |
|
705 |
|
706 Histogram::SampleSet::SampleSet() |
|
707 : counts_(), |
|
708 sum_(0), |
|
709 sum_squares_(0), |
|
710 log_sum_(0), |
|
711 log_sum_squares_(0), |
|
712 redundant_count_(0) { |
|
713 } |
|
714 |
|
715 Histogram::SampleSet::~SampleSet() { |
|
716 } |
|
717 |
|
718 void Histogram::SampleSet::Resize(const Histogram& histogram) { |
|
719 counts_.resize(histogram.bucket_count(), 0); |
|
720 } |
|
721 |
|
722 void Histogram::SampleSet::CheckSize(const Histogram& histogram) const { |
|
723 DCHECK_EQ(histogram.bucket_count(), counts_.size()); |
|
724 } |
|
725 |
|
726 void Histogram::SampleSet::Accumulate(Sample value, Count count, |
|
727 size_t index) { |
|
728 DCHECK(count == 1 || count == -1); |
|
729 counts_[index] += count; |
|
730 redundant_count_ += count; |
|
731 sum_ += static_cast<int64_t>(count) * value; |
|
732 DCHECK_GE(counts_[index], 0); |
|
733 DCHECK_GE(sum_, 0); |
|
734 DCHECK_GE(redundant_count_, 0); |
|
735 } |
|
736 |
|
737 void Histogram::SampleSet::AccumulateWithLinearStats(Sample value, |
|
738 Count count, |
|
739 size_t index) { |
|
740 Accumulate(value, count, index); |
|
741 sum_squares_ += static_cast<int64_t>(count) * value * value; |
|
742 } |
|
743 |
|
744 void Histogram::SampleSet::AccumulateWithExponentialStats(Sample value, |
|
745 Count count, |
|
746 size_t index, |
|
747 bool computeExtendedStatistics) { |
|
748 Accumulate(value, count, index); |
|
749 if (computeExtendedStatistics) { |
|
750 DCHECK_GE(value, 0); |
|
751 float value_log = logf(static_cast<float>(value) + 1.0f); |
|
752 log_sum_ += count * value_log; |
|
753 log_sum_squares_ += count * value_log * value_log; |
|
754 } |
|
755 } |
|
756 |
|
757 Count Histogram::SampleSet::TotalCount() const { |
|
758 Count total = 0; |
|
759 for (Counts::const_iterator it = counts_.begin(); |
|
760 it != counts_.end(); |
|
761 ++it) { |
|
762 total += *it; |
|
763 } |
|
764 return total; |
|
765 } |
|
766 |
|
767 void Histogram::SampleSet::Add(const SampleSet& other) { |
|
768 DCHECK_EQ(counts_.size(), other.counts_.size()); |
|
769 sum_ += other.sum_; |
|
770 sum_squares_ += other.sum_squares_; |
|
771 log_sum_ += other.log_sum_; |
|
772 log_sum_squares_ += other.log_sum_squares_; |
|
773 redundant_count_ += other.redundant_count_; |
|
774 for (size_t index = 0; index < counts_.size(); ++index) |
|
775 counts_[index] += other.counts_[index]; |
|
776 } |
|
777 |
|
778 void Histogram::SampleSet::Subtract(const SampleSet& other) { |
|
779 DCHECK_EQ(counts_.size(), other.counts_.size()); |
|
780 // Note: Race conditions in snapshotting a sum may lead to (temporary) |
|
781 // negative values when snapshots are later combined (and deltas calculated). |
|
782 // As a result, we don't currently CHCEK() for positive values. |
|
783 sum_ -= other.sum_; |
|
784 sum_squares_ -= other.sum_squares_; |
|
785 log_sum_ -= other.log_sum_; |
|
786 log_sum_squares_ -= other.log_sum_squares_; |
|
787 redundant_count_ -= other.redundant_count_; |
|
788 for (size_t index = 0; index < counts_.size(); ++index) { |
|
789 counts_[index] -= other.counts_[index]; |
|
790 DCHECK_GE(counts_[index], 0); |
|
791 } |
|
792 } |
|
793 |
|
794 bool Histogram::SampleSet::Serialize(Pickle* pickle) const { |
|
795 pickle->WriteInt64(sum_); |
|
796 pickle->WriteInt64(redundant_count_); |
|
797 pickle->WriteSize(counts_.size()); |
|
798 |
|
799 for (size_t index = 0; index < counts_.size(); ++index) { |
|
800 pickle->WriteInt(counts_[index]); |
|
801 } |
|
802 |
|
803 return true; |
|
804 } |
|
805 |
|
806 bool Histogram::SampleSet::Deserialize(void** iter, const Pickle& pickle) { |
|
807 DCHECK_EQ(counts_.size(), 0u); |
|
808 DCHECK_EQ(sum_, 0); |
|
809 DCHECK_EQ(redundant_count_, 0); |
|
810 |
|
811 size_t counts_size; |
|
812 |
|
813 if (!pickle.ReadInt64(iter, &sum_) || |
|
814 !pickle.ReadInt64(iter, &redundant_count_) || |
|
815 !pickle.ReadSize(iter, &counts_size)) { |
|
816 return false; |
|
817 } |
|
818 |
|
819 if (counts_size == 0) |
|
820 return false; |
|
821 |
|
822 int count = 0; |
|
823 for (size_t index = 0; index < counts_size; ++index) { |
|
824 int i; |
|
825 if (!pickle.ReadInt(iter, &i)) |
|
826 return false; |
|
827 counts_.push_back(i); |
|
828 count += i; |
|
829 } |
|
830 |
|
831 return true; |
|
832 } |
|
833 |
|
834 //------------------------------------------------------------------------------ |
|
835 // LinearHistogram: This histogram uses a traditional set of evenly spaced |
|
836 // buckets. |
|
837 //------------------------------------------------------------------------------ |
|
838 |
|
839 LinearHistogram::~LinearHistogram() { |
|
840 } |
|
841 |
|
842 Histogram* LinearHistogram::FactoryGet(const std::string& name, |
|
843 Sample minimum, |
|
844 Sample maximum, |
|
845 size_t bucket_count, |
|
846 Flags flags) { |
|
847 Histogram* histogram(NULL); |
|
848 |
|
849 if (minimum < 1) |
|
850 minimum = 1; |
|
851 if (maximum > kSampleType_MAX - 1) |
|
852 maximum = kSampleType_MAX - 1; |
|
853 |
|
854 if (!StatisticsRecorder::FindHistogram(name, &histogram)) { |
|
855 LinearHistogram* tentative_histogram = |
|
856 new LinearHistogram(name, minimum, maximum, bucket_count); |
|
857 tentative_histogram->InitializeBucketRange(); |
|
858 tentative_histogram->SetFlags(flags); |
|
859 histogram = |
|
860 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
|
861 } |
|
862 |
|
863 DCHECK_EQ(LINEAR_HISTOGRAM, histogram->histogram_type()); |
|
864 DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count)); |
|
865 return histogram; |
|
866 } |
|
867 |
|
868 Histogram* LinearHistogram::FactoryTimeGet(const std::string& name, |
|
869 TimeDelta minimum, |
|
870 TimeDelta maximum, |
|
871 size_t bucket_count, |
|
872 Flags flags) { |
|
873 return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(), |
|
874 bucket_count, flags); |
|
875 } |
|
876 |
|
877 Histogram::ClassType LinearHistogram::histogram_type() const { |
|
878 return LINEAR_HISTOGRAM; |
|
879 } |
|
880 |
|
881 void LinearHistogram::Accumulate(Sample value, Count count, size_t index) { |
|
882 sample_.AccumulateWithLinearStats(value, count, index); |
|
883 } |
|
884 |
|
885 void LinearHistogram::SetRangeDescriptions( |
|
886 const DescriptionPair descriptions[]) { |
|
887 for (int i =0; descriptions[i].description; ++i) { |
|
888 bucket_description_[descriptions[i].sample] = descriptions[i].description; |
|
889 } |
|
890 } |
|
891 |
|
892 LinearHistogram::LinearHistogram(const std::string& name, |
|
893 Sample minimum, |
|
894 Sample maximum, |
|
895 size_t bucket_count) |
|
896 : Histogram(name, minimum >= 1 ? minimum : 1, maximum, bucket_count) { |
|
897 } |
|
898 |
|
899 LinearHistogram::LinearHistogram(const std::string& name, |
|
900 TimeDelta minimum, |
|
901 TimeDelta maximum, |
|
902 size_t bucket_count) |
|
903 : Histogram(name, minimum >= TimeDelta::FromMilliseconds(1) ? |
|
904 minimum : TimeDelta::FromMilliseconds(1), |
|
905 maximum, bucket_count) { |
|
906 } |
|
907 |
|
908 void LinearHistogram::InitializeBucketRange() { |
|
909 DCHECK_GT(declared_min(), 0); // 0 is the underflow bucket here. |
|
910 double min = declared_min(); |
|
911 double max = declared_max(); |
|
912 size_t i; |
|
913 for (i = 1; i < bucket_count(); ++i) { |
|
914 double linear_range = (min * (bucket_count() -1 - i) + max * (i - 1)) / |
|
915 (bucket_count() - 2); |
|
916 SetBucketRange(i, static_cast<int> (linear_range + 0.5)); |
|
917 } |
|
918 ResetRangeChecksum(); |
|
919 } |
|
920 |
|
921 double LinearHistogram::GetBucketSize(Count current, size_t i) const { |
|
922 DCHECK_GT(ranges(i + 1), ranges(i)); |
|
923 // Adjacent buckets with different widths would have "surprisingly" many (few) |
|
924 // samples in a histogram if we didn't normalize this way. |
|
925 double denominator = ranges(i + 1) - ranges(i); |
|
926 return current/denominator; |
|
927 } |
|
928 |
|
929 const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const { |
|
930 int range = ranges(i); |
|
931 BucketDescriptionMap::const_iterator it = bucket_description_.find(range); |
|
932 if (it == bucket_description_.end()) |
|
933 return Histogram::GetAsciiBucketRange(i); |
|
934 return it->second; |
|
935 } |
|
936 |
|
937 bool LinearHistogram::PrintEmptyBucket(size_t index) const { |
|
938 return bucket_description_.find(ranges(index)) == bucket_description_.end(); |
|
939 } |
|
940 |
|
941 |
|
942 //------------------------------------------------------------------------------ |
|
943 // This section provides implementation for BooleanHistogram. |
|
944 //------------------------------------------------------------------------------ |
|
945 |
|
946 Histogram* BooleanHistogram::FactoryGet(const std::string& name, Flags flags) { |
|
947 Histogram* histogram(NULL); |
|
948 |
|
949 if (!StatisticsRecorder::FindHistogram(name, &histogram)) { |
|
950 BooleanHistogram* tentative_histogram = new BooleanHistogram(name); |
|
951 tentative_histogram->InitializeBucketRange(); |
|
952 tentative_histogram->SetFlags(flags); |
|
953 histogram = |
|
954 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
|
955 } |
|
956 |
|
957 DCHECK_EQ(BOOLEAN_HISTOGRAM, histogram->histogram_type()); |
|
958 return histogram; |
|
959 } |
|
960 |
|
961 Histogram::ClassType BooleanHistogram::histogram_type() const { |
|
962 return BOOLEAN_HISTOGRAM; |
|
963 } |
|
964 |
|
965 void BooleanHistogram::AddBoolean(bool value) { |
|
966 Add(value ? 1 : 0); |
|
967 } |
|
968 |
|
969 BooleanHistogram::BooleanHistogram(const std::string& name) |
|
970 : LinearHistogram(name, 1, 2, 3) { |
|
971 } |
|
972 |
|
973 void |
|
974 BooleanHistogram::Accumulate(Sample value, Count count, size_t index) |
|
975 { |
|
976 // Callers will have computed index based on the non-booleanified value. |
|
977 // So we need to adjust the index manually. |
|
978 LinearHistogram::Accumulate(!!value, count, value ? 1 : 0); |
|
979 } |
|
980 |
|
981 //------------------------------------------------------------------------------ |
|
982 // FlagHistogram: |
|
983 //------------------------------------------------------------------------------ |
|
984 |
|
985 Histogram * |
|
986 FlagHistogram::FactoryGet(const std::string &name, Flags flags) |
|
987 { |
|
988 Histogram *h(nullptr); |
|
989 |
|
990 if (!StatisticsRecorder::FindHistogram(name, &h)) { |
|
991 FlagHistogram *fh = new FlagHistogram(name); |
|
992 fh->InitializeBucketRange(); |
|
993 fh->SetFlags(flags); |
|
994 size_t zero_index = fh->BucketIndex(0); |
|
995 fh->LinearHistogram::Accumulate(0, 1, zero_index); |
|
996 h = StatisticsRecorder::RegisterOrDeleteDuplicate(fh); |
|
997 } |
|
998 |
|
999 return h; |
|
1000 } |
|
1001 |
|
1002 FlagHistogram::FlagHistogram(const std::string &name) |
|
1003 : BooleanHistogram(name), mSwitched(false) { |
|
1004 } |
|
1005 |
|
1006 Histogram::ClassType |
|
1007 FlagHistogram::histogram_type() const |
|
1008 { |
|
1009 return FLAG_HISTOGRAM; |
|
1010 } |
|
1011 |
|
1012 void |
|
1013 FlagHistogram::Accumulate(Sample value, Count count, size_t index) |
|
1014 { |
|
1015 if (mSwitched) { |
|
1016 return; |
|
1017 } |
|
1018 |
|
1019 mSwitched = true; |
|
1020 DCHECK_EQ(value, 1); |
|
1021 LinearHistogram::Accumulate(value, 1, index); |
|
1022 size_t zero_index = BucketIndex(0); |
|
1023 LinearHistogram::Accumulate(0, -1, zero_index); |
|
1024 } |
|
1025 |
|
1026 void |
|
1027 FlagHistogram::AddSampleSet(const SampleSet& sample) { |
|
1028 DCHECK_EQ(bucket_count(), sample.size()); |
|
1029 // We can't be sure the SampleSet provided came from another FlagHistogram, |
|
1030 // so we take the following steps: |
|
1031 // - If our flag has already been set do nothing. |
|
1032 // - Set our flag if the following hold: |
|
1033 // - The sum of the counts in the provided SampleSet is 1. |
|
1034 // - The bucket index for that single value is the same as the index where we |
|
1035 // would place our set flag. |
|
1036 // - Otherwise, take no action. |
|
1037 |
|
1038 if (mSwitched) { |
|
1039 return; |
|
1040 } |
|
1041 |
|
1042 if (sample.sum() != 1) { |
|
1043 return; |
|
1044 } |
|
1045 |
|
1046 size_t one_index = BucketIndex(1); |
|
1047 if (sample.counts(one_index) == 1) { |
|
1048 Accumulate(1, 1, one_index); |
|
1049 } |
|
1050 } |
|
1051 //------------------------------------------------------------------------------ |
|
1052 // CustomHistogram: |
|
1053 //------------------------------------------------------------------------------ |
|
1054 |
|
1055 Histogram* CustomHistogram::FactoryGet(const std::string& name, |
|
1056 const std::vector<Sample>& custom_ranges, |
|
1057 Flags flags) { |
|
1058 Histogram* histogram(NULL); |
|
1059 |
|
1060 // Remove the duplicates in the custom ranges array. |
|
1061 std::vector<int> ranges = custom_ranges; |
|
1062 ranges.push_back(0); // Ensure we have a zero value. |
|
1063 std::sort(ranges.begin(), ranges.end()); |
|
1064 ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end()); |
|
1065 if (ranges.size() <= 1) { |
|
1066 DCHECK(false); |
|
1067 // Note that we pushed a 0 in above, so for defensive code.... |
|
1068 ranges.push_back(1); // Put in some data so we can index to [1]. |
|
1069 } |
|
1070 |
|
1071 DCHECK_LT(ranges.back(), kSampleType_MAX); |
|
1072 |
|
1073 if (!StatisticsRecorder::FindHistogram(name, &histogram)) { |
|
1074 CustomHistogram* tentative_histogram = new CustomHistogram(name, ranges); |
|
1075 tentative_histogram->InitializedCustomBucketRange(ranges); |
|
1076 tentative_histogram->SetFlags(flags); |
|
1077 histogram = |
|
1078 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram); |
|
1079 } |
|
1080 |
|
1081 DCHECK_EQ(histogram->histogram_type(), CUSTOM_HISTOGRAM); |
|
1082 DCHECK(histogram->HasConstructorArguments(ranges[1], ranges.back(), |
|
1083 ranges.size())); |
|
1084 return histogram; |
|
1085 } |
|
1086 |
|
1087 Histogram::ClassType CustomHistogram::histogram_type() const { |
|
1088 return CUSTOM_HISTOGRAM; |
|
1089 } |
|
1090 |
|
1091 CustomHistogram::CustomHistogram(const std::string& name, |
|
1092 const std::vector<Sample>& custom_ranges) |
|
1093 : Histogram(name, custom_ranges[1], custom_ranges.back(), |
|
1094 custom_ranges.size()) { |
|
1095 DCHECK_GT(custom_ranges.size(), 1u); |
|
1096 DCHECK_EQ(custom_ranges[0], 0); |
|
1097 } |
|
1098 |
|
1099 void CustomHistogram::InitializedCustomBucketRange( |
|
1100 const std::vector<Sample>& custom_ranges) { |
|
1101 DCHECK_GT(custom_ranges.size(), 1u); |
|
1102 DCHECK_EQ(custom_ranges[0], 0); |
|
1103 DCHECK_LE(custom_ranges.size(), bucket_count()); |
|
1104 for (size_t index = 0; index < custom_ranges.size(); ++index) |
|
1105 SetBucketRange(index, custom_ranges[index]); |
|
1106 ResetRangeChecksum(); |
|
1107 } |
|
1108 |
|
1109 double CustomHistogram::GetBucketSize(Count current, size_t i) const { |
|
1110 return 1; |
|
1111 } |
|
1112 |
|
1113 //------------------------------------------------------------------------------ |
|
1114 // The next section handles global (central) support for all histograms, as well |
|
1115 // as startup/teardown of this service. |
|
1116 //------------------------------------------------------------------------------ |
|
1117 |
|
1118 // This singleton instance should be started during the single threaded portion |
|
1119 // of main(), and hence it is not thread safe. It initializes globals to |
|
1120 // provide support for all future calls. |
|
1121 StatisticsRecorder::StatisticsRecorder() { |
|
1122 DCHECK(!histograms_); |
|
1123 if (lock_ == NULL) { |
|
1124 // This will leak on purpose. It's the only way to make sure we won't race |
|
1125 // against the static uninitialization of the module while one of our |
|
1126 // static methods relying on the lock get called at an inappropriate time |
|
1127 // during the termination phase. Since it's a static data member, we will |
|
1128 // leak one per process, which would be similar to the instance allocated |
|
1129 // during static initialization and released only on process termination. |
|
1130 lock_ = new base::Lock; |
|
1131 } |
|
1132 base::AutoLock auto_lock(*lock_); |
|
1133 histograms_ = new HistogramMap; |
|
1134 } |
|
1135 |
|
1136 StatisticsRecorder::~StatisticsRecorder() { |
|
1137 DCHECK(histograms_ && lock_); |
|
1138 |
|
1139 if (dump_on_exit_) { |
|
1140 std::string output; |
|
1141 WriteGraph("", &output); |
|
1142 CHROMIUM_LOG(INFO) << output; |
|
1143 } |
|
1144 // Clean up. |
|
1145 HistogramMap* histograms = NULL; |
|
1146 { |
|
1147 base::AutoLock auto_lock(*lock_); |
|
1148 histograms = histograms_; |
|
1149 histograms_ = NULL; |
|
1150 for (HistogramMap::iterator it = histograms->begin(); |
|
1151 histograms->end() != it; |
|
1152 ++it) { |
|
1153 // No other clients permanently hold Histogram references, so we |
|
1154 // have the only one and it is safe to delete it. |
|
1155 delete it->second; |
|
1156 } |
|
1157 } |
|
1158 delete histograms; |
|
1159 // We don't delete lock_ on purpose to avoid having to properly protect |
|
1160 // against it going away after we checked for NULL in the static methods. |
|
1161 } |
|
1162 |
|
1163 // static |
|
1164 bool StatisticsRecorder::IsActive() { |
|
1165 if (lock_ == NULL) |
|
1166 return false; |
|
1167 base::AutoLock auto_lock(*lock_); |
|
1168 return NULL != histograms_; |
|
1169 } |
|
1170 |
|
1171 Histogram* StatisticsRecorder::RegisterOrDeleteDuplicate(Histogram* histogram) { |
|
1172 DCHECK(histogram->HasValidRangeChecksum()); |
|
1173 if (lock_ == NULL) |
|
1174 return histogram; |
|
1175 base::AutoLock auto_lock(*lock_); |
|
1176 if (!histograms_) |
|
1177 return histogram; |
|
1178 const std::string name = histogram->histogram_name(); |
|
1179 HistogramMap::iterator it = histograms_->find(name); |
|
1180 // Avoid overwriting a previous registration. |
|
1181 if (histograms_->end() == it) { |
|
1182 (*histograms_)[name] = histogram; |
|
1183 } else { |
|
1184 delete histogram; // We already have one by this name. |
|
1185 histogram = it->second; |
|
1186 } |
|
1187 return histogram; |
|
1188 } |
|
1189 |
|
1190 // static |
|
1191 void StatisticsRecorder::WriteHTMLGraph(const std::string& query, |
|
1192 std::string* output) { |
|
1193 if (!IsActive()) |
|
1194 return; |
|
1195 output->append("<html><head><title>About Histograms"); |
|
1196 if (!query.empty()) |
|
1197 output->append(" - " + query); |
|
1198 output->append("</title>" |
|
1199 // We'd like the following no-cache... but it doesn't work. |
|
1200 // "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">" |
|
1201 "</head><body>"); |
|
1202 |
|
1203 Histograms snapshot; |
|
1204 GetSnapshot(query, &snapshot); |
|
1205 for (Histograms::iterator it = snapshot.begin(); |
|
1206 it != snapshot.end(); |
|
1207 ++it) { |
|
1208 (*it)->WriteHTMLGraph(output); |
|
1209 output->append("<br><hr><br>"); |
|
1210 } |
|
1211 output->append("</body></html>"); |
|
1212 } |
|
1213 |
|
1214 // static |
|
1215 void StatisticsRecorder::WriteGraph(const std::string& query, |
|
1216 std::string* output) { |
|
1217 if (!IsActive()) |
|
1218 return; |
|
1219 if (query.length()) |
|
1220 StringAppendF(output, "Collections of histograms for %s\n", query.c_str()); |
|
1221 else |
|
1222 output->append("Collections of all histograms\n"); |
|
1223 |
|
1224 Histograms snapshot; |
|
1225 GetSnapshot(query, &snapshot); |
|
1226 for (Histograms::iterator it = snapshot.begin(); |
|
1227 it != snapshot.end(); |
|
1228 ++it) { |
|
1229 (*it)->WriteAscii(true, "\n", output); |
|
1230 output->append("\n"); |
|
1231 } |
|
1232 } |
|
1233 |
|
1234 // static |
|
1235 void StatisticsRecorder::GetHistograms(Histograms* output) { |
|
1236 if (lock_ == NULL) |
|
1237 return; |
|
1238 base::AutoLock auto_lock(*lock_); |
|
1239 if (!histograms_) |
|
1240 return; |
|
1241 for (HistogramMap::iterator it = histograms_->begin(); |
|
1242 histograms_->end() != it; |
|
1243 ++it) { |
|
1244 DCHECK_EQ(it->first, it->second->histogram_name()); |
|
1245 output->push_back(it->second); |
|
1246 } |
|
1247 } |
|
1248 |
|
1249 bool StatisticsRecorder::FindHistogram(const std::string& name, |
|
1250 Histogram** histogram) { |
|
1251 if (lock_ == NULL) |
|
1252 return false; |
|
1253 base::AutoLock auto_lock(*lock_); |
|
1254 if (!histograms_) |
|
1255 return false; |
|
1256 HistogramMap::iterator it = histograms_->find(name); |
|
1257 if (histograms_->end() == it) |
|
1258 return false; |
|
1259 *histogram = it->second; |
|
1260 return true; |
|
1261 } |
|
1262 |
|
1263 // private static |
|
1264 void StatisticsRecorder::GetSnapshot(const std::string& query, |
|
1265 Histograms* snapshot) { |
|
1266 if (lock_ == NULL) |
|
1267 return; |
|
1268 base::AutoLock auto_lock(*lock_); |
|
1269 if (!histograms_) |
|
1270 return; |
|
1271 for (HistogramMap::iterator it = histograms_->begin(); |
|
1272 histograms_->end() != it; |
|
1273 ++it) { |
|
1274 if (it->first.find(query) != std::string::npos) |
|
1275 snapshot->push_back(it->second); |
|
1276 } |
|
1277 } |
|
1278 |
|
1279 // static |
|
1280 StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL; |
|
1281 // static |
|
1282 base::Lock* StatisticsRecorder::lock_ = NULL; |
|
1283 // static |
|
1284 bool StatisticsRecorder::dump_on_exit_ = false; |
|
1285 |
|
1286 } // namespace base |