blob: 62c2bc808dad38aac0c125ee9ab6bd8d61de52c3 [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
avi9b6f42932015-12-26 22:15:1412#include <limits.h>
initial.commitd7cae122008-07-26 21:49:3813#include <math.h>
[email protected]f1633932010-08-17 23:05:2814
15#include <algorithm>
initial.commitd7cae122008-07-26 21:49:3816#include <string>
17
[email protected]ec0c0aa2012-08-14 02:02:0018#include "base/compiler_specific.h"
19#include "base/debug/alias.h"
initial.commitd7cae122008-07-26 21:49:3820#include "base/logging.h"
asvitkine454600f2015-06-16 16:34:5021#include "base/metrics/histogram_macros.h"
bcwhiteb036e4322015-12-10 18:36:3422#include "base/metrics/metrics_hashes.h"
[email protected]877ef562012-10-20 02:56:1823#include "base/metrics/sample_vector.h"
[email protected]567d30e2012-07-13 21:48:2924#include "base/metrics/statistics_recorder.h"
[email protected]3f383852009-04-03 18:18:5525#include "base/pickle.h"
[email protected]d529cb02013-06-10 19:06:5726#include "base/strings/string_util.h"
27#include "base/strings/stringprintf.h"
[email protected]bc581a682011-01-01 23:16:2028#include "base/synchronization/lock.h"
[email protected]24a7ec52012-10-08 10:31:5029#include "base/values.h"
initial.commitd7cae122008-07-26 21:49:3830
[email protected]835d7c82010-10-14 04:38:3831namespace base {
[email protected]e1acf6f2008-10-27 20:43:3332
[email protected]c50c21d2013-01-11 21:52:4433namespace {
34
35bool ReadHistogramArguments(PickleIterator* iter,
asvitkine24d3e9a2015-05-27 05:22:1436 std::string* histogram_name,
[email protected]c50c21d2013-01-11 21:52:4437 int* flags,
38 int* declared_min,
39 int* declared_max,
pkasting89a19f142014-10-02 03:01:0440 size_t* bucket_count,
avi9b6f42932015-12-26 22:15:1441 uint32_t* range_checksum) {
[email protected]c50c21d2013-01-11 21:52:4442 if (!iter->ReadString(histogram_name) ||
43 !iter->ReadInt(flags) ||
44 !iter->ReadInt(declared_min) ||
45 !iter->ReadInt(declared_max) ||
pkasting89a19f142014-10-02 03:01:0446 !iter->ReadSizeT(bucket_count) ||
[email protected]c50c21d2013-01-11 21:52:4447 !iter->ReadUInt32(range_checksum)) {
48 DLOG(ERROR) << "Pickle error decoding Histogram: " << *histogram_name;
49 return false;
50 }
51
52 // Since these fields may have come from an untrusted renderer, do additional
53 // checks above and beyond those in Histogram::Initialize()
54 if (*declared_max <= 0 ||
55 *declared_min <= 0 ||
56 *declared_max < *declared_min ||
57 INT_MAX / sizeof(HistogramBase::Count) <= *bucket_count ||
58 *bucket_count < 2) {
59 DLOG(ERROR) << "Values error decoding Histogram: " << histogram_name;
60 return false;
61 }
62
63 // We use the arguments to find or create the local version of the histogram
64 // in this process, so we need to clear the IPC flag.
65 DCHECK(*flags & HistogramBase::kIPCSerializationSourceFlag);
66 *flags &= ~HistogramBase::kIPCSerializationSourceFlag;
67
68 return true;
69}
70
71bool ValidateRangeChecksum(const HistogramBase& histogram,
avi9b6f42932015-12-26 22:15:1472 uint32_t range_checksum) {
[email protected]c50c21d2013-01-11 21:52:4473 const Histogram& casted_histogram =
74 static_cast<const Histogram&>(histogram);
75
76 return casted_histogram.bucket_ranges()->checksum() == range_checksum;
77}
78
79} // namespace
80
[email protected]34d062322012-08-01 21:34:0881typedef HistogramBase::Count Count;
82typedef HistogramBase::Sample Sample;
initial.commitd7cae122008-07-26 21:49:3883
[email protected]b122c0c2011-02-23 22:31:1884// static
[email protected]9fce5f02011-03-02 08:04:5785const size_t Histogram::kBucketCount_MAX = 16384u;
[email protected]b122c0c2011-02-23 22:31:1886
asvitkine24d3e9a2015-05-27 05:22:1487HistogramBase* Histogram::FactoryGet(const std::string& name,
[email protected]de415552013-01-23 04:12:1788 Sample minimum,
89 Sample maximum,
90 size_t bucket_count,
avi9b6f42932015-12-26 22:15:1491 int32_t flags) {
[email protected]e184be902012-08-07 04:49:2492 bool valid_arguments =
93 InspectConstructionArguments(name, &minimum, &maximum, &bucket_count);
94 DCHECK(valid_arguments);
[email protected]a93721e22012-01-06 02:13:2895
[email protected]cc7dec212013-03-01 03:53:2596 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]993ae742012-07-18 18:06:3097 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:5398 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:0899 BucketRanges* ranges = new BucketRanges(bucket_count + 1);
[email protected]15ce3842013-06-27 14:38:45100 InitializeBucketRanges(minimum, maximum, ranges);
[email protected]34d062322012-08-01 21:34:08101 const BucketRanges* registered_ranges =
102 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
103
104 Histogram* tentative_histogram =
[email protected]15ce3842013-06-27 14:38:45105 new Histogram(name, minimum, maximum, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54106
[email protected]81ce9f3b2011-04-05 04:48:53107 tentative_histogram->SetFlags(flags);
108 histogram =
109 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37110 }
111
[email protected]cc7dec212013-03-01 03:53:25112 DCHECK_EQ(HISTOGRAM, histogram->GetHistogramType());
[email protected]65796dc2014-03-13 14:08:18113 if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) {
114 // The construction arguments do not match the existing histogram. This can
115 // come about if an extension updates in the middle of a chrome run and has
116 // changed one of them, or simply by bad code within Chrome itself. We
117 // return NULL here with the expectation that bad code in Chrome will crash
118 // on dereference, but extension/Pepper APIs will guard against NULL and not
119 // crash.
120 DLOG(ERROR) << "Histogram " << name << " has bad construction arguments";
121 return NULL;
122 }
[email protected]e8829a192009-12-06 00:09:37123 return histogram;
124}
125
asvitkine24d3e9a2015-05-27 05:22:14126HistogramBase* Histogram::FactoryTimeGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17127 TimeDelta minimum,
128 TimeDelta maximum,
129 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14130 int32_t flags) {
pkasting9cf9b94a2014-10-01 22:18:43131 return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()),
132 static_cast<Sample>(maximum.InMilliseconds()), bucket_count,
133 flags);
[email protected]34d062322012-08-01 21:34:08134}
135
asvitkine5c2d5022015-06-19 00:37:50136HistogramBase* Histogram::FactoryGet(const char* name,
137 Sample minimum,
138 Sample maximum,
139 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14140 int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:50141 return FactoryGet(std::string(name), minimum, maximum, bucket_count, flags);
142}
143
144HistogramBase* Histogram::FactoryTimeGet(const char* name,
145 TimeDelta minimum,
146 TimeDelta maximum,
147 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14148 int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:50149 return FactoryTimeGet(std::string(name), minimum, maximum, bucket_count,
150 flags);
151}
152
[email protected]34d062322012-08-01 21:34:08153// Calculate what range of values are held in each bucket.
154// We have to be careful that we don't pick a ratio between starting points in
155// consecutive buckets that is sooo small, that the integer bounds are the same
156// (effectively making one bucket get no values). We need to avoid:
157// ranges(i) == ranges(i + 1)
158// To avoid that, we just do a fine-grained bucket width as far as we need to
159// until we get a ratio that moves us along at least 2 units at a time. From
160// that bucket onward we do use the exponential growth of buckets.
161//
162// static
163void Histogram::InitializeBucketRanges(Sample minimum,
164 Sample maximum,
[email protected]34d062322012-08-01 21:34:08165 BucketRanges* ranges) {
[email protected]34d062322012-08-01 21:34:08166 double log_max = log(static_cast<double>(maximum));
167 double log_ratio;
168 double log_next;
169 size_t bucket_index = 1;
170 Sample current = minimum;
171 ranges->set_range(bucket_index, current);
[email protected]15ce3842013-06-27 14:38:45172 size_t bucket_count = ranges->bucket_count();
[email protected]34d062322012-08-01 21:34:08173 while (bucket_count > ++bucket_index) {
174 double log_current;
175 log_current = log(static_cast<double>(current));
176 // Calculate the count'th root of the range.
177 log_ratio = (log_max - log_current) / (bucket_count - bucket_index);
178 // See where the next bucket would start.
179 log_next = log_current + log_ratio;
180 Sample next;
181 next = static_cast<int>(floor(exp(log_next) + 0.5));
182 if (next > current)
183 current = next;
184 else
185 ++current; // Just do a narrow bucket, and keep trying.
186 ranges->set_range(bucket_index, current);
187 }
[email protected]15ce3842013-06-27 14:38:45188 ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX);
[email protected]34d062322012-08-01 21:34:08189 ranges->ResetChecksum();
190}
191
[email protected]2f7d9cd2012-09-22 03:42:12192// static
193const int Histogram::kCommonRaceBasedCountMismatch = 5;
[email protected]34d062322012-08-01 21:34:08194
[email protected]cc7dec212013-03-01 03:53:25195int Histogram::FindCorruption(const HistogramSamples& samples) const {
[email protected]34d062322012-08-01 21:34:08196 int inconsistencies = NO_INCONSISTENCIES;
197 Sample previous_range = -1; // Bottom range is always 0.
[email protected]34d062322012-08-01 21:34:08198 for (size_t index = 0; index < bucket_count(); ++index) {
[email protected]34d062322012-08-01 21:34:08199 int new_range = ranges(index);
200 if (previous_range >= new_range)
201 inconsistencies |= BUCKET_ORDER_ERROR;
202 previous_range = new_range;
203 }
204
205 if (!bucket_ranges()->HasValidChecksum())
206 inconsistencies |= RANGE_CHECKSUM_ERROR;
207
avi9b6f42932015-12-26 22:15:14208 int64_t delta64 = samples.redundant_count() - samples.TotalCount();
[email protected]34d062322012-08-01 21:34:08209 if (delta64 != 0) {
210 int delta = static_cast<int>(delta64);
211 if (delta != delta64)
212 delta = INT_MAX; // Flag all giant errors as INT_MAX.
[email protected]34d062322012-08-01 21:34:08213 if (delta > 0) {
214 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta);
215 if (delta > kCommonRaceBasedCountMismatch)
216 inconsistencies |= COUNT_HIGH_ERROR;
217 } else {
218 DCHECK_GT(0, delta);
219 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta);
220 if (-delta > kCommonRaceBasedCountMismatch)
221 inconsistencies |= COUNT_LOW_ERROR;
222 }
223 }
[email protected]cc7dec212013-03-01 03:53:25224 return inconsistencies;
[email protected]34d062322012-08-01 21:34:08225}
226
[email protected]34d062322012-08-01 21:34:08227Sample Histogram::ranges(size_t i) const {
228 return bucket_ranges_->range(i);
229}
230
231size_t Histogram::bucket_count() const {
[email protected]15ce3842013-06-27 14:38:45232 return bucket_ranges_->bucket_count();
[email protected]34d062322012-08-01 21:34:08233}
234
[email protected]34d062322012-08-01 21:34:08235// static
asvitkine24d3e9a2015-05-27 05:22:14236bool Histogram::InspectConstructionArguments(const std::string& name,
[email protected]34d062322012-08-01 21:34:08237 Sample* minimum,
238 Sample* maximum,
239 size_t* bucket_count) {
240 // Defensive code for backward compatibility.
241 if (*minimum < 1) {
[email protected]a5c7bd792012-08-02 00:29:04242 DVLOG(1) << "Histogram: " << name << " has bad minimum: " << *minimum;
[email protected]34d062322012-08-01 21:34:08243 *minimum = 1;
244 }
245 if (*maximum >= kSampleType_MAX) {
[email protected]a5c7bd792012-08-02 00:29:04246 DVLOG(1) << "Histogram: " << name << " has bad maximum: " << *maximum;
247 *maximum = kSampleType_MAX - 1;
[email protected]34d062322012-08-01 21:34:08248 }
[email protected]e184be902012-08-07 04:49:24249 if (*bucket_count >= kBucketCount_MAX) {
250 DVLOG(1) << "Histogram: " << name << " has bad bucket_count: "
251 << *bucket_count;
252 *bucket_count = kBucketCount_MAX - 1;
253 }
[email protected]34d062322012-08-01 21:34:08254
[email protected]e184be902012-08-07 04:49:24255 if (*minimum >= *maximum)
256 return false;
257 if (*bucket_count < 3)
[email protected]34d062322012-08-01 21:34:08258 return false;
259 if (*bucket_count > static_cast<size_t>(*maximum - *minimum + 2))
260 return false;
261 return true;
262}
263
bcwhiteb036e4322015-12-10 18:36:34264uint64_t Histogram::name_hash() const {
265 return samples_->id();
266}
267
[email protected]07c02402012-10-31 06:20:25268HistogramType Histogram::GetHistogramType() const {
269 return HISTOGRAM;
270}
271
[email protected]15ce3842013-06-27 14:38:45272bool Histogram::HasConstructionArguments(Sample expected_minimum,
273 Sample expected_maximum,
274 size_t expected_bucket_count) const {
275 return ((expected_minimum == declared_min_) &&
276 (expected_maximum == declared_max_) &&
277 (expected_bucket_count == bucket_count()));
[email protected]abae9b022012-10-24 08:18:52278}
279
280void Histogram::Add(int value) {
amohammadkhan6779b5c32015-08-05 20:31:11281 AddCount(value, 1);
282}
283
284void Histogram::AddCount(int value, int count) {
[email protected]abae9b022012-10-24 08:18:52285 DCHECK_EQ(0, ranges(0));
[email protected]15ce3842013-06-27 14:38:45286 DCHECK_EQ(kSampleType_MAX, ranges(bucket_count()));
[email protected]abae9b022012-10-24 08:18:52287
288 if (value > kSampleType_MAX - 1)
289 value = kSampleType_MAX - 1;
290 if (value < 0)
291 value = 0;
amohammadkhan6779b5c32015-08-05 20:31:11292 if (count <= 0) {
293 NOTREACHED();
294 return;
295 }
296 samples_->Accumulate(value, count);
simonhatchdf5a8142015-07-15 22:22:57297
298 FindAndRunCallback(value);
[email protected]abae9b022012-10-24 08:18:52299}
300
301scoped_ptr<HistogramSamples> Histogram::SnapshotSamples() const {
danakj0c8d4aa2015-11-25 05:29:58302 return SnapshotSampleVector();
[email protected]abae9b022012-10-24 08:18:52303}
304
[email protected]c50c21d2013-01-11 21:52:44305void Histogram::AddSamples(const HistogramSamples& samples) {
306 samples_->Add(samples);
307}
308
309bool Histogram::AddSamplesFromPickle(PickleIterator* iter) {
310 return samples_->AddFromPickle(iter);
311}
312
[email protected]abae9b022012-10-24 08:18:52313// The following methods provide a graphical histogram display.
asvitkine24d3e9a2015-05-27 05:22:14314void Histogram::WriteHTMLGraph(std::string* output) const {
[email protected]abae9b022012-10-24 08:18:52315 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc.
316 output->append("<PRE>");
317 WriteAsciiImpl(true, "<br>", output);
318 output->append("</PRE>");
319}
320
asvitkine24d3e9a2015-05-27 05:22:14321void Histogram::WriteAscii(std::string* output) const {
[email protected]abae9b022012-10-24 08:18:52322 WriteAsciiImpl(true, "\n", output);
323}
324
[email protected]c50c21d2013-01-11 21:52:44325bool Histogram::SerializeInfoImpl(Pickle* pickle) const {
326 DCHECK(bucket_ranges()->HasValidChecksum());
327 return pickle->WriteString(histogram_name()) &&
328 pickle->WriteInt(flags()) &&
329 pickle->WriteInt(declared_min()) &&
330 pickle->WriteInt(declared_max()) &&
pkasting89a19f142014-10-02 03:01:04331 pickle->WriteSizeT(bucket_count()) &&
[email protected]c50c21d2013-01-11 21:52:44332 pickle->WriteUInt32(bucket_ranges()->checksum());
333}
334
asvitkine24d3e9a2015-05-27 05:22:14335Histogram::Histogram(const std::string& name,
[email protected]abae9b022012-10-24 08:18:52336 Sample minimum,
337 Sample maximum,
[email protected]abae9b022012-10-24 08:18:52338 const BucketRanges* ranges)
339 : HistogramBase(name),
340 bucket_ranges_(ranges),
341 declared_min_(minimum),
[email protected]15ce3842013-06-27 14:38:45342 declared_max_(maximum) {
[email protected]abae9b022012-10-24 08:18:52343 if (ranges)
bcwhiteb036e4322015-12-10 18:36:34344 samples_.reset(new SampleVector(HashMetricName(name), ranges));
[email protected]abae9b022012-10-24 08:18:52345}
346
347Histogram::~Histogram() {
[email protected]abae9b022012-10-24 08:18:52348}
349
[email protected]34d062322012-08-01 21:34:08350bool Histogram::PrintEmptyBucket(size_t index) const {
351 return true;
352}
353
[email protected]34d062322012-08-01 21:34:08354// Use the actual bucket widths (like a linear histogram) until the widths get
355// over some transition value, and then use that transition width. Exponentials
356// get so big so fast (and we don't expect to see a lot of entries in the large
357// buckets), so we need this to make it possible to see what is going on and
358// not have 0-graphical-height buckets.
359double Histogram::GetBucketSize(Count current, size_t i) const {
360 DCHECK_GT(ranges(i + 1), ranges(i));
361 static const double kTransitionWidth = 5;
362 double denominator = ranges(i + 1) - ranges(i);
363 if (denominator > kTransitionWidth)
364 denominator = kTransitionWidth; // Stop trying to normalize.
365 return current/denominator;
366}
367
asvitkine24d3e9a2015-05-27 05:22:14368const std::string Histogram::GetAsciiBucketRange(size_t i) const {
[email protected]f2bb3202013-04-05 21:21:54369 return GetSimpleAsciiBucketRange(ranges(i));
[email protected]34d062322012-08-01 21:34:08370}
371
[email protected]34d062322012-08-01 21:34:08372//------------------------------------------------------------------------------
373// Private methods
374
[email protected]c50c21d2013-01-11 21:52:44375// static
376HistogramBase* Histogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14377 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44378 int flags;
379 int declared_min;
380 int declared_max;
pkasting89a19f142014-10-02 03:01:04381 size_t bucket_count;
avi9b6f42932015-12-26 22:15:14382 uint32_t range_checksum;
[email protected]c50c21d2013-01-11 21:52:44383
384 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
385 &declared_max, &bucket_count, &range_checksum)) {
386 return NULL;
387 }
388
389 // Find or create the local version of the histogram in this process.
390 HistogramBase* histogram = Histogram::FactoryGet(
391 histogram_name, declared_min, declared_max, bucket_count, flags);
392
393 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
394 // The serialized histogram might be corrupted.
395 return NULL;
396 }
397 return histogram;
398}
399
[email protected]877ef562012-10-20 02:56:18400scoped_ptr<SampleVector> Histogram::SnapshotSampleVector() const {
bcwhiteb036e4322015-12-10 18:36:34401 scoped_ptr<SampleVector> samples(
402 new SampleVector(samples_->id(), bucket_ranges()));
[email protected]877ef562012-10-20 02:56:18403 samples->Add(*samples_);
danakj0c8d4aa2015-11-25 05:29:58404 return samples;
[email protected]877ef562012-10-20 02:56:18405}
406
[email protected]34d062322012-08-01 21:34:08407void Histogram::WriteAsciiImpl(bool graph_it,
asvitkine24d3e9a2015-05-27 05:22:14408 const std::string& newline,
409 std::string* output) const {
[email protected]34d062322012-08-01 21:34:08410 // Get local (stack) copies of all effectively volatile class data so that we
411 // are consistent across our output activities.
[email protected]877ef562012-10-20 02:56:18412 scoped_ptr<SampleVector> snapshot = SnapshotSampleVector();
[email protected]2f7d9cd2012-09-22 03:42:12413 Count sample_count = snapshot->TotalCount();
[email protected]34d062322012-08-01 21:34:08414
[email protected]2f7d9cd2012-09-22 03:42:12415 WriteAsciiHeader(*snapshot, sample_count, output);
[email protected]34d062322012-08-01 21:34:08416 output->append(newline);
417
418 // Prepare to normalize graphical rendering of bucket contents.
419 double max_size = 0;
420 if (graph_it)
[email protected]2f7d9cd2012-09-22 03:42:12421 max_size = GetPeakBucketSize(*snapshot);
[email protected]34d062322012-08-01 21:34:08422
423 // Calculate space needed to print bucket range numbers. Leave room to print
424 // nearly the largest bucket range without sliding over the histogram.
425 size_t largest_non_empty_bucket = bucket_count() - 1;
[email protected]2f7d9cd2012-09-22 03:42:12426 while (0 == snapshot->GetCountAtIndex(largest_non_empty_bucket)) {
[email protected]34d062322012-08-01 21:34:08427 if (0 == largest_non_empty_bucket)
428 break; // All buckets are empty.
429 --largest_non_empty_bucket;
430 }
431
432 // Calculate largest print width needed for any of our bucket range displays.
433 size_t print_width = 1;
434 for (size_t i = 0; i < bucket_count(); ++i) {
[email protected]2f7d9cd2012-09-22 03:42:12435 if (snapshot->GetCountAtIndex(i)) {
[email protected]34d062322012-08-01 21:34:08436 size_t width = GetAsciiBucketRange(i).size() + 1;
437 if (width > print_width)
438 print_width = width;
439 }
440 }
441
avi9b6f42932015-12-26 22:15:14442 int64_t remaining = sample_count;
443 int64_t past = 0;
[email protected]34d062322012-08-01 21:34:08444 // Output the actual histogram graph.
445 for (size_t i = 0; i < bucket_count(); ++i) {
[email protected]2f7d9cd2012-09-22 03:42:12446 Count current = snapshot->GetCountAtIndex(i);
[email protected]34d062322012-08-01 21:34:08447 if (!current && !PrintEmptyBucket(i))
448 continue;
449 remaining -= current;
asvitkine24d3e9a2015-05-27 05:22:14450 std::string range = GetAsciiBucketRange(i);
[email protected]34d062322012-08-01 21:34:08451 output->append(range);
452 for (size_t j = 0; range.size() + j < print_width + 1; ++j)
453 output->push_back(' ');
[email protected]2f7d9cd2012-09-22 03:42:12454 if (0 == current && i < bucket_count() - 1 &&
455 0 == snapshot->GetCountAtIndex(i + 1)) {
456 while (i < bucket_count() - 1 &&
457 0 == snapshot->GetCountAtIndex(i + 1)) {
[email protected]34d062322012-08-01 21:34:08458 ++i;
[email protected]2f7d9cd2012-09-22 03:42:12459 }
[email protected]34d062322012-08-01 21:34:08460 output->append("... ");
461 output->append(newline);
462 continue; // No reason to plot emptiness.
463 }
464 double current_size = GetBucketSize(current, i);
465 if (graph_it)
466 WriteAsciiBucketGraph(current_size, max_size, output);
467 WriteAsciiBucketContext(past, current, remaining, i, output);
468 output->append(newline);
469 past += current;
470 }
471 DCHECK_EQ(sample_count, past);
472}
473
[email protected]2f7d9cd2012-09-22 03:42:12474double Histogram::GetPeakBucketSize(const SampleVector& samples) const {
[email protected]34d062322012-08-01 21:34:08475 double max = 0;
476 for (size_t i = 0; i < bucket_count() ; ++i) {
[email protected]2f7d9cd2012-09-22 03:42:12477 double current_size = GetBucketSize(samples.GetCountAtIndex(i), i);
[email protected]34d062322012-08-01 21:34:08478 if (current_size > max)
479 max = current_size;
480 }
481 return max;
482}
483
[email protected]2f7d9cd2012-09-22 03:42:12484void Histogram::WriteAsciiHeader(const SampleVector& samples,
[email protected]34d062322012-08-01 21:34:08485 Count sample_count,
asvitkine24d3e9a2015-05-27 05:22:14486 std::string* output) const {
[email protected]34d062322012-08-01 21:34:08487 StringAppendF(output,
488 "Histogram: %s recorded %d samples",
489 histogram_name().c_str(),
490 sample_count);
491 if (0 == sample_count) {
[email protected]2f7d9cd2012-09-22 03:42:12492 DCHECK_EQ(samples.sum(), 0);
[email protected]34d062322012-08-01 21:34:08493 } else {
[email protected]2f7d9cd2012-09-22 03:42:12494 double average = static_cast<float>(samples.sum()) / sample_count;
[email protected]34d062322012-08-01 21:34:08495
496 StringAppendF(output, ", average = %.1f", average);
497 }
[email protected]7c7a42752012-08-09 05:14:15498 if (flags() & ~kHexRangePrintingFlag)
499 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag);
[email protected]34d062322012-08-01 21:34:08500}
501
avi9b6f42932015-12-26 22:15:14502void Histogram::WriteAsciiBucketContext(const int64_t past,
[email protected]34d062322012-08-01 21:34:08503 const Count current,
avi9b6f42932015-12-26 22:15:14504 const int64_t remaining,
[email protected]34d062322012-08-01 21:34:08505 const size_t i,
asvitkine24d3e9a2015-05-27 05:22:14506 std::string* output) const {
[email protected]34d062322012-08-01 21:34:08507 double scaled_sum = (past + current + remaining) / 100.0;
508 WriteAsciiBucketValue(current, scaled_sum, output);
509 if (0 < i) {
510 double percentage = past / scaled_sum;
511 StringAppendF(output, " {%3.1f%%}", percentage);
512 }
513}
514
[email protected]24a7ec52012-10-08 10:31:50515void Histogram::GetParameters(DictionaryValue* params) const {
[email protected]07c02402012-10-31 06:20:25516 params->SetString("type", HistogramTypeToString(GetHistogramType()));
[email protected]24a7ec52012-10-08 10:31:50517 params->SetInteger("min", declared_min());
518 params->SetInteger("max", declared_max());
519 params->SetInteger("bucket_count", static_cast<int>(bucket_count()));
520}
521
[email protected]cdd98fc2013-05-10 09:32:58522void Histogram::GetCountAndBucketData(Count* count,
avi9b6f42932015-12-26 22:15:14523 int64_t* sum,
[email protected]cdd98fc2013-05-10 09:32:58524 ListValue* buckets) const {
[email protected]877ef562012-10-20 02:56:18525 scoped_ptr<SampleVector> snapshot = SnapshotSampleVector();
[email protected]24a7ec52012-10-08 10:31:50526 *count = snapshot->TotalCount();
[email protected]cdd98fc2013-05-10 09:32:58527 *sum = snapshot->sum();
[email protected]24a7ec52012-10-08 10:31:50528 size_t index = 0;
529 for (size_t i = 0; i < bucket_count(); ++i) {
scottmgdcc933d2015-01-27 21:37:55530 Sample count_at_index = snapshot->GetCountAtIndex(i);
531 if (count_at_index > 0) {
[email protected]24a7ec52012-10-08 10:31:50532 scoped_ptr<DictionaryValue> bucket_value(new DictionaryValue());
533 bucket_value->SetInteger("low", ranges(i));
534 if (i != bucket_count() - 1)
535 bucket_value->SetInteger("high", ranges(i + 1));
scottmgdcc933d2015-01-27 21:37:55536 bucket_value->SetInteger("count", count_at_index);
[email protected]24a7ec52012-10-08 10:31:50537 buckets->Set(index, bucket_value.release());
538 ++index;
539 }
540 }
541}
542
[email protected]34d062322012-08-01 21:34:08543//------------------------------------------------------------------------------
544// LinearHistogram: This histogram uses a traditional set of evenly spaced
545// buckets.
546//------------------------------------------------------------------------------
547
548LinearHistogram::~LinearHistogram() {}
549
asvitkine24d3e9a2015-05-27 05:22:14550HistogramBase* LinearHistogram::FactoryGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17551 Sample minimum,
552 Sample maximum,
553 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14554 int32_t flags) {
[email protected]07c02402012-10-31 06:20:25555 return FactoryGetWithRangeDescription(
556 name, minimum, maximum, bucket_count, flags, NULL);
557}
558
asvitkine24d3e9a2015-05-27 05:22:14559HistogramBase* LinearHistogram::FactoryTimeGet(const std::string& name,
[email protected]de415552013-01-23 04:12:17560 TimeDelta minimum,
561 TimeDelta maximum,
562 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14563 int32_t flags) {
pkasting9cf9b94a2014-10-01 22:18:43564 return FactoryGet(name, static_cast<Sample>(minimum.InMilliseconds()),
565 static_cast<Sample>(maximum.InMilliseconds()), bucket_count,
566 flags);
[email protected]07c02402012-10-31 06:20:25567}
568
asvitkine5c2d5022015-06-19 00:37:50569HistogramBase* LinearHistogram::FactoryGet(const char* name,
570 Sample minimum,
571 Sample maximum,
572 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14573 int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:50574 return FactoryGet(std::string(name), minimum, maximum, bucket_count, flags);
575}
576
577HistogramBase* LinearHistogram::FactoryTimeGet(const char* name,
578 TimeDelta minimum,
579 TimeDelta maximum,
580 size_t bucket_count,
avi9b6f42932015-12-26 22:15:14581 int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:50582 return FactoryTimeGet(std::string(name), minimum, maximum, bucket_count,
583 flags);
584}
585
[email protected]de415552013-01-23 04:12:17586HistogramBase* LinearHistogram::FactoryGetWithRangeDescription(
avi9b6f42932015-12-26 22:15:14587 const std::string& name,
588 Sample minimum,
589 Sample maximum,
590 size_t bucket_count,
591 int32_t flags,
592 const DescriptionPair descriptions[]) {
[email protected]e184be902012-08-07 04:49:24593 bool valid_arguments = Histogram::InspectConstructionArguments(
594 name, &minimum, &maximum, &bucket_count);
595 DCHECK(valid_arguments);
[email protected]34d062322012-08-01 21:34:08596
[email protected]cc7dec212013-03-01 03:53:25597 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]34d062322012-08-01 21:34:08598 if (!histogram) {
599 // To avoid racy destruction at shutdown, the following will be leaked.
600 BucketRanges* ranges = new BucketRanges(bucket_count + 1);
[email protected]15ce3842013-06-27 14:38:45601 InitializeBucketRanges(minimum, maximum, ranges);
[email protected]34d062322012-08-01 21:34:08602 const BucketRanges* registered_ranges =
603 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
604
605 LinearHistogram* tentative_histogram =
[email protected]15ce3842013-06-27 14:38:45606 new LinearHistogram(name, minimum, maximum, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54607
[email protected]07c02402012-10-31 06:20:25608 // Set range descriptions.
609 if (descriptions) {
610 for (int i = 0; descriptions[i].description; ++i) {
611 tentative_histogram->bucket_description_[descriptions[i].sample] =
612 descriptions[i].description;
613 }
614 }
615
[email protected]34d062322012-08-01 21:34:08616 tentative_histogram->SetFlags(flags);
617 histogram =
618 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
619 }
620
[email protected]cc7dec212013-03-01 03:53:25621 DCHECK_EQ(LINEAR_HISTOGRAM, histogram->GetHistogramType());
[email protected]65796dc2014-03-13 14:08:18622 if (!histogram->HasConstructionArguments(minimum, maximum, bucket_count)) {
623 // The construction arguments do not match the existing histogram. This can
624 // come about if an extension updates in the middle of a chrome run and has
625 // changed one of them, or simply by bad code within Chrome itself. We
626 // return NULL here with the expectation that bad code in Chrome will crash
627 // on dereference, but extension/Pepper APIs will guard against NULL and not
628 // crash.
629 DLOG(ERROR) << "Histogram " << name << " has bad construction arguments";
630 return NULL;
631 }
[email protected]34d062322012-08-01 21:34:08632 return histogram;
633}
634
[email protected]07c02402012-10-31 06:20:25635HistogramType LinearHistogram::GetHistogramType() const {
[email protected]b7d08202011-01-25 17:29:39636 return LINEAR_HISTOGRAM;
637}
638
asvitkine24d3e9a2015-05-27 05:22:14639LinearHistogram::LinearHistogram(const std::string& name,
[email protected]835d7c82010-10-14 04:38:38640 Sample minimum,
641 Sample maximum,
[email protected]34d062322012-08-01 21:34:08642 const BucketRanges* ranges)
[email protected]15ce3842013-06-27 14:38:45643 : Histogram(name, minimum, maximum, ranges) {
initial.commitd7cae122008-07-26 21:49:38644}
645
initial.commitd7cae122008-07-26 21:49:38646double LinearHistogram::GetBucketSize(Count current, size_t i) const {
[email protected]2ef3748f2010-10-19 17:33:28647 DCHECK_GT(ranges(i + 1), ranges(i));
initial.commitd7cae122008-07-26 21:49:38648 // Adjacent buckets with different widths would have "surprisingly" many (few)
649 // samples in a histogram if we didn't normalize this way.
650 double denominator = ranges(i + 1) - ranges(i);
651 return current/denominator;
652}
653
asvitkine24d3e9a2015-05-27 05:22:14654const std::string LinearHistogram::GetAsciiBucketRange(size_t i) const {
[email protected]b7d08202011-01-25 17:29:39655 int range = ranges(i);
656 BucketDescriptionMap::const_iterator it = bucket_description_.find(range);
657 if (it == bucket_description_.end())
658 return Histogram::GetAsciiBucketRange(i);
659 return it->second;
660}
661
662bool LinearHistogram::PrintEmptyBucket(size_t index) const {
663 return bucket_description_.find(ranges(index)) == bucket_description_.end();
664}
665
[email protected]34d062322012-08-01 21:34:08666// static
667void LinearHistogram::InitializeBucketRanges(Sample minimum,
668 Sample maximum,
[email protected]34d062322012-08-01 21:34:08669 BucketRanges* ranges) {
[email protected]34d062322012-08-01 21:34:08670 double min = minimum;
671 double max = maximum;
[email protected]15ce3842013-06-27 14:38:45672 size_t bucket_count = ranges->bucket_count();
673 for (size_t i = 1; i < bucket_count; ++i) {
[email protected]34d062322012-08-01 21:34:08674 double linear_range =
[email protected]15ce3842013-06-27 14:38:45675 (min * (bucket_count - 1 - i) + max * (i - 1)) / (bucket_count - 2);
[email protected]34d062322012-08-01 21:34:08676 ranges->set_range(i, static_cast<Sample>(linear_range + 0.5));
677 }
[email protected]15ce3842013-06-27 14:38:45678 ranges->set_range(ranges->bucket_count(), HistogramBase::kSampleType_MAX);
[email protected]34d062322012-08-01 21:34:08679 ranges->ResetChecksum();
680}
[email protected]b7d08202011-01-25 17:29:39681
[email protected]c50c21d2013-01-11 21:52:44682// static
683HistogramBase* LinearHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14684 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44685 int flags;
686 int declared_min;
687 int declared_max;
pkasting89a19f142014-10-02 03:01:04688 size_t bucket_count;
avi9b6f42932015-12-26 22:15:14689 uint32_t range_checksum;
[email protected]c50c21d2013-01-11 21:52:44690
691 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
692 &declared_max, &bucket_count, &range_checksum)) {
693 return NULL;
694 }
695
696 HistogramBase* histogram = LinearHistogram::FactoryGet(
697 histogram_name, declared_min, declared_max, bucket_count, flags);
698 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
699 // The serialized histogram might be corrupted.
700 return NULL;
701 }
702 return histogram;
703}
704
initial.commitd7cae122008-07-26 21:49:38705//------------------------------------------------------------------------------
[email protected]e8829a192009-12-06 00:09:37706// This section provides implementation for BooleanHistogram.
707//------------------------------------------------------------------------------
708
asvitkine24d3e9a2015-05-27 05:22:14709HistogramBase* BooleanHistogram::FactoryGet(const std::string& name,
avi9b6f42932015-12-26 22:15:14710 int32_t flags) {
[email protected]cc7dec212013-03-01 03:53:25711 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]993ae742012-07-18 18:06:30712 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:53713 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:08714 BucketRanges* ranges = new BucketRanges(4);
[email protected]15ce3842013-06-27 14:38:45715 LinearHistogram::InitializeBucketRanges(1, 2, ranges);
[email protected]34d062322012-08-01 21:34:08716 const BucketRanges* registered_ranges =
717 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
718
719 BooleanHistogram* tentative_histogram =
720 new BooleanHistogram(name, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54721
[email protected]81ce9f3b2011-04-05 04:48:53722 tentative_histogram->SetFlags(flags);
723 histogram =
724 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37725 }
726
[email protected]cc7dec212013-03-01 03:53:25727 DCHECK_EQ(BOOLEAN_HISTOGRAM, histogram->GetHistogramType());
[email protected]e8829a192009-12-06 00:09:37728 return histogram;
729}
730
avi9b6f42932015-12-26 22:15:14731HistogramBase* BooleanHistogram::FactoryGet(const char* name, int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:50732 return FactoryGet(std::string(name), flags);
733}
734
[email protected]07c02402012-10-31 06:20:25735HistogramType BooleanHistogram::GetHistogramType() const {
[email protected]5d91c9e2010-07-28 17:25:28736 return BOOLEAN_HISTOGRAM;
737}
738
asvitkine24d3e9a2015-05-27 05:22:14739BooleanHistogram::BooleanHistogram(const std::string& name,
[email protected]34d062322012-08-01 21:34:08740 const BucketRanges* ranges)
[email protected]15ce3842013-06-27 14:38:45741 : LinearHistogram(name, 1, 2, ranges) {}
initial.commitd7cae122008-07-26 21:49:38742
[email protected]c50c21d2013-01-11 21:52:44743HistogramBase* BooleanHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14744 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44745 int flags;
746 int declared_min;
747 int declared_max;
pkasting89a19f142014-10-02 03:01:04748 size_t bucket_count;
avi9b6f42932015-12-26 22:15:14749 uint32_t range_checksum;
[email protected]c50c21d2013-01-11 21:52:44750
751 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
752 &declared_max, &bucket_count, &range_checksum)) {
753 return NULL;
754 }
755
756 HistogramBase* histogram = BooleanHistogram::FactoryGet(
757 histogram_name, flags);
758 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
759 // The serialized histogram might be corrupted.
760 return NULL;
761 }
762 return histogram;
763}
764
initial.commitd7cae122008-07-26 21:49:38765//------------------------------------------------------------------------------
[email protected]70cc56e42010-04-29 22:39:55766// CustomHistogram:
767//------------------------------------------------------------------------------
768
asvitkine24d3e9a2015-05-27 05:22:14769HistogramBase* CustomHistogram::FactoryGet(
770 const std::string& name,
771 const std::vector<Sample>& custom_ranges,
avi9b6f42932015-12-26 22:15:14772 int32_t flags) {
[email protected]34d062322012-08-01 21:34:08773 CHECK(ValidateCustomRanges(custom_ranges));
[email protected]70cc56e42010-04-29 22:39:55774
[email protected]cc7dec212013-03-01 03:53:25775 HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
[email protected]993ae742012-07-18 18:06:30776 if (!histogram) {
[email protected]34d062322012-08-01 21:34:08777 BucketRanges* ranges = CreateBucketRangesFromCustomRanges(custom_ranges);
778 const BucketRanges* registered_ranges =
779 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
780
[email protected]81ce9f3b2011-04-05 04:48:53781 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:08782 CustomHistogram* tentative_histogram =
783 new CustomHistogram(name, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54784
[email protected]81ce9f3b2011-04-05 04:48:53785 tentative_histogram->SetFlags(flags);
[email protected]34d062322012-08-01 21:34:08786
[email protected]81ce9f3b2011-04-05 04:48:53787 histogram =
788 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]70cc56e42010-04-29 22:39:55789 }
790
[email protected]cc7dec212013-03-01 03:53:25791 DCHECK_EQ(histogram->GetHistogramType(), CUSTOM_HISTOGRAM);
[email protected]70cc56e42010-04-29 22:39:55792 return histogram;
793}
794
asvitkine5c2d5022015-06-19 00:37:50795HistogramBase* CustomHistogram::FactoryGet(
796 const char* name,
797 const std::vector<Sample>& custom_ranges,
avi9b6f42932015-12-26 22:15:14798 int32_t flags) {
asvitkine5c2d5022015-06-19 00:37:50799 return FactoryGet(std::string(name), custom_ranges, flags);
800}
801
[email protected]07c02402012-10-31 06:20:25802HistogramType CustomHistogram::GetHistogramType() const {
[email protected]5d91c9e2010-07-28 17:25:28803 return CUSTOM_HISTOGRAM;
804}
805
[email protected]961fefb2011-05-24 13:59:58806// static
asvitkine24d3e9a2015-05-27 05:22:14807std::vector<Sample> CustomHistogram::ArrayToCustomRanges(
[email protected]961fefb2011-05-24 13:59:58808 const Sample* values, size_t num_values) {
asvitkine24d3e9a2015-05-27 05:22:14809 std::vector<Sample> all_values;
[email protected]961fefb2011-05-24 13:59:58810 for (size_t i = 0; i < num_values; ++i) {
811 Sample value = values[i];
812 all_values.push_back(value);
813
814 // Ensure that a guard bucket is added. If we end up with duplicate
815 // values, FactoryGet will take care of removing them.
816 all_values.push_back(value + 1);
817 }
818 return all_values;
819}
820
asvitkine24d3e9a2015-05-27 05:22:14821CustomHistogram::CustomHistogram(const std::string& name,
[email protected]34d062322012-08-01 21:34:08822 const BucketRanges* ranges)
823 : Histogram(name,
824 ranges->range(1),
[email protected]15ce3842013-06-27 14:38:45825 ranges->range(ranges->bucket_count() - 1),
[email protected]34d062322012-08-01 21:34:08826 ranges) {}
[email protected]70cc56e42010-04-29 22:39:55827
[email protected]c50c21d2013-01-11 21:52:44828bool CustomHistogram::SerializeInfoImpl(Pickle* pickle) const {
829 if (!Histogram::SerializeInfoImpl(pickle))
830 return false;
[email protected]cd56dff2011-11-13 04:19:15831
[email protected]c50c21d2013-01-11 21:52:44832 // Serialize ranges. First and last ranges are alwasy 0 and INT_MAX, so don't
833 // write them.
[email protected]15ce3842013-06-27 14:38:45834 for (size_t i = 1; i < bucket_ranges()->bucket_count(); ++i) {
[email protected]c50c21d2013-01-11 21:52:44835 if (!pickle->WriteInt(bucket_ranges()->range(i)))
[email protected]cd56dff2011-11-13 04:19:15836 return false;
837 }
838 return true;
839}
840
[email protected]70cc56e42010-04-29 22:39:55841double CustomHistogram::GetBucketSize(Count current, size_t i) const {
842 return 1;
843}
844
[email protected]34d062322012-08-01 21:34:08845// static
[email protected]c50c21d2013-01-11 21:52:44846HistogramBase* CustomHistogram::DeserializeInfoImpl(PickleIterator* iter) {
asvitkine24d3e9a2015-05-27 05:22:14847 std::string histogram_name;
[email protected]c50c21d2013-01-11 21:52:44848 int flags;
849 int declared_min;
850 int declared_max;
pkasting89a19f142014-10-02 03:01:04851 size_t bucket_count;
avi9b6f42932015-12-26 22:15:14852 uint32_t range_checksum;
[email protected]c50c21d2013-01-11 21:52:44853
854 if (!ReadHistogramArguments(iter, &histogram_name, &flags, &declared_min,
855 &declared_max, &bucket_count, &range_checksum)) {
856 return NULL;
857 }
858
859 // First and last ranges are not serialized.
asvitkine24d3e9a2015-05-27 05:22:14860 std::vector<Sample> sample_ranges(bucket_count - 1);
[email protected]c50c21d2013-01-11 21:52:44861
862 for (size_t i = 0; i < sample_ranges.size(); ++i) {
863 if (!iter->ReadInt(&sample_ranges[i]))
864 return NULL;
865 }
866
867 HistogramBase* histogram = CustomHistogram::FactoryGet(
868 histogram_name, sample_ranges, flags);
869 if (!ValidateRangeChecksum(*histogram, range_checksum)) {
870 // The serialized histogram might be corrupted.
871 return NULL;
872 }
873 return histogram;
874}
875
876// static
[email protected]34d062322012-08-01 21:34:08877bool CustomHistogram::ValidateCustomRanges(
asvitkine24d3e9a2015-05-27 05:22:14878 const std::vector<Sample>& custom_ranges) {
[email protected]640d95ef2012-08-04 06:23:03879 bool has_valid_range = false;
[email protected]34d062322012-08-01 21:34:08880 for (size_t i = 0; i < custom_ranges.size(); i++) {
[email protected]640d95ef2012-08-04 06:23:03881 Sample sample = custom_ranges[i];
882 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1)
[email protected]34d062322012-08-01 21:34:08883 return false;
[email protected]640d95ef2012-08-04 06:23:03884 if (sample != 0)
885 has_valid_range = true;
[email protected]34d062322012-08-01 21:34:08886 }
[email protected]640d95ef2012-08-04 06:23:03887 return has_valid_range;
[email protected]34d062322012-08-01 21:34:08888}
889
890// static
891BucketRanges* CustomHistogram::CreateBucketRangesFromCustomRanges(
asvitkine24d3e9a2015-05-27 05:22:14892 const std::vector<Sample>& custom_ranges) {
[email protected]34d062322012-08-01 21:34:08893 // Remove the duplicates in the custom ranges array.
asvitkine24d3e9a2015-05-27 05:22:14894 std::vector<int> ranges = custom_ranges;
[email protected]34d062322012-08-01 21:34:08895 ranges.push_back(0); // Ensure we have a zero value.
896 ranges.push_back(HistogramBase::kSampleType_MAX);
897 std::sort(ranges.begin(), ranges.end());
898 ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end());
899
900 BucketRanges* bucket_ranges = new BucketRanges(ranges.size());
901 for (size_t i = 0; i < ranges.size(); i++) {
902 bucket_ranges->set_range(i, ranges[i]);
903 }
904 bucket_ranges->ResetChecksum();
905 return bucket_ranges;
906}
907
[email protected]835d7c82010-10-14 04:38:38908} // namespace base