blob: 15504205e87e8db89e2d5d77fe8bd75c5e310a92 [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
[email protected]ec0c0aa2012-08-14 02:02:0017#include "base/compiler_specific.h"
18#include "base/debug/alias.h"
initial.commitd7cae122008-07-26 21:49:3819#include "base/logging.h"
[email protected]877ef562012-10-20 02:56:1820#include "base/metrics/sample_vector.h"
[email protected]567d30e2012-07-13 21:48:2921#include "base/metrics/statistics_recorder.h"
[email protected]3f383852009-04-03 18:18:5522#include "base/pickle.h"
[email protected]d529cb02013-06-10 19:06:5723#include "base/strings/string_util.h"
24#include "base/strings/stringprintf.h"
[email protected]bc581a682011-01-01 23:16:2025#include "base/synchronization/lock.h"
[email protected]24a7ec52012-10-08 10:31:5026#include "base/values.h"
initial.commitd7cae122008-07-26 21:49:3827
[email protected]835d7c82010-10-14 04:38:3828namespace base {
[email protected]e1acf6f2008-10-27 20:43:3329
[email protected]c50c21d2013-01-11 21:52:4430namespace {
31
32bool ReadHistogramArguments(PickleIterator* iter,
asvitkine24d3e9a2015-05-27 05:22:1433 std::string* histogram_name,
[email protected]c50c21d2013-01-11 21:52:4434 int* flags,
35 int* declared_min,
36 int* declared_max,
pkasting89a19f142014-10-02 03:01:0437 size_t* bucket_count,
[email protected]c50c21d2013-01-11 21:52:4438 uint32* range_checksum) {
39 if (!iter->ReadString(histogram_name) ||
40 !iter->ReadInt(flags) ||
41 !iter->ReadInt(declared_min) ||
42 !iter->ReadInt(declared_max) ||
pkasting89a19f142014-10-02 03:01:0443 !iter->ReadSizeT(bucket_count) ||
[email protected]c50c21d2013-01-11 21:52:4444 !iter->ReadUInt32(range_checksum)) {
45 DLOG(ERROR) << "Pickle error decoding Histogram: " << *histogram_name;
46 return false;
47 }
48
49 // Since these fields may have come from an untrusted renderer, do additional
50 // checks above and beyond those in Histogram::Initialize()
51 if (*declared_max <= 0 ||
52 *declared_min <= 0 ||
53 *declared_max < *declared_min ||
54 INT_MAX / sizeof(HistogramBase::Count) <= *bucket_count ||
55 *bucket_count < 2) {
56 DLOG(ERROR) << "Values error decoding Histogram: " << histogram_name;
57 return false;
58 }
59
60 // We use the arguments to find or create the local version of the histogram
61 // in this process, so we need to clear the IPC flag.
62 DCHECK(*flags & HistogramBase::kIPCSerializationSourceFlag);
63 *flags &= ~HistogramBase::kIPCSerializationSourceFlag;
64
65 return true;
66}
67
68bool ValidateRangeChecksum(const HistogramBase& histogram,
69 uint32 range_checksum) {
70 const Histogram& casted_histogram =
71 static_cast<const Histogram&>(histogram);
72
73 return casted_histogram.bucket_ranges()->checksum() == range_checksum;
74}
75
76} // namespace
77
[email protected]34d062322012-08-01 21:34:0878typedef HistogramBase::Count Count;
79typedef HistogramBase::Sample Sample;
initial.commitd7cae122008-07-26 21:49:3880
[email protected]b122c0c2011-02-23 22:31:1881// static
[email protected]9fce5f02011-03-02 08:04:5782const size_t Histogram::kBucketCount_MAX = 16384u;
[email protected]b122c0c2011-02-23 22:31:1883
asvitkine24d3e9a2015-05-27 05:22:1484HistogramBase* Histogram::FactoryGet(const std::string& name,
[email protected]de415552013-01-23 04:12:1785 Sample minimum,
86 Sample maximum,
87 size_t bucket_count,
88 int32 flags) {
[email protected]e184be902012-08-07 04:49:2489 bool valid_arguments =
90 InspectConstructionArguments(name, &minimum, &maximum, &bucket_count);
91 DCHECK(valid_arguments);
[email protected]a93721e22012-01-06 02:13:2892
[email protected]cc7dec212013-03-01 03:53:2593 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]993ae742012-07-18 18:06:3094 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:5395 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:0896 BucketRanges* ranges = new BucketRanges(bucket_count + 1);
[email protected]15ce3842013-06-27 14:38:4597 InitializeBucketRanges(minimum, maximum, ranges);
[email protected]34d062322012-08-01 21:34:0898 const BucketRanges* registered_ranges =
99 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
100
101 Histogram* tentative_histogram =
[email protected]15ce3842013-06-27 14:38:45102 new Histogram(name, minimum, maximum, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54103
[email protected]81ce9f3b2011-04-05 04:48:53104 tentative_histogram->SetFlags(flags);
105 histogram =
106 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37107 }
108
[email protected]cc7dec212013-03-01 03:53:25109 DCHECK_EQ(HISTOGRAM, histogram->GetHistogramType());
[email protected]65796dc2014-03-13 14:08:18110 if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) {
111 // The construction arguments do not match the existing histogram. This can
112 // come about if an extension updates in the middle of a chrome run and has
113 // changed one of them, or simply by bad code within Chrome itself. We
114 // return NULL here with the expectation that bad code in Chrome will crash
115 // on dereference, but extension/Pepper APIs will guard against NULL and not
116 // crash.
117 DLOG(ERROR) << "Histogram " << name << " has bad construction arguments";
118 return NULL;
119 }
[email protected]e8829a192009-12-06 00:09:37120 return histogram;
121}
122
asvitkine24d3e9a2015-05-27 05:22:14123HistogramBase* Histogram::FactoryTimeGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17124 TimeDelta minimum,
125 TimeDelta maximum,
126 size_t bucket_count,
127 int32 flags) {
pkasting9cf9b94a2014-10-01 22:18:43128 return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()),
129 static_cast<Sample>(maximum.InMilliseconds()), bucket_count,
130 flags);
[email protected]34d062322012-08-01 21:34:08131}
132
[email protected]34d062322012-08-01 21:34:08133// Calculate what range of values are held in each bucket.
134// We have to be careful that we don't pick a ratio between starting points in
135// consecutive buckets that is sooo small, that the integer bounds are the same
136// (effectively making one bucket get no values). We need to avoid:
137// ranges(i) == ranges(i + 1)
138// To avoid that, we just do a fine-grained bucket width as far as we need to
139// until we get a ratio that moves us along at least 2 units at a time. From
140// that bucket onward we do use the exponential growth of buckets.
141//
142// static
143void Histogram::InitializeBucketRanges(Sample minimum,
144 Sample maximum,
[email protected]34d062322012-08-01 21:34:08145 BucketRanges* ranges) {
[email protected]34d062322012-08-01 21:34:08146 double log_max = log(static_cast<double>(maximum));
147 double log_ratio;
148 double log_next;
149 size_t bucket_index = 1;
150 Sample current = minimum;
151 ranges->set_range(bucket_index, current);
[email protected]15ce3842013-06-27 14:38:45152 size_t bucket_count = ranges->bucket_count();
[email protected]34d062322012-08-01 21:34:08153 while (bucket_count > ++bucket_index) {
154 double log_current;
155 log_current = log(static_cast<double>(current));
156 // Calculate the count'th root of the range.
157 log_ratio = (log_max - log_current) / (bucket_count - bucket_index);
158 // See where the next bucket would start.
159 log_next = log_current + log_ratio;
160 Sample next;
161 next = static_cast<int>(floor(exp(log_next) + 0.5));
162 if (next > current)
163 current = next;
164 else
165 ++current; // Just do a narrow bucket, and keep trying.
166 ranges->set_range(bucket_index, current);
167 }
[email protected]15ce3842013-06-27 14:38:45168 ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX);
[email protected]34d062322012-08-01 21:34:08169 ranges->ResetChecksum();
170}
171
[email protected]2f7d9cd2012-09-22 03:42:12172// static
173const int Histogram::kCommonRaceBasedCountMismatch = 5;
[email protected]34d062322012-08-01 21:34:08174
[email protected]cc7dec212013-03-01 03:53:25175int Histogram::FindCorruption(const HistogramSamples& samples) const {
[email protected]34d062322012-08-01 21:34:08176 int inconsistencies = NO_INCONSISTENCIES;
177 Sample previous_range = -1; // Bottom range is always 0.
[email protected]34d062322012-08-01 21:34:08178 for (size_t index = 0; index < bucket_count(); ++index) {
[email protected]34d062322012-08-01 21:34:08179 int new_range = ranges(index);
180 if (previous_range >= new_range)
181 inconsistencies |= BUCKET_ORDER_ERROR;
182 previous_range = new_range;
183 }
184
185 if (!bucket_ranges()->HasValidChecksum())
186 inconsistencies |= RANGE_CHECKSUM_ERROR;
187
[email protected]2f7d9cd2012-09-22 03:42:12188 int64 delta64 = samples.redundant_count() - samples.TotalCount();
[email protected]34d062322012-08-01 21:34:08189 if (delta64 != 0) {
190 int delta = static_cast<int>(delta64);
191 if (delta != delta64)
192 delta = INT_MAX; // Flag all giant errors as INT_MAX.
[email protected]34d062322012-08-01 21:34:08193 if (delta > 0) {
194 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta);
195 if (delta > kCommonRaceBasedCountMismatch)
196 inconsistencies |= COUNT_HIGH_ERROR;
197 } else {
198 DCHECK_GT(0, delta);
199 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta);
200 if (-delta > kCommonRaceBasedCountMismatch)
201 inconsistencies |= COUNT_LOW_ERROR;
202 }
203 }
[email protected]cc7dec212013-03-01 03:53:25204 return inconsistencies;
[email protected]34d062322012-08-01 21:34:08205}
206
[email protected]34d062322012-08-01 21:34:08207Sample Histogram::ranges(size_t i) const {
208 return bucket_ranges_->range(i);
209}
210
211size_t Histogram::bucket_count() const {
[email protected]15ce3842013-06-27 14:38:45212 return bucket_ranges_->bucket_count();
[email protected]34d062322012-08-01 21:34:08213}
214
[email protected]34d062322012-08-01 21:34:08215// static
asvitkine24d3e9a2015-05-27 05:22:14216bool Histogram::InspectConstructionArguments(const std::string& name,
[email protected]34d062322012-08-01 21:34:08217 Sample* minimum,
218 Sample* maximum,
219 size_t* bucket_count) {
220 // Defensive code for backward compatibility.
221 if (*minimum < 1) {
[email protected]a5c7bd792012-08-02 00:29:04222 DVLOG(1) << "Histogram: " << name << " has bad minimum: " << *minimum;
[email protected]34d062322012-08-01 21:34:08223 *minimum = 1;
224 }
225 if (*maximum >= kSampleType_MAX) {
[email protected]a5c7bd792012-08-02 00:29:04226 DVLOG(1) << "Histogram: " << name << " has bad maximum: " << *maximum;
227 *maximum = kSampleType_MAX - 1;
[email protected]34d062322012-08-01 21:34:08228 }
[email protected]e184be902012-08-07 04:49:24229 if (*bucket_count >= kBucketCount_MAX) {
230 DVLOG(1) << "Histogram: " << name << " has bad bucket_count: "
231 << *bucket_count;
232 *bucket_count = kBucketCount_MAX - 1;
233 }
[email protected]34d062322012-08-01 21:34:08234
[email protected]e184be902012-08-07 04:49:24235 if (*minimum >= *maximum)
236 return false;
237 if (*bucket_count < 3)
[email protected]34d062322012-08-01 21:34:08238 return false;
239 if (*bucket_count > static_cast<size_t>(*maximum - *minimum + 2))
240 return false;
241 return true;
242}
243
[email protected]07c02402012-10-31 06:20:25244HistogramType Histogram::GetHistogramType() const {
245 return HISTOGRAM;
246}
247
[email protected]15ce3842013-06-27 14:38:45248bool Histogram::HasConstructionArguments(Sample expected_minimum,
249 Sample expected_maximum,
250 size_t expected_bucket_count) const {
251 return ((expected_minimum == declared_min_) &&
252 (expected_maximum == declared_max_) &&
253 (expected_bucket_count == bucket_count()));
[email protected]abae9b022012-10-24 08:18:52254}
255
256void Histogram::Add(int value) {
257 DCHECK_EQ(0, ranges(0));
[email protected]15ce3842013-06-27 14:38:45258 DCHECK_EQ(kSampleType_MAX, ranges(bucket_count()));
[email protected]abae9b022012-10-24 08:18:52259
260 if (value > kSampleType_MAX - 1)
261 value = kSampleType_MAX - 1;
262 if (value < 0)
263 value = 0;
264 samples_->Accumulate(value, 1);
265}
266
267scoped_ptr<HistogramSamples> Histogram::SnapshotSamples() const {
dcheng84b60292014-10-15 17:47:44268 return SnapshotSampleVector().Pass();
[email protected]abae9b022012-10-24 08:18:52269}
270
[email protected]c50c21d2013-01-11 21:52:44271void Histogram::AddSamples(const HistogramSamples& samples) {
272 samples_->Add(samples);
273}
274
275bool Histogram::AddSamplesFromPickle(PickleIterator* iter) {
276 return samples_->AddFromPickle(iter);
277}
278
[email protected]abae9b022012-10-24 08:18:52279// The following methods provide a graphical histogram display.
asvitkine24d3e9a2015-05-27 05:22:14280void Histogram::WriteHTMLGraph(std::string* output) const {
[email protected]abae9b022012-10-24 08:18:52281 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc.
282 output->append("<PRE>");
283 WriteAsciiImpl(true, "<br>", output);
284 output->append("</PRE>");
285}
286
asvitkine24d3e9a2015-05-27 05:22:14287void Histogram::WriteAscii(std::string* output) const {
[email protected]abae9b022012-10-24 08:18:52288 WriteAsciiImpl(true, "\n", output);
289}
290
[email protected]c50c21d2013-01-11 21:52:44291bool Histogram::SerializeInfoImpl(Pickle* pickle) const {
292 DCHECK(bucket_ranges()->HasValidChecksum());
293 return pickle->WriteString(histogram_name()) &&
294 pickle->WriteInt(flags()) &&
295 pickle->WriteInt(declared_min()) &&
296 pickle->WriteInt(declared_max()) &&
pkasting89a19f142014-10-02 03:01:04297 pickle->WriteSizeT(bucket_count()) &&
[email protected]c50c21d2013-01-11 21:52:44298 pickle->WriteUInt32(bucket_ranges()->checksum());
299}
300
asvitkine24d3e9a2015-05-27 05:22:14301Histogram::Histogram(const std::string& name,
[email protected]abae9b022012-10-24 08:18:52302 Sample minimum,
303 Sample maximum,
[email protected]abae9b022012-10-24 08:18:52304 const BucketRanges* ranges)
305 : HistogramBase(name),
306 bucket_ranges_(ranges),
307 declared_min_(minimum),
[email protected]15ce3842013-06-27 14:38:45308 declared_max_(maximum) {
[email protected]abae9b022012-10-24 08:18:52309 if (ranges)
310 samples_.reset(new SampleVector(ranges));
311}
312
313Histogram::~Histogram() {
[email protected]abae9b022012-10-24 08:18:52314}
315
[email protected]34d062322012-08-01 21:34:08316bool Histogram::PrintEmptyBucket(size_t index) const {
317 return true;
318}
319
[email protected]34d062322012-08-01 21:34:08320// Use the actual bucket widths (like a linear histogram) until the widths get
321// over some transition value, and then use that transition width. Exponentials
322// get so big so fast (and we don't expect to see a lot of entries in the large
323// buckets), so we need this to make it possible to see what is going on and
324// not have 0-graphical-height buckets.
325double Histogram::GetBucketSize(Count current, size_t i) const {
326 DCHECK_GT(ranges(i + 1), ranges(i));
327 static const double kTransitionWidth = 5;
328 double denominator = ranges(i + 1) - ranges(i);
329 if (denominator > kTransitionWidth)
330 denominator = kTransitionWidth; // Stop trying to normalize.
331 return current/denominator;
332}
333
asvitkine24d3e9a2015-05-27 05:22:14334const std::string Histogram::GetAsciiBucketRange(size_t i) const {
[email protected]f2bb3202013-04-05 21:21:54335 return GetSimpleAsciiBucketRange(ranges(i));
[email protected]34d062322012-08-01 21:34:08336}
337
[email protected]34d062322012-08-01 21:34:08338//------------------------------------------------------------------------------
339// Private methods
340
[email protected]c50c21d2013-01-11 21:52:44341// static
342HistogramBase* Histogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14343 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44344 int flags;
345 int declared_min;
346 int declared_max;
pkasting89a19f142014-10-02 03:01:04347 size_t bucket_count;
[email protected]c50c21d2013-01-11 21:52:44348 uint32 range_checksum;
349
350 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
351 &declared_max, &bucket_count, &range_checksum)) {
352 return NULL;
353 }
354
355 // Find or create the local version of the histogram in this process.
356 HistogramBase* histogram = Histogram::FactoryGet(
357 histogram_name, declared_min, declared_max, bucket_count, flags);
358
359 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
360 // The serialized histogram might be corrupted.
361 return NULL;
362 }
363 return histogram;
364}
365
[email protected]877ef562012-10-20 02:56:18366scoped_ptr<SampleVector> Histogram::SnapshotSampleVector() const {
367 scoped_ptr<SampleVector> samples(new SampleVector(bucket_ranges()));
368 samples->Add(*samples_);
scottmg3bec0ff2015-01-27 22:24:27369 return samples.Pass();
[email protected]877ef562012-10-20 02:56:18370}
371
[email protected]34d062322012-08-01 21:34:08372void Histogram::WriteAsciiImpl(bool graph_it,
asvitkine24d3e9a2015-05-27 05:22:14373 const std::string& newline,
374 std::string* output) const {
[email protected]34d062322012-08-01 21:34:08375 // Get local (stack) copies of all effectively volatile class data so that we
376 // are consistent across our output activities.
[email protected]877ef562012-10-20 02:56:18377 scoped_ptr<SampleVector> snapshot = SnapshotSampleVector();
[email protected]2f7d9cd2012-09-22 03:42:12378 Count sample_count = snapshot->TotalCount();
[email protected]34d062322012-08-01 21:34:08379
[email protected]2f7d9cd2012-09-22 03:42:12380 WriteAsciiHeader(*snapshot, sample_count, output);
[email protected]34d062322012-08-01 21:34:08381 output->append(newline);
382
383 // Prepare to normalize graphical rendering of bucket contents.
384 double max_size = 0;
385 if (graph_it)
[email protected]2f7d9cd2012-09-22 03:42:12386 max_size = GetPeakBucketSize(*snapshot);
[email protected]34d062322012-08-01 21:34:08387
388 // Calculate space needed to print bucket range numbers. Leave room to print
389 // nearly the largest bucket range without sliding over the histogram.
390 size_t largest_non_empty_bucket = bucket_count() - 1;
[email protected]2f7d9cd2012-09-22 03:42:12391 while (0 == snapshot->GetCountAtIndex(largest_non_empty_bucket)) {
[email protected]34d062322012-08-01 21:34:08392 if (0 == largest_non_empty_bucket)
393 break; // All buckets are empty.
394 --largest_non_empty_bucket;
395 }
396
397 // Calculate largest print width needed for any of our bucket range displays.
398 size_t print_width = 1;
399 for (size_t i = 0; i < bucket_count(); ++i) {
[email protected]2f7d9cd2012-09-22 03:42:12400 if (snapshot->GetCountAtIndex(i)) {
[email protected]34d062322012-08-01 21:34:08401 size_t width = GetAsciiBucketRange(i).size() + 1;
402 if (width > print_width)
403 print_width = width;
404 }
405 }
406
407 int64 remaining = sample_count;
408 int64 past = 0;
409 // Output the actual histogram graph.
410 for (size_t i = 0; i < bucket_count(); ++i) {
[email protected]2f7d9cd2012-09-22 03:42:12411 Count current = snapshot->GetCountAtIndex(i);
[email protected]34d062322012-08-01 21:34:08412 if (!current && !PrintEmptyBucket(i))
413 continue;
414 remaining -= current;
asvitkine24d3e9a2015-05-27 05:22:14415 std::string range = GetAsciiBucketRange(i);
[email protected]34d062322012-08-01 21:34:08416 output->append(range);
417 for (size_t j = 0; range.size() + j < print_width + 1; ++j)
418 output->push_back(' ');
[email protected]2f7d9cd2012-09-22 03:42:12419 if (0 == current && i < bucket_count() - 1 &&
420 0 == snapshot->GetCountAtIndex(i + 1)) {
421 while (i < bucket_count() - 1 &&
422 0 == snapshot->GetCountAtIndex(i + 1)) {
[email protected]34d062322012-08-01 21:34:08423 ++i;
[email protected]2f7d9cd2012-09-22 03:42:12424 }
[email protected]34d062322012-08-01 21:34:08425 output->append("... ");
426 output->append(newline);
427 continue; // No reason to plot emptiness.
428 }
429 double current_size = GetBucketSize(current, i);
430 if (graph_it)
431 WriteAsciiBucketGraph(current_size, max_size, output);
432 WriteAsciiBucketContext(past, current, remaining, i, output);
433 output->append(newline);
434 past += current;
435 }
436 DCHECK_EQ(sample_count, past);
437}
438
[email protected]2f7d9cd2012-09-22 03:42:12439double Histogram::GetPeakBucketSize(const SampleVector& samples) const {
[email protected]34d062322012-08-01 21:34:08440 double max = 0;
441 for (size_t i = 0; i < bucket_count() ; ++i) {
[email protected]2f7d9cd2012-09-22 03:42:12442 double current_size = GetBucketSize(samples.GetCountAtIndex(i), i);
[email protected]34d062322012-08-01 21:34:08443 if (current_size > max)
444 max = current_size;
445 }
446 return max;
447}
448
[email protected]2f7d9cd2012-09-22 03:42:12449void Histogram::WriteAsciiHeader(const SampleVector& samples,
[email protected]34d062322012-08-01 21:34:08450 Count sample_count,
asvitkine24d3e9a2015-05-27 05:22:14451 std::string* output) const {
[email protected]34d062322012-08-01 21:34:08452 StringAppendF(output,
453 "Histogram: %s recorded %d samples",
454 histogram_name().c_str(),
455 sample_count);
456 if (0 == sample_count) {
[email protected]2f7d9cd2012-09-22 03:42:12457 DCHECK_EQ(samples.sum(), 0);
[email protected]34d062322012-08-01 21:34:08458 } else {
[email protected]2f7d9cd2012-09-22 03:42:12459 double average = static_cast<float>(samples.sum()) / sample_count;
[email protected]34d062322012-08-01 21:34:08460
461 StringAppendF(output, ", average = %.1f", average);
462 }
[email protected]7c7a42752012-08-09 05:14:15463 if (flags() & ~kHexRangePrintingFlag)
464 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag);
[email protected]34d062322012-08-01 21:34:08465}
466
467void Histogram::WriteAsciiBucketContext(const int64 past,
468 const Count current,
469 const int64 remaining,
470 const size_t i,
asvitkine24d3e9a2015-05-27 05:22:14471 std::string* output) const {
[email protected]34d062322012-08-01 21:34:08472 double scaled_sum = (past + current + remaining) / 100.0;
473 WriteAsciiBucketValue(current, scaled_sum, output);
474 if (0 < i) {
475 double percentage = past / scaled_sum;
476 StringAppendF(output, " {%3.1f%%}", percentage);
477 }
478}
479
[email protected]24a7ec52012-10-08 10:31:50480void Histogram::GetParameters(DictionaryValue* params) const {
[email protected]07c02402012-10-31 06:20:25481 params->SetString("type", HistogramTypeToString(GetHistogramType()));
[email protected]24a7ec52012-10-08 10:31:50482 params->SetInteger("min", declared_min());
483 params->SetInteger("max", declared_max());
484 params->SetInteger("bucket_count", static_cast<int>(bucket_count()));
485}
486
[email protected]cdd98fc2013-05-10 09:32:58487void Histogram::GetCountAndBucketData(Count* count,
488 int64* sum,
489 ListValue* buckets) const {
[email protected]877ef562012-10-20 02:56:18490 scoped_ptr<SampleVector> snapshot = SnapshotSampleVector();
[email protected]24a7ec52012-10-08 10:31:50491 *count = snapshot->TotalCount();
[email protected]cdd98fc2013-05-10 09:32:58492 *sum = snapshot->sum();
[email protected]24a7ec52012-10-08 10:31:50493 size_t index = 0;
494 for (size_t i = 0; i < bucket_count(); ++i) {
scottmgdcc933d2015-01-27 21:37:55495 Sample count_at_index = snapshot->GetCountAtIndex(i);
496 if (count_at_index > 0) {
[email protected]24a7ec52012-10-08 10:31:50497 scoped_ptr<DictionaryValue> bucket_value(new DictionaryValue());
498 bucket_value->SetInteger("low", ranges(i));
499 if (i != bucket_count() - 1)
500 bucket_value->SetInteger("high", ranges(i + 1));
scottmgdcc933d2015-01-27 21:37:55501 bucket_value->SetInteger("count", count_at_index);
[email protected]24a7ec52012-10-08 10:31:50502 buckets->Set(index, bucket_value.release());
503 ++index;
504 }
505 }
506}
507
[email protected]34d062322012-08-01 21:34:08508//------------------------------------------------------------------------------
509// LinearHistogram: This histogram uses a traditional set of evenly spaced
510// buckets.
511//------------------------------------------------------------------------------
512
513LinearHistogram::~LinearHistogram() {}
514
asvitkine24d3e9a2015-05-27 05:22:14515HistogramBase* LinearHistogram::FactoryGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17516 Sample minimum,
517 Sample maximum,
518 size_t bucket_count,
519 int32 flags) {
[email protected]07c02402012-10-31 06:20:25520 return FactoryGetWithRangeDescription(
521 name, minimum, maximum, bucket_count, flags, NULL);
522}
523
asvitkine24d3e9a2015-05-27 05:22:14524HistogramBase* LinearHistogram::FactoryTimeGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17525 TimeDelta minimum,
526 TimeDelta maximum,
527 size_t bucket_count,
528 int32 flags) {
pkasting9cf9b94a2014-10-01 22:18:43529 return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()),
530 static_cast<Sample>(maximum.InMilliseconds()), bucket_count,
531 flags);
[email protected]07c02402012-10-31 06:20:25532}
533
[email protected]de415552013-01-23 04:12:17534HistogramBase* LinearHistogram::FactoryGetWithRangeDescription(
[email protected]07c02402012-10-31 06:20:25535 const std::string& name,
536 Sample minimum,
537 Sample maximum,
538 size_t bucket_count,
539 int32 flags,
540 const DescriptionPair descriptions[]) {
[email protected]e184be902012-08-07 04:49:24541 bool valid_arguments = Histogram::InspectConstructionArguments(
542 name, &minimum, &maximum, &bucket_count);
543 DCHECK(valid_arguments);
[email protected]34d062322012-08-01 21:34:08544
[email protected]cc7dec212013-03-01 03:53:25545 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]34d062322012-08-01 21:34:08546 if (!histogram) {
547 // To avoid racy destruction at shutdown, the following will be leaked.
548 BucketRanges* ranges = new BucketRanges(bucket_count + 1);
[email protected]15ce3842013-06-27 14:38:45549 InitializeBucketRanges(minimum, maximum, ranges);
[email protected]34d062322012-08-01 21:34:08550 const BucketRanges* registered_ranges =
551 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
552
553 LinearHistogram* tentative_histogram =
[email protected]15ce3842013-06-27 14:38:45554 new LinearHistogram(name, minimum, maximum, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54555
[email protected]07c02402012-10-31 06:20:25556 // Set range descriptions.
557 if (descriptions) {
558 for (int i = 0; descriptions[i].description; ++i) {
559 tentative_histogram->bucket_description_[descriptions[i].sample] =
560 descriptions[i].description;
561 }
562 }
563
[email protected]34d062322012-08-01 21:34:08564 tentative_histogram->SetFlags(flags);
565 histogram =
566 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
567 }
568
[email protected]cc7dec212013-03-01 03:53:25569 DCHECK_EQ(LINEAR_HISTOGRAM, histogram->GetHistogramType());
[email protected]65796dc2014-03-13 14:08:18570 if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) {
571 // The construction arguments do not match the existing histogram. This can
572 // come about if an extension updates in the middle of a chrome run and has
573 // changed one of them, or simply by bad code within Chrome itself. We
574 // return NULL here with the expectation that bad code in Chrome will crash
575 // on dereference, but extension/Pepper APIs will guard against NULL and not
576 // crash.
577 DLOG(ERROR) << "Histogram " << name << " has bad construction arguments";
578 return NULL;
579 }
[email protected]34d062322012-08-01 21:34:08580 return histogram;
581}
582
[email protected]07c02402012-10-31 06:20:25583HistogramType LinearHistogram::GetHistogramType() const {
[email protected]b7d08202011-01-25 17:29:39584 return LINEAR_HISTOGRAM;
585}
586
asvitkine24d3e9a2015-05-27 05:22:14587LinearHistogram::LinearHistogram(const std::string& name,
[email protected]835d7c82010-10-14 04:38:38588 Sample minimum,
589 Sample maximum,
[email protected]34d062322012-08-01 21:34:08590 const BucketRanges* ranges)
[email protected]15ce3842013-06-27 14:38:45591 : Histogram(name, minimum, maximum, ranges) {
initial.commitd7cae122008-07-26 21:49:38592}
593
initial.commitd7cae122008-07-26 21:49:38594double LinearHistogram::GetBucketSize(Count current, size_t i) const {
[email protected]2ef3748f2010-10-19 17:33:28595 DCHECK_GT(ranges(i + 1), ranges(i));
initial.commitd7cae122008-07-26 21:49:38596 // Adjacent buckets with different widths would have "surprisingly" many (few)
597 // samples in a histogram if we didn't normalize this way.
598 double denominator = ranges(i + 1) - ranges(i);
599 return current/denominator;
600}
601
asvitkine24d3e9a2015-05-27 05:22:14602const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const {
[email protected]b7d08202011-01-25 17:29:39603 int range = ranges(i);
604 BucketDescriptionMap::const_iterator it = bucket_description_.find(range);
605 if (it == bucket_description_.end())
606 return Histogram::GetAsciiBucketRange(i);
607 return it->second;
608}
609
610bool LinearHistogram::PrintEmptyBucket(size_t index) const {
611 return bucket_description_.find(ranges(index)) == bucket_description_.end();
612}
613
[email protected]34d062322012-08-01 21:34:08614// static
615void LinearHistogram::InitializeBucketRanges(Sample minimum,
616 Sample maximum,
[email protected]34d062322012-08-01 21:34:08617 BucketRanges* ranges) {
[email protected]34d062322012-08-01 21:34:08618 double min = minimum;
619 double max = maximum;
[email protected]15ce3842013-06-27 14:38:45620 size_t bucket_count = ranges->bucket_count();
621 for (size_t i = 1; i < bucket_count; ++i) {
[email protected]34d062322012-08-01 21:34:08622 double linear_range =
[email protected]15ce3842013-06-27 14:38:45623 (min * (bucket_count - 1 - i) + max * (i - 1)) / (bucket_count - 2);
[email protected]34d062322012-08-01 21:34:08624 ranges->set_range(i, static_cast<Sample>(linear_range + 0.5));
625 }
[email protected]15ce3842013-06-27 14:38:45626 ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX);
[email protected]34d062322012-08-01 21:34:08627 ranges->ResetChecksum();
628}
[email protected]b7d08202011-01-25 17:29:39629
[email protected]c50c21d2013-01-11 21:52:44630// static
631HistogramBase* LinearHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14632 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44633 int flags;
634 int declared_min;
635 int declared_max;
pkasting89a19f142014-10-02 03:01:04636 size_t bucket_count;
[email protected]c50c21d2013-01-11 21:52:44637 uint32 range_checksum;
638
639 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
640 &declared_max, &bucket_count, &range_checksum)) {
641 return NULL;
642 }
643
644 HistogramBase* histogram = LinearHistogram::FactoryGet(
645 histogram_name, declared_min, declared_max, bucket_count, flags);
646 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
647 // The serialized histogram might be corrupted.
648 return NULL;
649 }
650 return histogram;
651}
652
initial.commitd7cae122008-07-26 21:49:38653//------------------------------------------------------------------------------
[email protected]e8829a192009-12-06 00:09:37654// This section provides implementation for BooleanHistogram.
655//------------------------------------------------------------------------------
656
asvitkine24d3e9a2015-05-27 05:22:14657HistogramBase* BooleanHistogram::FactoryGet(const std::string& name,
658 int32 flags) {
[email protected]cc7dec212013-03-01 03:53:25659 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]993ae742012-07-18 18:06:30660 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:53661 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:08662 BucketRanges* ranges = new BucketRanges(4);
[email protected]15ce3842013-06-27 14:38:45663 LinearHistogram::InitializeBucketRanges(1, 2, ranges);
[email protected]34d062322012-08-01 21:34:08664 const BucketRanges* registered_ranges =
665 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
666
667 BooleanHistogram* tentative_histogram =
668 new BooleanHistogram(name, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54669
[email protected]81ce9f3b2011-04-05 04:48:53670 tentative_histogram->SetFlags(flags);
671 histogram =
672 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37673 }
674
[email protected]cc7dec212013-03-01 03:53:25675 DCHECK_EQ(BOOLEAN_HISTOGRAM, histogram->GetHistogramType());
[email protected]e8829a192009-12-06 00:09:37676 return histogram;
677}
678
[email protected]07c02402012-10-31 06:20:25679HistogramType BooleanHistogram::GetHistogramType() const {
[email protected]5d91c9e2010-07-28 17:25:28680 return BOOLEAN_HISTOGRAM;
681}
682
asvitkine24d3e9a2015-05-27 05:22:14683BooleanHistogram::BooleanHistogram(const std::string& name,
[email protected]34d062322012-08-01 21:34:08684 const BucketRanges* ranges)
[email protected]15ce3842013-06-27 14:38:45685 : LinearHistogram(name, 1, 2, ranges) {}
initial.commitd7cae122008-07-26 21:49:38686
[email protected]c50c21d2013-01-11 21:52:44687HistogramBase* BooleanHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14688 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44689 int flags;
690 int declared_min;
691 int declared_max;
pkasting89a19f142014-10-02 03:01:04692 size_t bucket_count;
[email protected]c50c21d2013-01-11 21:52:44693 uint32 range_checksum;
694
695 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
696 &declared_max, &bucket_count, &range_checksum)) {
697 return NULL;
698 }
699
700 HistogramBase* histogram = BooleanHistogram::FactoryGet(
701 histogram_name, flags);
702 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
703 // The serialized histogram might be corrupted.
704 return NULL;
705 }
706 return histogram;
707}
708
initial.commitd7cae122008-07-26 21:49:38709//------------------------------------------------------------------------------
[email protected]70cc56e42010-04-29 22:39:55710// CustomHistogram:
711//------------------------------------------------------------------------------
712
asvitkine24d3e9a2015-05-27 05:22:14713HistogramBase* CustomHistogram::FactoryGet(
714 const std::string& name,
715 const std::vector<Sample>& custom_ranges,
716 int32 flags) {
[email protected]34d062322012-08-01 21:34:08717 CHECK(ValidateCustomRanges(custom_ranges));
[email protected]70cc56e42010-04-29 22:39:55718
[email protected]cc7dec212013-03-01 03:53:25719 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]993ae742012-07-18 18:06:30720 if (!histogram) {
[email protected]34d062322012-08-01 21:34:08721 BucketRanges* ranges = CreateBucketRangesFromCustomRanges(custom_ranges);
722 const BucketRanges* registered_ranges =
723 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
724
[email protected]81ce9f3b2011-04-05 04:48:53725 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:08726 CustomHistogram* tentative_histogram =
727 new CustomHistogram(name, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54728
[email protected]81ce9f3b2011-04-05 04:48:53729 tentative_histogram->SetFlags(flags);
[email protected]34d062322012-08-01 21:34:08730
[email protected]81ce9f3b2011-04-05 04:48:53731 histogram =
732 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]70cc56e42010-04-29 22:39:55733 }
734
[email protected]cc7dec212013-03-01 03:53:25735 DCHECK_EQ(histogram->GetHistogramType(), CUSTOM_HISTOGRAM);
[email protected]70cc56e42010-04-29 22:39:55736 return histogram;
737}
738
[email protected]07c02402012-10-31 06:20:25739HistogramType CustomHistogram::GetHistogramType() const {
[email protected]5d91c9e2010-07-28 17:25:28740 return CUSTOM_HISTOGRAM;
741}
742
[email protected]961fefb2011-05-24 13:59:58743// static
asvitkine24d3e9a2015-05-27 05:22:14744std::vector<Sample> CustomHistogram::ArrayToCustomRanges(
[email protected]961fefb2011-05-24 13:59:58745 const Sample* values, size_t num_values) {
asvitkine24d3e9a2015-05-27 05:22:14746 std::vector<Sample> all_values;
[email protected]961fefb2011-05-24 13:59:58747 for (size_t i = 0; i < num_values; ++i) {
748 Sample value = values[i];
749 all_values.push_back(value);
750
751 // Ensure that a guard bucket is added. If we end up with duplicate
752 // values, FactoryGet will take care of removing them.
753 all_values.push_back(value + 1);
754 }
755 return all_values;
756}
757
asvitkine24d3e9a2015-05-27 05:22:14758CustomHistogram::CustomHistogram(const std::string& name,
[email protected]34d062322012-08-01 21:34:08759 const BucketRanges* ranges)
760 : Histogram(name,
761 ranges->range(1),
[email protected]15ce3842013-06-27 14:38:45762 ranges->range(ranges->bucket_count() - 1),
[email protected]34d062322012-08-01 21:34:08763 ranges) {}
[email protected]70cc56e42010-04-29 22:39:55764
[email protected]c50c21d2013-01-11 21:52:44765bool CustomHistogram::SerializeInfoImpl(Pickle* pickle) const {
766 if (!Histogram::SerializeInfoImpl(pickle))
767 return false;
[email protected]cd56dff2011-11-13 04:19:15768
[email protected]c50c21d2013-01-11 21:52:44769 // Serialize ranges. First and last ranges are alwasy 0 and INT_MAX, so don't
770 // write them.
[email protected]15ce3842013-06-27 14:38:45771 for (size_t i = 1; i < bucket_ranges()->bucket_count(); ++i) {
[email protected]c50c21d2013-01-11 21:52:44772 if (!pickle->WriteInt(bucket_ranges()->range(i)))
[email protected]cd56dff2011-11-13 04:19:15773 return false;
774 }
775 return true;
776}
777
[email protected]70cc56e42010-04-29 22:39:55778double CustomHistogram::GetBucketSize(Count current, size_t i) const {
779 return 1;
780}
781
[email protected]34d062322012-08-01 21:34:08782// static
[email protected]c50c21d2013-01-11 21:52:44783HistogramBase* CustomHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14784 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44785 int flags;
786 int declared_min;
787 int declared_max;
pkasting89a19f142014-10-02 03:01:04788 size_t bucket_count;
[email protected]c50c21d2013-01-11 21:52:44789 uint32 range_checksum;
790
791 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
792 &declared_max, &bucket_count, &range_checksum)) {
793 return NULL;
794 }
795
796 // First and last ranges are not serialized.
asvitkine24d3e9a2015-05-27 05:22:14797 std::vector<Sample> sample_ranges(bucket_count - 1);
[email protected]c50c21d2013-01-11 21:52:44798
799 for (size_t i = 0; i < sample_ranges.size(); ++i) {
800 if (!iter->ReadInt(&sample_ranges[i]))
801 return NULL;
802 }
803
804 HistogramBase* histogram = CustomHistogram::FactoryGet(
805 histogram_name, sample_ranges, flags);
806 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
807 // The serialized histogram might be corrupted.
808 return NULL;
809 }
810 return histogram;
811}
812
813// static
[email protected]34d062322012-08-01 21:34:08814bool CustomHistogram::ValidateCustomRanges(
asvitkine24d3e9a2015-05-27 05:22:14815 const std::vector<Sample>& custom_ranges) {
[email protected]640d95ef2012-08-04 06:23:03816 bool has_valid_range = false;
[email protected]34d062322012-08-01 21:34:08817 for (size_t i = 0; i < custom_ranges.size(); i++) {
[email protected]640d95ef2012-08-04 06:23:03818 Sample sample = custom_ranges[i];
819 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1)
[email protected]34d062322012-08-01 21:34:08820 return false;
[email protected]640d95ef2012-08-04 06:23:03821 if (sample != 0)
822 has_valid_range = true;
[email protected]34d062322012-08-01 21:34:08823 }
[email protected]640d95ef2012-08-04 06:23:03824 return has_valid_range;
[email protected]34d062322012-08-01 21:34:08825}
826
827// static
828BucketRanges* CustomHistogram::CreateBucketRangesFromCustomRanges(
asvitkine24d3e9a2015-05-27 05:22:14829 const std::vector<Sample>& custom_ranges) {
[email protected]34d062322012-08-01 21:34:08830 // Remove the duplicates in the custom ranges array.
asvitkine24d3e9a2015-05-27 05:22:14831 std::vector<int> ranges = custom_ranges;
[email protected]34d062322012-08-01 21:34:08832 ranges.push_back(0); // Ensure we have a zero value.
833 ranges.push_back(HistogramBase::kSampleType_MAX);
834 std::sort(ranges.begin(), ranges.end());
835 ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end());
836
837 BucketRanges* bucket_ranges = new BucketRanges(ranges.size());
838 for (size_t i = 0; i < ranges.size(); i++) {
839 bucket_ranges->set_range(i, ranges[i]);
840 }
841 bucket_ranges->ResetChecksum();
842 return bucket_ranges;
843}
844
[email protected]835d7c82010-10-14 04:38:38845} // namespace base