blob: 97069ccd6b977c50c8fd1df8ca51846f2506e628 [file] [log] [blame]
[email protected]a93721e22012-01-06 02:13:281// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
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
[email protected]835d7c82010-10-14 04:38:3810#include "base/metrics/histogram.h"
initial.commitd7cae122008-07-26 21:49:3811
12#include <math.h>
[email protected]f1633932010-08-17 23:05:2813
14#include <algorithm>
initial.commitd7cae122008-07-26 21:49:3815#include <string>
16
initial.commitd7cae122008-07-26 21:49:3817#include "base/logging.h"
[email protected]567d30e2012-07-13 21:48:2918#include "base/metrics/statistics_recorder.h"
[email protected]3f383852009-04-03 18:18:5519#include "base/pickle.h"
[email protected]f1633932010-08-17 23:05:2820#include "base/stringprintf.h"
[email protected]bc581a682011-01-01 23:16:2021#include "base/synchronization/lock.h"
initial.commitd7cae122008-07-26 21:49:3822
[email protected]835d7c82010-10-14 04:38:3823namespace base {
[email protected]e1acf6f2008-10-27 20:43:3324
[email protected]98359ce2011-03-05 06:22:2425// Static table of checksums for all possible 8 bit bytes.
26const uint32 Histogram::kCrcTable[256] = {0x0, 0x77073096L, 0xee0e612cL,
270x990951baL, 0x76dc419L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0xedb8832L,
280x79dcb8a4L, 0xe0d5e91eL, 0x97d2d988L, 0x9b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
290x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, 0x1adad47dL,
300x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L, 0x646ba8c0L, 0xfd62f97aL,
310x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L,
320x4c69105eL, 0xd56041e4L, 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL,
330xa50ab56bL, 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
340x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL, 0xc8d75180L,
350xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, 0xb8bda50fL, 0x2802b89eL,
360x5f058808L, 0xc60cd9b2L, 0xb10be924L, 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL,
370xb6662d3dL, 0x76dc4190L, 0x1db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L,
380x6b6b51fL, 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0xf00f934L, 0x9609a88eL,
390xe10e9818L, 0x7f6a0dbbL, 0x86d3d2dL, 0x91646c97L, 0xe6635c01L, 0x6b6b51f4L,
400x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, 0x1b01a57bL, 0x8208f4c1L,
410xf50fc457L, 0x65b0d9c6L, 0x12b7e950L, 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL,
420x15da2d49L, 0x8cd37cf3L, 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L,
430xd4bb30e2L, 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
440x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, 0xaa0a4c5fL,
450xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L, 0xc90c2086L, 0x5768b525L,
460x206f85b3L, 0xb966d409L, 0xce61e49fL, 0x5edef90eL, 0x29d9c998L, 0xb0d09822L,
470xc7d7a8b4L, 0x59b33d17L, 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L,
480x9abfb3b6L, 0x3b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x4db2615L,
490x73dc1683L, 0xe3630b12L, 0x94643b84L, 0xd6d6a3eL, 0x7a6a5aa8L, 0xe40ecf0bL,
500x9309ff9dL, 0xa00ae27L, 0x7d079eb1L, 0xf00f9344L, 0x8708a3d2L, 0x1e01f268L,
510x6906c2feL, 0xf762575dL, 0x806567cbL, 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L,
520x89d32be0L, 0x10da7a5aL, 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L,
530x60b08ed5L, 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
540xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL, 0x36034af6L,
550x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, 0x4669be79L, 0xcb61b38cL,
560xbc66831aL, 0x256fd2a0L, 0x5268e236L, 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L,
570x5505262fL, 0xc5ba3bbeL, 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L,
580xb5d0cf31L, 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
590x26d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x5005713L, 0x95bf4a82L,
600xe2b87a14L, 0x7bb12baeL, 0xcb61b38L, 0x92d28e9bL, 0xe5d5be0dL, 0x7cdcefb7L,
610xbdbdf21L, 0x86d3d2d4L, 0xf1d4e242L, 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL,
620xf6b9265bL, 0x6fb077e1L, 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL,
630x11010b5cL, 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
640xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, 0x4969474dL,
650x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, 0x37d83bf0L, 0xa9bcae53L,
660xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L,
670x24b4a3a6L, 0xbad03605L, 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL,
680xc4614ab8L, 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
690x2d02ef8dL,
70};
71
initial.commitd7cae122008-07-26 21:49:3872typedef Histogram::Count Count;
73
[email protected]b122c0c2011-02-23 22:31:1874// static
[email protected]9fce5f02011-03-02 08:04:5775const size_t Histogram::kBucketCount_MAX = 16384u;
[email protected]b122c0c2011-02-23 22:31:1876
[email protected]81ce9f3b2011-04-05 04:48:5377Histogram* Histogram::FactoryGet(const std::string& name,
78 Sample minimum,
79 Sample maximum,
80 size_t bucket_count,
81 Flags flags) {
[email protected]e8829a192009-12-06 00:09:3782 // Defensive code.
[email protected]2ef3748f2010-10-19 17:33:2883 if (minimum < 1)
[email protected]e8829a192009-12-06 00:09:3784 minimum = 1;
[email protected]2ef3748f2010-10-19 17:33:2885 if (maximum > kSampleType_MAX - 1)
[email protected]e8829a192009-12-06 00:09:3786 maximum = kSampleType_MAX - 1;
87
[email protected]a93721e22012-01-06 02:13:2888 DCHECK_GT(maximum, minimum);
89 DCHECK_GT((Sample) bucket_count, 2);
90 DCHECK_LE((Sample) bucket_count, maximum - minimum + 2);
91
[email protected]993ae742012-07-18 18:06:3092 Histogram* histogram = StatisticsRecorder::FindHistogram(name);
93 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:5394 // Extra variable is not needed... but this keeps this section basically
95 // identical to other derived classes in this file (and compiler will
96 // optimize away the extra variable.
97 // To avoid racy destruction at shutdown, the following will be leaked.
98 Histogram* tentative_histogram =
99 new Histogram(name, minimum, maximum, bucket_count);
100 tentative_histogram->InitializeBucketRange();
101 tentative_histogram->SetFlags(flags);
102 histogram =
103 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37104 }
105
[email protected]2ef3748f2010-10-19 17:33:28106 DCHECK_EQ(HISTOGRAM, histogram->histogram_type());
[email protected]e8829a192009-12-06 00:09:37107 DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count));
[email protected]e8829a192009-12-06 00:09:37108 return histogram;
109}
110
[email protected]81ce9f3b2011-04-05 04:48:53111Histogram* Histogram::FactoryTimeGet(const std::string& name,
112 TimeDelta minimum,
113 TimeDelta maximum,
114 size_t bucket_count,
115 Flags flags) {
[email protected]2753b392009-12-28 06:59:52116 return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
117 bucket_count, flags);
[email protected]e8829a192009-12-06 00:09:37118}
119
[email protected]b404b1f2011-10-16 07:29:17120TimeTicks Histogram::DebugNow() {
121#ifndef NDEBUG
122 return TimeTicks::Now();
123#else
124 return TimeTicks();
125#endif
126}
127
initial.commitd7cae122008-07-26 21:49:38128void Histogram::Add(int value) {
[email protected]2ef3748f2010-10-19 17:33:28129 if (value > kSampleType_MAX - 1)
initial.commitd7cae122008-07-26 21:49:38130 value = kSampleType_MAX - 1;
initial.commitd7cae122008-07-26 21:49:38131 if (value < 0)
132 value = 0;
133 size_t index = BucketIndex(value);
[email protected]2ef3748f2010-10-19 17:33:28134 DCHECK_GE(value, ranges(index));
135 DCHECK_LT(value, ranges(index + 1));
initial.commitd7cae122008-07-26 21:49:38136 Accumulate(value, 1, index);
137}
138
[email protected]5d91c9e2010-07-28 17:25:28139void Histogram::AddBoolean(bool value) {
140 DCHECK(false);
141}
142
[email protected]55e57d42009-02-25 06:10:17143void Histogram::AddSampleSet(const SampleSet& sample) {
144 sample_.Add(sample);
145}
146
[email protected]5d91c9e2010-07-28 17:25:28147void Histogram::SetRangeDescriptions(const DescriptionPair descriptions[]) {
148 DCHECK(false);
149}
150
initial.commitd7cae122008-07-26 21:49:38151// The following methods provide a graphical histogram display.
152void Histogram::WriteHTMLGraph(std::string* output) const {
153 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc.
154 output->append("<PRE>");
155 WriteAscii(true, "<br>", output);
156 output->append("</PRE>");
157}
158
159void Histogram::WriteAscii(bool graph_it, const std::string& newline,
160 std::string* output) const {
161 // Get local (stack) copies of all effectively volatile class data so that we
162 // are consistent across our output activities.
163 SampleSet snapshot;
164 SnapshotSample(&snapshot);
165 Count sample_count = snapshot.TotalCount();
166
167 WriteAsciiHeader(snapshot, sample_count, output);
168 output->append(newline);
169
170 // Prepare to normalize graphical rendering of bucket contents.
171 double max_size = 0;
172 if (graph_it)
173 max_size = GetPeakBucketSize(snapshot);
174
175 // Calculate space needed to print bucket range numbers. Leave room to print
176 // nearly the largest bucket range without sliding over the histogram.
[email protected]e2951cf2008-09-24 23:51:25177 size_t largest_non_empty_bucket = bucket_count() - 1;
178 while (0 == snapshot.counts(largest_non_empty_bucket)) {
initial.commitd7cae122008-07-26 21:49:38179 if (0 == largest_non_empty_bucket)
180 break; // All buckets are empty.
[email protected]55e57d42009-02-25 06:10:17181 --largest_non_empty_bucket;
initial.commitd7cae122008-07-26 21:49:38182 }
183
184 // Calculate largest print width needed for any of our bucket range displays.
185 size_t print_width = 1;
[email protected]e2951cf2008-09-24 23:51:25186 for (size_t i = 0; i < bucket_count(); ++i) {
initial.commitd7cae122008-07-26 21:49:38187 if (snapshot.counts(i)) {
188 size_t width = GetAsciiBucketRange(i).size() + 1;
189 if (width > print_width)
190 print_width = width;
191 }
192 }
193
194 int64 remaining = sample_count;
195 int64 past = 0;
196 // Output the actual histogram graph.
[email protected]55e57d42009-02-25 06:10:17197 for (size_t i = 0; i < bucket_count(); ++i) {
initial.commitd7cae122008-07-26 21:49:38198 Count current = snapshot.counts(i);
199 if (!current && !PrintEmptyBucket(i))
200 continue;
201 remaining -= current;
[email protected]34b2b002009-11-20 06:53:28202 std::string range = GetAsciiBucketRange(i);
203 output->append(range);
204 for (size_t j = 0; range.size() + j < print_width + 1; ++j)
205 output->push_back(' ');
[email protected]e2951cf2008-09-24 23:51:25206 if (0 == current && i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) {
207 while (i < bucket_count() - 1 && 0 == snapshot.counts(i + 1))
[email protected]55e57d42009-02-25 06:10:17208 ++i;
initial.commitd7cae122008-07-26 21:49:38209 output->append("... ");
210 output->append(newline);
211 continue; // No reason to plot emptiness.
212 }
213 double current_size = GetBucketSize(current, i);
214 if (graph_it)
215 WriteAsciiBucketGraph(current_size, max_size, output);
216 WriteAsciiBucketContext(past, current, remaining, i, output);
217 output->append(newline);
218 past += current;
219 }
[email protected]2ef3748f2010-10-19 17:33:28220 DCHECK_EQ(sample_count, past);
initial.commitd7cae122008-07-26 21:49:38221}
222
[email protected]55e57d42009-02-25 06:10:17223// static
224std::string Histogram::SerializeHistogramInfo(const Histogram& histogram,
225 const SampleSet& snapshot) {
[email protected]2ef3748f2010-10-19 17:33:28226 DCHECK_NE(NOT_VALID_IN_RENDERER, histogram.histogram_type());
[email protected]55e57d42009-02-25 06:10:17227
[email protected]e8829a192009-12-06 00:09:37228 Pickle pickle;
[email protected]55e57d42009-02-25 06:10:17229 pickle.WriteString(histogram.histogram_name());
230 pickle.WriteInt(histogram.declared_min());
231 pickle.WriteInt(histogram.declared_max());
[email protected]ee028ac2012-03-14 00:08:02232 pickle.WriteUInt64(histogram.bucket_count());
[email protected]98359ce2011-03-05 06:22:24233 pickle.WriteUInt32(histogram.range_checksum());
[email protected]55e57d42009-02-25 06:10:17234 pickle.WriteInt(histogram.histogram_type());
[email protected]1f4fc8e8c2010-01-02 00:46:41235 pickle.WriteInt(histogram.flags());
[email protected]55e57d42009-02-25 06:10:17236
237 snapshot.Serialize(&pickle);
[email protected]cd56dff2011-11-13 04:19:15238
239 histogram.SerializeRanges(&pickle);
240
[email protected]55e57d42009-02-25 06:10:17241 return std::string(static_cast<const char*>(pickle.data()), pickle.size());
242}
243
244// static
[email protected]55e57d42009-02-25 06:10:17245bool Histogram::DeserializeHistogramInfo(const std::string& histogram_info) {
246 if (histogram_info.empty()) {
[email protected]cd56dff2011-11-13 04:19:15247 return false;
[email protected]55e57d42009-02-25 06:10:17248 }
249
250 Pickle pickle(histogram_info.data(),
251 static_cast<int>(histogram_info.size()));
[email protected]93a41d72010-11-03 23:36:24252 std::string histogram_name;
[email protected]55e57d42009-02-25 06:10:17253 int declared_min;
254 int declared_max;
[email protected]ee028ac2012-03-14 00:08:02255 uint64 bucket_count;
[email protected]98359ce2011-03-05 06:22:24256 uint32 range_checksum;
[email protected]55e57d42009-02-25 06:10:17257 int histogram_type;
[email protected]2753b392009-12-28 06:59:52258 int pickle_flags;
[email protected]55e57d42009-02-25 06:10:17259 SampleSet sample;
260
[email protected]ce208f872012-03-07 20:42:56261 PickleIterator iter(pickle);
262 if (!iter.ReadString(&histogram_name) ||
263 !iter.ReadInt(&declared_min) ||
264 !iter.ReadInt(&declared_max) ||
[email protected]ee028ac2012-03-14 00:08:02265 !iter.ReadUInt64(&bucket_count) ||
[email protected]ce208f872012-03-07 20:42:56266 !iter.ReadUInt32(&range_checksum) ||
267 !iter.ReadInt(&histogram_type) ||
268 !iter.ReadInt(&pickle_flags) ||
269 !sample.Histogram::SampleSet::Deserialize(&iter)) {
[email protected]a42d4632011-10-26 21:48:00270 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name;
[email protected]55e57d42009-02-25 06:10:17271 return false;
272 }
[email protected]cd56dff2011-11-13 04:19:15273
[email protected]1f4fc8e8c2010-01-02 00:46:41274 DCHECK(pickle_flags & kIPCSerializationSourceFlag);
[email protected]86440f52009-12-31 05:17:23275 // Since these fields may have come from an untrusted renderer, do additional
276 // checks above and beyond those in Histogram::Initialize()
277 if (declared_max <= 0 || declared_min <= 0 || declared_max < declared_min ||
278 INT_MAX / sizeof(Count) <= bucket_count || bucket_count < 2) {
[email protected]a42d4632011-10-26 21:48:00279 DLOG(ERROR) << "Values error decoding Histogram: " << histogram_name;
[email protected]86440f52009-12-31 05:17:23280 return false;
281 }
282
[email protected]2753b392009-12-28 06:59:52283 Flags flags = static_cast<Flags>(pickle_flags & ~kIPCSerializationSourceFlag);
[email protected]55e57d42009-02-25 06:10:17284
[email protected]2ef3748f2010-10-19 17:33:28285 DCHECK_NE(NOT_VALID_IN_RENDERER, histogram_type);
[email protected]55e57d42009-02-25 06:10:17286
[email protected]81ce9f3b2011-04-05 04:48:53287 Histogram* render_histogram(NULL);
[email protected]e8829a192009-12-06 00:09:37288
[email protected]a764bf5e2010-06-02 21:31:44289 if (histogram_type == HISTOGRAM) {
[email protected]2753b392009-12-28 06:59:52290 render_histogram = Histogram::FactoryGet(
291 histogram_name, declared_min, declared_max, bucket_count, flags);
[email protected]e8829a192009-12-06 00:09:37292 } else if (histogram_type == LINEAR_HISTOGRAM) {
[email protected]2753b392009-12-28 06:59:52293 render_histogram = LinearHistogram::FactoryGet(
294 histogram_name, declared_min, declared_max, bucket_count, flags);
[email protected]e8829a192009-12-06 00:09:37295 } else if (histogram_type == BOOLEAN_HISTOGRAM) {
[email protected]2753b392009-12-28 06:59:52296 render_histogram = BooleanHistogram::FactoryGet(histogram_name, flags);
[email protected]cd56dff2011-11-13 04:19:15297 } else if (histogram_type == CUSTOM_HISTOGRAM) {
298 std::vector<Histogram::Sample> sample_ranges(bucket_count);
[email protected]ce208f872012-03-07 20:42:56299 if (!CustomHistogram::DeserializeRanges(&iter, &sample_ranges)) {
[email protected]cd56dff2011-11-13 04:19:15300 DLOG(ERROR) << "Pickle error decoding ranges: " << histogram_name;
301 return false;
302 }
303 render_histogram =
304 CustomHistogram::FactoryGet(histogram_name, sample_ranges, flags);
[email protected]e8829a192009-12-06 00:09:37305 } else {
[email protected]a42d4632011-10-26 21:48:00306 DLOG(ERROR) << "Error Deserializing Histogram Unknown histogram_type: "
307 << histogram_type;
[email protected]e8829a192009-12-06 00:09:37308 return false;
[email protected]55e57d42009-02-25 06:10:17309 }
310
[email protected]2ef3748f2010-10-19 17:33:28311 DCHECK_EQ(render_histogram->declared_min(), declared_min);
312 DCHECK_EQ(render_histogram->declared_max(), declared_max);
313 DCHECK_EQ(render_histogram->bucket_count(), bucket_count);
[email protected]93a41d72010-11-03 23:36:24314 DCHECK_EQ(render_histogram->range_checksum(), range_checksum);
[email protected]2ef3748f2010-10-19 17:33:28315 DCHECK_EQ(render_histogram->histogram_type(), histogram_type);
[email protected]55e57d42009-02-25 06:10:17316
[email protected]e8829a192009-12-06 00:09:37317 if (render_histogram->flags() & kIPCSerializationSourceFlag) {
[email protected]2ef3748f2010-10-19 17:33:28318 DVLOG(1) << "Single process mode, histogram observed and not copied: "
319 << histogram_name;
[email protected]e8829a192009-12-06 00:09:37320 } else {
[email protected]2ef3748f2010-10-19 17:33:28321 DCHECK_EQ(flags & render_histogram->flags(), flags);
[email protected]e8829a192009-12-06 00:09:37322 render_histogram->AddSampleSet(sample);
[email protected]55e57d42009-02-25 06:10:17323 }
324
325 return true;
326}
327
initial.commitd7cae122008-07-26 21:49:38328//------------------------------------------------------------------------------
[email protected]93a41d72010-11-03 23:36:24329// Methods for the validating a sample and a related histogram.
330//------------------------------------------------------------------------------
331
332Histogram::Inconsistencies Histogram::FindCorruption(
333 const SampleSet& snapshot) const {
334 int inconsistencies = NO_INCONSISTENCIES;
335 Sample previous_range = -1; // Bottom range is always 0.
[email protected]93a41d72010-11-03 23:36:24336 int64 count = 0;
337 for (size_t index = 0; index < bucket_count(); ++index) {
338 count += snapshot.counts(index);
339 int new_range = ranges(index);
[email protected]93a41d72010-11-03 23:36:24340 if (previous_range >= new_range)
341 inconsistencies |= BUCKET_ORDER_ERROR;
342 previous_range = new_range;
343 }
344
[email protected]98359ce2011-03-05 06:22:24345 if (!HasValidRangeChecksum())
[email protected]93a41d72010-11-03 23:36:24346 inconsistencies |= RANGE_CHECKSUM_ERROR;
347
348 int64 delta64 = snapshot.redundant_count() - count;
349 if (delta64 != 0) {
350 int delta = static_cast<int>(delta64);
351 if (delta != delta64)
352 delta = INT_MAX; // Flag all giant errors as INT_MAX.
353 // Since snapshots of histograms are taken asynchronously relative to
354 // sampling (and snapped from different threads), it is pretty likely that
355 // we'll catch a redundant count that doesn't match the sample count. We
356 // allow for a certain amount of slop before flagging this as an
357 // inconsistency. Even with an inconsistency, we'll snapshot it again (for
358 // UMA in about a half hour, so we'll eventually get the data, if it was
359 // not the result of a corruption. If histograms show that 1 is "too tight"
360 // then we may try to use 2 or 3 for this slop value.
361 const int kCommonRaceBasedCountMismatch = 1;
362 if (delta > 0) {
363 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta);
364 if (delta > kCommonRaceBasedCountMismatch)
365 inconsistencies |= COUNT_HIGH_ERROR;
366 } else {
367 DCHECK_GT(0, delta);
368 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta);
369 if (-delta > kCommonRaceBasedCountMismatch)
370 inconsistencies |= COUNT_LOW_ERROR;
371 }
372 }
373 return static_cast<Inconsistencies>(inconsistencies);
374}
375
[email protected]7cf40912010-12-09 18:25:03376Histogram::ClassType Histogram::histogram_type() const {
377 return HISTOGRAM;
378}
379
380Histogram::Sample Histogram::ranges(size_t i) const {
[email protected]908de522011-10-20 00:55:00381 return cached_ranges_->ranges(i);
[email protected]7cf40912010-12-09 18:25:03382}
383
384size_t Histogram::bucket_count() const {
385 return bucket_count_;
386}
387
[email protected]b7d08202011-01-25 17:29:39388// Do a safe atomic snapshot of sample data.
389// This implementation assumes we are on a safe single thread.
390void Histogram::SnapshotSample(SampleSet* sample) const {
391 // Note locking not done in this version!!!
392 *sample = sample_;
393}
394
395bool Histogram::HasConstructorArguments(Sample minimum,
396 Sample maximum,
397 size_t bucket_count) {
398 return ((minimum == declared_min_) && (maximum == declared_max_) &&
399 (bucket_count == bucket_count_));
400}
401
402bool Histogram::HasConstructorTimeDeltaArguments(TimeDelta minimum,
403 TimeDelta maximum,
404 size_t bucket_count) {
405 return ((minimum.InMilliseconds() == declared_min_) &&
406 (maximum.InMilliseconds() == declared_max_) &&
407 (bucket_count == bucket_count_));
408}
409
[email protected]98359ce2011-03-05 06:22:24410bool Histogram::HasValidRangeChecksum() const {
411 return CalculateRangeChecksum() == range_checksum_;
412}
413
[email protected]b7d08202011-01-25 17:29:39414Histogram::Histogram(const std::string& name, Sample minimum,
415 Sample maximum, size_t bucket_count)
416 : histogram_name_(name),
417 declared_min_(minimum),
418 declared_max_(maximum),
419 bucket_count_(bucket_count),
420 flags_(kNoFlags),
[email protected]908de522011-10-20 00:55:00421 cached_ranges_(new CachedRanges(bucket_count + 1, 0)),
[email protected]b7d08202011-01-25 17:29:39422 range_checksum_(0),
423 sample_() {
424 Initialize();
425}
426
427Histogram::Histogram(const std::string& name, TimeDelta minimum,
428 TimeDelta maximum, size_t bucket_count)
429 : histogram_name_(name),
430 declared_min_(static_cast<int> (minimum.InMilliseconds())),
431 declared_max_(static_cast<int> (maximum.InMilliseconds())),
432 bucket_count_(bucket_count),
433 flags_(kNoFlags),
[email protected]908de522011-10-20 00:55:00434 cached_ranges_(new CachedRanges(bucket_count + 1, 0)),
[email protected]b7d08202011-01-25 17:29:39435 range_checksum_(0),
436 sample_() {
437 Initialize();
438}
439
440Histogram::~Histogram() {
441 if (StatisticsRecorder::dump_on_exit()) {
442 std::string output;
443 WriteAscii(true, "\n", &output);
[email protected]a42d4632011-10-26 21:48:00444 DLOG(INFO) << output;
[email protected]b7d08202011-01-25 17:29:39445 }
446
447 // Just to make sure most derived class did this properly...
448 DCHECK(ValidateBucketRanges());
[email protected]7767bdb2011-03-02 23:38:00449}
450
[email protected]cd56dff2011-11-13 04:19:15451bool Histogram::SerializeRanges(Pickle* pickle) const {
452 return true;
453}
454
[email protected]b7d08202011-01-25 17:29:39455// Calculate what range of values are held in each bucket.
456// We have to be careful that we don't pick a ratio between starting points in
457// consecutive buckets that is sooo small, that the integer bounds are the same
458// (effectively making one bucket get no values). We need to avoid:
[email protected]908de522011-10-20 00:55:00459// ranges(i) == ranges(i + 1)
[email protected]b7d08202011-01-25 17:29:39460// To avoid that, we just do a fine-grained bucket width as far as we need to
461// until we get a ratio that moves us along at least 2 units at a time. From
462// that bucket onward we do use the exponential growth of buckets.
463void Histogram::InitializeBucketRange() {
464 double log_max = log(static_cast<double>(declared_max()));
465 double log_ratio;
466 double log_next;
467 size_t bucket_index = 1;
468 Sample current = declared_min();
469 SetBucketRange(bucket_index, current);
470 while (bucket_count() > ++bucket_index) {
471 double log_current;
472 log_current = log(static_cast<double>(current));
473 // Calculate the count'th root of the range.
474 log_ratio = (log_max - log_current) / (bucket_count() - bucket_index);
475 // See where the next bucket would start.
476 log_next = log_current + log_ratio;
477 int next;
478 next = static_cast<int>(floor(exp(log_next) + 0.5));
479 if (next > current)
480 current = next;
481 else
482 ++current; // Just do a narrow bucket, and keep trying.
483 SetBucketRange(bucket_index, current);
484 }
485 ResetRangeChecksum();
486
487 DCHECK_EQ(bucket_count(), bucket_index);
488}
489
[email protected]98359ce2011-03-05 06:22:24490bool Histogram::PrintEmptyBucket(size_t index) const {
491 return true;
492}
493
[email protected]b7d08202011-01-25 17:29:39494size_t Histogram::BucketIndex(Sample value) const {
495 // Use simple binary search. This is very general, but there are better
496 // approaches if we knew that the buckets were linearly distributed.
497 DCHECK_LE(ranges(0), value);
498 DCHECK_GT(ranges(bucket_count()), value);
499 size_t under = 0;
500 size_t over = bucket_count();
501 size_t mid;
502
503 do {
504 DCHECK_GE(over, under);
[email protected]b122c0c2011-02-23 22:31:18505 mid = under + (over - under)/2;
[email protected]b7d08202011-01-25 17:29:39506 if (mid == under)
507 break;
508 if (ranges(mid) <= value)
509 under = mid;
510 else
511 over = mid;
512 } while (true);
513
514 DCHECK_LE(ranges(mid), value);
[email protected]b122c0c2011-02-23 22:31:18515 CHECK_GT(ranges(mid+1), value);
[email protected]b7d08202011-01-25 17:29:39516 return mid;
517}
518
519// Use the actual bucket widths (like a linear histogram) until the widths get
520// over some transition value, and then use that transition width. Exponentials
521// get so big so fast (and we don't expect to see a lot of entries in the large
522// buckets), so we need this to make it possible to see what is going on and
523// not have 0-graphical-height buckets.
524double Histogram::GetBucketSize(Count current, size_t i) const {
525 DCHECK_GT(ranges(i + 1), ranges(i));
526 static const double kTransitionWidth = 5;
527 double denominator = ranges(i + 1) - ranges(i);
528 if (denominator > kTransitionWidth)
529 denominator = kTransitionWidth; // Stop trying to normalize.
530 return current/denominator;
531}
532
533void Histogram::ResetRangeChecksum() {
534 range_checksum_ = CalculateRangeChecksum();
535}
536
537const std::string Histogram::GetAsciiBucketRange(size_t i) const {
538 std::string result;
539 if (kHexRangePrintingFlag & flags_)
540 StringAppendF(&result, "%#x", ranges(i));
541 else
542 StringAppendF(&result, "%d", ranges(i));
543 return result;
544}
545
546// Update histogram data with new sample.
547void Histogram::Accumulate(Sample value, Count count, size_t index) {
548 // Note locking not done in this version!!!
549 sample_.Accumulate(value, count, index);
550}
551
552void Histogram::SetBucketRange(size_t i, Sample value) {
553 DCHECK_GT(bucket_count_, i);
[email protected]c89b2442011-05-26 14:28:27554 DCHECK_GE(value, 0);
[email protected]908de522011-10-20 00:55:00555 cached_ranges_->SetBucketRange(i, value);
[email protected]b7d08202011-01-25 17:29:39556}
557
558bool Histogram::ValidateBucketRanges() const {
559 // Standard assertions that all bucket ranges should satisfy.
[email protected]908de522011-10-20 00:55:00560 DCHECK_EQ(bucket_count_ + 1, cached_ranges_->size());
561 DCHECK_EQ(0, ranges(0));
562 DCHECK_EQ(declared_min(), ranges(1));
563 DCHECK_EQ(declared_max(), ranges(bucket_count_ - 1));
564 DCHECK_EQ(kSampleType_MAX, ranges(bucket_count_));
[email protected]b7d08202011-01-25 17:29:39565 return true;
566}
567
[email protected]98359ce2011-03-05 06:22:24568uint32 Histogram::CalculateRangeChecksum() const {
[email protected]908de522011-10-20 00:55:00569 DCHECK_EQ(cached_ranges_->size(), bucket_count() + 1);
570 // Seed checksum.
571 uint32 checksum = static_cast<uint32>(cached_ranges_->size());
[email protected]98359ce2011-03-05 06:22:24572 for (size_t index = 0; index < bucket_count(); ++index)
573 checksum = Crc32(checksum, ranges(index));
574 return checksum;
575}
576
[email protected]b7d08202011-01-25 17:29:39577void Histogram::Initialize() {
578 sample_.Resize(*this);
579 if (declared_min_ < 1)
580 declared_min_ = 1;
581 if (declared_max_ > kSampleType_MAX - 1)
582 declared_max_ = kSampleType_MAX - 1;
583 DCHECK_LE(declared_min_, declared_max_);
584 DCHECK_GT(bucket_count_, 1u);
[email protected]b122c0c2011-02-23 22:31:18585 CHECK_LT(bucket_count_, kBucketCount_MAX);
[email protected]b7d08202011-01-25 17:29:39586 size_t maximal_bucket_count = declared_max_ - declared_min_ + 2;
587 DCHECK_LE(bucket_count_, maximal_bucket_count);
[email protected]908de522011-10-20 00:55:00588 DCHECK_EQ(0, ranges(0));
589 cached_ranges_->SetBucketRange(bucket_count_, kSampleType_MAX);
[email protected]b7d08202011-01-25 17:29:39590}
591
[email protected]98359ce2011-03-05 06:22:24592// We generate the CRC-32 using the low order bits to select whether to XOR in
593// the reversed polynomial 0xedb88320L. This is nice and simple, and allows us
594// to keep the quotient in a uint32. Since we're not concerned about the nature
595// of corruptions (i.e., we don't care about bit sequencing, since we are
596// handling memory changes, which are more grotesque) so we don't bother to
597// get the CRC correct for big-endian vs little-ending calculations. All we
598// need is a nice hash, that tends to depend on all the bits of the sample, with
599// very little chance of changes in one place impacting changes in another
600// place.
601uint32 Histogram::Crc32(uint32 sum, Histogram::Sample range) {
602 const bool kUseRealCrc = true; // TODO(jar): Switch to false and watch stats.
603 if (kUseRealCrc) {
604 union {
605 Histogram::Sample range;
606 unsigned char bytes[sizeof(Histogram::Sample)];
607 } converter;
608 converter.range = range;
609 for (size_t i = 0; i < sizeof(converter); ++i)
610 sum = kCrcTable[(sum & 0xff) ^ converter.bytes[i]] ^ (sum >> 8);
611 } else {
612 // Use hash techniques provided in ReallyFastHash, except we don't care
613 // about "avalanching" (which would worsten the hash, and add collisions),
614 // and we don't care about edge cases since we have an even number of bytes.
615 union {
616 Histogram::Sample range;
617 uint16 ints[sizeof(Histogram::Sample) / 2];
618 } converter;
619 DCHECK_EQ(sizeof(Histogram::Sample), sizeof(converter));
620 converter.range = range;
621 sum += converter.ints[0];
622 sum = (sum << 16) ^ sum ^ (static_cast<uint32>(converter.ints[1]) << 11);
623 sum += sum >> 11;
[email protected]b7d08202011-01-25 17:29:39624 }
[email protected]98359ce2011-03-05 06:22:24625 return sum;
[email protected]b7d08202011-01-25 17:29:39626}
627
628//------------------------------------------------------------------------------
629// Private methods
630
631double Histogram::GetPeakBucketSize(const SampleSet& snapshot) const {
632 double max = 0;
633 for (size_t i = 0; i < bucket_count() ; ++i) {
634 double current_size = GetBucketSize(snapshot.counts(i), i);
635 if (current_size > max)
636 max = current_size;
637 }
638 return max;
639}
640
641void Histogram::WriteAsciiHeader(const SampleSet& snapshot,
642 Count sample_count,
643 std::string* output) const {
644 StringAppendF(output,
645 "Histogram: %s recorded %d samples",
646 histogram_name().c_str(),
647 sample_count);
648 if (0 == sample_count) {
649 DCHECK_EQ(snapshot.sum(), 0);
650 } else {
651 double average = static_cast<float>(snapshot.sum()) / sample_count;
[email protected]b7d08202011-01-25 17:29:39652
[email protected]0704a972011-03-24 00:30:27653 StringAppendF(output, ", average = %.1f", average);
[email protected]b7d08202011-01-25 17:29:39654 }
655 if (flags_ & ~kHexRangePrintingFlag)
656 StringAppendF(output, " (flags = 0x%x)", flags_ & ~kHexRangePrintingFlag);
657}
658
659void Histogram::WriteAsciiBucketContext(const int64 past,
660 const Count current,
661 const int64 remaining,
662 const size_t i,
663 std::string* output) const {
664 double scaled_sum = (past + current + remaining) / 100.0;
665 WriteAsciiBucketValue(current, scaled_sum, output);
666 if (0 < i) {
667 double percentage = past / scaled_sum;
668 StringAppendF(output, " {%3.1f%%}", percentage);
669 }
670}
671
672void Histogram::WriteAsciiBucketValue(Count current, double scaled_sum,
673 std::string* output) const {
674 StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum);
675}
676
677void Histogram::WriteAsciiBucketGraph(double current_size, double max_size,
678 std::string* output) const {
679 const int k_line_length = 72; // Maximal horizontal width of graph.
680 int x_count = static_cast<int>(k_line_length * (current_size / max_size)
681 + 0.5);
682 int x_remainder = k_line_length - x_count;
683
684 while (0 < x_count--)
685 output->append("-");
686 output->append("O");
687 while (0 < x_remainder--)
688 output->append(" ");
689}
690
[email protected]93a41d72010-11-03 23:36:24691//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38692// Methods for the Histogram::SampleSet class
693//------------------------------------------------------------------------------
694
695Histogram::SampleSet::SampleSet()
696 : counts_(),
697 sum_(0),
[email protected]93a41d72010-11-03 23:36:24698 redundant_count_(0) {
initial.commitd7cae122008-07-26 21:49:38699}
700
[email protected]d4799a32010-09-28 22:54:58701Histogram::SampleSet::~SampleSet() {
702}
703
initial.commitd7cae122008-07-26 21:49:38704void Histogram::SampleSet::Resize(const Histogram& histogram) {
705 counts_.resize(histogram.bucket_count(), 0);
706}
707
708void Histogram::SampleSet::CheckSize(const Histogram& histogram) const {
[email protected]2ef3748f2010-10-19 17:33:28709 DCHECK_EQ(histogram.bucket_count(), counts_.size());
initial.commitd7cae122008-07-26 21:49:38710}
711
712
713void Histogram::SampleSet::Accumulate(Sample value, Count count,
714 size_t index) {
715 DCHECK(count == 1 || count == -1);
716 counts_[index] += count;
717 sum_ += count * value;
[email protected]93a41d72010-11-03 23:36:24718 redundant_count_ += count;
[email protected]2753b392009-12-28 06:59:52719 DCHECK_GE(counts_[index], 0);
720 DCHECK_GE(sum_, 0);
[email protected]93a41d72010-11-03 23:36:24721 DCHECK_GE(redundant_count_, 0);
initial.commitd7cae122008-07-26 21:49:38722}
723
724Count Histogram::SampleSet::TotalCount() const {
725 Count total = 0;
726 for (Counts::const_iterator it = counts_.begin();
727 it != counts_.end();
[email protected]55e57d42009-02-25 06:10:17728 ++it) {
initial.commitd7cae122008-07-26 21:49:38729 total += *it;
730 }
731 return total;
732}
733
734void Histogram::SampleSet::Add(const SampleSet& other) {
[email protected]2ef3748f2010-10-19 17:33:28735 DCHECK_EQ(counts_.size(), other.counts_.size());
initial.commitd7cae122008-07-26 21:49:38736 sum_ += other.sum_;
[email protected]93a41d72010-11-03 23:36:24737 redundant_count_ += other.redundant_count_;
[email protected]55e57d42009-02-25 06:10:17738 for (size_t index = 0; index < counts_.size(); ++index)
initial.commitd7cae122008-07-26 21:49:38739 counts_[index] += other.counts_[index];
740}
741
742void Histogram::SampleSet::Subtract(const SampleSet& other) {
[email protected]2ef3748f2010-10-19 17:33:28743 DCHECK_EQ(counts_.size(), other.counts_.size());
[email protected]0704a972011-03-24 00:30:27744 // Note: Race conditions in snapshotting a sum may lead to (temporary)
745 // negative values when snapshots are later combined (and deltas calculated).
746 // As a result, we don't currently CHCEK() for positive values.
initial.commitd7cae122008-07-26 21:49:38747 sum_ -= other.sum_;
[email protected]93a41d72010-11-03 23:36:24748 redundant_count_ -= other.redundant_count_;
[email protected]55e57d42009-02-25 06:10:17749 for (size_t index = 0; index < counts_.size(); ++index) {
initial.commitd7cae122008-07-26 21:49:38750 counts_[index] -= other.counts_[index];
[email protected]2753b392009-12-28 06:59:52751 DCHECK_GE(counts_[index], 0);
initial.commitd7cae122008-07-26 21:49:38752 }
753}
754
[email protected]55e57d42009-02-25 06:10:17755bool Histogram::SampleSet::Serialize(Pickle* pickle) const {
756 pickle->WriteInt64(sum_);
[email protected]93a41d72010-11-03 23:36:24757 pickle->WriteInt64(redundant_count_);
[email protected]ee028ac2012-03-14 00:08:02758 pickle->WriteUInt64(counts_.size());
[email protected]55e57d42009-02-25 06:10:17759
760 for (size_t index = 0; index < counts_.size(); ++index) {
761 pickle->WriteInt(counts_[index]);
762 }
763
764 return true;
765}
766
[email protected]ce208f872012-03-07 20:42:56767bool Histogram::SampleSet::Deserialize(PickleIterator* iter) {
[email protected]2753b392009-12-28 06:59:52768 DCHECK_EQ(counts_.size(), 0u);
769 DCHECK_EQ(sum_, 0);
[email protected]93a41d72010-11-03 23:36:24770 DCHECK_EQ(redundant_count_, 0);
[email protected]55e57d42009-02-25 06:10:17771
[email protected]ee028ac2012-03-14 00:08:02772 uint64 counts_size;
[email protected]55e57d42009-02-25 06:10:17773
[email protected]ce208f872012-03-07 20:42:56774 if (!iter->ReadInt64(&sum_) ||
775 !iter->ReadInt64(&redundant_count_) ||
[email protected]ee028ac2012-03-14 00:08:02776 !iter->ReadUInt64(&counts_size)) {
[email protected]55e57d42009-02-25 06:10:17777 return false;
778 }
779
[email protected]86440f52009-12-31 05:17:23780 if (counts_size == 0)
[email protected]55e57d42009-02-25 06:10:17781 return false;
782
[email protected]93a41d72010-11-03 23:36:24783 int count = 0;
[email protected]ee028ac2012-03-14 00:08:02784 for (uint64 index = 0; index < counts_size; ++index) {
[email protected]86440f52009-12-31 05:17:23785 int i;
[email protected]ce208f872012-03-07 20:42:56786 if (!iter->ReadInt(&i))
[email protected]55e57d42009-02-25 06:10:17787 return false;
[email protected]86440f52009-12-31 05:17:23788 counts_.push_back(i);
[email protected]93a41d72010-11-03 23:36:24789 count += i;
[email protected]55e57d42009-02-25 06:10:17790 }
[email protected]93a41d72010-11-03 23:36:24791 DCHECK_EQ(count, redundant_count_);
792 return count == redundant_count_;
[email protected]55e57d42009-02-25 06:10:17793}
794
initial.commitd7cae122008-07-26 21:49:38795//------------------------------------------------------------------------------
796// LinearHistogram: This histogram uses a traditional set of evenly spaced
797// buckets.
798//------------------------------------------------------------------------------
799
[email protected]b7d08202011-01-25 17:29:39800LinearHistogram::~LinearHistogram() {
801}
802
[email protected]81ce9f3b2011-04-05 04:48:53803Histogram* LinearHistogram::FactoryGet(const std::string& name,
804 Sample minimum,
805 Sample maximum,
806 size_t bucket_count,
807 Flags flags) {
[email protected]2ef3748f2010-10-19 17:33:28808 if (minimum < 1)
[email protected]e8829a192009-12-06 00:09:37809 minimum = 1;
[email protected]2ef3748f2010-10-19 17:33:28810 if (maximum > kSampleType_MAX - 1)
[email protected]e8829a192009-12-06 00:09:37811 maximum = kSampleType_MAX - 1;
812
[email protected]a93721e22012-01-06 02:13:28813 DCHECK_GT(maximum, minimum);
814 DCHECK_GT((Sample) bucket_count, 2);
815 DCHECK_LE((Sample) bucket_count, maximum - minimum + 2);
816
[email protected]993ae742012-07-18 18:06:30817 Histogram* histogram = StatisticsRecorder::FindHistogram(name);
818 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:53819 // To avoid racy destruction at shutdown, the following will be leaked.
820 LinearHistogram* tentative_histogram =
[email protected]98359ce2011-03-05 06:22:24821 new LinearHistogram(name, minimum, maximum, bucket_count);
[email protected]81ce9f3b2011-04-05 04:48:53822 tentative_histogram->InitializeBucketRange();
823 tentative_histogram->SetFlags(flags);
824 histogram =
825 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37826 }
827
[email protected]2ef3748f2010-10-19 17:33:28828 DCHECK_EQ(LINEAR_HISTOGRAM, histogram->histogram_type());
[email protected]e8829a192009-12-06 00:09:37829 DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count));
[email protected]e8829a192009-12-06 00:09:37830 return histogram;
831}
832
[email protected]81ce9f3b2011-04-05 04:48:53833Histogram* LinearHistogram::FactoryTimeGet(const std::string& name,
834 TimeDelta minimum,
835 TimeDelta maximum,
836 size_t bucket_count,
837 Flags flags) {
[email protected]2753b392009-12-28 06:59:52838 return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
839 bucket_count, flags);
[email protected]e8829a192009-12-06 00:09:37840}
841
[email protected]b7d08202011-01-25 17:29:39842Histogram::ClassType LinearHistogram::histogram_type() const {
843 return LINEAR_HISTOGRAM;
844}
845
846void LinearHistogram::SetRangeDescriptions(
847 const DescriptionPair descriptions[]) {
848 for (int i =0; descriptions[i].description; ++i) {
849 bucket_description_[descriptions[i].sample] = descriptions[i].description;
850 }
[email protected]d4799a32010-09-28 22:54:58851}
852
[email protected]835d7c82010-10-14 04:38:38853LinearHistogram::LinearHistogram(const std::string& name,
854 Sample minimum,
855 Sample maximum,
856 size_t bucket_count)
initial.commitd7cae122008-07-26 21:49:38857 : Histogram(name, minimum >= 1 ? minimum : 1, maximum, bucket_count) {
initial.commitd7cae122008-07-26 21:49:38858}
859
[email protected]e8829a192009-12-06 00:09:37860LinearHistogram::LinearHistogram(const std::string& name,
[email protected]835d7c82010-10-14 04:38:38861 TimeDelta minimum,
862 TimeDelta maximum,
863 size_t bucket_count)
initial.commitd7cae122008-07-26 21:49:38864 : Histogram(name, minimum >= TimeDelta::FromMilliseconds(1) ?
865 minimum : TimeDelta::FromMilliseconds(1),
866 maximum, bucket_count) {
initial.commitd7cae122008-07-26 21:49:38867}
868
initial.commitd7cae122008-07-26 21:49:38869void LinearHistogram::InitializeBucketRange() {
[email protected]70cc56e42010-04-29 22:39:55870 DCHECK_GT(declared_min(), 0); // 0 is the underflow bucket here.
initial.commitd7cae122008-07-26 21:49:38871 double min = declared_min();
872 double max = declared_max();
873 size_t i;
[email protected]55e57d42009-02-25 06:10:17874 for (i = 1; i < bucket_count(); ++i) {
initial.commitd7cae122008-07-26 21:49:38875 double linear_range = (min * (bucket_count() -1 - i) + max * (i - 1)) /
876 (bucket_count() - 2);
877 SetBucketRange(i, static_cast<int> (linear_range + 0.5));
878 }
[email protected]93a41d72010-11-03 23:36:24879 ResetRangeChecksum();
initial.commitd7cae122008-07-26 21:49:38880}
881
initial.commitd7cae122008-07-26 21:49:38882double LinearHistogram::GetBucketSize(Count current, size_t i) const {
[email protected]2ef3748f2010-10-19 17:33:28883 DCHECK_GT(ranges(i + 1), ranges(i));
initial.commitd7cae122008-07-26 21:49:38884 // Adjacent buckets with different widths would have "surprisingly" many (few)
885 // samples in a histogram if we didn't normalize this way.
886 double denominator = ranges(i + 1) - ranges(i);
887 return current/denominator;
888}
889
[email protected]b7d08202011-01-25 17:29:39890const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const {
891 int range = ranges(i);
892 BucketDescriptionMap::const_iterator it = bucket_description_.find(range);
893 if (it == bucket_description_.end())
894 return Histogram::GetAsciiBucketRange(i);
895 return it->second;
896}
897
898bool LinearHistogram::PrintEmptyBucket(size_t index) const {
899 return bucket_description_.find(ranges(index)) == bucket_description_.end();
900}
901
902
initial.commitd7cae122008-07-26 21:49:38903//------------------------------------------------------------------------------
[email protected]e8829a192009-12-06 00:09:37904// This section provides implementation for BooleanHistogram.
905//------------------------------------------------------------------------------
906
[email protected]81ce9f3b2011-04-05 04:48:53907Histogram* BooleanHistogram::FactoryGet(const std::string& name, Flags flags) {
[email protected]993ae742012-07-18 18:06:30908 Histogram* histogram = StatisticsRecorder::FindHistogram(name);
909 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:53910 // To avoid racy destruction at shutdown, the following will be leaked.
911 BooleanHistogram* tentative_histogram = new BooleanHistogram(name);
912 tentative_histogram->InitializeBucketRange();
913 tentative_histogram->SetFlags(flags);
914 histogram =
915 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37916 }
917
[email protected]2ef3748f2010-10-19 17:33:28918 DCHECK_EQ(BOOLEAN_HISTOGRAM, histogram->histogram_type());
[email protected]e8829a192009-12-06 00:09:37919 return histogram;
920}
921
[email protected]5d91c9e2010-07-28 17:25:28922Histogram::ClassType BooleanHistogram::histogram_type() const {
923 return BOOLEAN_HISTOGRAM;
924}
925
926void BooleanHistogram::AddBoolean(bool value) {
927 Add(value ? 1 : 0);
928}
929
930BooleanHistogram::BooleanHistogram(const std::string& name)
931 : LinearHistogram(name, 1, 2, 3) {
932}
initial.commitd7cae122008-07-26 21:49:38933
934//------------------------------------------------------------------------------
[email protected]70cc56e42010-04-29 22:39:55935// CustomHistogram:
936//------------------------------------------------------------------------------
937
[email protected]81ce9f3b2011-04-05 04:48:53938Histogram* CustomHistogram::FactoryGet(const std::string& name,
939 const std::vector<Sample>& custom_ranges,
940 Flags flags) {
[email protected]70cc56e42010-04-29 22:39:55941 // Remove the duplicates in the custom ranges array.
942 std::vector<int> ranges = custom_ranges;
943 ranges.push_back(0); // Ensure we have a zero value.
944 std::sort(ranges.begin(), ranges.end());
945 ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end());
946 if (ranges.size() <= 1) {
947 DCHECK(false);
948 // Note that we pushed a 0 in above, so for defensive code....
949 ranges.push_back(1); // Put in some data so we can index to [1].
950 }
951
952 DCHECK_LT(ranges.back(), kSampleType_MAX);
953
[email protected]993ae742012-07-18 18:06:30954 Histogram* histogram = StatisticsRecorder::FindHistogram(name);
955 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:53956 // To avoid racy destruction at shutdown, the following will be leaked.
957 CustomHistogram* tentative_histogram = new CustomHistogram(name, ranges);
958 tentative_histogram->InitializedCustomBucketRange(ranges);
959 tentative_histogram->SetFlags(flags);
960 histogram =
961 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]70cc56e42010-04-29 22:39:55962 }
963
964 DCHECK_EQ(histogram->histogram_type(), CUSTOM_HISTOGRAM);
965 DCHECK(histogram->HasConstructorArguments(ranges[1], ranges.back(),
966 ranges.size()));
[email protected]70cc56e42010-04-29 22:39:55967 return histogram;
968}
969
[email protected]5d91c9e2010-07-28 17:25:28970Histogram::ClassType CustomHistogram::histogram_type() const {
971 return CUSTOM_HISTOGRAM;
972}
973
[email protected]961fefb2011-05-24 13:59:58974// static
975std::vector<Histogram::Sample> CustomHistogram::ArrayToCustomRanges(
976 const Sample* values, size_t num_values) {
977 std::vector<Sample> all_values;
978 for (size_t i = 0; i < num_values; ++i) {
979 Sample value = values[i];
980 all_values.push_back(value);
981
982 // Ensure that a guard bucket is added. If we end up with duplicate
983 // values, FactoryGet will take care of removing them.
984 all_values.push_back(value + 1);
985 }
986 return all_values;
987}
988
[email protected]70cc56e42010-04-29 22:39:55989CustomHistogram::CustomHistogram(const std::string& name,
[email protected]93a41d72010-11-03 23:36:24990 const std::vector<Sample>& custom_ranges)
[email protected]70cc56e42010-04-29 22:39:55991 : Histogram(name, custom_ranges[1], custom_ranges.back(),
992 custom_ranges.size()) {
993 DCHECK_GT(custom_ranges.size(), 1u);
994 DCHECK_EQ(custom_ranges[0], 0);
[email protected]70cc56e42010-04-29 22:39:55995}
996
[email protected]cd56dff2011-11-13 04:19:15997bool CustomHistogram::SerializeRanges(Pickle* pickle) const {
998 for (size_t i = 0; i < cached_ranges()->size(); ++i) {
999 if (!pickle->WriteInt(cached_ranges()->ranges(i)))
1000 return false;
1001 }
1002 return true;
1003}
1004
1005// static
1006bool CustomHistogram::DeserializeRanges(
[email protected]ce208f872012-03-07 20:42:561007 PickleIterator* iter, std::vector<Histogram::Sample>* ranges) {
[email protected]cd56dff2011-11-13 04:19:151008 for (size_t i = 0; i < ranges->size(); ++i) {
[email protected]ce208f872012-03-07 20:42:561009 if (!iter->ReadInt(&(*ranges)[i]))
[email protected]cd56dff2011-11-13 04:19:151010 return false;
1011 }
1012 return true;
1013}
1014
[email protected]98359ce2011-03-05 06:22:241015void CustomHistogram::InitializedCustomBucketRange(
1016 const std::vector<Sample>& custom_ranges) {
1017 DCHECK_GT(custom_ranges.size(), 1u);
1018 DCHECK_EQ(custom_ranges[0], 0);
1019 DCHECK_LE(custom_ranges.size(), bucket_count());
1020 for (size_t index = 0; index < custom_ranges.size(); ++index)
1021 SetBucketRange(index, custom_ranges[index]);
[email protected]93a41d72010-11-03 23:36:241022 ResetRangeChecksum();
[email protected]70cc56e42010-04-29 22:39:551023}
1024
1025double CustomHistogram::GetBucketSize(Count current, size_t i) const {
1026 return 1;
1027}
1028
[email protected]908de522011-10-20 00:55:001029CachedRanges::CachedRanges(size_t bucket_count, int initial_value)
1030 : ranges_(bucket_count, initial_value),
1031 range_checksum_(0) {
1032}
1033
1034CachedRanges::~CachedRanges() {
1035}
1036
1037void CachedRanges::SetBucketRange(size_t i, Histogram::Sample value) {
1038 DCHECK_LT(i, ranges_.size());
1039 DCHECK_GE(value, 0);
1040 ranges_[i] = value;
1041}
1042
1043bool CachedRanges::Equals(CachedRanges* other) const {
1044 if (range_checksum_ != other->range_checksum_)
1045 return false;
1046 if (ranges_.size() != other->ranges_.size())
1047 return false;
1048 for (size_t index = 0; index < ranges_.size(); ++index) {
1049 if (ranges_[index] != other->ranges_[index])
1050 return false;
1051 }
1052 return true;
1053}
1054
[email protected]835d7c82010-10-14 04:38:381055} // namespace base