blob: dbc76ef8b0efdea86efd372e62814d212fa0d85c [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]567d30e2012-07-13 21:48:2920#include "base/metrics/statistics_recorder.h"
[email protected]3f383852009-04-03 18:18:5521#include "base/pickle.h"
[email protected]ec0c0aa2012-08-14 02:02:0022#include "base/string_util.h"
[email protected]f1633932010-08-17 23:05:2823#include "base/stringprintf.h"
[email protected]bc581a682011-01-01 23:16:2024#include "base/synchronization/lock.h"
initial.commitd7cae122008-07-26 21:49:3825
[email protected]34d062322012-08-01 21:34:0826using std::string;
27using std::vector;
28
[email protected]835d7c82010-10-14 04:38:3829namespace base {
[email protected]e1acf6f2008-10-27 20:43:3330
[email protected]34d062322012-08-01 21:34:0831typedef HistogramBase::Count Count;
32typedef HistogramBase::Sample Sample;
initial.commitd7cae122008-07-26 21:49:3833
[email protected]b122c0c2011-02-23 22:31:1834// static
[email protected]9fce5f02011-03-02 08:04:5735const size_t Histogram::kBucketCount_MAX = 16384u;
[email protected]b122c0c2011-02-23 22:31:1836
[email protected]34d062322012-08-01 21:34:0837Histogram::SampleSet::SampleSet(size_t size)
38 : counts_(size, 0),
39 sum_(0),
40 redundant_count_(0) {}
initial.commitd7cae122008-07-26 21:49:3841
42Histogram::SampleSet::SampleSet()
43 : counts_(),
44 sum_(0),
[email protected]34d062322012-08-01 21:34:0845 redundant_count_(0) {}
initial.commitd7cae122008-07-26 21:49:3846
[email protected]34d062322012-08-01 21:34:0847Histogram::SampleSet::~SampleSet() {}
[email protected]d4799a32010-09-28 22:54:5848
[email protected]34d062322012-08-01 21:34:0849void Histogram::SampleSet::Resize(size_t size) {
50 counts_.resize(size, 0);
initial.commitd7cae122008-07-26 21:49:3851}
52
initial.commitd7cae122008-07-26 21:49:3853void Histogram::SampleSet::Accumulate(Sample value, Count count,
54 size_t index) {
55 DCHECK(count == 1 || count == -1);
56 counts_[index] += count;
57 sum_ += count * value;
[email protected]93a41d72010-11-03 23:36:2458 redundant_count_ += count;
[email protected]2753b392009-12-28 06:59:5259 DCHECK_GE(counts_[index], 0);
60 DCHECK_GE(sum_, 0);
[email protected]93a41d72010-11-03 23:36:2461 DCHECK_GE(redundant_count_, 0);
initial.commitd7cae122008-07-26 21:49:3862}
63
64Count Histogram::SampleSet::TotalCount() const {
65 Count total = 0;
66 for (Counts::const_iterator it = counts_.begin();
67 it != counts_.end();
[email protected]55e57d42009-02-25 06:10:1768 ++it) {
initial.commitd7cae122008-07-26 21:49:3869 total += *it;
70 }
71 return total;
72}
73
74void Histogram::SampleSet::Add(const SampleSet& other) {
[email protected]2ef3748f2010-10-19 17:33:2875 DCHECK_EQ(counts_.size(), other.counts_.size());
initial.commitd7cae122008-07-26 21:49:3876 sum_ += other.sum_;
[email protected]93a41d72010-11-03 23:36:2477 redundant_count_ += other.redundant_count_;
[email protected]55e57d42009-02-25 06:10:1778 for (size_t index = 0; index < counts_.size(); ++index)
initial.commitd7cae122008-07-26 21:49:3879 counts_[index] += other.counts_[index];
80}
81
82void Histogram::SampleSet::Subtract(const SampleSet& other) {
[email protected]2ef3748f2010-10-19 17:33:2883 DCHECK_EQ(counts_.size(), other.counts_.size());
[email protected]0704a972011-03-24 00:30:2784 // Note: Race conditions in snapshotting a sum may lead to (temporary)
85 // negative values when snapshots are later combined (and deltas calculated).
86 // As a result, we don't currently CHCEK() for positive values.
initial.commitd7cae122008-07-26 21:49:3887 sum_ -= other.sum_;
[email protected]93a41d72010-11-03 23:36:2488 redundant_count_ -= other.redundant_count_;
[email protected]55e57d42009-02-25 06:10:1789 for (size_t index = 0; index < counts_.size(); ++index) {
initial.commitd7cae122008-07-26 21:49:3890 counts_[index] -= other.counts_[index];
[email protected]2753b392009-12-28 06:59:5291 DCHECK_GE(counts_[index], 0);
initial.commitd7cae122008-07-26 21:49:3892 }
93}
94
[email protected]55e57d42009-02-25 06:10:1795bool Histogram::SampleSet::Serialize(Pickle* pickle) const {
96 pickle->WriteInt64(sum_);
[email protected]93a41d72010-11-03 23:36:2497 pickle->WriteInt64(redundant_count_);
[email protected]ee028ac2012-03-14 00:08:0298 pickle->WriteUInt64(counts_.size());
[email protected]55e57d42009-02-25 06:10:1799
100 for (size_t index = 0; index < counts_.size(); ++index) {
101 pickle->WriteInt(counts_[index]);
102 }
103
104 return true;
105}
106
[email protected]ce208f872012-03-07 20:42:56107bool Histogram::SampleSet::Deserialize(PickleIterator* iter) {
[email protected]2753b392009-12-28 06:59:52108 DCHECK_EQ(counts_.size(), 0u);
109 DCHECK_EQ(sum_, 0);
[email protected]93a41d72010-11-03 23:36:24110 DCHECK_EQ(redundant_count_, 0);
[email protected]55e57d42009-02-25 06:10:17111
[email protected]ee028ac2012-03-14 00:08:02112 uint64 counts_size;
[email protected]55e57d42009-02-25 06:10:17113
[email protected]ce208f872012-03-07 20:42:56114 if (!iter->ReadInt64(&sum_) ||
115 !iter->ReadInt64(&redundant_count_) ||
[email protected]ee028ac2012-03-14 00:08:02116 !iter->ReadUInt64(&counts_size)) {
[email protected]55e57d42009-02-25 06:10:17117 return false;
118 }
119
[email protected]86440f52009-12-31 05:17:23120 if (counts_size == 0)
[email protected]55e57d42009-02-25 06:10:17121 return false;
122
[email protected]93a41d72010-11-03 23:36:24123 int count = 0;
[email protected]ee028ac2012-03-14 00:08:02124 for (uint64 index = 0; index < counts_size; ++index) {
[email protected]86440f52009-12-31 05:17:23125 int i;
[email protected]ce208f872012-03-07 20:42:56126 if (!iter->ReadInt(&i))
[email protected]55e57d42009-02-25 06:10:17127 return false;
[email protected]86440f52009-12-31 05:17:23128 counts_.push_back(i);
[email protected]93a41d72010-11-03 23:36:24129 count += i;
[email protected]55e57d42009-02-25 06:10:17130 }
[email protected]93a41d72010-11-03 23:36:24131 DCHECK_EQ(count, redundant_count_);
132 return count == redundant_count_;
[email protected]55e57d42009-02-25 06:10:17133}
134
[email protected]ec0c0aa2012-08-14 02:02:00135// TODO(rtenneti): delete this code after debugging.
[email protected]263a17a2012-08-16 03:03:54136void CheckCorruption(const Histogram& histogram, bool new_histogram) {
[email protected]ec0c0aa2012-08-14 02:02:00137 const std::string& histogram_name = histogram.histogram_name();
138 char histogram_name_buf[128];
139 base::strlcpy(histogram_name_buf,
140 histogram_name.c_str(),
141 arraysize(histogram_name_buf));
142 base::debug::Alias(histogram_name_buf);
143
[email protected]263a17a2012-08-16 03:03:54144 bool debug_new_histogram[1];
145 debug_new_histogram[0] = new_histogram;
146 base::debug::Alias(debug_new_histogram);
147
[email protected]ec0c0aa2012-08-14 02:02:00148 Sample previous_range = -1; // Bottom range is always 0.
149 for (size_t index = 0; index < histogram.bucket_count(); ++index) {
150 int new_range = histogram.ranges(index);
[email protected]263a17a2012-08-16 03:03:54151 CHECK_LT(previous_range, new_range);
[email protected]ec0c0aa2012-08-14 02:02:00152 previous_range = new_range;
153 }
154
[email protected]263a17a2012-08-16 03:03:54155 CHECK(histogram.bucket_ranges()->HasValidChecksum());
[email protected]ec0c0aa2012-08-14 02:02:00156}
157
[email protected]34d062322012-08-01 21:34:08158Histogram* Histogram::FactoryGet(const string& name,
159 Sample minimum,
160 Sample maximum,
161 size_t bucket_count,
[email protected]7c7a42752012-08-09 05:14:15162 int32 flags) {
[email protected]e184be902012-08-07 04:49:24163 bool valid_arguments =
164 InspectConstructionArguments(name, &minimum, &maximum, &bucket_count);
165 DCHECK(valid_arguments);
[email protected]a93721e22012-01-06 02:13:28166
[email protected]993ae742012-07-18 18:06:30167 Histogram* histogram = StatisticsRecorder::FindHistogram(name);
168 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:53169 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:08170 BucketRanges* ranges = new BucketRanges(bucket_count + 1);
171 InitializeBucketRanges(minimum, maximum, bucket_count, ranges);
172 const BucketRanges* registered_ranges =
173 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
174
175 Histogram* tentative_histogram =
176 new Histogram(name, minimum, maximum, bucket_count, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54177 CheckCorruption(*tentative_histogram, true);
178
[email protected]81ce9f3b2011-04-05 04:48:53179 tentative_histogram->SetFlags(flags);
180 histogram =
181 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37182 }
[email protected]ec0c0aa2012-08-14 02:02:00183 // TODO(rtenneti): delete this code after debugging.
[email protected]263a17a2012-08-16 03:03:54184 CheckCorruption(*histogram, false);
[email protected]e8829a192009-12-06 00:09:37185
[email protected]34d062322012-08-01 21:34:08186 CHECK_EQ(HISTOGRAM, histogram->histogram_type());
[email protected]065d1702012-08-23 23:55:13187 CHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count));
[email protected]e8829a192009-12-06 00:09:37188 return histogram;
189}
190
[email protected]34d062322012-08-01 21:34:08191Histogram* Histogram::FactoryTimeGet(const string& name,
192 TimeDelta minimum,
193 TimeDelta maximum,
194 size_t bucket_count,
[email protected]7c7a42752012-08-09 05:14:15195 int32 flags) {
[email protected]34d062322012-08-01 21:34:08196 return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
197 bucket_count, flags);
198}
199
200TimeTicks Histogram::DebugNow() {
201#ifndef NDEBUG
202 return TimeTicks::Now();
203#else
204 return TimeTicks();
205#endif
206}
207
208// Calculate what range of values are held in each bucket.
209// We have to be careful that we don't pick a ratio between starting points in
210// consecutive buckets that is sooo small, that the integer bounds are the same
211// (effectively making one bucket get no values). We need to avoid:
212// ranges(i) == ranges(i + 1)
213// To avoid that, we just do a fine-grained bucket width as far as we need to
214// until we get a ratio that moves us along at least 2 units at a time. From
215// that bucket onward we do use the exponential growth of buckets.
216//
217// static
218void Histogram::InitializeBucketRanges(Sample minimum,
219 Sample maximum,
220 size_t bucket_count,
221 BucketRanges* ranges) {
222 DCHECK_EQ(ranges->size(), bucket_count + 1);
223 double log_max = log(static_cast<double>(maximum));
224 double log_ratio;
225 double log_next;
226 size_t bucket_index = 1;
227 Sample current = minimum;
228 ranges->set_range(bucket_index, current);
229 while (bucket_count > ++bucket_index) {
230 double log_current;
231 log_current = log(static_cast<double>(current));
232 // Calculate the count'th root of the range.
233 log_ratio = (log_max - log_current) / (bucket_count - bucket_index);
234 // See where the next bucket would start.
235 log_next = log_current + log_ratio;
236 Sample next;
237 next = static_cast<int>(floor(exp(log_next) + 0.5));
238 if (next > current)
239 current = next;
240 else
241 ++current; // Just do a narrow bucket, and keep trying.
242 ranges->set_range(bucket_index, current);
243 }
244 ranges->set_range(ranges->size() - 1, HistogramBase::kSampleType_MAX);
245 ranges->ResetChecksum();
246}
247
248// static
249void Histogram::Add(int value) {
250 if (value > kSampleType_MAX - 1)
251 value = kSampleType_MAX - 1;
252 if (value < 0)
253 value = 0;
254 size_t index = BucketIndex(value);
255 DCHECK_GE(value, ranges(index));
256 DCHECK_LT(value, ranges(index + 1));
257 Accumulate(value, 1, index);
258}
259
260void Histogram::AddBoolean(bool value) {
261 DCHECK(false);
262}
263
264void Histogram::AddSampleSet(const SampleSet& sample) {
265 sample_.Add(sample);
266}
267
268void Histogram::SetRangeDescriptions(const DescriptionPair descriptions[]) {
269 DCHECK(false);
270}
271
272// The following methods provide a graphical histogram display.
273void Histogram::WriteHTMLGraph(string* output) const {
274 // TBD(jar) Write a nice HTML bar chart, with divs an mouse-overs etc.
275 output->append("<PRE>");
276 WriteAsciiImpl(true, "<br>", output);
277 output->append("</PRE>");
278}
279
280void Histogram::WriteAscii(string* output) const {
281 WriteAsciiImpl(true, "\n", output);
282}
283
284// static
285string Histogram::SerializeHistogramInfo(const Histogram& histogram,
286 const SampleSet& snapshot) {
287 DCHECK_NE(NOT_VALID_IN_RENDERER, histogram.histogram_type());
288 DCHECK(histogram.bucket_ranges()->HasValidChecksum());
289
290 Pickle pickle;
291 pickle.WriteString(histogram.histogram_name());
292 pickle.WriteInt(histogram.declared_min());
293 pickle.WriteInt(histogram.declared_max());
294 pickle.WriteUInt64(histogram.bucket_count());
295 pickle.WriteUInt32(histogram.bucket_ranges()->checksum());
296 pickle.WriteInt(histogram.histogram_type());
297 pickle.WriteInt(histogram.flags());
298
299 snapshot.Serialize(&pickle);
300
301 histogram.SerializeRanges(&pickle);
302
303 return string(static_cast<const char*>(pickle.data()), pickle.size());
304}
305
306// static
307bool Histogram::DeserializeHistogramInfo(const string& histogram_info) {
308 if (histogram_info.empty()) {
309 return false;
310 }
311
312 Pickle pickle(histogram_info.data(),
313 static_cast<int>(histogram_info.size()));
314 string histogram_name;
315 int declared_min;
316 int declared_max;
317 uint64 bucket_count;
318 uint32 range_checksum;
319 int histogram_type;
320 int pickle_flags;
321 SampleSet sample;
322
323 PickleIterator iter(pickle);
324 if (!iter.ReadString(&histogram_name) ||
325 !iter.ReadInt(&declared_min) ||
326 !iter.ReadInt(&declared_max) ||
327 !iter.ReadUInt64(&bucket_count) ||
328 !iter.ReadUInt32(&range_checksum) ||
329 !iter.ReadInt(&histogram_type) ||
330 !iter.ReadInt(&pickle_flags) ||
331 !sample.Histogram::SampleSet::Deserialize(&iter)) {
332 DLOG(ERROR) << "Pickle error decoding Histogram: " << histogram_name;
333 return false;
334 }
335
336 DCHECK(pickle_flags & kIPCSerializationSourceFlag);
337 // Since these fields may have come from an untrusted renderer, do additional
338 // checks above and beyond those in Histogram::Initialize()
339 if (declared_max <= 0 || declared_min <= 0 || declared_max < declared_min ||
340 INT_MAX / sizeof(Count) <= bucket_count || bucket_count < 2) {
341 DLOG(ERROR) << "Values error decoding Histogram: " << histogram_name;
342 return false;
343 }
344
345 Flags flags = static_cast<Flags>(pickle_flags & ~kIPCSerializationSourceFlag);
346
347 DCHECK_NE(NOT_VALID_IN_RENDERER, histogram_type);
348
349 Histogram* render_histogram(NULL);
350
351 if (histogram_type == HISTOGRAM) {
352 render_histogram = Histogram::FactoryGet(
353 histogram_name, declared_min, declared_max, bucket_count, flags);
354 } else if (histogram_type == LINEAR_HISTOGRAM) {
355 render_histogram = LinearHistogram::FactoryGet(
356 histogram_name, declared_min, declared_max, bucket_count, flags);
357 } else if (histogram_type == BOOLEAN_HISTOGRAM) {
358 render_histogram = BooleanHistogram::FactoryGet(histogram_name, flags);
359 } else if (histogram_type == CUSTOM_HISTOGRAM) {
360 vector<Sample> sample_ranges(bucket_count);
361 if (!CustomHistogram::DeserializeRanges(&iter, &sample_ranges)) {
362 DLOG(ERROR) << "Pickle error decoding ranges: " << histogram_name;
363 return false;
364 }
365 render_histogram =
366 CustomHistogram::FactoryGet(histogram_name, sample_ranges, flags);
367 } else {
368 DLOG(ERROR) << "Error Deserializing Histogram Unknown histogram_type: "
369 << histogram_type;
370 return false;
371 }
372
373 DCHECK_EQ(render_histogram->declared_min(), declared_min);
374 DCHECK_EQ(render_histogram->declared_max(), declared_max);
375 DCHECK_EQ(render_histogram->bucket_count(), bucket_count);
376 DCHECK_EQ(render_histogram->histogram_type(), histogram_type);
377
378 if (render_histogram->bucket_ranges()->checksum() != range_checksum) {
379 return false;
380 }
381
382 if (render_histogram->flags() & kIPCSerializationSourceFlag) {
383 DVLOG(1) << "Single process mode, histogram observed and not copied: "
384 << histogram_name;
385 } else {
386 DCHECK_EQ(flags & render_histogram->flags(), flags);
387 render_histogram->AddSampleSet(sample);
388 }
389
390 return true;
391}
392
393
394// Validate a sample and related histogram.
395Histogram::Inconsistencies Histogram::FindCorruption(
396 const SampleSet& snapshot) const {
397 int inconsistencies = NO_INCONSISTENCIES;
398 Sample previous_range = -1; // Bottom range is always 0.
399 int64 count = 0;
400 for (size_t index = 0; index < bucket_count(); ++index) {
401 count += snapshot.counts(index);
402 int new_range = ranges(index);
403 if (previous_range >= new_range)
404 inconsistencies |= BUCKET_ORDER_ERROR;
405 previous_range = new_range;
406 }
407
408 if (!bucket_ranges()->HasValidChecksum())
409 inconsistencies |= RANGE_CHECKSUM_ERROR;
410
411 int64 delta64 = snapshot.redundant_count() - count;
412 if (delta64 != 0) {
413 int delta = static_cast<int>(delta64);
414 if (delta != delta64)
415 delta = INT_MAX; // Flag all giant errors as INT_MAX.
416 // Since snapshots of histograms are taken asynchronously relative to
417 // sampling (and snapped from different threads), it is pretty likely that
418 // we'll catch a redundant count that doesn't match the sample count. We
419 // allow for a certain amount of slop before flagging this as an
420 // inconsistency. Even with an inconsistency, we'll snapshot it again (for
421 // UMA in about a half hour, so we'll eventually get the data, if it was
422 // not the result of a corruption. If histograms show that 1 is "too tight"
423 // then we may try to use 2 or 3 for this slop value.
424 const int kCommonRaceBasedCountMismatch = 5;
425 if (delta > 0) {
426 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountHigh", delta);
427 if (delta > kCommonRaceBasedCountMismatch)
428 inconsistencies |= COUNT_HIGH_ERROR;
429 } else {
430 DCHECK_GT(0, delta);
431 UMA_HISTOGRAM_COUNTS("Histogram.InconsistentCountLow", -delta);
432 if (-delta > kCommonRaceBasedCountMismatch)
433 inconsistencies |= COUNT_LOW_ERROR;
434 }
435 }
436 return static_cast<Inconsistencies>(inconsistencies);
437}
438
439Histogram::ClassType Histogram::histogram_type() const {
440 return HISTOGRAM;
441}
442
443Sample Histogram::ranges(size_t i) const {
444 return bucket_ranges_->range(i);
445}
446
447size_t Histogram::bucket_count() const {
448 return bucket_count_;
449}
450
451// Do a safe atomic snapshot of sample data.
452// This implementation assumes we are on a safe single thread.
453void Histogram::SnapshotSample(SampleSet* sample) const {
454 // Note locking not done in this version!!!
455 *sample = sample_;
456}
457
458bool Histogram::HasConstructionArguments(Sample minimum,
459 Sample maximum,
460 size_t bucket_count) {
461 return ((minimum == declared_min_) && (maximum == declared_max_) &&
462 (bucket_count == bucket_count_));
463}
464
465Histogram::Histogram(const string& name,
466 Sample minimum,
467 Sample maximum,
468 size_t bucket_count,
469 const BucketRanges* ranges)
470 : HistogramBase(name),
471 bucket_ranges_(ranges),
472 declared_min_(minimum),
473 declared_max_(maximum),
474 bucket_count_(bucket_count),
[email protected]34d062322012-08-01 21:34:08475 sample_(bucket_count) {}
476
477Histogram::~Histogram() {
478 if (StatisticsRecorder::dump_on_exit()) {
479 string output;
480 WriteAsciiImpl(true, "\n", &output);
481 DLOG(INFO) << output;
482 }
483}
484
485// static
486bool Histogram::InspectConstructionArguments(const string& name,
487 Sample* minimum,
488 Sample* maximum,
489 size_t* bucket_count) {
490 // Defensive code for backward compatibility.
491 if (*minimum < 1) {
[email protected]a5c7bd792012-08-02 00:29:04492 DVLOG(1) << "Histogram: " << name << " has bad minimum: " << *minimum;
[email protected]34d062322012-08-01 21:34:08493 *minimum = 1;
494 }
495 if (*maximum >= kSampleType_MAX) {
[email protected]a5c7bd792012-08-02 00:29:04496 DVLOG(1) << "Histogram: " << name << " has bad maximum: " << *maximum;
497 *maximum = kSampleType_MAX - 1;
[email protected]34d062322012-08-01 21:34:08498 }
[email protected]e184be902012-08-07 04:49:24499 if (*bucket_count >= kBucketCount_MAX) {
500 DVLOG(1) << "Histogram: " << name << " has bad bucket_count: "
501 << *bucket_count;
502 *bucket_count = kBucketCount_MAX - 1;
503 }
[email protected]34d062322012-08-01 21:34:08504
[email protected]e184be902012-08-07 04:49:24505 if (*minimum >= *maximum)
506 return false;
507 if (*bucket_count < 3)
[email protected]34d062322012-08-01 21:34:08508 return false;
509 if (*bucket_count > static_cast<size_t>(*maximum - *minimum + 2))
510 return false;
511 return true;
512}
513
514bool Histogram::SerializeRanges(Pickle* pickle) const {
515 return true;
516}
517
518bool Histogram::PrintEmptyBucket(size_t index) const {
519 return true;
520}
521
522size_t Histogram::BucketIndex(Sample value) const {
523 // Use simple binary search. This is very general, but there are better
524 // approaches if we knew that the buckets were linearly distributed.
525 DCHECK_LE(ranges(0), value);
526 DCHECK_GT(ranges(bucket_count()), value);
527 size_t under = 0;
528 size_t over = bucket_count();
529 size_t mid;
530
531 do {
532 DCHECK_GE(over, under);
533 mid = under + (over - under)/2;
534 if (mid == under)
535 break;
536 if (ranges(mid) <= value)
537 under = mid;
538 else
539 over = mid;
540 } while (true);
541
542 DCHECK_LE(ranges(mid), value);
543 CHECK_GT(ranges(mid+1), value);
544 return mid;
545}
546
547// Use the actual bucket widths (like a linear histogram) until the widths get
548// over some transition value, and then use that transition width. Exponentials
549// get so big so fast (and we don't expect to see a lot of entries in the large
550// buckets), so we need this to make it possible to see what is going on and
551// not have 0-graphical-height buckets.
552double Histogram::GetBucketSize(Count current, size_t i) const {
553 DCHECK_GT(ranges(i + 1), ranges(i));
554 static const double kTransitionWidth = 5;
555 double denominator = ranges(i + 1) - ranges(i);
556 if (denominator > kTransitionWidth)
557 denominator = kTransitionWidth; // Stop trying to normalize.
558 return current/denominator;
559}
560
561const string Histogram::GetAsciiBucketRange(size_t i) const {
562 string result;
[email protected]7c7a42752012-08-09 05:14:15563 if (kHexRangePrintingFlag & flags())
[email protected]34d062322012-08-01 21:34:08564 StringAppendF(&result, "%#x", ranges(i));
565 else
566 StringAppendF(&result, "%d", ranges(i));
567 return result;
568}
569
570// Update histogram data with new sample.
571void Histogram::Accumulate(Sample value, Count count, size_t index) {
572 // Note locking not done in this version!!!
573 sample_.Accumulate(value, count, index);
574}
575
576//------------------------------------------------------------------------------
577// Private methods
578
579void Histogram::WriteAsciiImpl(bool graph_it,
580 const string& newline,
581 string* output) const {
582 // Get local (stack) copies of all effectively volatile class data so that we
583 // are consistent across our output activities.
584 SampleSet snapshot;
585 SnapshotSample(&snapshot);
586 Count sample_count = snapshot.TotalCount();
587
588 WriteAsciiHeader(snapshot, sample_count, output);
589 output->append(newline);
590
591 // Prepare to normalize graphical rendering of bucket contents.
592 double max_size = 0;
593 if (graph_it)
594 max_size = GetPeakBucketSize(snapshot);
595
596 // Calculate space needed to print bucket range numbers. Leave room to print
597 // nearly the largest bucket range without sliding over the histogram.
598 size_t largest_non_empty_bucket = bucket_count() - 1;
599 while (0 == snapshot.counts(largest_non_empty_bucket)) {
600 if (0 == largest_non_empty_bucket)
601 break; // All buckets are empty.
602 --largest_non_empty_bucket;
603 }
604
605 // Calculate largest print width needed for any of our bucket range displays.
606 size_t print_width = 1;
607 for (size_t i = 0; i < bucket_count(); ++i) {
608 if (snapshot.counts(i)) {
609 size_t width = GetAsciiBucketRange(i).size() + 1;
610 if (width > print_width)
611 print_width = width;
612 }
613 }
614
615 int64 remaining = sample_count;
616 int64 past = 0;
617 // Output the actual histogram graph.
618 for (size_t i = 0; i < bucket_count(); ++i) {
619 Count current = snapshot.counts(i);
620 if (!current && !PrintEmptyBucket(i))
621 continue;
622 remaining -= current;
623 string range = GetAsciiBucketRange(i);
624 output->append(range);
625 for (size_t j = 0; range.size() + j < print_width + 1; ++j)
626 output->push_back(' ');
627 if (0 == current && i < bucket_count() - 1 && 0 == snapshot.counts(i + 1)) {
628 while (i < bucket_count() - 1 && 0 == snapshot.counts(i + 1))
629 ++i;
630 output->append("... ");
631 output->append(newline);
632 continue; // No reason to plot emptiness.
633 }
634 double current_size = GetBucketSize(current, i);
635 if (graph_it)
636 WriteAsciiBucketGraph(current_size, max_size, output);
637 WriteAsciiBucketContext(past, current, remaining, i, output);
638 output->append(newline);
639 past += current;
640 }
641 DCHECK_EQ(sample_count, past);
642}
643
644double Histogram::GetPeakBucketSize(const SampleSet& snapshot) const {
645 double max = 0;
646 for (size_t i = 0; i < bucket_count() ; ++i) {
647 double current_size = GetBucketSize(snapshot.counts(i), i);
648 if (current_size > max)
649 max = current_size;
650 }
651 return max;
652}
653
654void Histogram::WriteAsciiHeader(const SampleSet& snapshot,
655 Count sample_count,
656 string* output) const {
657 StringAppendF(output,
658 "Histogram: %s recorded %d samples",
659 histogram_name().c_str(),
660 sample_count);
661 if (0 == sample_count) {
662 DCHECK_EQ(snapshot.sum(), 0);
663 } else {
664 double average = static_cast<float>(snapshot.sum()) / sample_count;
665
666 StringAppendF(output, ", average = %.1f", average);
667 }
[email protected]7c7a42752012-08-09 05:14:15668 if (flags() & ~kHexRangePrintingFlag)
669 StringAppendF(output, " (flags = 0x%x)", flags() & ~kHexRangePrintingFlag);
[email protected]34d062322012-08-01 21:34:08670}
671
672void Histogram::WriteAsciiBucketContext(const int64 past,
673 const Count current,
674 const int64 remaining,
675 const size_t i,
676 string* output) const {
677 double scaled_sum = (past + current + remaining) / 100.0;
678 WriteAsciiBucketValue(current, scaled_sum, output);
679 if (0 < i) {
680 double percentage = past / scaled_sum;
681 StringAppendF(output, " {%3.1f%%}", percentage);
682 }
683}
684
685void Histogram::WriteAsciiBucketValue(Count current,
686 double scaled_sum,
687 string* output) const {
688 StringAppendF(output, " (%d = %3.1f%%)", current, current/scaled_sum);
689}
690
691void Histogram::WriteAsciiBucketGraph(double current_size,
692 double max_size,
693 string* output) const {
694 const int k_line_length = 72; // Maximal horizontal width of graph.
695 int x_count = static_cast<int>(k_line_length * (current_size / max_size)
696 + 0.5);
697 int x_remainder = k_line_length - x_count;
698
699 while (0 < x_count--)
700 output->append("-");
701 output->append("O");
702 while (0 < x_remainder--)
703 output->append(" ");
704}
705
706//------------------------------------------------------------------------------
707// LinearHistogram: This histogram uses a traditional set of evenly spaced
708// buckets.
709//------------------------------------------------------------------------------
710
711LinearHistogram::~LinearHistogram() {}
712
713Histogram* LinearHistogram::FactoryGet(const string& name,
714 Sample minimum,
715 Sample maximum,
716 size_t bucket_count,
[email protected]7c7a42752012-08-09 05:14:15717 int32 flags) {
[email protected]e184be902012-08-07 04:49:24718 bool valid_arguments = Histogram::InspectConstructionArguments(
719 name, &minimum, &maximum, &bucket_count);
720 DCHECK(valid_arguments);
[email protected]34d062322012-08-01 21:34:08721
722 Histogram* histogram = StatisticsRecorder::FindHistogram(name);
723 if (!histogram) {
724 // To avoid racy destruction at shutdown, the following will be leaked.
725 BucketRanges* ranges = new BucketRanges(bucket_count + 1);
726 InitializeBucketRanges(minimum, maximum, bucket_count, ranges);
727 const BucketRanges* registered_ranges =
728 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
729
730 LinearHistogram* tentative_histogram =
731 new LinearHistogram(name, minimum, maximum, bucket_count,
732 registered_ranges);
[email protected]263a17a2012-08-16 03:03:54733 CheckCorruption(*tentative_histogram, true);
734
[email protected]34d062322012-08-01 21:34:08735 tentative_histogram->SetFlags(flags);
736 histogram =
737 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
738 }
[email protected]ec0c0aa2012-08-14 02:02:00739 // TODO(rtenneti): delete this code after debugging.
[email protected]263a17a2012-08-16 03:03:54740 CheckCorruption(*histogram, false);
[email protected]34d062322012-08-01 21:34:08741
742 CHECK_EQ(LINEAR_HISTOGRAM, histogram->histogram_type());
[email protected]065d1702012-08-23 23:55:13743 CHECK(histogram->HasConstructionArguments(minimum, maximum, bucket_count));
[email protected]34d062322012-08-01 21:34:08744 return histogram;
745}
746
747Histogram* LinearHistogram::FactoryTimeGet(const string& name,
[email protected]81ce9f3b2011-04-05 04:48:53748 TimeDelta minimum,
749 TimeDelta maximum,
750 size_t bucket_count,
[email protected]7c7a42752012-08-09 05:14:15751 int32 flags) {
[email protected]2753b392009-12-28 06:59:52752 return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
753 bucket_count, flags);
[email protected]e8829a192009-12-06 00:09:37754}
755
[email protected]b7d08202011-01-25 17:29:39756Histogram::ClassType LinearHistogram::histogram_type() const {
757 return LINEAR_HISTOGRAM;
758}
759
760void LinearHistogram::SetRangeDescriptions(
761 const DescriptionPair descriptions[]) {
762 for (int i =0; descriptions[i].description; ++i) {
763 bucket_description_[descriptions[i].sample] = descriptions[i].description;
764 }
[email protected]d4799a32010-09-28 22:54:58765}
766
[email protected]34d062322012-08-01 21:34:08767LinearHistogram::LinearHistogram(const string& name,
[email protected]835d7c82010-10-14 04:38:38768 Sample minimum,
769 Sample maximum,
[email protected]34d062322012-08-01 21:34:08770 size_t bucket_count,
771 const BucketRanges* ranges)
772 : Histogram(name, minimum, maximum, bucket_count, ranges) {
initial.commitd7cae122008-07-26 21:49:38773}
774
initial.commitd7cae122008-07-26 21:49:38775double LinearHistogram::GetBucketSize(Count current, size_t i) const {
[email protected]2ef3748f2010-10-19 17:33:28776 DCHECK_GT(ranges(i + 1), ranges(i));
initial.commitd7cae122008-07-26 21:49:38777 // Adjacent buckets with different widths would have "surprisingly" many (few)
778 // samples in a histogram if we didn't normalize this way.
779 double denominator = ranges(i + 1) - ranges(i);
780 return current/denominator;
781}
782
[email protected]34d062322012-08-01 21:34:08783const string LinearHistogram::GetAsciiBucketRange(size_t i) const {
[email protected]b7d08202011-01-25 17:29:39784 int range = ranges(i);
785 BucketDescriptionMap::const_iterator it = bucket_description_.find(range);
786 if (it == bucket_description_.end())
787 return Histogram::GetAsciiBucketRange(i);
788 return it->second;
789}
790
791bool LinearHistogram::PrintEmptyBucket(size_t index) const {
792 return bucket_description_.find(ranges(index)) == bucket_description_.end();
793}
794
[email protected]34d062322012-08-01 21:34:08795// static
796void LinearHistogram::InitializeBucketRanges(Sample minimum,
797 Sample maximum,
798 size_t bucket_count,
799 BucketRanges* ranges) {
800 DCHECK_EQ(ranges->size(), bucket_count + 1);
801 double min = minimum;
802 double max = maximum;
803 size_t i;
804 for (i = 1; i < bucket_count; ++i) {
805 double linear_range =
806 (min * (bucket_count -1 - i) + max * (i - 1)) / (bucket_count - 2);
807 ranges->set_range(i, static_cast<Sample>(linear_range + 0.5));
808 }
809 ranges->set_range(ranges->size() - 1, HistogramBase::kSampleType_MAX);
810 ranges->ResetChecksum();
811}
[email protected]b7d08202011-01-25 17:29:39812
initial.commitd7cae122008-07-26 21:49:38813//------------------------------------------------------------------------------
[email protected]e8829a192009-12-06 00:09:37814// This section provides implementation for BooleanHistogram.
815//------------------------------------------------------------------------------
816
[email protected]7c7a42752012-08-09 05:14:15817Histogram* BooleanHistogram::FactoryGet(const string& name, int32 flags) {
[email protected]993ae742012-07-18 18:06:30818 Histogram* histogram = StatisticsRecorder::FindHistogram(name);
819 if (!histogram) {
[email protected]81ce9f3b2011-04-05 04:48:53820 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:08821 BucketRanges* ranges = new BucketRanges(4);
822 LinearHistogram::InitializeBucketRanges(1, 2, 3, ranges);
823 const BucketRanges* registered_ranges =
824 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
825
826 BooleanHistogram* tentative_histogram =
827 new BooleanHistogram(name, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54828 CheckCorruption(*tentative_histogram, true);
829
[email protected]81ce9f3b2011-04-05 04:48:53830 tentative_histogram->SetFlags(flags);
831 histogram =
832 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]e8829a192009-12-06 00:09:37833 }
[email protected]ec0c0aa2012-08-14 02:02:00834 // TODO(rtenneti): delete this code after debugging.
[email protected]263a17a2012-08-16 03:03:54835 CheckCorruption(*histogram, false);
[email protected]e8829a192009-12-06 00:09:37836
[email protected]34d062322012-08-01 21:34:08837 CHECK_EQ(BOOLEAN_HISTOGRAM, histogram->histogram_type());
[email protected]e8829a192009-12-06 00:09:37838 return histogram;
839}
840
[email protected]5d91c9e2010-07-28 17:25:28841Histogram::ClassType BooleanHistogram::histogram_type() const {
842 return BOOLEAN_HISTOGRAM;
843}
844
845void BooleanHistogram::AddBoolean(bool value) {
846 Add(value ? 1 : 0);
847}
848
[email protected]34d062322012-08-01 21:34:08849BooleanHistogram::BooleanHistogram(const string& name,
850 const BucketRanges* ranges)
851 : LinearHistogram(name, 1, 2, 3, ranges) {}
initial.commitd7cae122008-07-26 21:49:38852
853//------------------------------------------------------------------------------
[email protected]70cc56e42010-04-29 22:39:55854// CustomHistogram:
855//------------------------------------------------------------------------------
856
[email protected]34d062322012-08-01 21:34:08857Histogram* CustomHistogram::FactoryGet(const string& name,
858 const vector<Sample>& custom_ranges,
[email protected]7c7a42752012-08-09 05:14:15859 int32 flags) {
[email protected]34d062322012-08-01 21:34:08860 CHECK(ValidateCustomRanges(custom_ranges));
[email protected]70cc56e42010-04-29 22:39:55861
[email protected]993ae742012-07-18 18:06:30862 Histogram* histogram = StatisticsRecorder::FindHistogram(name);
863 if (!histogram) {
[email protected]34d062322012-08-01 21:34:08864 BucketRanges* ranges = CreateBucketRangesFromCustomRanges(custom_ranges);
865 const BucketRanges* registered_ranges =
866 StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges);
867
[email protected]81ce9f3b2011-04-05 04:48:53868 // To avoid racy destruction at shutdown, the following will be leaked.
[email protected]34d062322012-08-01 21:34:08869 CustomHistogram* tentative_histogram =
870 new CustomHistogram(name, registered_ranges);
[email protected]263a17a2012-08-16 03:03:54871 CheckCorruption(*tentative_histogram, true);
872
[email protected]81ce9f3b2011-04-05 04:48:53873 tentative_histogram->SetFlags(flags);
[email protected]34d062322012-08-01 21:34:08874
[email protected]81ce9f3b2011-04-05 04:48:53875 histogram =
876 StatisticsRecorder::RegisterOrDeleteDuplicate(tentative_histogram);
[email protected]70cc56e42010-04-29 22:39:55877 }
[email protected]ec0c0aa2012-08-14 02:02:00878 // TODO(rtenneti): delete this code after debugging.
[email protected]263a17a2012-08-16 03:03:54879 CheckCorruption(*histogram, false);
[email protected]70cc56e42010-04-29 22:39:55880
[email protected]34d062322012-08-01 21:34:08881 CHECK_EQ(histogram->histogram_type(), CUSTOM_HISTOGRAM);
[email protected]70cc56e42010-04-29 22:39:55882 return histogram;
883}
884
[email protected]5d91c9e2010-07-28 17:25:28885Histogram::ClassType CustomHistogram::histogram_type() const {
886 return CUSTOM_HISTOGRAM;
887}
888
[email protected]961fefb2011-05-24 13:59:58889// static
[email protected]34d062322012-08-01 21:34:08890vector<Sample> CustomHistogram::ArrayToCustomRanges(
[email protected]961fefb2011-05-24 13:59:58891 const Sample* values, size_t num_values) {
[email protected]34d062322012-08-01 21:34:08892 vector<Sample> all_values;
[email protected]961fefb2011-05-24 13:59:58893 for (size_t i = 0; i < num_values; ++i) {
894 Sample value = values[i];
895 all_values.push_back(value);
896
897 // Ensure that a guard bucket is added. If we end up with duplicate
898 // values, FactoryGet will take care of removing them.
899 all_values.push_back(value + 1);
900 }
901 return all_values;
902}
903
[email protected]34d062322012-08-01 21:34:08904CustomHistogram::CustomHistogram(const string& name,
905 const BucketRanges* ranges)
906 : Histogram(name,
907 ranges->range(1),
908 ranges->range(ranges->size() - 2),
909 ranges->size() - 1,
910 ranges) {}
[email protected]70cc56e42010-04-29 22:39:55911
[email protected]cd56dff2011-11-13 04:19:15912bool CustomHistogram::SerializeRanges(Pickle* pickle) const {
[email protected]4a32f122012-07-25 20:02:48913 for (size_t i = 0; i < bucket_ranges()->size(); ++i) {
914 if (!pickle->WriteInt(bucket_ranges()->range(i)))
[email protected]cd56dff2011-11-13 04:19:15915 return false;
916 }
917 return true;
918}
919
920// static
921bool CustomHistogram::DeserializeRanges(
[email protected]34d062322012-08-01 21:34:08922 PickleIterator* iter, vector<Sample>* ranges) {
[email protected]cd56dff2011-11-13 04:19:15923 for (size_t i = 0; i < ranges->size(); ++i) {
[email protected]ce208f872012-03-07 20:42:56924 if (!iter->ReadInt(&(*ranges)[i]))
[email protected]cd56dff2011-11-13 04:19:15925 return false;
926 }
927 return true;
928}
929
[email protected]70cc56e42010-04-29 22:39:55930double CustomHistogram::GetBucketSize(Count current, size_t i) const {
931 return 1;
932}
933
[email protected]34d062322012-08-01 21:34:08934// static
935bool CustomHistogram::ValidateCustomRanges(
936 const vector<Sample>& custom_ranges) {
[email protected]640d95ef2012-08-04 06:23:03937 bool has_valid_range = false;
[email protected]34d062322012-08-01 21:34:08938 for (size_t i = 0; i < custom_ranges.size(); i++) {
[email protected]640d95ef2012-08-04 06:23:03939 Sample sample = custom_ranges[i];
940 if (sample < 0 || sample > HistogramBase::kSampleType_MAX - 1)
[email protected]34d062322012-08-01 21:34:08941 return false;
[email protected]640d95ef2012-08-04 06:23:03942 if (sample != 0)
943 has_valid_range = true;
[email protected]34d062322012-08-01 21:34:08944 }
[email protected]640d95ef2012-08-04 06:23:03945 return has_valid_range;
[email protected]34d062322012-08-01 21:34:08946}
947
948// static
949BucketRanges* CustomHistogram::CreateBucketRangesFromCustomRanges(
950 const vector<Sample>& custom_ranges) {
951 // Remove the duplicates in the custom ranges array.
952 vector<int> ranges = custom_ranges;
953 ranges.push_back(0); // Ensure we have a zero value.
954 ranges.push_back(HistogramBase::kSampleType_MAX);
955 std::sort(ranges.begin(), ranges.end());
956 ranges.erase(std::unique(ranges.begin(), ranges.end()), ranges.end());
957
958 BucketRanges* bucket_ranges = new BucketRanges(ranges.size());
959 for (size_t i = 0; i < ranges.size(); i++) {
960 bucket_ranges->set_range(i, ranges[i]);
961 }
962 bucket_ranges->ResetChecksum();
963 return bucket_ranges;
964}
965
[email protected]835d7c82010-10-14 04:38:38966} // namespace base