blob: 7ff1b16184f10c9e54dd501b1b95a1f34fefc253 [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"
asvitkine454600f2015-06-16 16:34:5020#include "base/metrics/histogram_macros.h"
[email protected]877ef562012-10-20 02:56:1821#include "base/metrics/sample_vector.h"
[email protected]567d30e2012-07-13 21:48:2922#include "base/metrics/statistics_recorder.h"
[email protected]3f383852009-04-03 18:18:5523#include "base/pickle.h"
[email protected]d529cb02013-06-10 19:06:5724#include "base/strings/string_util.h"
25#include "base/strings/stringprintf.h"
[email protected]bc581a682011-01-01 23:16:2026#include "base/synchronization/lock.h"
[email protected]24a7ec52012-10-08 10:31:5027#include "base/values.h"
initial.commitd7cae122008-07-26 21:49:3828
[email protected]835d7c82010-10-14 04:38:3829namespace base {
[email protected]e1acf6f2008-10-27 20:43:3330
[email protected]c50c21d2013-01-11 21:52:4431namespace {
32
33bool ReadHistogramArguments(PickleIterator* iter,
asvitkine24d3e9a2015-05-27 05:22:1434 std::string* histogram_name,
[email protected]c50c21d2013-01-11 21:52:4435 int* flags,
36 int* declared_min,
37 int* declared_max,
pkasting89a19f142014-10-02 03:01:0438 size_t* bucket_count,
[email protected]c50c21d2013-01-11 21:52:4439 uint32* range_checksum) {
40 if (!iter->ReadString(histogram_name) ||
41 !iter->ReadInt(flags) ||
42 !iter->ReadInt(declared_min) ||
43 !iter->ReadInt(declared_max) ||
pkasting89a19f142014-10-02 03:01:0444 !iter->ReadSizeT(bucket_count) ||
[email protected]c50c21d2013-01-11 21:52:4445 !iter->ReadUInt32(range_checksum)) {
46 DLOG(ERROR) << "Pickle error decoding Histogram: " << *histogram_name;
47 return false;
48 }
49
50 // Since these fields may have come from an untrusted renderer, do additional
51 // checks above and beyond those in Histogram::Initialize()
52 if (*declared_max <= 0 ||
53 *declared_min <= 0 ||
54 *declared_max < *declared_min ||
55 INT_MAX / sizeof(HistogramBase::Count) <= *bucket_count ||
56 *bucket_count < 2) {
57 DLOG(ERROR) << "Values error decoding Histogram: " << histogram_name;
58 return false;
59 }
60
61 // We use the arguments to find or create the local version of the histogram
62 // in this process, so we need to clear the IPC flag.
63 DCHECK(*flags & HistogramBase::kIPCSerializationSourceFlag);
64 *flags &= ~HistogramBase::kIPCSerializationSourceFlag;
65
66 return true;
67}
68
69bool ValidateRangeChecksum(const HistogramBase& histogram,
70 uint32 range_checksum) {
71 const Histogram& casted_histogram =
72 static_cast<const Histogram&>(histogram);
73
74 return casted_histogram.bucket_ranges()->checksum() == range_checksum;
75}
76
77} // namespace
78
[email protected]34d062322012-08-01 21:34:0879typedef HistogramBase::Count Count;
80typedef HistogramBase::Sample Sample;
initial.commitd7cae122008-07-26 21:49:3881
[email protected]b122c0c2011-02-23 22:31:1882// static
[email protected]9fce5f02011-03-02 08:04:5783const size_t Histogram::kBucketCount_MAX = 16384u;
[email protected]b122c0c2011-02-23 22:31:1884
asvitkine24d3e9a2015-05-27 05:22:1485HistogramBase* Histogram::FactoryGet(const std::string& name,
[email protected]de415552013-01-23 04:12:1786 Sample minimum,
87 Sample maximum,
88 size_t bucket_count,
89 int32 flags) {
[email protected]e184be902012-08-07 04:49:2490 bool valid_arguments =
91 InspectConstructionArguments(name, &minimum, &maximum, &bucket_count);
92 DCHECK(valid_arguments);
[email protected]a93721e22012-01-06 02:13:2893
[email protected]cc7dec212013-03-01 03:53:2594 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]993ae742012-07-18 18:06:3095 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:5396 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:0897 BucketRanges* ranges = new BucketRanges(bucket_count + 1);
[email protected]15ce3842013-06-27 14:38:4598 InitializeBucketRanges(minimum, maximum, ranges);
[email protected]34d062322012-08-01 21:34:0899 const BucketRanges* registered_ranges =
100 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
101
102 Histogram* tentative_histogram =
[email protected]15ce3842013-06-27 14:38:45103 new Histogram(name, minimum, maximum, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54104
[email protected]81ce9f3b2011-04-05 04:48:53105 tentative_histogram->SetFlags(flags);
106 histogram =
107 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37108 }
109
[email protected]cc7dec212013-03-01 03:53:25110 DCHECK_EQ(HISTOGRAM, histogram->GetHistogramType());
[email protected]65796dc2014-03-13 14:08:18111 if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) {
112 // The construction arguments do not match the existing histogram. This can
113 // come about if an extension updates in the middle of a chrome run and has
114 // changed one of them, or simply by bad code within Chrome itself. We
115 // return NULL here with the expectation that bad code in Chrome will crash
116 // on dereference, but extension/Pepper APIs will guard against NULL and not
117 // crash.
118 DLOG(ERROR) << "Histogram " << name << " has bad construction arguments";
119 return NULL;
120 }
[email protected]e8829a192009-12-06 00:09:37121 return histogram;
122}
123
asvitkine24d3e9a2015-05-27 05:22:14124HistogramBase* Histogram::FactoryTimeGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17125 TimeDelta minimum,
126 TimeDelta maximum,
127 size_t bucket_count,
128 int32 flags) {
pkasting9cf9b94a2014-10-01 22:18:43129 return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()),
130 static_cast<Sample>(maximum.InMilliseconds()), bucket_count,
131 flags);
[email protected]34d062322012-08-01 21:34:08132}
133
asvitkine5c2d5022015-06-19 00:37:50134HistogramBase* Histogram::FactoryGet(const char* name,
135 Sample minimum,
136 Sample maximum,
137 size_t bucket_count,
138 int32 flags) {
139 return FactoryGet(std::string(name), minimum, maximum, bucket_count, flags);
140}
141
142HistogramBase* Histogram::FactoryTimeGet(const char* name,
143 TimeDelta minimum,
144 TimeDelta maximum,
145 size_t bucket_count,
146 int32 flags) {
147 return FactoryTimeGet(std::string(name), minimum, maximum, bucket_count,
148 flags);
149}
150
[email protected]34d062322012-08-01 21:34:08151// Calculate what range of values are held in each bucket.
152// We have to be careful that we don't pick a ratio between starting points in
153// consecutive buckets that is sooo small, that the integer bounds are the same
154// (effectively making one bucket get no values). We need to avoid:
155// ranges(i) == ranges(i + 1)
156// To avoid that, we just do a fine-grained bucket width as far as we need to
157// until we get a ratio that moves us along at least 2 units at a time. From
158// that bucket onward we do use the exponential growth of buckets.
159//
160// static
161void Histogram::InitializeBucketRanges(Sample minimum,
162 Sample maximum,
[email protected]34d062322012-08-01 21:34:08163 BucketRanges* ranges) {
[email protected]34d062322012-08-01 21:34:08164 double log_max = log(static_cast<double>(maximum));
165 double log_ratio;
166 double log_next;
167 size_t bucket_index = 1;
168 Sample current = minimum;
169 ranges->set_range(bucket_index, current);
[email protected]15ce3842013-06-27 14:38:45170 size_t bucket_count = ranges->bucket_count();
[email protected]34d062322012-08-01 21:34:08171 while (bucket_count > ++bucket_index) {
172 double log_current;
173 log_current = log(static_cast<double>(current));
174 // Calculate the count'th root of the range.
175 log_ratio = (log_max - log_current) / (bucket_count - bucket_index);
176 // See where the next bucket would start.
177 log_next = log_current + log_ratio;
178 Sample next;
179 next = static_cast<int>(floor(exp(log_next) + 0.5));
180 if (next > current)
181 current = next;
182 else
183 ++current; // Just do a narrow bucket, and keep trying.
184 ranges->set_range(bucket_index, current);
185 }
[email protected]15ce3842013-06-27 14:38:45186 ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX);
[email protected]34d062322012-08-01 21:34:08187 ranges->ResetChecksum();
188}
189
[email protected]2f7d9cd2012-09-22 03:42:12190// static
191const int Histogram::kCommonRaceBasedCountMismatch = 5;
[email protected]34d062322012-08-01 21:34:08192
[email protected]cc7dec212013-03-01 03:53:25193int Histogram::FindCorruption(const HistogramSamples& samples) const {
[email protected]34d062322012-08-01 21:34:08194 int inconsistencies = NO_INCONSISTENCIES;
195 Sample previous_range = -1; // Bottom range is always 0.
[email protected]34d062322012-08-01 21:34:08196 for (size_t index = 0; index < bucket_count(); ++index) {
[email protected]34d062322012-08-01 21:34:08197 int new_range = ranges(index);
198 if (previous_range >= new_range)
199 inconsistencies |= BUCKET_ORDER_ERROR;
200 previous_range = new_range;
201 }
202
203 if (!bucket_ranges()->HasValidChecksum())
204 inconsistencies |= RANGE_CHECKSUM_ERROR;
205
[email protected]2f7d9cd2012-09-22 03:42:12206 int64 delta64 = samples.redundant_count() - samples.TotalCount();
[email protected]34d062322012-08-01 21:34:08207 if (delta64 != 0) {
208 int delta = static_cast<int>(delta64);
209 if (delta != delta64)
210 delta = INT_MAX; // Flag all giant errors as INT_MAX.
[email protected]34d062322012-08-01 21:34:08211 if (delta > 0) {
212 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta);
213 if (delta > kCommonRaceBasedCountMismatch)
214 inconsistencies |= COUNT_HIGH_ERROR;
215 } else {
216 DCHECK_GT(0, delta);
217 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta);
218 if (-delta > kCommonRaceBasedCountMismatch)
219 inconsistencies |= COUNT_LOW_ERROR;
220 }
221 }
[email protected]cc7dec212013-03-01 03:53:25222 return inconsistencies;
[email protected]34d062322012-08-01 21:34:08223}
224
[email protected]34d062322012-08-01 21:34:08225Sample Histogram::ranges(size_t i) const {
226 return bucket_ranges_->range(i);
227}
228
229size_t Histogram::bucket_count() const {
[email protected]15ce3842013-06-27 14:38:45230 return bucket_ranges_->bucket_count();
[email protected]34d062322012-08-01 21:34:08231}
232
[email protected]34d062322012-08-01 21:34:08233// static
asvitkine24d3e9a2015-05-27 05:22:14234bool Histogram::InspectConstructionArguments(const std::string& name,
[email protected]34d062322012-08-01 21:34:08235 Sample* minimum,
236 Sample* maximum,
237 size_t* bucket_count) {
238 // Defensive code for backward compatibility.
239 if (*minimum < 1) {
[email protected]a5c7bd792012-08-02 00:29:04240 DVLOG(1) << "Histogram: " << name << " has bad minimum: " << *minimum;
[email protected]34d062322012-08-01 21:34:08241 *minimum = 1;
242 }
243 if (*maximum >= kSampleType_MAX) {
[email protected]a5c7bd792012-08-02 00:29:04244 DVLOG(1) << "Histogram: " << name << " has bad maximum: " << *maximum;
245 *maximum = kSampleType_MAX - 1;
[email protected]34d062322012-08-01 21:34:08246 }
[email protected]e184be902012-08-07 04:49:24247 if (*bucket_count >= kBucketCount_MAX) {
248 DVLOG(1) << "Histogram: " << name << " has bad bucket_count: "
249 << *bucket_count;
250 *bucket_count = kBucketCount_MAX - 1;
251 }
[email protected]34d062322012-08-01 21:34:08252
[email protected]e184be902012-08-07 04:49:24253 if (*minimum >= *maximum)
254 return false;
255 if (*bucket_count < 3)
[email protected]34d062322012-08-01 21:34:08256 return false;
257 if (*bucket_count > static_cast<size_t>(*maximum - *minimum + 2))
258 return false;
259 return true;
260}
261
[email protected]07c02402012-10-31 06:20:25262HistogramType Histogram::GetHistogramType() const {
263 return HISTOGRAM;
264}
265
[email protected]15ce3842013-06-27 14:38:45266bool Histogram::HasConstructionArguments(Sample expected_minimum,
267 Sample expected_maximum,
268 size_t expected_bucket_count) const {
269 return ((expected_minimum == declared_min_) &&
270 (expected_maximum == declared_max_) &&
271 (expected_bucket_count == bucket_count()));
[email protected]abae9b022012-10-24 08:18:52272}
273
274void Histogram::Add(int value) {
275 DCHECK_EQ(0, ranges(0));
[email protected]15ce3842013-06-27 14:38:45276 DCHECK_EQ(kSampleType_MAX, ranges(bucket_count()));
[email protected]abae9b022012-10-24 08:18:52277
278 if (value > kSampleType_MAX - 1)
279 value = kSampleType_MAX - 1;
280 if (value < 0)
281 value = 0;
282 samples_->Accumulate(value, 1);
simonhatchdf5a8142015-07-15 22:22:57283
284 FindAndRunCallback(value);
[email protected]abae9b022012-10-24 08:18:52285}
286
287scoped_ptr<HistogramSamples> Histogram::SnapshotSamples() const {
dcheng84b60292014-10-15 17:47:44288 return SnapshotSampleVector().Pass();
[email protected]abae9b022012-10-24 08:18:52289}
290
[email protected]c50c21d2013-01-11 21:52:44291void Histogram::AddSamples(const HistogramSamples& samples) {
292 samples_->Add(samples);
293}
294
295bool Histogram::AddSamplesFromPickle(PickleIterator* iter) {
296 return samples_->AddFromPickle(iter);
297}
298
[email protected]abae9b022012-10-24 08:18:52299// The following methods provide a graphical histogram display.
asvitkine24d3e9a2015-05-27 05:22:14300void Histogram::WriteHTMLGraph(std::string* output) const {
[email protected]abae9b022012-10-24 08:18:52301 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc.
302 output->append("<PRE>");
303 WriteAsciiImpl(true, "<br>", output);
304 output->append("</PRE>");
305}
306
asvitkine24d3e9a2015-05-27 05:22:14307void Histogram::WriteAscii(std::string* output) const {
[email protected]abae9b022012-10-24 08:18:52308 WriteAsciiImpl(true, "\n", output);
309}
310
[email protected]c50c21d2013-01-11 21:52:44311bool Histogram::SerializeInfoImpl(Pickle* pickle) const {
312 DCHECK(bucket_ranges()->HasValidChecksum());
313 return pickle->WriteString(histogram_name()) &&
314 pickle->WriteInt(flags()) &&
315 pickle->WriteInt(declared_min()) &&
316 pickle->WriteInt(declared_max()) &&
pkasting89a19f142014-10-02 03:01:04317 pickle->WriteSizeT(bucket_count()) &&
[email protected]c50c21d2013-01-11 21:52:44318 pickle->WriteUInt32(bucket_ranges()->checksum());
319}
320
asvitkine24d3e9a2015-05-27 05:22:14321Histogram::Histogram(const std::string& name,
[email protected]abae9b022012-10-24 08:18:52322 Sample minimum,
323 Sample maximum,
[email protected]abae9b022012-10-24 08:18:52324 const BucketRanges* ranges)
325 : HistogramBase(name),
326 bucket_ranges_(ranges),
327 declared_min_(minimum),
[email protected]15ce3842013-06-27 14:38:45328 declared_max_(maximum) {
[email protected]abae9b022012-10-24 08:18:52329 if (ranges)
330 samples_.reset(new SampleVector(ranges));
331}
332
333Histogram::~Histogram() {
[email protected]abae9b022012-10-24 08:18:52334}
335
[email protected]34d062322012-08-01 21:34:08336bool Histogram::PrintEmptyBucket(size_t index) const {
337 return true;
338}
339
[email protected]34d062322012-08-01 21:34:08340// Use the actual bucket widths (like a linear histogram) until the widths get
341// over some transition value, and then use that transition width. Exponentials
342// get so big so fast (and we don't expect to see a lot of entries in the large
343// buckets), so we need this to make it possible to see what is going on and
344// not have 0-graphical-height buckets.
345double Histogram::GetBucketSize(Count current, size_t i) const {
346 DCHECK_GT(ranges(i + 1), ranges(i));
347 static const double kTransitionWidth = 5;
348 double denominator = ranges(i + 1) - ranges(i);
349 if (denominator > kTransitionWidth)
350 denominator = kTransitionWidth; // Stop trying to normalize.
351 return current/denominator;
352}
353
asvitkine24d3e9a2015-05-27 05:22:14354const std::string Histogram::GetAsciiBucketRange(size_t i) const {
[email protected]f2bb3202013-04-05 21:21:54355 return GetSimpleAsciiBucketRange(ranges(i));
[email protected]34d062322012-08-01 21:34:08356}
357
[email protected]34d062322012-08-01 21:34:08358//------------------------------------------------------------------------------
359// Private methods
360
[email protected]c50c21d2013-01-11 21:52:44361// static
362HistogramBase* Histogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14363 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44364 int flags;
365 int declared_min;
366 int declared_max;
pkasting89a19f142014-10-02 03:01:04367 size_t bucket_count;
[email protected]c50c21d2013-01-11 21:52:44368 uint32 range_checksum;
369
370 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
371 &declared_max, &bucket_count, &range_checksum)) {
372 return NULL;
373 }
374
375 // Find or create the local version of the histogram in this process.
376 HistogramBase* histogram = Histogram::FactoryGet(
377 histogram_name, declared_min, declared_max, bucket_count, flags);
378
379 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
380 // The serialized histogram might be corrupted.
381 return NULL;
382 }
383 return histogram;
384}
385
[email protected]877ef562012-10-20 02:56:18386scoped_ptr<SampleVector> Histogram::SnapshotSampleVector() const {
387 scoped_ptr<SampleVector> samples(new SampleVector(bucket_ranges()));
388 samples->Add(*samples_);
scottmg3bec0ff2015-01-27 22:24:27389 return samples.Pass();
[email protected]877ef562012-10-20 02:56:18390}
391
[email protected]34d062322012-08-01 21:34:08392void Histogram::WriteAsciiImpl(bool graph_it,
asvitkine24d3e9a2015-05-27 05:22:14393 const std::string& newline,
394 std::string* output) const {
[email protected]34d062322012-08-01 21:34:08395 // Get local (stack) copies of all effectively volatile class data so that we
396 // are consistent across our output activities.
[email protected]877ef562012-10-20 02:56:18397 scoped_ptr<SampleVector> snapshot = SnapshotSampleVector();
[email protected]2f7d9cd2012-09-22 03:42:12398 Count sample_count = snapshot->TotalCount();
[email protected]34d062322012-08-01 21:34:08399
[email protected]2f7d9cd2012-09-22 03:42:12400 WriteAsciiHeader(*snapshot, sample_count, output);
[email protected]34d062322012-08-01 21:34:08401 output->append(newline);
402
403 // Prepare to normalize graphical rendering of bucket contents.
404 double max_size = 0;
405 if (graph_it)
[email protected]2f7d9cd2012-09-22 03:42:12406 max_size = GetPeakBucketSize(*snapshot);
[email protected]34d062322012-08-01 21:34:08407
408 // Calculate space needed to print bucket range numbers. Leave room to print
409 // nearly the largest bucket range without sliding over the histogram.
410 size_t largest_non_empty_bucket = bucket_count() - 1;
[email protected]2f7d9cd2012-09-22 03:42:12411 while (0 == snapshot->GetCountAtIndex(largest_non_empty_bucket)) {
[email protected]34d062322012-08-01 21:34:08412 if (0 == largest_non_empty_bucket)
413 break; // All buckets are empty.
414 --largest_non_empty_bucket;
415 }
416
417 // Calculate largest print width needed for any of our bucket range displays.
418 size_t print_width = 1;
419 for (size_t i = 0; i < bucket_count(); ++i) {
[email protected]2f7d9cd2012-09-22 03:42:12420 if (snapshot->GetCountAtIndex(i)) {
[email protected]34d062322012-08-01 21:34:08421 size_t width = GetAsciiBucketRange(i).size() + 1;
422 if (width > print_width)
423 print_width = width;
424 }
425 }
426
427 int64 remaining = sample_count;
428 int64 past = 0;
429 // Output the actual histogram graph.
430 for (size_t i = 0; i < bucket_count(); ++i) {
[email protected]2f7d9cd2012-09-22 03:42:12431 Count current = snapshot->GetCountAtIndex(i);
[email protected]34d062322012-08-01 21:34:08432 if (!current && !PrintEmptyBucket(i))
433 continue;
434 remaining -= current;
asvitkine24d3e9a2015-05-27 05:22:14435 std::string range = GetAsciiBucketRange(i);
[email protected]34d062322012-08-01 21:34:08436 output->append(range);
437 for (size_t j = 0; range.size() + j < print_width + 1; ++j)
438 output->push_back(' ');
[email protected]2f7d9cd2012-09-22 03:42:12439 if (0 == current && i < bucket_count() - 1 &&
440 0 == snapshot->GetCountAtIndex(i + 1)) {
441 while (i < bucket_count() - 1 &&
442 0 == snapshot->GetCountAtIndex(i + 1)) {
[email protected]34d062322012-08-01 21:34:08443 ++i;
[email protected]2f7d9cd2012-09-22 03:42:12444 }
[email protected]34d062322012-08-01 21:34:08445 output->append("... ");
446 output->append(newline);
447 continue; // No reason to plot emptiness.
448 }
449 double current_size = GetBucketSize(current, i);
450 if (graph_it)
451 WriteAsciiBucketGraph(current_size, max_size, output);
452 WriteAsciiBucketContext(past, current, remaining, i, output);
453 output->append(newline);
454 past += current;
455 }
456 DCHECK_EQ(sample_count, past);
457}
458
[email protected]2f7d9cd2012-09-22 03:42:12459double Histogram::GetPeakBucketSize(const SampleVector& samples) const {
[email protected]34d062322012-08-01 21:34:08460 double max = 0;
461 for (size_t i = 0; i < bucket_count() ; ++i) {
[email protected]2f7d9cd2012-09-22 03:42:12462 double current_size = GetBucketSize(samples.GetCountAtIndex(i), i);
[email protected]34d062322012-08-01 21:34:08463 if (current_size > max)
464 max = current_size;
465 }
466 return max;
467}
468
[email protected]2f7d9cd2012-09-22 03:42:12469void Histogram::WriteAsciiHeader(const SampleVector& samples,
[email protected]34d062322012-08-01 21:34:08470 Count sample_count,
asvitkine24d3e9a2015-05-27 05:22:14471 std::string* output) const {
[email protected]34d062322012-08-01 21:34:08472 StringAppendF(output,
473 "Histogram: %s recorded %d samples",
474 histogram_name().c_str(),
475 sample_count);
476 if (0 == sample_count) {
[email protected]2f7d9cd2012-09-22 03:42:12477 DCHECK_EQ(samples.sum(), 0);
[email protected]34d062322012-08-01 21:34:08478 } else {
[email protected]2f7d9cd2012-09-22 03:42:12479 double average = static_cast<float>(samples.sum()) / sample_count;
[email protected]34d062322012-08-01 21:34:08480
481 StringAppendF(output, ", average = %.1f", average);
482 }
[email protected]7c7a42752012-08-09 05:14:15483 if (flags() & ~kHexRangePrintingFlag)
484 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag);
[email protected]34d062322012-08-01 21:34:08485}
486
487void Histogram::WriteAsciiBucketContext(const int64 past,
488 const Count current,
489 const int64 remaining,
490 const size_t i,
asvitkine24d3e9a2015-05-27 05:22:14491 std::string* output) const {
[email protected]34d062322012-08-01 21:34:08492 double scaled_sum = (past + current + remaining) / 100.0;
493 WriteAsciiBucketValue(current, scaled_sum, output);
494 if (0 < i) {
495 double percentage = past / scaled_sum;
496 StringAppendF(output, " {%3.1f%%}", percentage);
497 }
498}
499
[email protected]24a7ec52012-10-08 10:31:50500void Histogram::GetParameters(DictionaryValue* params) const {
[email protected]07c02402012-10-31 06:20:25501 params->SetString("type", HistogramTypeToString(GetHistogramType()));
[email protected]24a7ec52012-10-08 10:31:50502 params->SetInteger("min", declared_min());
503 params->SetInteger("max", declared_max());
504 params->SetInteger("bucket_count", static_cast<int>(bucket_count()));
505}
506
[email protected]cdd98fc2013-05-10 09:32:58507void Histogram::GetCountAndBucketData(Count* count,
508 int64* sum,
509 ListValue* buckets) const {
[email protected]877ef562012-10-20 02:56:18510 scoped_ptr<SampleVector> snapshot = SnapshotSampleVector();
[email protected]24a7ec52012-10-08 10:31:50511 *count = snapshot->TotalCount();
[email protected]cdd98fc2013-05-10 09:32:58512 *sum = snapshot->sum();
[email protected]24a7ec52012-10-08 10:31:50513 size_t index = 0;
514 for (size_t i = 0; i < bucket_count(); ++i) {
scottmgdcc933d2015-01-27 21:37:55515 Sample count_at_index = snapshot->GetCountAtIndex(i);
516 if (count_at_index > 0) {
[email protected]24a7ec52012-10-08 10:31:50517 scoped_ptr<DictionaryValue> bucket_value(new DictionaryValue());
518 bucket_value->SetInteger("low", ranges(i));
519 if (i != bucket_count() - 1)
520 bucket_value->SetInteger("high", ranges(i + 1));
scottmgdcc933d2015-01-27 21:37:55521 bucket_value->SetInteger("count", count_at_index);
[email protected]24a7ec52012-10-08 10:31:50522 buckets->Set(index, bucket_value.release());
523 ++index;
524 }
525 }
526}
527
[email protected]34d062322012-08-01 21:34:08528//------------------------------------------------------------------------------
529// LinearHistogram: This histogram uses a traditional set of evenly spaced
530// buckets.
531//------------------------------------------------------------------------------
532
533LinearHistogram::~LinearHistogram() {}
534
asvitkine24d3e9a2015-05-27 05:22:14535HistogramBase* LinearHistogram::FactoryGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17536 Sample minimum,
537 Sample maximum,
538 size_t bucket_count,
539 int32 flags) {
[email protected]07c02402012-10-31 06:20:25540 return FactoryGetWithRangeDescription(
541 name, minimum, maximum, bucket_count, flags, NULL);
542}
543
asvitkine24d3e9a2015-05-27 05:22:14544HistogramBase* LinearHistogram::FactoryTimeGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17545 TimeDelta minimum,
546 TimeDelta maximum,
547 size_t bucket_count,
548 int32 flags) {
pkasting9cf9b94a2014-10-01 22:18:43549 return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()),
550 static_cast<Sample>(maximum.InMilliseconds()), bucket_count,
551 flags);
[email protected]07c02402012-10-31 06:20:25552}
553
asvitkine5c2d5022015-06-19 00:37:50554HistogramBase* LinearHistogram::FactoryGet(const char* name,
555 Sample minimum,
556 Sample maximum,
557 size_t bucket_count,
558 int32 flags) {
559 return FactoryGet(std::string(name), minimum, maximum, bucket_count, flags);
560}
561
562HistogramBase* LinearHistogram::FactoryTimeGet(const char* name,
563 TimeDelta minimum,
564 TimeDelta maximum,
565 size_t bucket_count,
566 int32 flags) {
567 return FactoryTimeGet(std::string(name), minimum, maximum, bucket_count,
568 flags);
569}
570
[email protected]de415552013-01-23 04:12:17571HistogramBase* LinearHistogram::FactoryGetWithRangeDescription(
[email protected]07c02402012-10-31 06:20:25572 const std::string& name,
573 Sample minimum,
574 Sample maximum,
575 size_t bucket_count,
576 int32 flags,
577 const DescriptionPair descriptions[]) {
[email protected]e184be902012-08-07 04:49:24578 bool valid_arguments = Histogram::InspectConstructionArguments(
579 name, &minimum, &maximum, &bucket_count);
580 DCHECK(valid_arguments);
[email protected]34d062322012-08-01 21:34:08581
[email protected]cc7dec212013-03-01 03:53:25582 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]34d062322012-08-01 21:34:08583 if (!histogram) {
584 // To avoid racy destruction at shutdown, the following will be leaked.
585 BucketRanges* ranges = new BucketRanges(bucket_count + 1);
[email protected]15ce3842013-06-27 14:38:45586 InitializeBucketRanges(minimum, maximum, ranges);
[email protected]34d062322012-08-01 21:34:08587 const BucketRanges* registered_ranges =
588 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
589
590 LinearHistogram* tentative_histogram =
[email protected]15ce3842013-06-27 14:38:45591 new LinearHistogram(name, minimum, maximum, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54592
[email protected]07c02402012-10-31 06:20:25593 // Set range descriptions.
594 if (descriptions) {
595 for (int i = 0; descriptions[i].description; ++i) {
596 tentative_histogram->bucket_description_[descriptions[i].sample] =
597 descriptions[i].description;
598 }
599 }
600
[email protected]34d062322012-08-01 21:34:08601 tentative_histogram->SetFlags(flags);
602 histogram =
603 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
604 }
605
[email protected]cc7dec212013-03-01 03:53:25606 DCHECK_EQ(LINEAR_HISTOGRAM, histogram->GetHistogramType());
[email protected]65796dc2014-03-13 14:08:18607 if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) {
608 // The construction arguments do not match the existing histogram. This can
609 // come about if an extension updates in the middle of a chrome run and has
610 // changed one of them, or simply by bad code within Chrome itself. We
611 // return NULL here with the expectation that bad code in Chrome will crash
612 // on dereference, but extension/Pepper APIs will guard against NULL and not
613 // crash.
614 DLOG(ERROR) << "Histogram " << name << " has bad construction arguments";
615 return NULL;
616 }
[email protected]34d062322012-08-01 21:34:08617 return histogram;
618}
619
[email protected]07c02402012-10-31 06:20:25620HistogramType LinearHistogram::GetHistogramType() const {
[email protected]b7d08202011-01-25 17:29:39621 return LINEAR_HISTOGRAM;
622}
623
asvitkine24d3e9a2015-05-27 05:22:14624LinearHistogram::LinearHistogram(const std::string& name,
[email protected]835d7c82010-10-14 04:38:38625 Sample minimum,
626 Sample maximum,
[email protected]34d062322012-08-01 21:34:08627 const BucketRanges* ranges)
[email protected]15ce3842013-06-27 14:38:45628 : Histogram(name, minimum, maximum, ranges) {
initial.commitd7cae122008-07-26 21:49:38629}
630
initial.commitd7cae122008-07-26 21:49:38631double LinearHistogram::GetBucketSize(Count current, size_t i) const {
[email protected]2ef3748f2010-10-19 17:33:28632 DCHECK_GT(ranges(i + 1), ranges(i));
initial.commitd7cae122008-07-26 21:49:38633 // Adjacent buckets with different widths would have "surprisingly" many (few)
634 // samples in a histogram if we didn't normalize this way.
635 double denominator = ranges(i + 1) - ranges(i);
636 return current/denominator;
637}
638
asvitkine24d3e9a2015-05-27 05:22:14639const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const {
[email protected]b7d08202011-01-25 17:29:39640 int range = ranges(i);
641 BucketDescriptionMap::const_iterator it = bucket_description_.find(range);
642 if (it == bucket_description_.end())
643 return Histogram::GetAsciiBucketRange(i);
644 return it->second;
645}
646
647bool LinearHistogram::PrintEmptyBucket(size_t index) const {
648 return bucket_description_.find(ranges(index)) == bucket_description_.end();
649}
650
[email protected]34d062322012-08-01 21:34:08651// static
652void LinearHistogram::InitializeBucketRanges(Sample minimum,
653 Sample maximum,
[email protected]34d062322012-08-01 21:34:08654 BucketRanges* ranges) {
[email protected]34d062322012-08-01 21:34:08655 double min = minimum;
656 double max = maximum;
[email protected]15ce3842013-06-27 14:38:45657 size_t bucket_count = ranges->bucket_count();
658 for (size_t i = 1; i < bucket_count; ++i) {
[email protected]34d062322012-08-01 21:34:08659 double linear_range =
[email protected]15ce3842013-06-27 14:38:45660 (min * (bucket_count - 1 - i) + max * (i - 1)) / (bucket_count - 2);
[email protected]34d062322012-08-01 21:34:08661 ranges->set_range(i, static_cast<Sample>(linear_range + 0.5));
662 }
[email protected]15ce3842013-06-27 14:38:45663 ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX);
[email protected]34d062322012-08-01 21:34:08664 ranges->ResetChecksum();
665}
[email protected]b7d08202011-01-25 17:29:39666
[email protected]c50c21d2013-01-11 21:52:44667// static
668HistogramBase* LinearHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14669 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44670 int flags;
671 int declared_min;
672 int declared_max;
pkasting89a19f142014-10-02 03:01:04673 size_t bucket_count;
[email protected]c50c21d2013-01-11 21:52:44674 uint32 range_checksum;
675
676 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
677 &declared_max, &bucket_count, &range_checksum)) {
678 return NULL;
679 }
680
681 HistogramBase* histogram = LinearHistogram::FactoryGet(
682 histogram_name, declared_min, declared_max, bucket_count, flags);
683 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
684 // The serialized histogram might be corrupted.
685 return NULL;
686 }
687 return histogram;
688}
689
initial.commitd7cae122008-07-26 21:49:38690//------------------------------------------------------------------------------
[email protected]e8829a192009-12-06 00:09:37691// This section provides implementation for BooleanHistogram.
692//------------------------------------------------------------------------------
693
asvitkine24d3e9a2015-05-27 05:22:14694HistogramBase* BooleanHistogram::FactoryGet(const std::string& name,
695 int32 flags) {
[email protected]cc7dec212013-03-01 03:53:25696 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]993ae742012-07-18 18:06:30697 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:53698 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:08699 BucketRanges* ranges = new BucketRanges(4);
[email protected]15ce3842013-06-27 14:38:45700 LinearHistogram::InitializeBucketRanges(1, 2, ranges);
[email protected]34d062322012-08-01 21:34:08701 const BucketRanges* registered_ranges =
702 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
703
704 BooleanHistogram* tentative_histogram =
705 new BooleanHistogram(name, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54706
[email protected]81ce9f3b2011-04-05 04:48:53707 tentative_histogram->SetFlags(flags);
708 histogram =
709 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37710 }
711
[email protected]cc7dec212013-03-01 03:53:25712 DCHECK_EQ(BOOLEAN_HISTOGRAM, histogram->GetHistogramType());
[email protected]e8829a192009-12-06 00:09:37713 return histogram;
714}
715
asvitkine5c2d5022015-06-19 00:37:50716HistogramBase* BooleanHistogram::FactoryGet(const char* name, int32 flags) {
717 return FactoryGet(std::string(name), flags);
718}
719
[email protected]07c02402012-10-31 06:20:25720HistogramType BooleanHistogram::GetHistogramType() const {
[email protected]5d91c9e2010-07-28 17:25:28721 return BOOLEAN_HISTOGRAM;
722}
723
asvitkine24d3e9a2015-05-27 05:22:14724BooleanHistogram::BooleanHistogram(const std::string& name,
[email protected]34d062322012-08-01 21:34:08725 const BucketRanges* ranges)
[email protected]15ce3842013-06-27 14:38:45726 : LinearHistogram(name, 1, 2, ranges) {}
initial.commitd7cae122008-07-26 21:49:38727
[email protected]c50c21d2013-01-11 21:52:44728HistogramBase* BooleanHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14729 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44730 int flags;
731 int declared_min;
732 int declared_max;
pkasting89a19f142014-10-02 03:01:04733 size_t bucket_count;
[email protected]c50c21d2013-01-11 21:52:44734 uint32 range_checksum;
735
736 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
737 &declared_max, &bucket_count, &range_checksum)) {
738 return NULL;
739 }
740
741 HistogramBase* histogram = BooleanHistogram::FactoryGet(
742 histogram_name, flags);
743 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
744 // The serialized histogram might be corrupted.
745 return NULL;
746 }
747 return histogram;
748}
749
initial.commitd7cae122008-07-26 21:49:38750//------------------------------------------------------------------------------
[email protected]70cc56e42010-04-29 22:39:55751// CustomHistogram:
752//------------------------------------------------------------------------------
753
asvitkine24d3e9a2015-05-27 05:22:14754HistogramBase* CustomHistogram::FactoryGet(
755 const std::string& name,
756 const std::vector<Sample>& custom_ranges,
757 int32 flags) {
[email protected]34d062322012-08-01 21:34:08758 CHECK(ValidateCustomRanges(custom_ranges));
[email protected]70cc56e42010-04-29 22:39:55759
[email protected]cc7dec212013-03-01 03:53:25760 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]993ae742012-07-18 18:06:30761 if (!histogram) {
[email protected]34d062322012-08-01 21:34:08762 BucketRanges* ranges = CreateBucketRangesFromCustomRanges(custom_ranges);
763 const BucketRanges* registered_ranges =
764 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
765
[email protected]81ce9f3b2011-04-05 04:48:53766 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:08767 CustomHistogram* tentative_histogram =
768 new CustomHistogram(name, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54769
[email protected]81ce9f3b2011-04-05 04:48:53770 tentative_histogram->SetFlags(flags);
[email protected]34d062322012-08-01 21:34:08771
[email protected]81ce9f3b2011-04-05 04:48:53772 histogram =
773 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]70cc56e42010-04-29 22:39:55774 }
775
[email protected]cc7dec212013-03-01 03:53:25776 DCHECK_EQ(histogram->GetHistogramType(), CUSTOM_HISTOGRAM);
[email protected]70cc56e42010-04-29 22:39:55777 return histogram;
778}
779
asvitkine5c2d5022015-06-19 00:37:50780HistogramBase* CustomHistogram::FactoryGet(
781 const char* name,
782 const std::vector<Sample>& custom_ranges,
783 int32 flags) {
784 return FactoryGet(std::string(name), custom_ranges, flags);
785}
786
[email protected]07c02402012-10-31 06:20:25787HistogramType CustomHistogram::GetHistogramType() const {
[email protected]5d91c9e2010-07-28 17:25:28788 return CUSTOM_HISTOGRAM;
789}
790
[email protected]961fefb2011-05-24 13:59:58791// static
asvitkine24d3e9a2015-05-27 05:22:14792std::vector<Sample> CustomHistogram::ArrayToCustomRanges(
[email protected]961fefb2011-05-24 13:59:58793 const Sample* values, size_t num_values) {
asvitkine24d3e9a2015-05-27 05:22:14794 std::vector<Sample> all_values;
[email protected]961fefb2011-05-24 13:59:58795 for (size_t i = 0; i < num_values; ++i) {
796 Sample value = values[i];
797 all_values.push_back(value);
798
799 // Ensure that a guard bucket is added. If we end up with duplicate
800 // values, FactoryGet will take care of removing them.
801 all_values.push_back(value + 1);
802 }
803 return all_values;
804}
805
asvitkine24d3e9a2015-05-27 05:22:14806CustomHistogram::CustomHistogram(const std::string& name,
[email protected]34d062322012-08-01 21:34:08807 const BucketRanges* ranges)
808 : Histogram(name,
809 ranges->range(1),
[email protected]15ce3842013-06-27 14:38:45810 ranges->range(ranges->bucket_count() - 1),
[email protected]34d062322012-08-01 21:34:08811 ranges) {}
[email protected]70cc56e42010-04-29 22:39:55812
[email protected]c50c21d2013-01-11 21:52:44813bool CustomHistogram::SerializeInfoImpl(Pickle* pickle) const {
814 if (!Histogram::SerializeInfoImpl(pickle))
815 return false;
[email protected]cd56dff2011-11-13 04:19:15816
[email protected]c50c21d2013-01-11 21:52:44817 // Serialize ranges. First and last ranges are alwasy 0 and INT_MAX, so don't
818 // write them.
[email protected]15ce3842013-06-27 14:38:45819 for (size_t i = 1; i < bucket_ranges()->bucket_count(); ++i) {
[email protected]c50c21d2013-01-11 21:52:44820 if (!pickle->WriteInt(bucket_ranges()->range(i)))
[email protected]cd56dff2011-11-13 04:19:15821 return false;
822 }
823 return true;
824}
825
[email protected]70cc56e42010-04-29 22:39:55826double CustomHistogram::GetBucketSize(Count current, size_t i) const {
827 return 1;
828}
829
[email protected]34d062322012-08-01 21:34:08830// static
[email protected]c50c21d2013-01-11 21:52:44831HistogramBase* CustomHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14832 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44833 int flags;
834 int declared_min;
835 int declared_max;
pkasting89a19f142014-10-02 03:01:04836 size_t bucket_count;
[email protected]c50c21d2013-01-11 21:52:44837 uint32 range_checksum;
838
839 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
840 &declared_max, &bucket_count, &range_checksum)) {
841 return NULL;
842 }
843
844 // First and last ranges are not serialized.
asvitkine24d3e9a2015-05-27 05:22:14845 std::vector<Sample> sample_ranges(bucket_count - 1);
[email protected]c50c21d2013-01-11 21:52:44846
847 for (size_t i = 0; i < sample_ranges.size(); ++i) {
848 if (!iter->ReadInt(&sample_ranges[i]))
849 return NULL;
850 }
851
852 HistogramBase* histogram = CustomHistogram::FactoryGet(
853 histogram_name, sample_ranges, flags);
854 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
855 // The serialized histogram might be corrupted.
856 return NULL;
857 }
858 return histogram;
859}
860
861// static
[email protected]34d062322012-08-01 21:34:08862bool CustomHistogram::ValidateCustomRanges(
asvitkine24d3e9a2015-05-27 05:22:14863 const std::vector<Sample>& custom_ranges) {
[email protected]640d95ef2012-08-04 06:23:03864 bool has_valid_range = false;
[email protected]34d062322012-08-01 21:34:08865 for (size_t i = 0; i < custom_ranges.size(); i++) {
[email protected]640d95ef2012-08-04 06:23:03866 Sample sample = custom_ranges[i];
867 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1)
[email protected]34d062322012-08-01 21:34:08868 return false;
[email protected]640d95ef2012-08-04 06:23:03869 if (sample != 0)
870 has_valid_range = true;
[email protected]34d062322012-08-01 21:34:08871 }
[email protected]640d95ef2012-08-04 06:23:03872 return has_valid_range;
[email protected]34d062322012-08-01 21:34:08873}
874
875// static
876BucketRanges* CustomHistogram::CreateBucketRangesFromCustomRanges(
asvitkine24d3e9a2015-05-27 05:22:14877 const std::vector<Sample>& custom_ranges) {
[email protected]34d062322012-08-01 21:34:08878 // Remove the duplicates in the custom ranges array.
asvitkine24d3e9a2015-05-27 05:22:14879 std::vector<int> ranges = custom_ranges;
[email protected]34d062322012-08-01 21:34:08880 ranges.push_back(0); // Ensure we have a zero value.
881 ranges.push_back(HistogramBase::kSampleType_MAX);
882 std::sort(ranges.begin(), ranges.end());
883 ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end());
884
885 BucketRanges* bucket_ranges = new BucketRanges(ranges.size());
886 for (size_t i = 0; i < ranges.size(); i++) {
887 bucket_ranges->set_range(i, ranges[i]);
888 }
889 bucket_ranges->ResetChecksum();
890 return bucket_ranges;
891}
892
[email protected]835d7c82010-10-14 04:38:38893} // namespace base