blob: 7077b090715d6bab39ab489435cbb97f3af133db [file] [log] [blame]
[email protected]0704a972011-03-24 00:30:271// Copyright (c) 2011 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
[email protected]9b3bbd62011-05-31 07:37:0917#include "base/debug/leak_annotations.h"
initial.commitd7cae122008-07-26 21:49:3818#include "base/logging.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]908de522011-10-20 00:55:0077// Collect the number of histograms created.
78static uint32 number_of_histograms_ = 0;
79// Collect the number of vectors saved because of caching ranges.
80static uint32 number_of_vectors_saved_ = 0;
81// Collect the number of ranges_ elements saved because of caching ranges.
82static size_t saved_ranges_size_ = 0;
83
[email protected]81ce9f3b2011-04-05 04:48:5384Histogram* Histogram::FactoryGet(const std::string& name,
85 Sample minimum,
86 Sample maximum,
87 size_t bucket_count,
88 Flags flags) {
89 Histogram* histogram(NULL);
[email protected]e8829a192009-12-06 00:09:3790
91 // Defensive code.
[email protected]2ef3748f2010-10-19 17:33:2892 if (minimum < 1)
[email protected]e8829a192009-12-06 00:09:3793 minimum = 1;
[email protected]2ef3748f2010-10-19 17:33:2894 if (maximum > kSampleType_MAX - 1)
[email protected]e8829a192009-12-06 00:09:3795 maximum = kSampleType_MAX - 1;
96
[email protected]f2bc0cb52010-06-25 15:55:1597 if (!StatisticsRecorder::FindHistogram(name, &histogram)) {
[email protected]81ce9f3b2011-04-05 04:48:5398 // Extra variable is not needed... but this keeps this section basically
99 // identical to other derived classes in this file (and compiler will
100 // optimize away the extra variable.
101 // To avoid racy destruction at shutdown, the following will be leaked.
102 Histogram* tentative_histogram =
103 new Histogram(name, minimum, maximum, bucket_count);
104 tentative_histogram->InitializeBucketRange();
105 tentative_histogram->SetFlags(flags);
106 histogram =
107 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37108 }
109
[email protected]2ef3748f2010-10-19 17:33:28110 DCHECK_EQ(HISTOGRAM, histogram->histogram_type());
[email protected]e8829a192009-12-06 00:09:37111 DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count));
[email protected]e8829a192009-12-06 00:09:37112 return histogram;
113}
114
[email protected]81ce9f3b2011-04-05 04:48:53115Histogram* Histogram::FactoryTimeGet(const std::string& name,
116 TimeDelta minimum,
117 TimeDelta maximum,
118 size_t bucket_count,
119 Flags flags) {
[email protected]2753b392009-12-28 06:59:52120 return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
121 bucket_count, flags);
[email protected]e8829a192009-12-06 00:09:37122}
123
[email protected]b404b1f2011-10-16 07:29:17124TimeTicks Histogram::DebugNow() {
125#ifndef NDEBUG
126 return TimeTicks::Now();
127#else
128 return TimeTicks();
129#endif
130}
131
initial.commitd7cae122008-07-26 21:49:38132void Histogram::Add(int value) {
[email protected]2ef3748f2010-10-19 17:33:28133 if (value > kSampleType_MAX - 1)
initial.commitd7cae122008-07-26 21:49:38134 value = kSampleType_MAX - 1;
initial.commitd7cae122008-07-26 21:49:38135 if (value < 0)
136 value = 0;
137 size_t index = BucketIndex(value);
[email protected]2ef3748f2010-10-19 17:33:28138 DCHECK_GE(value, ranges(index));
139 DCHECK_LT(value, ranges(index + 1));
initial.commitd7cae122008-07-26 21:49:38140 Accumulate(value, 1, index);
141}
142
[email protected]5d91c9e2010-07-28 17:25:28143void Histogram::AddBoolean(bool value) {
144 DCHECK(false);
145}
146
[email protected]55e57d42009-02-25 06:10:17147void Histogram::AddSampleSet(const SampleSet& sample) {
148 sample_.Add(sample);
149}
150
[email protected]5d91c9e2010-07-28 17:25:28151void Histogram::SetRangeDescriptions(const DescriptionPair descriptions[]) {
152 DCHECK(false);
153}
154
initial.commitd7cae122008-07-26 21:49:38155// The following methods provide a graphical histogram display.
156void Histogram::WriteHTMLGraph(std::string* output) const {
157 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc.
158 output->append("<PRE>");
159 WriteAscii(true, "<br>", output);
160 output->append("</PRE>");
161}
162
163void Histogram::WriteAscii(bool graph_it, const std::string& newline,
164 std::string* output) const {
165 // Get local (stack) copies of all effectively volatile class data so that we
166 // are consistent across our output activities.
167 SampleSet snapshot;
168 SnapshotSample(&snapshot);
169 Count sample_count = snapshot.TotalCount();
170
171 WriteAsciiHeader(snapshot, sample_count, output);
172 output->append(newline);
173
174 // Prepare to normalize graphical rendering of bucket contents.
175 double max_size = 0;
176 if (graph_it)
177 max_size = GetPeakBucketSize(snapshot);
178
179 // Calculate space needed to print bucket range numbers. Leave room to print
180 // nearly the largest bucket range without sliding over the histogram.
[email protected]e2951cf2008-09-24 23:51:25181 size_t largest_non_empty_bucket = bucket_count() - 1;
182 while (0 == snapshot.counts(largest_non_empty_bucket)) {
initial.commitd7cae122008-07-26 21:49:38183 if (0 == largest_non_empty_bucket)
184 break; // All buckets are empty.
[email protected]55e57d42009-02-25 06:10:17185 --largest_non_empty_bucket;
initial.commitd7cae122008-07-26 21:49:38186 }
187
188 // Calculate largest print width needed for any of our bucket range displays.
189 size_t print_width = 1;
[email protected]e2951cf2008-09-24 23:51:25190 for (size_t i = 0; i < bucket_count(); ++i) {
initial.commitd7cae122008-07-26 21:49:38191 if (snapshot.counts(i)) {
192 size_t width = GetAsciiBucketRange(i).size() + 1;
193 if (width > print_width)
194 print_width = width;
195 }
196 }
197
198 int64 remaining = sample_count;
199 int64 past = 0;
200 // Output the actual histogram graph.
[email protected]55e57d42009-02-25 06:10:17201 for (size_t i = 0; i < bucket_count(); ++i) {
initial.commitd7cae122008-07-26 21:49:38202 Count current = snapshot.counts(i);
203 if (!current && !PrintEmptyBucket(i))
204 continue;
205 remaining -= current;
[email protected]34b2b002009-11-20 06:53:28206 std::string range = GetAsciiBucketRange(i);
207 output->append(range);
208 for (size_t j = 0; range.size() + j < print_width + 1; ++j)
209 output->push_back(' ');
[email protected]e2951cf2008-09-24 23:51:25210 if (0 == current && i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) {
211 while (i < bucket_count() - 1 && 0 == snapshot.counts(i + 1))
[email protected]55e57d42009-02-25 06:10:17212 ++i;
initial.commitd7cae122008-07-26 21:49:38213 output->append("... ");
214 output->append(newline);
215 continue; // No reason to plot emptiness.
216 }
217 double current_size = GetBucketSize(current, i);
218 if (graph_it)
219 WriteAsciiBucketGraph(current_size, max_size, output);
220 WriteAsciiBucketContext(past, current, remaining, i, output);
221 output->append(newline);
222 past += current;
223 }
[email protected]2ef3748f2010-10-19 17:33:28224 DCHECK_EQ(sample_count, past);
initial.commitd7cae122008-07-26 21:49:38225}
226
[email protected]55e57d42009-02-25 06:10:17227// static
228std::string Histogram::SerializeHistogramInfo(const Histogram& histogram,
229 const SampleSet& snapshot) {
[email protected]2ef3748f2010-10-19 17:33:28230 DCHECK_NE(NOT_VALID_IN_RENDERER, histogram.histogram_type());
[email protected]55e57d42009-02-25 06:10:17231
[email protected]e8829a192009-12-06 00:09:37232 Pickle pickle;
[email protected]55e57d42009-02-25 06:10:17233 pickle.WriteString(histogram.histogram_name());
234 pickle.WriteInt(histogram.declared_min());
235 pickle.WriteInt(histogram.declared_max());
236 pickle.WriteSize(histogram.bucket_count());
[email protected]98359ce2011-03-05 06:22:24237 pickle.WriteUInt32(histogram.range_checksum());
[email protected]55e57d42009-02-25 06:10:17238 pickle.WriteInt(histogram.histogram_type());
[email protected]1f4fc8e8c2010-01-02 00:46:41239 pickle.WriteInt(histogram.flags());
[email protected]55e57d42009-02-25 06:10:17240
241 snapshot.Serialize(&pickle);
242 return std::string(static_cast<const char*>(pickle.data()), pickle.size());
243}
244
245// static
[email protected]55e57d42009-02-25 06:10:17246bool Histogram::DeserializeHistogramInfo(const std::string& histogram_info) {
247 if (histogram_info.empty()) {
248 return false;
249 }
250
251 Pickle pickle(histogram_info.data(),
252 static_cast<int>(histogram_info.size()));
[email protected]93a41d72010-11-03 23:36:24253 std::string histogram_name;
[email protected]55e57d42009-02-25 06:10:17254 int declared_min;
255 int declared_max;
[email protected]93a41d72010-11-03 23:36:24256 size_t bucket_count;
[email protected]98359ce2011-03-05 06:22:24257 uint32 range_checksum;
[email protected]55e57d42009-02-25 06:10:17258 int histogram_type;
[email protected]2753b392009-12-28 06:59:52259 int pickle_flags;
[email protected]55e57d42009-02-25 06:10:17260 SampleSet sample;
261
[email protected]93a41d72010-11-03 23:36:24262 void* iter = NULL;
[email protected]55e57d42009-02-25 06:10:17263 if (!pickle.ReadString(&iter, &histogram_name) ||
264 !pickle.ReadInt(&iter, &declared_min) ||
265 !pickle.ReadInt(&iter, &declared_max) ||
266 !pickle.ReadSize(&iter, &bucket_count) ||
[email protected]98359ce2011-03-05 06:22:24267 !pickle.ReadUInt32(&iter, &range_checksum) ||
[email protected]55e57d42009-02-25 06:10:17268 !pickle.ReadInt(&iter, &histogram_type) ||
[email protected]2753b392009-12-28 06:59:52269 !pickle.ReadInt(&iter, &pickle_flags) ||
[email protected]55e57d42009-02-25 06:10:17270 !sample.Histogram::SampleSet::Deserialize(&iter, pickle)) {
[email protected]a42d4632011-10-26 21:48:00271 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name;
[email protected]55e57d42009-02-25 06:10:17272 return false;
273 }
[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]e8829a192009-12-06 00:09:37297 } else {
[email protected]a42d4632011-10-26 21:48:00298 DLOG(ERROR) << "Error Deserializing Histogram Unknown histogram_type: "
299 << histogram_type;
[email protected]e8829a192009-12-06 00:09:37300 return false;
[email protected]55e57d42009-02-25 06:10:17301 }
302
[email protected]2ef3748f2010-10-19 17:33:28303 DCHECK_EQ(render_histogram->declared_min(), declared_min);
304 DCHECK_EQ(render_histogram->declared_max(), declared_max);
305 DCHECK_EQ(render_histogram->bucket_count(), bucket_count);
[email protected]93a41d72010-11-03 23:36:24306 DCHECK_EQ(render_histogram->range_checksum(), range_checksum);
[email protected]2ef3748f2010-10-19 17:33:28307 DCHECK_EQ(render_histogram->histogram_type(), histogram_type);
[email protected]55e57d42009-02-25 06:10:17308
[email protected]e8829a192009-12-06 00:09:37309 if (render_histogram->flags() & kIPCSerializationSourceFlag) {
[email protected]2ef3748f2010-10-19 17:33:28310 DVLOG(1) << "Single process mode, histogram observed and not copied: "
311 << histogram_name;
[email protected]e8829a192009-12-06 00:09:37312 } else {
[email protected]2ef3748f2010-10-19 17:33:28313 DCHECK_EQ(flags & render_histogram->flags(), flags);
[email protected]e8829a192009-12-06 00:09:37314 render_histogram->AddSampleSet(sample);
[email protected]55e57d42009-02-25 06:10:17315 }
316
317 return true;
318}
319
initial.commitd7cae122008-07-26 21:49:38320//------------------------------------------------------------------------------
[email protected]93a41d72010-11-03 23:36:24321// Methods for the validating a sample and a related histogram.
322//------------------------------------------------------------------------------
323
324Histogram::Inconsistencies Histogram::FindCorruption(
325 const SampleSet& snapshot) const {
326 int inconsistencies = NO_INCONSISTENCIES;
327 Sample previous_range = -1; // Bottom range is always 0.
[email protected]93a41d72010-11-03 23:36:24328 int64 count = 0;
329 for (size_t index = 0; index < bucket_count(); ++index) {
330 count += snapshot.counts(index);
331 int new_range = ranges(index);
[email protected]93a41d72010-11-03 23:36:24332 if (previous_range >= new_range)
333 inconsistencies |= BUCKET_ORDER_ERROR;
334 previous_range = new_range;
335 }
336
[email protected]98359ce2011-03-05 06:22:24337 if (!HasValidRangeChecksum())
[email protected]93a41d72010-11-03 23:36:24338 inconsistencies |= RANGE_CHECKSUM_ERROR;
339
340 int64 delta64 = snapshot.redundant_count() - count;
341 if (delta64 != 0) {
342 int delta = static_cast<int>(delta64);
343 if (delta != delta64)
344 delta = INT_MAX; // Flag all giant errors as INT_MAX.
345 // Since snapshots of histograms are taken asynchronously relative to
346 // sampling (and snapped from different threads), it is pretty likely that
347 // we'll catch a redundant count that doesn't match the sample count. We
348 // allow for a certain amount of slop before flagging this as an
349 // inconsistency. Even with an inconsistency, we'll snapshot it again (for
350 // UMA in about a half hour, so we'll eventually get the data, if it was
351 // not the result of a corruption. If histograms show that 1 is "too tight"
352 // then we may try to use 2 or 3 for this slop value.
353 const int kCommonRaceBasedCountMismatch = 1;
354 if (delta > 0) {
355 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta);
356 if (delta > kCommonRaceBasedCountMismatch)
357 inconsistencies |= COUNT_HIGH_ERROR;
358 } else {
359 DCHECK_GT(0, delta);
360 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta);
361 if (-delta > kCommonRaceBasedCountMismatch)
362 inconsistencies |= COUNT_LOW_ERROR;
363 }
364 }
365 return static_cast<Inconsistencies>(inconsistencies);
366}
367
[email protected]7cf40912010-12-09 18:25:03368Histogram::ClassType Histogram::histogram_type() const {
369 return HISTOGRAM;
370}
371
372Histogram::Sample Histogram::ranges(size_t i) const {
[email protected]908de522011-10-20 00:55:00373 return cached_ranges_->ranges(i);
[email protected]7cf40912010-12-09 18:25:03374}
375
376size_t Histogram::bucket_count() const {
377 return bucket_count_;
378}
379
[email protected]b7d08202011-01-25 17:29:39380// Do a safe atomic snapshot of sample data.
381// This implementation assumes we are on a safe single thread.
382void Histogram::SnapshotSample(SampleSet* sample) const {
383 // Note locking not done in this version!!!
384 *sample = sample_;
385}
386
387bool Histogram::HasConstructorArguments(Sample minimum,
388 Sample maximum,
389 size_t bucket_count) {
390 return ((minimum == declared_min_) && (maximum == declared_max_) &&
391 (bucket_count == bucket_count_));
392}
393
394bool Histogram::HasConstructorTimeDeltaArguments(TimeDelta minimum,
395 TimeDelta maximum,
396 size_t bucket_count) {
397 return ((minimum.InMilliseconds() == declared_min_) &&
398 (maximum.InMilliseconds() == declared_max_) &&
399 (bucket_count == bucket_count_));
400}
401
[email protected]98359ce2011-03-05 06:22:24402bool Histogram::HasValidRangeChecksum() const {
403 return CalculateRangeChecksum() == range_checksum_;
404}
405
[email protected]b7d08202011-01-25 17:29:39406Histogram::Histogram(const std::string& name, Sample minimum,
407 Sample maximum, size_t bucket_count)
408 : histogram_name_(name),
409 declared_min_(minimum),
410 declared_max_(maximum),
411 bucket_count_(bucket_count),
412 flags_(kNoFlags),
[email protected]908de522011-10-20 00:55:00413 cached_ranges_(new CachedRanges(bucket_count + 1, 0)),
[email protected]b7d08202011-01-25 17:29:39414 range_checksum_(0),
415 sample_() {
416 Initialize();
417}
418
419Histogram::Histogram(const std::string& name, TimeDelta minimum,
420 TimeDelta maximum, size_t bucket_count)
421 : histogram_name_(name),
422 declared_min_(static_cast<int> (minimum.InMilliseconds())),
423 declared_max_(static_cast<int> (maximum.InMilliseconds())),
424 bucket_count_(bucket_count),
425 flags_(kNoFlags),
[email protected]908de522011-10-20 00:55:00426 cached_ranges_(new CachedRanges(bucket_count + 1, 0)),
[email protected]b7d08202011-01-25 17:29:39427 range_checksum_(0),
428 sample_() {
429 Initialize();
430}
431
432Histogram::~Histogram() {
433 if (StatisticsRecorder::dump_on_exit()) {
434 std::string output;
435 WriteAscii(true, "\n", &output);
[email protected]a42d4632011-10-26 21:48:00436 DLOG(INFO) << output;
[email protected]b7d08202011-01-25 17:29:39437 }
438
439 // Just to make sure most derived class did this properly...
440 DCHECK(ValidateBucketRanges());
[email protected]7767bdb2011-03-02 23:38:00441}
442
[email protected]b7d08202011-01-25 17:29:39443// Calculate what range of values are held in each bucket.
444// We have to be careful that we don't pick a ratio between starting points in
445// consecutive buckets that is sooo small, that the integer bounds are the same
446// (effectively making one bucket get no values). We need to avoid:
[email protected]908de522011-10-20 00:55:00447// ranges(i) == ranges(i + 1)
[email protected]b7d08202011-01-25 17:29:39448// To avoid that, we just do a fine-grained bucket width as far as we need to
449// until we get a ratio that moves us along at least 2 units at a time. From
450// that bucket onward we do use the exponential growth of buckets.
451void Histogram::InitializeBucketRange() {
452 double log_max = log(static_cast<double>(declared_max()));
453 double log_ratio;
454 double log_next;
455 size_t bucket_index = 1;
456 Sample current = declared_min();
457 SetBucketRange(bucket_index, current);
458 while (bucket_count() > ++bucket_index) {
459 double log_current;
460 log_current = log(static_cast<double>(current));
461 // Calculate the count'th root of the range.
462 log_ratio = (log_max - log_current) / (bucket_count() - bucket_index);
463 // See where the next bucket would start.
464 log_next = log_current + log_ratio;
465 int next;
466 next = static_cast<int>(floor(exp(log_next) + 0.5));
467 if (next > current)
468 current = next;
469 else
470 ++current; // Just do a narrow bucket, and keep trying.
471 SetBucketRange(bucket_index, current);
472 }
473 ResetRangeChecksum();
474
475 DCHECK_EQ(bucket_count(), bucket_index);
476}
477
[email protected]98359ce2011-03-05 06:22:24478bool Histogram::PrintEmptyBucket(size_t index) const {
479 return true;
480}
481
[email protected]b7d08202011-01-25 17:29:39482size_t Histogram::BucketIndex(Sample value) const {
483 // Use simple binary search. This is very general, but there are better
484 // approaches if we knew that the buckets were linearly distributed.
485 DCHECK_LE(ranges(0), value);
486 DCHECK_GT(ranges(bucket_count()), value);
487 size_t under = 0;
488 size_t over = bucket_count();
489 size_t mid;
490
491 do {
492 DCHECK_GE(over, under);
[email protected]b122c0c2011-02-23 22:31:18493 mid = under + (over - under)/2;
[email protected]b7d08202011-01-25 17:29:39494 if (mid == under)
495 break;
496 if (ranges(mid) <= value)
497 under = mid;
498 else
499 over = mid;
500 } while (true);
501
502 DCHECK_LE(ranges(mid), value);
[email protected]b122c0c2011-02-23 22:31:18503 CHECK_GT(ranges(mid+1), value);
[email protected]b7d08202011-01-25 17:29:39504 return mid;
505}
506
507// Use the actual bucket widths (like a linear histogram) until the widths get
508// over some transition value, and then use that transition width. Exponentials
509// get so big so fast (and we don't expect to see a lot of entries in the large
510// buckets), so we need this to make it possible to see what is going on and
511// not have 0-graphical-height buckets.
512double Histogram::GetBucketSize(Count current, size_t i) const {
513 DCHECK_GT(ranges(i + 1), ranges(i));
514 static const double kTransitionWidth = 5;
515 double denominator = ranges(i + 1) - ranges(i);
516 if (denominator > kTransitionWidth)
517 denominator = kTransitionWidth; // Stop trying to normalize.
518 return current/denominator;
519}
520
521void Histogram::ResetRangeChecksum() {
522 range_checksum_ = CalculateRangeChecksum();
523}
524
525const std::string Histogram::GetAsciiBucketRange(size_t i) const {
526 std::string result;
527 if (kHexRangePrintingFlag & flags_)
528 StringAppendF(&result, "%#x", ranges(i));
529 else
530 StringAppendF(&result, "%d", ranges(i));
531 return result;
532}
533
534// Update histogram data with new sample.
535void Histogram::Accumulate(Sample value, Count count, size_t index) {
536 // Note locking not done in this version!!!
537 sample_.Accumulate(value, count, index);
538}
539
540void Histogram::SetBucketRange(size_t i, Sample value) {
541 DCHECK_GT(bucket_count_, i);
[email protected]c89b2442011-05-26 14:28:27542 DCHECK_GE(value, 0);
[email protected]908de522011-10-20 00:55:00543 cached_ranges_->SetBucketRange(i, value);
[email protected]b7d08202011-01-25 17:29:39544}
545
546bool Histogram::ValidateBucketRanges() const {
547 // Standard assertions that all bucket ranges should satisfy.
[email protected]908de522011-10-20 00:55:00548 DCHECK_EQ(bucket_count_ + 1, cached_ranges_->size());
549 DCHECK_EQ(0, ranges(0));
550 DCHECK_EQ(declared_min(), ranges(1));
551 DCHECK_EQ(declared_max(), ranges(bucket_count_ - 1));
552 DCHECK_EQ(kSampleType_MAX, ranges(bucket_count_));
[email protected]b7d08202011-01-25 17:29:39553 return true;
554}
555
[email protected]98359ce2011-03-05 06:22:24556uint32 Histogram::CalculateRangeChecksum() const {
[email protected]908de522011-10-20 00:55:00557 DCHECK_EQ(cached_ranges_->size(), bucket_count() + 1);
558 // Seed checksum.
559 uint32 checksum = static_cast<uint32>(cached_ranges_->size());
[email protected]98359ce2011-03-05 06:22:24560 for (size_t index = 0; index < bucket_count(); ++index)
561 checksum = Crc32(checksum, ranges(index));
562 return checksum;
563}
564
[email protected]b7d08202011-01-25 17:29:39565void Histogram::Initialize() {
566 sample_.Resize(*this);
567 if (declared_min_ < 1)
568 declared_min_ = 1;
569 if (declared_max_ > kSampleType_MAX - 1)
570 declared_max_ = kSampleType_MAX - 1;
571 DCHECK_LE(declared_min_, declared_max_);
572 DCHECK_GT(bucket_count_, 1u);
[email protected]b122c0c2011-02-23 22:31:18573 CHECK_LT(bucket_count_, kBucketCount_MAX);
[email protected]b7d08202011-01-25 17:29:39574 size_t maximal_bucket_count = declared_max_ - declared_min_ + 2;
575 DCHECK_LE(bucket_count_, maximal_bucket_count);
[email protected]908de522011-10-20 00:55:00576 DCHECK_EQ(0, ranges(0));
577 cached_ranges_->SetBucketRange(bucket_count_, kSampleType_MAX);
[email protected]b7d08202011-01-25 17:29:39578}
579
[email protected]98359ce2011-03-05 06:22:24580// We generate the CRC-32 using the low order bits to select whether to XOR in
581// the reversed polynomial 0xedb88320L. This is nice and simple, and allows us
582// to keep the quotient in a uint32. Since we're not concerned about the nature
583// of corruptions (i.e., we don't care about bit sequencing, since we are
584// handling memory changes, which are more grotesque) so we don't bother to
585// get the CRC correct for big-endian vs little-ending calculations. All we
586// need is a nice hash, that tends to depend on all the bits of the sample, with
587// very little chance of changes in one place impacting changes in another
588// place.
589uint32 Histogram::Crc32(uint32 sum, Histogram::Sample range) {
590 const bool kUseRealCrc = true; // TODO(jar): Switch to false and watch stats.
591 if (kUseRealCrc) {
592 union {
593 Histogram::Sample range;
594 unsigned char bytes[sizeof(Histogram::Sample)];
595 } converter;
596 converter.range = range;
597 for (size_t i = 0; i < sizeof(converter); ++i)
598 sum = kCrcTable[(sum & 0xff) ^ converter.bytes[i]] ^ (sum >> 8);
599 } else {
600 // Use hash techniques provided in ReallyFastHash, except we don't care
601 // about "avalanching" (which would worsten the hash, and add collisions),
602 // and we don't care about edge cases since we have an even number of bytes.
603 union {
604 Histogram::Sample range;
605 uint16 ints[sizeof(Histogram::Sample) / 2];
606 } converter;
607 DCHECK_EQ(sizeof(Histogram::Sample), sizeof(converter));
608 converter.range = range;
609 sum += converter.ints[0];
610 sum = (sum << 16) ^ sum ^ (static_cast<uint32>(converter.ints[1]) << 11);
611 sum += sum >> 11;
[email protected]b7d08202011-01-25 17:29:39612 }
[email protected]98359ce2011-03-05 06:22:24613 return sum;
[email protected]b7d08202011-01-25 17:29:39614}
615
616//------------------------------------------------------------------------------
617// Private methods
618
619double Histogram::GetPeakBucketSize(const SampleSet& snapshot) const {
620 double max = 0;
621 for (size_t i = 0; i < bucket_count() ; ++i) {
622 double current_size = GetBucketSize(snapshot.counts(i), i);
623 if (current_size > max)
624 max = current_size;
625 }
626 return max;
627}
628
629void Histogram::WriteAsciiHeader(const SampleSet& snapshot,
630 Count sample_count,
631 std::string* output) const {
632 StringAppendF(output,
633 "Histogram: %s recorded %d samples",
634 histogram_name().c_str(),
635 sample_count);
636 if (0 == sample_count) {
637 DCHECK_EQ(snapshot.sum(), 0);
638 } else {
639 double average = static_cast<float>(snapshot.sum()) / sample_count;
[email protected]b7d08202011-01-25 17:29:39640
[email protected]0704a972011-03-24 00:30:27641 StringAppendF(output, ", average = %.1f", average);
[email protected]b7d08202011-01-25 17:29:39642 }
643 if (flags_ & ~kHexRangePrintingFlag)
644 StringAppendF(output, " (flags = 0x%x)", flags_ & ~kHexRangePrintingFlag);
645}
646
647void Histogram::WriteAsciiBucketContext(const int64 past,
648 const Count current,
649 const int64 remaining,
650 const size_t i,
651 std::string* output) const {
652 double scaled_sum = (past + current + remaining) / 100.0;
653 WriteAsciiBucketValue(current, scaled_sum, output);
654 if (0 < i) {
655 double percentage = past / scaled_sum;
656 StringAppendF(output, " {%3.1f%%}", percentage);
657 }
658}
659
660void Histogram::WriteAsciiBucketValue(Count current, double scaled_sum,
661 std::string* output) const {
662 StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum);
663}
664
665void Histogram::WriteAsciiBucketGraph(double current_size, double max_size,
666 std::string* output) const {
667 const int k_line_length = 72; // Maximal horizontal width of graph.
668 int x_count = static_cast<int>(k_line_length * (current_size / max_size)
669 + 0.5);
670 int x_remainder = k_line_length - x_count;
671
672 while (0 < x_count--)
673 output->append("-");
674 output->append("O");
675 while (0 < x_remainder--)
676 output->append(" ");
677}
678
[email protected]93a41d72010-11-03 23:36:24679//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38680// Methods for the Histogram::SampleSet class
681//------------------------------------------------------------------------------
682
683Histogram::SampleSet::SampleSet()
684 : counts_(),
685 sum_(0),
[email protected]93a41d72010-11-03 23:36:24686 redundant_count_(0) {
initial.commitd7cae122008-07-26 21:49:38687}
688
[email protected]d4799a32010-09-28 22:54:58689Histogram::SampleSet::~SampleSet() {
690}
691
initial.commitd7cae122008-07-26 21:49:38692void Histogram::SampleSet::Resize(const Histogram& histogram) {
693 counts_.resize(histogram.bucket_count(), 0);
694}
695
696void Histogram::SampleSet::CheckSize(const Histogram& histogram) const {
[email protected]2ef3748f2010-10-19 17:33:28697 DCHECK_EQ(histogram.bucket_count(), counts_.size());
initial.commitd7cae122008-07-26 21:49:38698}
699
700
701void Histogram::SampleSet::Accumulate(Sample value, Count count,
702 size_t index) {
703 DCHECK(count == 1 || count == -1);
704 counts_[index] += count;
705 sum_ += count * value;
[email protected]93a41d72010-11-03 23:36:24706 redundant_count_ += count;
[email protected]2753b392009-12-28 06:59:52707 DCHECK_GE(counts_[index], 0);
708 DCHECK_GE(sum_, 0);
[email protected]93a41d72010-11-03 23:36:24709 DCHECK_GE(redundant_count_, 0);
initial.commitd7cae122008-07-26 21:49:38710}
711
712Count Histogram::SampleSet::TotalCount() const {
713 Count total = 0;
714 for (Counts::const_iterator it = counts_.begin();
715 it != counts_.end();
[email protected]55e57d42009-02-25 06:10:17716 ++it) {
initial.commitd7cae122008-07-26 21:49:38717 total += *it;
718 }
719 return total;
720}
721
722void Histogram::SampleSet::Add(const SampleSet& other) {
[email protected]2ef3748f2010-10-19 17:33:28723 DCHECK_EQ(counts_.size(), other.counts_.size());
initial.commitd7cae122008-07-26 21:49:38724 sum_ += other.sum_;
[email protected]93a41d72010-11-03 23:36:24725 redundant_count_ += other.redundant_count_;
[email protected]55e57d42009-02-25 06:10:17726 for (size_t index = 0; index < counts_.size(); ++index)
initial.commitd7cae122008-07-26 21:49:38727 counts_[index] += other.counts_[index];
728}
729
730void Histogram::SampleSet::Subtract(const SampleSet& other) {
[email protected]2ef3748f2010-10-19 17:33:28731 DCHECK_EQ(counts_.size(), other.counts_.size());
[email protected]0704a972011-03-24 00:30:27732 // Note: Race conditions in snapshotting a sum may lead to (temporary)
733 // negative values when snapshots are later combined (and deltas calculated).
734 // As a result, we don't currently CHCEK() for positive values.
initial.commitd7cae122008-07-26 21:49:38735 sum_ -= other.sum_;
[email protected]93a41d72010-11-03 23:36:24736 redundant_count_ -= other.redundant_count_;
[email protected]55e57d42009-02-25 06:10:17737 for (size_t index = 0; index < counts_.size(); ++index) {
initial.commitd7cae122008-07-26 21:49:38738 counts_[index] -= other.counts_[index];
[email protected]2753b392009-12-28 06:59:52739 DCHECK_GE(counts_[index], 0);
initial.commitd7cae122008-07-26 21:49:38740 }
741}
742
[email protected]55e57d42009-02-25 06:10:17743bool Histogram::SampleSet::Serialize(Pickle* pickle) const {
744 pickle->WriteInt64(sum_);
[email protected]93a41d72010-11-03 23:36:24745 pickle->WriteInt64(redundant_count_);
[email protected]55e57d42009-02-25 06:10:17746 pickle->WriteSize(counts_.size());
747
748 for (size_t index = 0; index < counts_.size(); ++index) {
749 pickle->WriteInt(counts_[index]);
750 }
751
752 return true;
753}
754
755bool Histogram::SampleSet::Deserialize(void** iter, const Pickle& pickle) {
[email protected]2753b392009-12-28 06:59:52756 DCHECK_EQ(counts_.size(), 0u);
757 DCHECK_EQ(sum_, 0);
[email protected]93a41d72010-11-03 23:36:24758 DCHECK_EQ(redundant_count_, 0);
[email protected]55e57d42009-02-25 06:10:17759
760 size_t counts_size;
761
762 if (!pickle.ReadInt64(iter, &sum_) ||
[email protected]93a41d72010-11-03 23:36:24763 !pickle.ReadInt64(iter, &redundant_count_) ||
[email protected]55e57d42009-02-25 06:10:17764 !pickle.ReadSize(iter, &counts_size)) {
765 return false;
766 }
767
[email protected]86440f52009-12-31 05:17:23768 if (counts_size == 0)
[email protected]55e57d42009-02-25 06:10:17769 return false;
770
[email protected]93a41d72010-11-03 23:36:24771 int count = 0;
[email protected]55e57d42009-02-25 06:10:17772 for (size_t index = 0; index < counts_size; ++index) {
[email protected]86440f52009-12-31 05:17:23773 int i;
774 if (!pickle.ReadInt(iter, &i))
[email protected]55e57d42009-02-25 06:10:17775 return false;
[email protected]86440f52009-12-31 05:17:23776 counts_.push_back(i);
[email protected]93a41d72010-11-03 23:36:24777 count += i;
[email protected]55e57d42009-02-25 06:10:17778 }
[email protected]93a41d72010-11-03 23:36:24779 DCHECK_EQ(count, redundant_count_);
780 return count == redundant_count_;
[email protected]55e57d42009-02-25 06:10:17781}
782
initial.commitd7cae122008-07-26 21:49:38783//------------------------------------------------------------------------------
784// LinearHistogram: This histogram uses a traditional set of evenly spaced
785// buckets.
786//------------------------------------------------------------------------------
787
[email protected]b7d08202011-01-25 17:29:39788LinearHistogram::~LinearHistogram() {
789}
790
[email protected]81ce9f3b2011-04-05 04:48:53791Histogram* LinearHistogram::FactoryGet(const std::string& name,
792 Sample minimum,
793 Sample maximum,
794 size_t bucket_count,
795 Flags flags) {
796 Histogram* histogram(NULL);
[email protected]e8829a192009-12-06 00:09:37797
[email protected]2ef3748f2010-10-19 17:33:28798 if (minimum < 1)
[email protected]e8829a192009-12-06 00:09:37799 minimum = 1;
[email protected]2ef3748f2010-10-19 17:33:28800 if (maximum > kSampleType_MAX - 1)
[email protected]e8829a192009-12-06 00:09:37801 maximum = kSampleType_MAX - 1;
802
[email protected]f2bc0cb52010-06-25 15:55:15803 if (!StatisticsRecorder::FindHistogram(name, &histogram)) {
[email protected]81ce9f3b2011-04-05 04:48:53804 // To avoid racy destruction at shutdown, the following will be leaked.
805 LinearHistogram* tentative_histogram =
[email protected]98359ce2011-03-05 06:22:24806 new LinearHistogram(name, minimum, maximum, bucket_count);
[email protected]81ce9f3b2011-04-05 04:48:53807 tentative_histogram->InitializeBucketRange();
808 tentative_histogram->SetFlags(flags);
809 histogram =
810 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37811 }
812
[email protected]2ef3748f2010-10-19 17:33:28813 DCHECK_EQ(LINEAR_HISTOGRAM, histogram->histogram_type());
[email protected]e8829a192009-12-06 00:09:37814 DCHECK(histogram->HasConstructorArguments(minimum, maximum, bucket_count));
[email protected]e8829a192009-12-06 00:09:37815 return histogram;
816}
817
[email protected]81ce9f3b2011-04-05 04:48:53818Histogram* LinearHistogram::FactoryTimeGet(const std::string& name,
819 TimeDelta minimum,
820 TimeDelta maximum,
821 size_t bucket_count,
822 Flags flags) {
[email protected]2753b392009-12-28 06:59:52823 return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
824 bucket_count, flags);
[email protected]e8829a192009-12-06 00:09:37825}
826
[email protected]b7d08202011-01-25 17:29:39827Histogram::ClassType LinearHistogram::histogram_type() const {
828 return LINEAR_HISTOGRAM;
829}
830
831void LinearHistogram::SetRangeDescriptions(
832 const DescriptionPair descriptions[]) {
833 for (int i =0; descriptions[i].description; ++i) {
834 bucket_description_[descriptions[i].sample] = descriptions[i].description;
835 }
[email protected]d4799a32010-09-28 22:54:58836}
837
[email protected]835d7c82010-10-14 04:38:38838LinearHistogram::LinearHistogram(const std::string& name,
839 Sample minimum,
840 Sample maximum,
841 size_t bucket_count)
initial.commitd7cae122008-07-26 21:49:38842 : Histogram(name, minimum >= 1 ? minimum : 1, maximum, bucket_count) {
initial.commitd7cae122008-07-26 21:49:38843}
844
[email protected]e8829a192009-12-06 00:09:37845LinearHistogram::LinearHistogram(const std::string& name,
[email protected]835d7c82010-10-14 04:38:38846 TimeDelta minimum,
847 TimeDelta maximum,
848 size_t bucket_count)
initial.commitd7cae122008-07-26 21:49:38849 : Histogram(name, minimum >= TimeDelta::FromMilliseconds(1) ?
850 minimum : TimeDelta::FromMilliseconds(1),
851 maximum, bucket_count) {
initial.commitd7cae122008-07-26 21:49:38852}
853
initial.commitd7cae122008-07-26 21:49:38854void LinearHistogram::InitializeBucketRange() {
[email protected]70cc56e42010-04-29 22:39:55855 DCHECK_GT(declared_min(), 0); // 0 is the underflow bucket here.
initial.commitd7cae122008-07-26 21:49:38856 double min = declared_min();
857 double max = declared_max();
858 size_t i;
[email protected]55e57d42009-02-25 06:10:17859 for (i = 1; i < bucket_count(); ++i) {
initial.commitd7cae122008-07-26 21:49:38860 double linear_range = (min * (bucket_count() -1 - i) + max * (i - 1)) /
861 (bucket_count() - 2);
862 SetBucketRange(i, static_cast<int> (linear_range + 0.5));
863 }
[email protected]93a41d72010-11-03 23:36:24864 ResetRangeChecksum();
initial.commitd7cae122008-07-26 21:49:38865}
866
initial.commitd7cae122008-07-26 21:49:38867double LinearHistogram::GetBucketSize(Count current, size_t i) const {
[email protected]2ef3748f2010-10-19 17:33:28868 DCHECK_GT(ranges(i + 1), ranges(i));
initial.commitd7cae122008-07-26 21:49:38869 // Adjacent buckets with different widths would have "surprisingly" many (few)
870 // samples in a histogram if we didn't normalize this way.
871 double denominator = ranges(i + 1) - ranges(i);
872 return current/denominator;
873}
874
[email protected]b7d08202011-01-25 17:29:39875const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const {
876 int range = ranges(i);
877 BucketDescriptionMap::const_iterator it = bucket_description_.find(range);
878 if (it == bucket_description_.end())
879 return Histogram::GetAsciiBucketRange(i);
880 return it->second;
881}
882
883bool LinearHistogram::PrintEmptyBucket(size_t index) const {
884 return bucket_description_.find(ranges(index)) == bucket_description_.end();
885}
886
887
initial.commitd7cae122008-07-26 21:49:38888//------------------------------------------------------------------------------
[email protected]e8829a192009-12-06 00:09:37889// This section provides implementation for BooleanHistogram.
890//------------------------------------------------------------------------------
891
[email protected]81ce9f3b2011-04-05 04:48:53892Histogram* BooleanHistogram::FactoryGet(const std::string& name, Flags flags) {
893 Histogram* histogram(NULL);
[email protected]e8829a192009-12-06 00:09:37894
[email protected]f2bc0cb52010-06-25 15:55:15895 if (!StatisticsRecorder::FindHistogram(name, &histogram)) {
[email protected]81ce9f3b2011-04-05 04:48:53896 // To avoid racy destruction at shutdown, the following will be leaked.
897 BooleanHistogram* tentative_histogram = new BooleanHistogram(name);
898 tentative_histogram->InitializeBucketRange();
899 tentative_histogram->SetFlags(flags);
900 histogram =
901 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37902 }
903
[email protected]2ef3748f2010-10-19 17:33:28904 DCHECK_EQ(BOOLEAN_HISTOGRAM, histogram->histogram_type());
[email protected]e8829a192009-12-06 00:09:37905 return histogram;
906}
907
[email protected]5d91c9e2010-07-28 17:25:28908Histogram::ClassType BooleanHistogram::histogram_type() const {
909 return BOOLEAN_HISTOGRAM;
910}
911
912void BooleanHistogram::AddBoolean(bool value) {
913 Add(value ? 1 : 0);
914}
915
916BooleanHistogram::BooleanHistogram(const std::string& name)
917 : LinearHistogram(name, 1, 2, 3) {
918}
initial.commitd7cae122008-07-26 21:49:38919
920//------------------------------------------------------------------------------
[email protected]70cc56e42010-04-29 22:39:55921// CustomHistogram:
922//------------------------------------------------------------------------------
923
[email protected]81ce9f3b2011-04-05 04:48:53924Histogram* CustomHistogram::FactoryGet(const std::string& name,
925 const std::vector<Sample>& custom_ranges,
926 Flags flags) {
927 Histogram* histogram(NULL);
[email protected]70cc56e42010-04-29 22:39:55928
929 // Remove the duplicates in the custom ranges array.
930 std::vector<int> ranges = custom_ranges;
931 ranges.push_back(0); // Ensure we have a zero value.
932 std::sort(ranges.begin(), ranges.end());
933 ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end());
934 if (ranges.size() <= 1) {
935 DCHECK(false);
936 // Note that we pushed a 0 in above, so for defensive code....
937 ranges.push_back(1); // Put in some data so we can index to [1].
938 }
939
940 DCHECK_LT(ranges.back(), kSampleType_MAX);
941
[email protected]f2bc0cb52010-06-25 15:55:15942 if (!StatisticsRecorder::FindHistogram(name, &histogram)) {
[email protected]81ce9f3b2011-04-05 04:48:53943 // To avoid racy destruction at shutdown, the following will be leaked.
944 CustomHistogram* tentative_histogram = new CustomHistogram(name, ranges);
945 tentative_histogram->InitializedCustomBucketRange(ranges);
946 tentative_histogram->SetFlags(flags);
947 histogram =
948 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]70cc56e42010-04-29 22:39:55949 }
950
951 DCHECK_EQ(histogram->histogram_type(), CUSTOM_HISTOGRAM);
952 DCHECK(histogram->HasConstructorArguments(ranges[1], ranges.back(),
953 ranges.size()));
[email protected]70cc56e42010-04-29 22:39:55954 return histogram;
955}
956
[email protected]5d91c9e2010-07-28 17:25:28957Histogram::ClassType CustomHistogram::histogram_type() const {
958 return CUSTOM_HISTOGRAM;
959}
960
[email protected]961fefb2011-05-24 13:59:58961// static
962std::vector<Histogram::Sample> CustomHistogram::ArrayToCustomRanges(
963 const Sample* values, size_t num_values) {
964 std::vector<Sample> all_values;
965 for (size_t i = 0; i < num_values; ++i) {
966 Sample value = values[i];
967 all_values.push_back(value);
968
969 // Ensure that a guard bucket is added. If we end up with duplicate
970 // values, FactoryGet will take care of removing them.
971 all_values.push_back(value + 1);
972 }
973 return all_values;
974}
975
[email protected]70cc56e42010-04-29 22:39:55976CustomHistogram::CustomHistogram(const std::string& name,
[email protected]93a41d72010-11-03 23:36:24977 const std::vector<Sample>& custom_ranges)
[email protected]70cc56e42010-04-29 22:39:55978 : Histogram(name, custom_ranges[1], custom_ranges.back(),
979 custom_ranges.size()) {
980 DCHECK_GT(custom_ranges.size(), 1u);
981 DCHECK_EQ(custom_ranges[0], 0);
[email protected]70cc56e42010-04-29 22:39:55982}
983
[email protected]98359ce2011-03-05 06:22:24984void CustomHistogram::InitializedCustomBucketRange(
985 const std::vector<Sample>& custom_ranges) {
986 DCHECK_GT(custom_ranges.size(), 1u);
987 DCHECK_EQ(custom_ranges[0], 0);
988 DCHECK_LE(custom_ranges.size(), bucket_count());
989 for (size_t index = 0; index < custom_ranges.size(); ++index)
990 SetBucketRange(index, custom_ranges[index]);
[email protected]93a41d72010-11-03 23:36:24991 ResetRangeChecksum();
[email protected]70cc56e42010-04-29 22:39:55992}
993
994double CustomHistogram::GetBucketSize(Count current, size_t i) const {
995 return 1;
996}
997
998//------------------------------------------------------------------------------
initial.commitd7cae122008-07-26 21:49:38999// The next section handles global (central) support for all histograms, as well
1000// as startup/teardown of this service.
1001//------------------------------------------------------------------------------
1002
1003// This singleton instance should be started during the single threaded portion
1004// of main(), and hence it is not thread safe. It initializes globals to
1005// provide support for all future calls.
1006StatisticsRecorder::StatisticsRecorder() {
1007 DCHECK(!histograms_);
[email protected]d14425542010-12-23 14:40:101008 if (lock_ == NULL) {
1009 // This will leak on purpose. It's the only way to make sure we won't race
1010 // against the static uninitialization of the module while one of our
1011 // static methods relying on the lock get called at an inappropriate time
1012 // during the termination phase. Since it's a static data member, we will
1013 // leak one per process, which would be similar to the instance allocated
1014 // during static initialization and released only on process termination.
[email protected]bc581a682011-01-01 23:16:201015 lock_ = new base::Lock;
[email protected]d14425542010-12-23 14:40:101016 }
[email protected]bc581a682011-01-01 23:16:201017 base::AutoLock auto_lock(*lock_);
initial.commitd7cae122008-07-26 21:49:381018 histograms_ = new HistogramMap;
[email protected]908de522011-10-20 00:55:001019 ranges_ = new RangesMap;
initial.commitd7cae122008-07-26 21:49:381020}
1021
1022StatisticsRecorder::~StatisticsRecorder() {
[email protected]d14425542010-12-23 14:40:101023 DCHECK(histograms_ && lock_);
initial.commitd7cae122008-07-26 21:49:381024
1025 if (dump_on_exit_) {
1026 std::string output;
1027 WriteGraph("", &output);
[email protected]a42d4632011-10-26 21:48:001028 DLOG(INFO) << output;
initial.commitd7cae122008-07-26 21:49:381029 }
initial.commitd7cae122008-07-26 21:49:381030 // Clean up.
[email protected]d14425542010-12-23 14:40:101031 HistogramMap* histograms = NULL;
1032 {
[email protected]bc581a682011-01-01 23:16:201033 base::AutoLock auto_lock(*lock_);
[email protected]d14425542010-12-23 14:40:101034 histograms = histograms_;
1035 histograms_ = NULL;
1036 }
[email protected]908de522011-10-20 00:55:001037 RangesMap* ranges = NULL;
1038 {
1039 base::AutoLock auto_lock(*lock_);
1040 ranges = ranges_;
1041 ranges_ = NULL;
1042 }
1043 // We are going to leak the histograms and the ranges.
[email protected]d14425542010-12-23 14:40:101044 delete histograms;
[email protected]908de522011-10-20 00:55:001045 delete ranges;
[email protected]d14425542010-12-23 14:40:101046 // We don't delete lock_ on purpose to avoid having to properly protect
1047 // against it going away after we checked for NULL in the static methods.
initial.commitd7cae122008-07-26 21:49:381048}
1049
1050// static
[email protected]d14425542010-12-23 14:40:101051bool StatisticsRecorder::IsActive() {
1052 if (lock_ == NULL)
1053 return false;
[email protected]bc581a682011-01-01 23:16:201054 base::AutoLock auto_lock(*lock_);
initial.commitd7cae122008-07-26 21:49:381055 return NULL != histograms_;
1056}
1057
[email protected]81ce9f3b2011-04-05 04:48:531058Histogram* StatisticsRecorder::RegisterOrDeleteDuplicate(Histogram* histogram) {
[email protected]9b3bbd62011-05-31 07:37:091059 // As per crbug.com/79322 the histograms are intentionally leaked, so we need
1060 // to annotate them. Because ANNOTATE_LEAKING_OBJECT_PTR may be used only once
1061 // for an object, the duplicates should not be annotated.
1062 // Callers are responsible for not calling RegisterOrDeleteDuplicate(ptr)
1063 // twice if (lock_ == NULL) || (!histograms_).
[email protected]81ce9f3b2011-04-05 04:48:531064 DCHECK(histogram->HasValidRangeChecksum());
[email protected]9b3bbd62011-05-31 07:37:091065 if (lock_ == NULL) {
1066 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322
[email protected]81ce9f3b2011-04-05 04:48:531067 return histogram;
[email protected]9b3bbd62011-05-31 07:37:091068 }
[email protected]bc581a682011-01-01 23:16:201069 base::AutoLock auto_lock(*lock_);
[email protected]9b3bbd62011-05-31 07:37:091070 if (!histograms_) {
1071 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322
[email protected]81ce9f3b2011-04-05 04:48:531072 return histogram;
[email protected]9b3bbd62011-05-31 07:37:091073 }
[email protected]81ce9f3b2011-04-05 04:48:531074 const std::string name = histogram->histogram_name();
[email protected]98359ce2011-03-05 06:22:241075 HistogramMap::iterator it = histograms_->find(name);
[email protected]cc82864b2010-08-17 19:46:511076 // Avoid overwriting a previous registration.
[email protected]81ce9f3b2011-04-05 04:48:531077 if (histograms_->end() == it) {
1078 (*histograms_)[name] = histogram;
[email protected]9b3bbd62011-05-31 07:37:091079 ANNOTATE_LEAKING_OBJECT_PTR(histogram); // see crbug.com/79322
[email protected]908de522011-10-20 00:55:001080 RegisterOrDeleteDuplicateRanges(histogram);
1081 ++number_of_histograms_;
[email protected]81ce9f3b2011-04-05 04:48:531082 } else {
1083 delete histogram; // We already have one by this name.
1084 histogram = it->second;
1085 }
1086 return histogram;
initial.commitd7cae122008-07-26 21:49:381087}
1088
1089// static
[email protected]908de522011-10-20 00:55:001090void StatisticsRecorder::RegisterOrDeleteDuplicateRanges(Histogram* histogram) {
1091 DCHECK(histogram);
1092 CachedRanges* histogram_ranges = histogram->cached_ranges();
1093 DCHECK(histogram_ranges);
1094 uint32 checksum = histogram->range_checksum();
1095 histogram_ranges->SetRangeChecksum(checksum);
1096
1097 RangesMap::iterator ranges_it = ranges_->find(checksum);
1098 if (ranges_->end() == ranges_it) {
1099 // Register the new CachedRanges.
1100 std::list<CachedRanges*>* checksum_matching_list(
1101 new std::list<CachedRanges*>());
1102 checksum_matching_list->push_front(histogram_ranges);
1103 (*ranges_)[checksum] = checksum_matching_list;
1104 return;
1105 }
1106
1107 // Use the registered CachedRanges if the registered CachedRanges has same
1108 // ranges_ as |histogram|'s CachedRanges.
1109 std::list<CachedRanges*>* checksum_matching_list = ranges_it->second;
1110 std::list<CachedRanges*>::iterator checksum_matching_list_it;
1111 for (checksum_matching_list_it = checksum_matching_list->begin();
1112 checksum_matching_list_it != checksum_matching_list->end();
1113 ++checksum_matching_list_it) {
1114 CachedRanges* existing_histogram_ranges = *checksum_matching_list_it;
1115 DCHECK(existing_histogram_ranges);
1116 if (existing_histogram_ranges->Equals(histogram_ranges)) {
1117 histogram->set_cached_ranges(existing_histogram_ranges);
1118 ++number_of_vectors_saved_;
1119 saved_ranges_size_ += histogram_ranges->size();
1120 delete histogram_ranges;
1121 return;
1122 }
1123 }
1124
1125 // We haven't found a CachedRanges which has the same ranges. Register the
1126 // new CachedRanges.
1127 DCHECK(checksum_matching_list_it == checksum_matching_list->end());
1128 checksum_matching_list->push_front(histogram_ranges);
1129}
1130
1131// static
1132void StatisticsRecorder::CollectHistogramStats(const std::string& suffix) {
1133 static int uma_upload_attempt = 0;
1134 ++uma_upload_attempt;
1135 if (uma_upload_attempt == 1) {
1136 UMA_HISTOGRAM_COUNTS_10000(
1137 "Histogram.SharedRange.Count.FirstUpload." + suffix,
1138 number_of_histograms_);
1139 UMA_HISTOGRAM_COUNTS_10000(
1140 "Histogram.SharedRange.RangesSaved.FirstUpload." + suffix,
1141 number_of_vectors_saved_);
1142 UMA_HISTOGRAM_COUNTS(
1143 "Histogram.SharedRange.ElementsSaved.FirstUpload." + suffix,
1144 static_cast<int>(saved_ranges_size_));
1145 number_of_histograms_ = 0;
1146 number_of_vectors_saved_ = 0;
1147 saved_ranges_size_ = 0;
1148 return;
1149 }
1150 if (uma_upload_attempt == 2) {
1151 UMA_HISTOGRAM_COUNTS_10000(
1152 "Histogram.SharedRange.Count.SecondUpload." + suffix,
1153 number_of_histograms_);
1154 UMA_HISTOGRAM_COUNTS_10000(
1155 "Histogram.SharedRange.RangesSaved.SecondUpload." + suffix,
1156 number_of_vectors_saved_);
1157 UMA_HISTOGRAM_COUNTS(
1158 "Histogram.SharedRange.ElementsSaved.SecondUpload." + suffix,
1159 static_cast<int>(saved_ranges_size_));
1160 number_of_histograms_ = 0;
1161 number_of_vectors_saved_ = 0;
1162 saved_ranges_size_ = 0;
1163 return;
1164 }
1165 UMA_HISTOGRAM_COUNTS_10000(
1166 "Histogram.SharedRange.Count.RestOfUploads." + suffix,
1167 number_of_histograms_);
1168 UMA_HISTOGRAM_COUNTS_10000(
1169 "Histogram.SharedRange.RangesSaved.RestOfUploads." + suffix,
1170 number_of_vectors_saved_);
1171 UMA_HISTOGRAM_COUNTS(
1172 "Histogram.SharedRange.ElementsSaved.RestOfUploads." + suffix,
1173 static_cast<int>(saved_ranges_size_));
1174}
1175
1176// static
initial.commitd7cae122008-07-26 21:49:381177void StatisticsRecorder::WriteHTMLGraph(const std::string& query,
1178 std::string* output) {
[email protected]d14425542010-12-23 14:40:101179 if (!IsActive())
initial.commitd7cae122008-07-26 21:49:381180 return;
initial.commitd7cae122008-07-26 21:49:381181
1182 Histograms snapshot;
1183 GetSnapshot(query, &snapshot);
1184 for (Histograms::iterator it = snapshot.begin();
1185 it != snapshot.end();
[email protected]55e57d42009-02-25 06:10:171186 ++it) {
initial.commitd7cae122008-07-26 21:49:381187 (*it)->WriteHTMLGraph(output);
1188 output->append("<br><hr><br>");
1189 }
initial.commitd7cae122008-07-26 21:49:381190}
1191
1192// static
1193void StatisticsRecorder::WriteGraph(const std::string& query,
[email protected]55e57d42009-02-25 06:10:171194 std::string* output) {
[email protected]d14425542010-12-23 14:40:101195 if (!IsActive())
initial.commitd7cae122008-07-26 21:49:381196 return;
[email protected]835d7c82010-10-14 04:38:381197 if (query.length())
1198 StringAppendF(output, "Collections of histograms for %s\n", query.c_str());
1199 else
initial.commitd7cae122008-07-26 21:49:381200 output->append("Collections of all histograms\n");
1201
1202 Histograms snapshot;
1203 GetSnapshot(query, &snapshot);
1204 for (Histograms::iterator it = snapshot.begin();
1205 it != snapshot.end();
[email protected]55e57d42009-02-25 06:10:171206 ++it) {
initial.commitd7cae122008-07-26 21:49:381207 (*it)->WriteAscii(true, "\n", output);
1208 output->append("\n");
1209 }
1210}
1211
1212// static
1213void StatisticsRecorder::GetHistograms(Histograms* output) {
[email protected]d14425542010-12-23 14:40:101214 if (lock_ == NULL)
initial.commitd7cae122008-07-26 21:49:381215 return;
[email protected]bc581a682011-01-01 23:16:201216 base::AutoLock auto_lock(*lock_);
[email protected]d14425542010-12-23 14:40:101217 if (!histograms_)
1218 return;
initial.commitd7cae122008-07-26 21:49:381219 for (HistogramMap::iterator it = histograms_->begin();
1220 histograms_->end() != it;
[email protected]55e57d42009-02-25 06:10:171221 ++it) {
[email protected]2ef3748f2010-10-19 17:33:281222 DCHECK_EQ(it->first, it->second->histogram_name());
initial.commitd7cae122008-07-26 21:49:381223 output->push_back(it->second);
1224 }
1225}
1226
[email protected]e8829a192009-12-06 00:09:371227bool StatisticsRecorder::FindHistogram(const std::string& name,
[email protected]81ce9f3b2011-04-05 04:48:531228 Histogram** histogram) {
[email protected]d14425542010-12-23 14:40:101229 if (lock_ == NULL)
[email protected]e8829a192009-12-06 00:09:371230 return false;
[email protected]bc581a682011-01-01 23:16:201231 base::AutoLock auto_lock(*lock_);
[email protected]d14425542010-12-23 14:40:101232 if (!histograms_)
1233 return false;
[email protected]e8829a192009-12-06 00:09:371234 HistogramMap::iterator it = histograms_->find(name);
1235 if (histograms_->end() == it)
1236 return false;
1237 *histogram = it->second;
1238 return true;
[email protected]55e57d42009-02-25 06:10:171239}
1240
initial.commitd7cae122008-07-26 21:49:381241// private static
1242void StatisticsRecorder::GetSnapshot(const std::string& query,
1243 Histograms* snapshot) {
[email protected]d14425542010-12-23 14:40:101244 if (lock_ == NULL)
1245 return;
[email protected]bc581a682011-01-01 23:16:201246 base::AutoLock auto_lock(*lock_);
[email protected]d14425542010-12-23 14:40:101247 if (!histograms_)
1248 return;
initial.commitd7cae122008-07-26 21:49:381249 for (HistogramMap::iterator it = histograms_->begin();
1250 histograms_->end() != it;
[email protected]55e57d42009-02-25 06:10:171251 ++it) {
initial.commitd7cae122008-07-26 21:49:381252 if (it->first.find(query) != std::string::npos)
1253 snapshot->push_back(it->second);
1254 }
1255}
1256
[email protected]908de522011-10-20 00:55:001257CachedRanges::CachedRanges(size_t bucket_count, int initial_value)
1258 : ranges_(bucket_count, initial_value),
1259 range_checksum_(0) {
1260}
1261
1262CachedRanges::~CachedRanges() {
1263}
1264
1265void CachedRanges::SetBucketRange(size_t i, Histogram::Sample value) {
1266 DCHECK_LT(i, ranges_.size());
1267 DCHECK_GE(value, 0);
1268 ranges_[i] = value;
1269}
1270
1271bool CachedRanges::Equals(CachedRanges* other) const {
1272 if (range_checksum_ != other->range_checksum_)
1273 return false;
1274 if (ranges_.size() != other->ranges_.size())
1275 return false;
1276 for (size_t index = 0; index < ranges_.size(); ++index) {
1277 if (ranges_[index] != other->ranges_[index])
1278 return false;
1279 }
1280 return true;
1281}
1282
initial.commitd7cae122008-07-26 21:49:381283// static
1284StatisticsRecorder::HistogramMap* StatisticsRecorder::histograms_ = NULL;
1285// static
[email protected]908de522011-10-20 00:55:001286StatisticsRecorder::RangesMap* StatisticsRecorder::ranges_ = NULL;
1287// static
[email protected]bc581a682011-01-01 23:16:201288base::Lock* StatisticsRecorder::lock_ = NULL;
initial.commitd7cae122008-07-26 21:49:381289// static
1290bool StatisticsRecorder::dump_on_exit_ = false;
[email protected]835d7c82010-10-14 04:38:381291} // namespace base