histogram.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 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 9// It supports calls to accumulate either time intervals (which are processed 10// as integral number of milliseconds), or arbitrary integral units. 11 12// For Histogram(exponential histogram), LinearHistogram and CustomHistogram, 13// the minimum for a declared range is 1 (instead of 0), while the maximum is 14// (HistogramBase::kSampleType_MAX - 1). Currently you can declare histograms 15// with ranges exceeding those limits (e.g. 0 as minimal or 16// HistogramBase::kSampleType_MAX as maximal), but those excesses will be 17// silently clamped to those limits (for backwards compatibility with existing 18// code). Best practice is to not exceed the limits. 19 20// Each use of a histogram with the same name will reference the same underlying 21// data, so it is safe to record to the same histogram from multiple locations 22// in the code. It is a runtime error if all uses of the same histogram do not 23// agree exactly in type, bucket size and range. 24 25// For Histogram and LinearHistogram, the maximum for a declared range should 26// always be larger (not equal) than minmal range. Zero and 27// HistogramBase::kSampleType_MAX are implicitly added as first and last ranges, 28// so the smallest legal bucket_count is 3. However CustomHistogram can have 29// bucket count as 2 (when you give a custom ranges vector containing only 1 30// range). 31// For these 3 kinds of histograms, the max bucket count is always 32// (Histogram::kBucketCount_MAX - 1). 33 34// The buckets layout of class Histogram is exponential. For example, buckets 35// might contain (sequentially) the count of values in the following intervals: 36// [0,1), [1,2), [2,4), [4,8), [8,16), [16,32), [32,64), [64,infinity) 37// That bucket allocation would actually result from construction of a histogram 38// for values between 1 and 64, with 8 buckets, such as: 39// Histogram count("some name", 1, 64, 8); 40// Note that the underflow bucket [0,1) and the overflow bucket [64,infinity) 41// are also counted by the constructor in the user supplied "bucket_count" 42// argument. 43// The above example has an exponential ratio of 2 (doubling the bucket width 44// in each consecutive bucket. The Histogram class automatically calculates 45// the smallest ratio that it can use to construct the number of buckets 46// selected in the constructor. An another example, if you had 50 buckets, 47// and millisecond time values from 1 to 10000, then the ratio between 48// consecutive bucket widths will be approximately somewhere around the 50th 49// root of 10000. This approach provides very fine grain (narrow) buckets 50// at the low end of the histogram scale, but allows the histogram to cover a 51// gigantic range with the addition of very few buckets. 52 53// Usually we use macros to define and use a histogram. These macros use a 54// pattern involving a function static variable, that is a pointer to a 55// histogram. This static is explicitly initialized on any thread 56// that detects a uninitialized (NULL) pointer. The potentially racy 57// initialization is not a problem as it is always set to point to the same 58// value (i.e., the FactoryGet always returns the same value). FactoryGet 59// is also completely thread safe, which results in a completely thread safe, 60// and relatively fast, set of counters. To avoid races at shutdown, the static 61// pointer is NOT deleted, and we leak the histograms at process termination. 62 63#ifndef BASE_METRICS_HISTOGRAM_H_ 64#define BASE_METRICS_HISTOGRAM_H_ 65 66#include <map> 67#include <string> 68#include <vector> 69 70#include "base/atomicops.h" 71#include "base/base_export.h" 72#include "base/basictypes.h" 73#include "base/compiler_specific.h" 74#include "base/gtest_prod_util.h" 75#include "base/logging.h" 76#include "base/memory/scoped_ptr.h" 77#include "base/metrics/bucket_ranges.h" 78#include "base/metrics/histogram_base.h" 79#include "base/metrics/histogram_samples.h" 80#include "base/time.h" 81 82class Pickle; 83class PickleIterator; 84 85namespace base { 86 87class Lock; 88//------------------------------------------------------------------------------ 89// Histograms are often put in areas where they are called many many times, and 90// performance is critical. As a result, they are designed to have a very low 91// recurring cost of executing (adding additional samples). Toward that end, 92// the macros declare a static pointer to the histogram in question, and only 93// take a "slow path" to construct (or find) the histogram on the first run 94// through the macro. We leak the histograms at shutdown time so that we don't 95// have to validate using the pointers at any time during the running of the 96// process. 97 98// The following code is generally what a thread-safe static pointer 99// initializaion looks like for a histogram (after a macro is expanded). This 100// sample is an expansion (with comments) of the code for 101// HISTOGRAM_CUSTOM_COUNTS(). 102 103/* 104 do { 105 // The pointer's presence indicates the initialization is complete. 106 // Initialization is idempotent, so it can safely be atomically repeated. 107 static base::subtle::AtomicWord atomic_histogram_pointer = 0; 108 109 // Acquire_Load() ensures that we acquire visibility to the pointed-to data 110 // in the histogrom. 111 base::Histogram* histogram_pointer(reinterpret_cast<base::Histogram*>( 112 base::subtle::Acquire_Load(&atomic_histogram_pointer))); 113 114 if (!histogram_pointer) { 115 // This is the slow path, which will construct OR find the matching 116 // histogram. FactoryGet includes locks on a global histogram name map 117 // and is completely thread safe. 118 histogram_pointer = base::Histogram::FactoryGet( 119 name, min, max, bucket_count, base::HistogramBase::kNoFlags); 120 121 // Use Release_Store to ensure that the histogram data is made available 122 // globally before we make the pointer visible. 123 // Several threads may perform this store, but the same value will be 124 // stored in all cases (for a given named/spec'ed histogram). 125 // We could do this without any barrier, since FactoryGet entered and 126 // exited a lock after construction, but this barrier makes things clear. 127 base::subtle::Release_Store(&atomic_histogram_pointer, 128 reinterpret_cast<base::subtle::AtomicWord>(histogram_pointer)); 129 } 130 131 // Ensure calling contract is upheld, and the name does NOT vary. 132 DCHECK(histogram_pointer->histogram_name() == constant_histogram_name); 133 134 histogram_pointer->Add(sample); 135 } while (0); 136*/ 137 138// The above pattern is repeated in several macros. The only elements that 139// vary are the invocation of the Add(sample) vs AddTime(sample), and the choice 140// of which FactoryGet method to use. The different FactoryGet methods have 141// various argument lists, so the function with its argument list is provided as 142// a macro argument here. The name is only used in a DCHECK, to assure that 143// callers don't try to vary the name of the histogram (which would tend to be 144// ignored by the one-time initialization of the histogtram_pointer). 145#define STATIC_HISTOGRAM_POINTER_BLOCK(constant_histogram_name, \ 146 histogram_add_method_invocation, \ 147 histogram_factory_get_invocation) \ 148 do { \ 149 static base::subtle::AtomicWord atomic_histogram_pointer = 0; \ 150 base::HistogramBase* histogram_pointer( \ 151 reinterpret_cast<base::HistogramBase*>( \ 152 base::subtle::Acquire_Load(&atomic_histogram_pointer))); \ 153 if (!histogram_pointer) { \ 154 histogram_pointer = histogram_factory_get_invocation; \ 155 base::subtle::Release_Store(&atomic_histogram_pointer, \ 156 reinterpret_cast<base::subtle::AtomicWord>(histogram_pointer)); \ 157 } \ 158 DCHECK_EQ(histogram_pointer->histogram_name(), \ 159 std::string(constant_histogram_name)); \ 160 histogram_pointer->histogram_add_method_invocation; \ 161 } while (0) 162 163 164//------------------------------------------------------------------------------ 165// Provide easy general purpose histogram in a macro, just like stats counters. 166// The first four macros use 50 buckets. 167 168#define HISTOGRAM_TIMES(name, sample) HISTOGRAM_CUSTOM_TIMES( \ 169 name, sample, base::TimeDelta::FromMilliseconds(1), \ 170 base::TimeDelta::FromSeconds(10), 50) 171 172// For folks that need real specific times, use this to select a precise range 173// of times you want plotted, and the number of buckets you want used. 174#define HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \ 175 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddTime(sample), \ 176 base::Histogram::FactoryTimeGet(name, min, max, bucket_count, \ 177 base::HistogramBase::kNoFlags)) 178 179#define HISTOGRAM_COUNTS(name, sample) HISTOGRAM_CUSTOM_COUNTS( \ 180 name, sample, 1, 1000000, 50) 181 182#define HISTOGRAM_COUNTS_100(name, sample) HISTOGRAM_CUSTOM_COUNTS( \ 183 name, sample, 1, 100, 50) 184 185#define HISTOGRAM_COUNTS_10000(name, sample) HISTOGRAM_CUSTOM_COUNTS( \ 186 name, sample, 1, 10000, 50) 187 188#define HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \ 189 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ 190 base::Histogram::FactoryGet(name, min, max, bucket_count, \ 191 base::HistogramBase::kNoFlags)) 192 193#define HISTOGRAM_PERCENTAGE(name, under_one_hundred) \ 194 HISTOGRAM_ENUMERATION(name, under_one_hundred, 101) 195 196#define HISTOGRAM_BOOLEAN(name, sample) \ 197 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \ 198 base::BooleanHistogram::FactoryGet(name, base::Histogram::kNoFlags)) 199 200// Support histograming of an enumerated value. The samples should always be 201// strictly less than |boundary_value| -- this prevents you from running into 202// problems down the line if you add additional buckets to the histogram. Note 203// also that, despite explicitly setting the minimum bucket value to |1| below, 204// it is fine for enumerated histograms to be 0-indexed -- this is because 205// enumerated histograms should never have underflow. 206#define HISTOGRAM_ENUMERATION(name, sample, boundary_value) \ 207 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ 208 base::LinearHistogram::FactoryGet(name, 1, boundary_value, \ 209 boundary_value + 1, base::HistogramBase::kNoFlags)) 210 211// Support histograming of an enumerated value. Samples should be one of the 212// std::vector<int> list provided via |custom_ranges|. See comments above 213// CustomRanges::FactoryGet about the requirement of |custom_ranges|. 214// You can use the helper function CustomHistogram::ArrayToCustomRanges to 215// transform a C-style array of valid sample values to a std::vector<int>. 216#define HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \ 217 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ 218 base::CustomHistogram::FactoryGet(name, custom_ranges, \ 219 base::HistogramBase::kNoFlags)) 220 221#define HISTOGRAM_MEMORY_KB(name, sample) HISTOGRAM_CUSTOM_COUNTS( \ 222 name, sample, 1000, 500000, 50) 223 224//------------------------------------------------------------------------------ 225// Define Debug vs non-debug flavors of macros. 226#ifndef NDEBUG 227 228#define DHISTOGRAM_TIMES(name, sample) HISTOGRAM_TIMES(name, sample) 229#define DHISTOGRAM_COUNTS(name, sample) HISTOGRAM_COUNTS(name, sample) 230#define DHISTOGRAM_PERCENTAGE(name, under_one_hundred) HISTOGRAM_PERCENTAGE(\ 231 name, under_one_hundred) 232#define DHISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \ 233 HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) 234#define DHISTOGRAM_CLIPPED_TIMES(name, sample, min, max, bucket_count) \ 235 HISTOGRAM_CLIPPED_TIMES(name, sample, min, max, bucket_count) 236#define DHISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \ 237 HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) 238#define DHISTOGRAM_ENUMERATION(name, sample, boundary_value) \ 239 HISTOGRAM_ENUMERATION(name, sample, boundary_value) 240#define DHISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \ 241 HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) 242 243#else // NDEBUG 244// Keep a mention of passed variables to avoid unused variable warnings in 245// release build if these variables are only used in macros. 246#define DISCARD_2_ARGUMENTS(a, b) \ 247 while (0) { \ 248 static_cast<void>(a); \ 249 static_cast<void>(b); \ 250 } 251#define DISCARD_3_ARGUMENTS(a, b, c) \ 252 while (0) { \ 253 static_cast<void>(a); \ 254 static_cast<void>(b); \ 255 static_cast<void>(c); \ 256 } 257#define DISCARD_5_ARGUMENTS(a, b, c, d ,e) \ 258 while (0) { \ 259 static_cast<void>(a); \ 260 static_cast<void>(b); \ 261 static_cast<void>(c); \ 262 static_cast<void>(d); \ 263 static_cast<void>(e); \ 264 } 265#define DHISTOGRAM_TIMES(name, sample) \ 266 DISCARD_2_ARGUMENTS(name, sample) 267 268#define DHISTOGRAM_COUNTS(name, sample) \ 269 DISCARD_2_ARGUMENTS(name, sample) 270 271#define DHISTOGRAM_PERCENTAGE(name, under_one_hundred) \ 272 DISCARD_2_ARGUMENTS(name, under_one_hundred) 273 274#define DHISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \ 275 DISCARD_5_ARGUMENTS(name, sample, min, max, bucket_count) 276 277#define DHISTOGRAM_CLIPPED_TIMES(name, sample, min, max, bucket_count) \ 278 DISCARD_5_ARGUMENTS(name, sample, min, max, bucket_count) 279 280#define DHISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \ 281 DISCARD_5_ARGUMENTS(name, sample, min, max, bucket_count) 282 283#define DHISTOGRAM_ENUMERATION(name, sample, boundary_value) \ 284 DISCARD_3_ARGUMENTS(name, sample, boundary_value) 285 286#define DHISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \ 287 DISCARD_3_ARGUMENTS(name, sample, custom_ranges) 288 289#endif // NDEBUG 290 291//------------------------------------------------------------------------------ 292// The following macros provide typical usage scenarios for callers that wish 293// to record histogram data, and have the data submitted/uploaded via UMA. 294// Not all systems support such UMA, but if they do, the following macros 295// should work with the service. 296 297#define UMA_HISTOGRAM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ 298 name, sample, base::TimeDelta::FromMilliseconds(1), \ 299 base::TimeDelta::FromSeconds(10), 50) 300 301#define UMA_HISTOGRAM_MEDIUM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ 302 name, sample, base::TimeDelta::FromMilliseconds(10), \ 303 base::TimeDelta::FromMinutes(3), 50) 304 305// Use this macro when times can routinely be much longer than 10 seconds. 306#define UMA_HISTOGRAM_LONG_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ 307 name, sample, base::TimeDelta::FromMilliseconds(1), \ 308 base::TimeDelta::FromHours(1), 50) 309 310#define UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \ 311 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddTime(sample), \ 312 base::Histogram::FactoryTimeGet(name, min, max, bucket_count, \ 313 base::HistogramBase::kUmaTargetedHistogramFlag)) 314 315#define UMA_HISTOGRAM_COUNTS(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 316 name, sample, 1, 1000000, 50) 317 318#define UMA_HISTOGRAM_COUNTS_100(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 319 name, sample, 1, 100, 50) 320 321#define UMA_HISTOGRAM_COUNTS_10000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 322 name, sample, 1, 10000, 50) 323 324#define UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \ 325 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ 326 base::Histogram::FactoryGet(name, min, max, bucket_count, \ 327 base::HistogramBase::kUmaTargetedHistogramFlag)) 328 329#define UMA_HISTOGRAM_MEMORY_KB(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 330 name, sample, 1000, 500000, 50) 331 332#define UMA_HISTOGRAM_MEMORY_MB(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 333 name, sample, 1, 1000, 50) 334 335#define UMA_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \ 336 UMA_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101) 337 338#define UMA_HISTOGRAM_BOOLEAN(name, sample) \ 339 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \ 340 base::BooleanHistogram::FactoryGet(name, \ 341 base::HistogramBase::kUmaTargetedHistogramFlag)) 342 343// The samples should always be strictly less than |boundary_value|. For more 344// details, see the comment for the |HISTOGRAM_ENUMERATION| macro, above. 345#define UMA_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \ 346 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ 347 base::LinearHistogram::FactoryGet(name, 1, boundary_value, \ 348 boundary_value + 1, base::HistogramBase::kUmaTargetedHistogramFlag)) 349 350#define UMA_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \ 351 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ 352 base::CustomHistogram::FactoryGet(name, custom_ranges, \ 353 base::HistogramBase::kUmaTargetedHistogramFlag)) 354 355//------------------------------------------------------------------------------ 356 357class BucketRanges; 358class SampleVector; 359 360class BooleanHistogram; 361class CustomHistogram; 362class Histogram; 363class LinearHistogram; 364 365class BASE_EXPORT Histogram : public HistogramBase { 366 public: 367 // Initialize maximum number of buckets in histograms as 16,384. 368 static const size_t kBucketCount_MAX; 369 370 typedef std::vector<Count> Counts; 371 372 //---------------------------------------------------------------------------- 373 // For a valid histogram, input should follow these restrictions: 374 // minimum > 0 (if a minimum below 1 is specified, it will implicitly be 375 // normalized up to 1) 376 // maximum > minimum 377 // buckets > 2 [minimum buckets needed: underflow, overflow and the range] 378 // Additionally, 379 // buckets <= (maximum - minimum + 2) - this is to ensure that we don't have 380 // more buckets than the range of numbers; having more buckets than 1 per 381 // value in the range would be nonsensical. 382 static HistogramBase* FactoryGet(const std::string& name, 383 Sample minimum, 384 Sample maximum, 385 size_t bucket_count, 386 int32 flags); 387 static HistogramBase* FactoryTimeGet(const std::string& name, 388 base::TimeDelta minimum, 389 base::TimeDelta maximum, 390 size_t bucket_count, 391 int32 flags); 392 393 // Time call for use with DHISTOGRAM*. 394 // Returns TimeTicks::Now() in debug and TimeTicks() in release build. 395 static TimeTicks DebugNow(); 396 397 static void InitializeBucketRanges(Sample minimum, 398 Sample maximum, 399 size_t bucket_count, 400 BucketRanges* ranges); 401 402 // This constant if for FindCorruption. Since snapshots of histograms are 403 // taken asynchronously relative to sampling, and our counting code currently 404 // does not prevent race conditions, it is pretty likely that we'll catch a 405 // redundant count that doesn't match the sample count. We allow for a 406 // certain amount of slop before flagging this as an inconsistency. Even with 407 // an inconsistency, we'll snapshot it again (for UMA in about a half hour), 408 // so we'll eventually get the data, if it was not the result of a corruption. 409 static const int kCommonRaceBasedCountMismatch; 410 411 // Check to see if bucket ranges, counts and tallies in the snapshot are 412 // consistent with the bucket ranges and checksums in our histogram. This can 413 // produce a false-alarm if a race occurred in the reading of the data during 414 // a SnapShot process, but should otherwise be false at all times (unless we 415 // have memory over-writes, or DRAM failures). 416 virtual int FindCorruption(const HistogramSamples& samples) const OVERRIDE; 417 418 //---------------------------------------------------------------------------- 419 // Accessors for factory constuction, serialization and testing. 420 //---------------------------------------------------------------------------- 421 Sample declared_min() const { return declared_min_; } 422 Sample declared_max() const { return declared_max_; } 423 virtual Sample ranges(size_t i) const; 424 virtual size_t bucket_count() const; 425 const BucketRanges* bucket_ranges() const { return bucket_ranges_; } 426 427 // This function validates histogram construction arguments. It returns false 428 // if some of the arguments are totally bad. 429 // Note. Currently it allow some bad input, e.g. 0 as minimum, but silently 430 // converts it to good input: 1. 431 // TODO(kaiwang): Be more restrict and return false for any bad input, and 432 // make this a readonly validating function. 433 static bool InspectConstructionArguments(const std::string& name, 434 Sample* minimum, 435 Sample* maximum, 436 size_t* bucket_count); 437 438 // HistogramBase implementation: 439 virtual HistogramType GetHistogramType() const OVERRIDE; 440 virtual bool HasConstructionArguments(Sample minimum, 441 Sample maximum, 442 size_t bucket_count) const OVERRIDE; 443 virtual void Add(Sample value) OVERRIDE; 444 virtual scoped_ptr<HistogramSamples> SnapshotSamples() const OVERRIDE; 445 virtual void AddSamples(const HistogramSamples& samples) OVERRIDE; 446 virtual bool AddSamplesFromPickle(PickleIterator* iter) OVERRIDE; 447 virtual void WriteHTMLGraph(std::string* output) const OVERRIDE; 448 virtual void WriteAscii(std::string* output) const OVERRIDE; 449 450 protected: 451 // |bucket_count| and |ranges| should contain the underflow and overflow 452 // buckets. See top comments for example. 453 Histogram(const std::string& name, 454 Sample minimum, 455 Sample maximum, 456 size_t bucket_count, 457 const BucketRanges* ranges); 458 459 virtual ~Histogram(); 460 461 // HistogramBase implementation: 462 virtual bool SerializeInfoImpl(Pickle* pickle) const OVERRIDE; 463 464 // Method to override to skip the display of the i'th bucket if it's empty. 465 virtual bool PrintEmptyBucket(size_t index) const; 466 467 // Get normalized size, relative to the ranges(i). 468 virtual double GetBucketSize(Count current, size_t i) const; 469 470 // Return a string description of what goes in a given bucket. 471 // Most commonly this is the numeric value, but in derived classes it may 472 // be a name (or string description) given to the bucket. 473 virtual const std::string GetAsciiBucketRange(size_t it) const; 474 475 private: 476 // Allow tests to corrupt our innards for testing purposes. 477 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BoundsTest); 478 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BucketPlacementTest); 479 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptBucketBounds); 480 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts); 481 FRIEND_TEST_ALL_PREFIXES(HistogramTest, NameMatchTest); 482 483 friend class StatisticsRecorder; // To allow it to delete duplicates. 484 friend class StatisticsRecorderTest; 485 486 friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo( 487 PickleIterator* iter); 488 static HistogramBase* DeserializeInfoImpl(PickleIterator* iter); 489 490 // Implementation of SnapshotSamples function. 491 scoped_ptr<SampleVector> SnapshotSampleVector() const; 492 493 //---------------------------------------------------------------------------- 494 // Helpers for emitting Ascii graphic. Each method appends data to output. 495 496 void WriteAsciiImpl(bool graph_it, 497 const std::string& newline, 498 std::string* output) const; 499 500 // Find out how large (graphically) the largest bucket will appear to be. 501 double GetPeakBucketSize(const SampleVector& samples) const; 502 503 // Write a common header message describing this histogram. 504 void WriteAsciiHeader(const SampleVector& samples, 505 Count sample_count, 506 std::string* output) const; 507 508 // Write information about previous, current, and next buckets. 509 // Information such as cumulative percentage, etc. 510 void WriteAsciiBucketContext(const int64 past, const Count current, 511 const int64 remaining, const size_t i, 512 std::string* output) const; 513 514 // Write textual description of the bucket contents (relative to histogram). 515 // Output is the count in the buckets, as well as the percentage. 516 void WriteAsciiBucketValue(Count current, double scaled_sum, 517 std::string* output) const; 518 519 // Produce actual graph (set of blank vs non blank char's) for a bucket. 520 void WriteAsciiBucketGraph(double current_size, double max_size, 521 std::string* output) const; 522 523 // WriteJSON calls these. 524 virtual void GetParameters(DictionaryValue* params) const OVERRIDE; 525 526 virtual void GetCountAndBucketData(Count* count, 527 ListValue* buckets) const OVERRIDE; 528 529 // Does not own this object. Should get from StatisticsRecorder. 530 const BucketRanges* bucket_ranges_; 531 532 Sample declared_min_; // Less than this goes into counts_[0] 533 Sample declared_max_; // Over this goes into counts_[bucket_count_ - 1]. 534 size_t bucket_count_; // Dimension of counts_[]. 535 536 // Finally, provide the state that changes with the addition of each new 537 // sample. 538 scoped_ptr<SampleVector> samples_; 539 540 DISALLOW_COPY_AND_ASSIGN(Histogram); 541}; 542 543//------------------------------------------------------------------------------ 544 545// LinearHistogram is a more traditional histogram, with evenly spaced 546// buckets. 547class BASE_EXPORT LinearHistogram : public Histogram { 548 public: 549 virtual ~LinearHistogram(); 550 551 /* minimum should start from 1. 0 is as minimum is invalid. 0 is an implicit 552 default underflow bucket. */ 553 static HistogramBase* FactoryGet(const std::string& name, 554 Sample minimum, 555 Sample maximum, 556 size_t bucket_count, 557 int32 flags); 558 static HistogramBase* FactoryTimeGet(const std::string& name, 559 TimeDelta minimum, 560 TimeDelta maximum, 561 size_t bucket_count, 562 int32 flags); 563 564 struct DescriptionPair { 565 Sample sample; 566 const char* description; // Null means end of a list of pairs. 567 }; 568 569 // Create a LinearHistogram and store a list of number/text values for use in 570 // writing the histogram graph. 571 // |descriptions| can be NULL, which means no special descriptions to set. If 572 // it's not NULL, the last element in the array must has a NULL in its 573 // "description" field. 574 static HistogramBase* FactoryGetWithRangeDescription( 575 const std::string& name, 576 Sample minimum, 577 Sample maximum, 578 size_t bucket_count, 579 int32 flags, 580 const DescriptionPair descriptions[]); 581 582 static void InitializeBucketRanges(Sample minimum, 583 Sample maximum, 584 size_t bucket_count, 585 BucketRanges* ranges); 586 587 // Overridden from Histogram: 588 virtual HistogramType GetHistogramType() const OVERRIDE; 589 590 protected: 591 LinearHistogram(const std::string& name, 592 Sample minimum, 593 Sample maximum, 594 size_t bucket_count, 595 const BucketRanges* ranges); 596 597 virtual double GetBucketSize(Count current, size_t i) const OVERRIDE; 598 599 // If we have a description for a bucket, then return that. Otherwise 600 // let parent class provide a (numeric) description. 601 virtual const std::string GetAsciiBucketRange(size_t i) const OVERRIDE; 602 603 // Skip printing of name for numeric range if we have a name (and if this is 604 // an empty bucket). 605 virtual bool PrintEmptyBucket(size_t index) const OVERRIDE; 606 607 private: 608 friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo( 609 PickleIterator* iter); 610 static HistogramBase* DeserializeInfoImpl(PickleIterator* iter); 611 612 // For some ranges, we store a printable description of a bucket range. 613 // If there is no desciption, then GetAsciiBucketRange() uses parent class 614 // to provide a description. 615 typedef std::map<Sample, std::string> BucketDescriptionMap; 616 BucketDescriptionMap bucket_description_; 617 618 DISALLOW_COPY_AND_ASSIGN(LinearHistogram); 619}; 620 621//------------------------------------------------------------------------------ 622 623// BooleanHistogram is a histogram for booleans. 624class BASE_EXPORT BooleanHistogram : public LinearHistogram { 625 public: 626 static HistogramBase* FactoryGet(const std::string& name, int32 flags); 627 628 virtual HistogramType GetHistogramType() const OVERRIDE; 629 630 private: 631 BooleanHistogram(const std::string& name, const BucketRanges* ranges); 632 633 friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo( 634 PickleIterator* iter); 635 static HistogramBase* DeserializeInfoImpl(PickleIterator* iter); 636 637 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram); 638}; 639 640//------------------------------------------------------------------------------ 641 642// CustomHistogram is a histogram for a set of custom integers. 643class BASE_EXPORT CustomHistogram : public Histogram { 644 public: 645 // |custom_ranges| contains a vector of limits on ranges. Each limit should be 646 // > 0 and < kSampleType_MAX. (Currently 0 is still accepted for backward 647 // compatibility). The limits can be unordered or contain duplication, but 648 // client should not depend on this. 649 static HistogramBase* FactoryGet(const std::string& name, 650 const std::vector<Sample>& custom_ranges, 651 int32 flags); 652 653 // Overridden from Histogram: 654 virtual HistogramType GetHistogramType() const OVERRIDE; 655 656 // Helper method for transforming an array of valid enumeration values 657 // to the std::vector<int> expected by HISTOGRAM_CUSTOM_ENUMERATION. 658 // This function ensures that a guard bucket exists right after any 659 // valid sample value (unless the next higher sample is also a valid value), 660 // so that invalid samples never fall into the same bucket as valid samples. 661 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges. 662 static std::vector<Sample> ArrayToCustomRanges(const Sample* values, 663 size_t num_values); 664 protected: 665 CustomHistogram(const std::string& name, 666 const BucketRanges* ranges); 667 668 // HistogramBase implementation: 669 virtual bool SerializeInfoImpl(Pickle* pickle) const OVERRIDE; 670 671 virtual double GetBucketSize(Count current, size_t i) const OVERRIDE; 672 673 private: 674 friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo( 675 PickleIterator* iter); 676 static HistogramBase* DeserializeInfoImpl(PickleIterator* iter); 677 678 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); 679 static BucketRanges* CreateBucketRangesFromCustomRanges( 680 const std::vector<Sample>& custom_ranges); 681 682 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); 683}; 684 685} // namespace base 686 687#endif // BASE_METRICS_HISTOGRAM_H_ 688