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 minimal 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/time.h" 81 82 class Pickle; 83 class PickleIterator; 84 85 namespace base { 86 87 class 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 // initialization 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 histogram. 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 if (DCHECK_IS_ON) \ 159 histogram_pointer->CheckName(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 // This is a helper macro used by other macros and shouldn't be used directly. 194 #define HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary, flag) \ 195 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ 196 base::LinearHistogram::FactoryGet(name, 1, boundary, boundary + 1, \ 197 flag)) 198 199 #define HISTOGRAM_PERCENTAGE(name, under_one_hundred) \ 200 HISTOGRAM_ENUMERATION(name, under_one_hundred, 101) 201 202 #define HISTOGRAM_BOOLEAN(name, sample) \ 203 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \ 204 base::BooleanHistogram::FactoryGet(name, base::Histogram::kNoFlags)) 205 206 // Support histograming of an enumerated value. The samples should always be 207 // strictly less than |boundary_value| -- this prevents you from running into 208 // problems down the line if you add additional buckets to the histogram. Note 209 // also that, despite explicitly setting the minimum bucket value to |1| below, 210 // it is fine for enumerated histograms to be 0-indexed -- this is because 211 // enumerated histograms should never have underflow. 212 #define HISTOGRAM_ENUMERATION(name, sample, boundary_value) \ 213 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ 214 base::LinearHistogram::FactoryGet(name, 1, boundary_value, \ 215 boundary_value + 1, base::HistogramBase::kNoFlags)) 216 217 // Support histograming of an enumerated value. Samples should be one of the 218 // std::vector<int> list provided via |custom_ranges|. See comments above 219 // CustomRanges::FactoryGet about the requirement of |custom_ranges|. 220 // You can use the helper function CustomHistogram::ArrayToCustomRanges to 221 // transform a C-style array of valid sample values to a std::vector<int>. 222 #define HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \ 223 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ 224 base::CustomHistogram::FactoryGet(name, custom_ranges, \ 225 base::HistogramBase::kNoFlags)) 226 227 #define HISTOGRAM_MEMORY_KB(name, sample) HISTOGRAM_CUSTOM_COUNTS( \ 228 name, sample, 1000, 500000, 50) 229 230 //------------------------------------------------------------------------------ 231 // Define Debug vs non-debug flavors of macros. 232 #ifndef NDEBUG 233 234 #define DHISTOGRAM_TIMES(name, sample) HISTOGRAM_TIMES(name, sample) 235 #define DHISTOGRAM_COUNTS(name, sample) HISTOGRAM_COUNTS(name, sample) 236 #define DHISTOGRAM_PERCENTAGE(name, under_one_hundred) HISTOGRAM_PERCENTAGE(\ 237 name, under_one_hundred) 238 #define DHISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \ 239 HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) 240 #define DHISTOGRAM_CLIPPED_TIMES(name, sample, min, max, bucket_count) \ 241 HISTOGRAM_CLIPPED_TIMES(name, sample, min, max, bucket_count) 242 #define DHISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \ 243 HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) 244 #define DHISTOGRAM_ENUMERATION(name, sample, boundary_value) \ 245 HISTOGRAM_ENUMERATION(name, sample, boundary_value) 246 #define DHISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \ 247 HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) 248 249 #else // NDEBUG 250 // Keep a mention of passed variables to avoid unused variable warnings in 251 // release build if these variables are only used in macros. 252 #define DISCARD_2_ARGUMENTS(a, b) \ 253 while (0) { \ 254 static_cast<void>(a); \ 255 static_cast<void>(b); \ 256 } 257 #define DISCARD_3_ARGUMENTS(a, b, c) \ 258 while (0) { \ 259 static_cast<void>(a); \ 260 static_cast<void>(b); \ 261 static_cast<void>(c); \ 262 } 263 #define DISCARD_5_ARGUMENTS(a, b, c, d ,e) \ 264 while (0) { \ 265 static_cast<void>(a); \ 266 static_cast<void>(b); \ 267 static_cast<void>(c); \ 268 static_cast<void>(d); \ 269 static_cast<void>(e); \ 270 } 271 #define DHISTOGRAM_TIMES(name, sample) \ 272 DISCARD_2_ARGUMENTS(name, sample) 273 274 #define DHISTOGRAM_COUNTS(name, sample) \ 275 DISCARD_2_ARGUMENTS(name, sample) 276 277 #define DHISTOGRAM_PERCENTAGE(name, under_one_hundred) \ 278 DISCARD_2_ARGUMENTS(name, under_one_hundred) 279 280 #define DHISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \ 281 DISCARD_5_ARGUMENTS(name, sample, min, max, bucket_count) 282 283 #define DHISTOGRAM_CLIPPED_TIMES(name, sample, min, max, bucket_count) \ 284 DISCARD_5_ARGUMENTS(name, sample, min, max, bucket_count) 285 286 #define DHISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \ 287 DISCARD_5_ARGUMENTS(name, sample, min, max, bucket_count) 288 289 #define DHISTOGRAM_ENUMERATION(name, sample, boundary_value) \ 290 DISCARD_3_ARGUMENTS(name, sample, boundary_value) 291 292 #define DHISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \ 293 DISCARD_3_ARGUMENTS(name, sample, custom_ranges) 294 295 #endif // NDEBUG 296 297 //------------------------------------------------------------------------------ 298 // The following macros provide typical usage scenarios for callers that wish 299 // to record histogram data, and have the data submitted/uploaded via UMA. 300 // Not all systems support such UMA, but if they do, the following macros 301 // should work with the service. 302 303 #define UMA_HISTOGRAM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ 304 name, sample, base::TimeDelta::FromMilliseconds(1), \ 305 base::TimeDelta::FromSeconds(10), 50) 306 307 #define UMA_HISTOGRAM_MEDIUM_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ 308 name, sample, base::TimeDelta::FromMilliseconds(10), \ 309 base::TimeDelta::FromMinutes(3), 50) 310 311 // Use this macro when times can routinely be much longer than 10 seconds. 312 #define UMA_HISTOGRAM_LONG_TIMES(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ 313 name, sample, base::TimeDelta::FromMilliseconds(1), \ 314 base::TimeDelta::FromHours(1), 50) 315 316 // Use this macro when times can routinely be much longer than 10 seconds and 317 // you want 100 buckets. 318 #define UMA_HISTOGRAM_LONG_TIMES_100(name, sample) UMA_HISTOGRAM_CUSTOM_TIMES( \ 319 name, sample, base::TimeDelta::FromMilliseconds(1), \ 320 base::TimeDelta::FromHours(1), 100) 321 322 #define UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \ 323 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddTime(sample), \ 324 base::Histogram::FactoryTimeGet(name, min, max, bucket_count, \ 325 base::HistogramBase::kUmaTargetedHistogramFlag)) 326 327 #define UMA_HISTOGRAM_COUNTS(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 328 name, sample, 1, 1000000, 50) 329 330 #define UMA_HISTOGRAM_COUNTS_100(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 331 name, sample, 1, 100, 50) 332 333 #define UMA_HISTOGRAM_COUNTS_10000(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 334 name, sample, 1, 10000, 50) 335 336 #define UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \ 337 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ 338 base::Histogram::FactoryGet(name, min, max, bucket_count, \ 339 base::HistogramBase::kUmaTargetedHistogramFlag)) 340 341 #define UMA_HISTOGRAM_MEMORY_KB(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 342 name, sample, 1000, 500000, 50) 343 344 #define UMA_HISTOGRAM_MEMORY_MB(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \ 345 name, sample, 1, 1000, 50) 346 347 #define UMA_HISTOGRAM_PERCENTAGE(name, under_one_hundred) \ 348 UMA_HISTOGRAM_ENUMERATION(name, under_one_hundred, 101) 349 350 #define UMA_HISTOGRAM_BOOLEAN(name, sample) \ 351 STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \ 352 base::BooleanHistogram::FactoryGet(name, \ 353 base::HistogramBase::kUmaTargetedHistogramFlag)) 354 355 // The samples should always be strictly less than |boundary_value|. For more 356 // details, see the comment for the |HISTOGRAM_ENUMERATION| macro, above. 357 #define UMA_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \ 358 HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary_value, \ 359 base::HistogramBase::kUmaTargetedHistogramFlag) 360 361 // Similar to UMA_HISTOGRAM_ENUMERATION, but used for recording stability 362 // histograms. Use this if recording a histogram that should be part of the 363 // initial stability log. 364 #define UMA_STABILITY_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \ 365 HISTOGRAM_ENUMERATION_WITH_FLAG(name, sample, boundary_value, \ 366 base::HistogramBase::kUmaStabilityHistogramFlag) 367 368 #define UMA_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \ 369 STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \ 370 base::CustomHistogram::FactoryGet(name, custom_ranges, \ 371 base::HistogramBase::kUmaTargetedHistogramFlag)) 372 373 //------------------------------------------------------------------------------ 374 375 class BucketRanges; 376 class SampleVector; 377 378 class BooleanHistogram; 379 class CustomHistogram; 380 class Histogram; 381 class LinearHistogram; 382 383 class BASE_EXPORT Histogram : public HistogramBase { 384 public: 385 // Initialize maximum number of buckets in histograms as 16,384. 386 static const size_t kBucketCount_MAX; 387 388 typedef std::vector<Count> Counts; 389 390 //---------------------------------------------------------------------------- 391 // For a valid histogram, input should follow these restrictions: 392 // minimum > 0 (if a minimum below 1 is specified, it will implicitly be 393 // normalized up to 1) 394 // maximum > minimum 395 // buckets > 2 [minimum buckets needed: underflow, overflow and the range] 396 // Additionally, 397 // buckets <= (maximum - minimum + 2) - this is to ensure that we don't have 398 // more buckets than the range of numbers; having more buckets than 1 per 399 // value in the range would be nonsensical. 400 static HistogramBase* FactoryGet(const std::string& name, 401 Sample minimum, 402 Sample maximum, 403 size_t bucket_count, 404 int32 flags); 405 static HistogramBase* FactoryTimeGet(const std::string& name, 406 base::TimeDelta minimum, 407 base::TimeDelta maximum, 408 size_t bucket_count, 409 int32 flags); 410 411 // Time call for use with DHISTOGRAM*. 412 // Returns TimeTicks::Now() in debug and TimeTicks() in release build. 413 static TimeTicks DebugNow(); 414 415 static void InitializeBucketRanges(Sample minimum, 416 Sample maximum, 417 BucketRanges* ranges); 418 419 // This constant if for FindCorruption. Since snapshots of histograms are 420 // taken asynchronously relative to sampling, and our counting code currently 421 // does not prevent race conditions, it is pretty likely that we'll catch a 422 // redundant count that doesn't match the sample count. We allow for a 423 // certain amount of slop before flagging this as an inconsistency. Even with 424 // an inconsistency, we'll snapshot it again (for UMA in about a half hour), 425 // so we'll eventually get the data, if it was not the result of a corruption. 426 static const int kCommonRaceBasedCountMismatch; 427 428 // Check to see if bucket ranges, counts and tallies in the snapshot are 429 // consistent with the bucket ranges and checksums in our histogram. This can 430 // produce a false-alarm if a race occurred in the reading of the data during 431 // a SnapShot process, but should otherwise be false at all times (unless we 432 // have memory over-writes, or DRAM failures). 433 virtual int FindCorruption(const HistogramSamples& samples) const OVERRIDE; 434 435 //---------------------------------------------------------------------------- 436 // Accessors for factory construction, serialization and testing. 437 //---------------------------------------------------------------------------- 438 Sample declared_min() const { return declared_min_; } 439 Sample declared_max() const { return declared_max_; } 440 virtual Sample ranges(size_t i) const; 441 virtual size_t bucket_count() const; 442 const BucketRanges* bucket_ranges() const { return bucket_ranges_; } 443 444 // This function validates histogram construction arguments. It returns false 445 // if some of the arguments are totally bad. 446 // Note. Currently it allow some bad input, e.g. 0 as minimum, but silently 447 // converts it to good input: 1. 448 // TODO(kaiwang): Be more restrict and return false for any bad input, and 449 // make this a readonly validating function. 450 static bool InspectConstructionArguments(const std::string& name, 451 Sample* minimum, 452 Sample* maximum, 453 size_t* bucket_count); 454 455 // HistogramBase implementation: 456 virtual HistogramType GetHistogramType() const OVERRIDE; 457 virtual bool HasConstructionArguments( 458 Sample expected_minimum, 459 Sample expected_maximum, 460 size_t expected_bucket_count) const OVERRIDE; 461 virtual void Add(Sample value) OVERRIDE; 462 virtual scoped_ptr<HistogramSamples> SnapshotSamples() const OVERRIDE; 463 virtual void AddSamples(const HistogramSamples& samples) OVERRIDE; 464 virtual bool AddSamplesFromPickle(PickleIterator* iter) OVERRIDE; 465 virtual void WriteHTMLGraph(std::string* output) const OVERRIDE; 466 virtual void WriteAscii(std::string* output) const OVERRIDE; 467 468 protected: 469 // |ranges| should contain the underflow and overflow buckets. See top 470 // comments for example. 471 Histogram(const std::string& name, 472 Sample minimum, 473 Sample maximum, 474 const BucketRanges* ranges); 475 476 virtual ~Histogram(); 477 478 // HistogramBase implementation: 479 virtual bool SerializeInfoImpl(Pickle* pickle) const OVERRIDE; 480 481 // Method to override to skip the display of the i'th bucket if it's empty. 482 virtual bool PrintEmptyBucket(size_t index) const; 483 484 // Get normalized size, relative to the ranges(i). 485 virtual double GetBucketSize(Count current, size_t i) const; 486 487 // Return a string description of what goes in a given bucket. 488 // Most commonly this is the numeric value, but in derived classes it may 489 // be a name (or string description) given to the bucket. 490 virtual const std::string GetAsciiBucketRange(size_t it) const; 491 492 private: 493 // Allow tests to corrupt our innards for testing purposes. 494 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BoundsTest); 495 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BucketPlacementTest); 496 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptBucketBounds); 497 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts); 498 FRIEND_TEST_ALL_PREFIXES(HistogramTest, NameMatchTest); 499 500 friend class StatisticsRecorder; // To allow it to delete duplicates. 501 friend class StatisticsRecorderTest; 502 503 friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo( 504 PickleIterator* iter); 505 static HistogramBase* DeserializeInfoImpl(PickleIterator* iter); 506 507 // Implementation of SnapshotSamples function. 508 scoped_ptr<SampleVector> SnapshotSampleVector() const; 509 510 //---------------------------------------------------------------------------- 511 // Helpers for emitting Ascii graphic. Each method appends data to output. 512 513 void WriteAsciiImpl(bool graph_it, 514 const std::string& newline, 515 std::string* output) const; 516 517 // Find out how large (graphically) the largest bucket will appear to be. 518 double GetPeakBucketSize(const SampleVector& samples) const; 519 520 // Write a common header message describing this histogram. 521 void WriteAsciiHeader(const SampleVector& samples, 522 Count sample_count, 523 std::string* output) const; 524 525 // Write information about previous, current, and next buckets. 526 // Information such as cumulative percentage, etc. 527 void WriteAsciiBucketContext(const int64 past, const Count current, 528 const int64 remaining, const size_t i, 529 std::string* output) const; 530 531 // WriteJSON calls these. 532 virtual void GetParameters(DictionaryValue* params) const OVERRIDE; 533 534 virtual void GetCountAndBucketData(Count* count, 535 int64* sum, 536 ListValue* buckets) const OVERRIDE; 537 538 // Does not own this object. Should get from StatisticsRecorder. 539 const BucketRanges* bucket_ranges_; 540 541 Sample declared_min_; // Less than this goes into the first bucket. 542 Sample declared_max_; // Over this goes into the last bucket. 543 544 // Finally, provide the state that changes with the addition of each new 545 // sample. 546 scoped_ptr<SampleVector> samples_; 547 548 DISALLOW_COPY_AND_ASSIGN(Histogram); 549 }; 550 551 //------------------------------------------------------------------------------ 552 553 // LinearHistogram is a more traditional histogram, with evenly spaced 554 // buckets. 555 class BASE_EXPORT LinearHistogram : public Histogram { 556 public: 557 virtual ~LinearHistogram(); 558 559 /* minimum should start from 1. 0 is as minimum is invalid. 0 is an implicit 560 default underflow bucket. */ 561 static HistogramBase* FactoryGet(const std::string& name, 562 Sample minimum, 563 Sample maximum, 564 size_t bucket_count, 565 int32 flags); 566 static HistogramBase* FactoryTimeGet(const std::string& name, 567 TimeDelta minimum, 568 TimeDelta maximum, 569 size_t bucket_count, 570 int32 flags); 571 572 struct DescriptionPair { 573 Sample sample; 574 const char* description; // Null means end of a list of pairs. 575 }; 576 577 // Create a LinearHistogram and store a list of number/text values for use in 578 // writing the histogram graph. 579 // |descriptions| can be NULL, which means no special descriptions to set. If 580 // it's not NULL, the last element in the array must has a NULL in its 581 // "description" field. 582 static HistogramBase* FactoryGetWithRangeDescription( 583 const std::string& name, 584 Sample minimum, 585 Sample maximum, 586 size_t bucket_count, 587 int32 flags, 588 const DescriptionPair descriptions[]); 589 590 static void InitializeBucketRanges(Sample minimum, 591 Sample maximum, 592 BucketRanges* ranges); 593 594 // Overridden from Histogram: 595 virtual HistogramType GetHistogramType() const OVERRIDE; 596 597 protected: 598 LinearHistogram(const std::string& name, 599 Sample minimum, 600 Sample maximum, 601 const BucketRanges* ranges); 602 603 virtual double GetBucketSize(Count current, size_t i) const OVERRIDE; 604 605 // If we have a description for a bucket, then return that. Otherwise 606 // let parent class provide a (numeric) description. 607 virtual const std::string GetAsciiBucketRange(size_t i) const OVERRIDE; 608 609 // Skip printing of name for numeric range if we have a name (and if this is 610 // an empty bucket). 611 virtual bool PrintEmptyBucket(size_t index) const OVERRIDE; 612 613 private: 614 friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo( 615 PickleIterator* iter); 616 static HistogramBase* DeserializeInfoImpl(PickleIterator* iter); 617 618 // For some ranges, we store a printable description of a bucket range. 619 // If there is no description, then GetAsciiBucketRange() uses parent class 620 // to provide a description. 621 typedef std::map<Sample, std::string> BucketDescriptionMap; 622 BucketDescriptionMap bucket_description_; 623 624 DISALLOW_COPY_AND_ASSIGN(LinearHistogram); 625 }; 626 627 //------------------------------------------------------------------------------ 628 629 // BooleanHistogram is a histogram for booleans. 630 class BASE_EXPORT BooleanHistogram : public LinearHistogram { 631 public: 632 static HistogramBase* FactoryGet(const std::string& name, int32 flags); 633 634 virtual HistogramType GetHistogramType() const OVERRIDE; 635 636 private: 637 BooleanHistogram(const std::string& name, const BucketRanges* ranges); 638 639 friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo( 640 PickleIterator* iter); 641 static HistogramBase* DeserializeInfoImpl(PickleIterator* iter); 642 643 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram); 644 }; 645 646 //------------------------------------------------------------------------------ 647 648 // CustomHistogram is a histogram for a set of custom integers. 649 class BASE_EXPORT CustomHistogram : public Histogram { 650 public: 651 // |custom_ranges| contains a vector of limits on ranges. Each limit should be 652 // > 0 and < kSampleType_MAX. (Currently 0 is still accepted for backward 653 // compatibility). The limits can be unordered or contain duplication, but 654 // client should not depend on this. 655 static HistogramBase* FactoryGet(const std::string& name, 656 const std::vector<Sample>& custom_ranges, 657 int32 flags); 658 659 // Overridden from Histogram: 660 virtual HistogramType GetHistogramType() const OVERRIDE; 661 662 // Helper method for transforming an array of valid enumeration values 663 // to the std::vector<int> expected by HISTOGRAM_CUSTOM_ENUMERATION. 664 // This function ensures that a guard bucket exists right after any 665 // valid sample value (unless the next higher sample is also a valid value), 666 // so that invalid samples never fall into the same bucket as valid samples. 667 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges. 668 static std::vector<Sample> ArrayToCustomRanges(const Sample* values, 669 size_t num_values); 670 protected: 671 CustomHistogram(const std::string& name, 672 const BucketRanges* ranges); 673 674 // HistogramBase implementation: 675 virtual bool SerializeInfoImpl(Pickle* pickle) const OVERRIDE; 676 677 virtual double GetBucketSize(Count current, size_t i) const OVERRIDE; 678 679 private: 680 friend BASE_EXPORT_PRIVATE HistogramBase* DeserializeHistogramInfo( 681 PickleIterator* iter); 682 static HistogramBase* DeserializeInfoImpl(PickleIterator* iter); 683 684 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); 685 static BucketRanges* CreateBucketRangesFromCustomRanges( 686 const std::vector<Sample>& custom_ranges); 687 688 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); 689 }; 690 691 } // namespace base 692 693 #endif // BASE_METRICS_HISTOGRAM_H_ 694