OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Histogram is an object that aggregates statistics, and can summarize them in | 5 // Histogram is an object that aggregates statistics, and can summarize them in |
6 // various forms, including ASCII graphical, HTML, and numerically (as a | 6 // various forms, including ASCII graphical, HTML, and numerically (as a |
7 // vector of numbers corresponding to each of the aggregating buckets). | 7 // vector of numbers corresponding to each of the aggregating buckets). |
8 | 8 |
9 // It supports calls to accumulate either time intervals (which are processed | 9 // It supports calls to accumulate either time intervals (which are processed |
10 // as integral number of milliseconds), or arbitrary integral units. | 10 // as integral number of milliseconds), or arbitrary integral units. |
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
404 virtual Inconsistencies FindCorruption(const SampleSet& snapshot) const; | 404 virtual Inconsistencies FindCorruption(const SampleSet& snapshot) const; |
405 | 405 |
406 //---------------------------------------------------------------------------- | 406 //---------------------------------------------------------------------------- |
407 // Accessors for factory constuction, serialization and testing. | 407 // Accessors for factory constuction, serialization and testing. |
408 //---------------------------------------------------------------------------- | 408 //---------------------------------------------------------------------------- |
409 virtual ClassType histogram_type() const; | 409 virtual ClassType histogram_type() const; |
410 const std::string& histogram_name() const { return histogram_name_; } | 410 const std::string& histogram_name() const { return histogram_name_; } |
411 Sample declared_min() const { return declared_min_; } | 411 Sample declared_min() const { return declared_min_; } |
412 Sample declared_max() const { return declared_max_; } | 412 Sample declared_max() const { return declared_max_; } |
413 virtual Sample ranges(size_t i) const; | 413 virtual Sample ranges(size_t i) const; |
414 Sample range_checksum() const { return range_checksum_; } | 414 uint32 range_checksum() const { return range_checksum_; } |
415 virtual size_t bucket_count() const; | 415 virtual size_t bucket_count() const; |
416 // Snapshot the current complete set of sample data. | 416 // Snapshot the current complete set of sample data. |
417 // Override with atomic/locked snapshot if needed. | 417 // Override with atomic/locked snapshot if needed. |
418 virtual void SnapshotSample(SampleSet* sample) const; | 418 virtual void SnapshotSample(SampleSet* sample) const; |
419 | 419 |
420 virtual bool HasConstructorArguments(Sample minimum, Sample maximum, | 420 virtual bool HasConstructorArguments(Sample minimum, Sample maximum, |
421 size_t bucket_count); | 421 size_t bucket_count); |
422 | 422 |
423 virtual bool HasConstructorTimeDeltaArguments(TimeDelta minimum, | 423 virtual bool HasConstructorTimeDeltaArguments(TimeDelta minimum, |
424 TimeDelta maximum, | 424 TimeDelta maximum, |
425 size_t bucket_count); | 425 size_t bucket_count); |
426 // Return true iff the range_checksum_ matches current ranges_ vector. | |
427 bool HasValidRangeChecksum() const; | |
426 | 428 |
427 protected: | 429 protected: |
428 friend class base::RefCountedThreadSafe<Histogram>; | 430 friend class base::RefCountedThreadSafe<Histogram>; |
429 Histogram(const std::string& name, Sample minimum, | 431 Histogram(const std::string& name, Sample minimum, |
430 Sample maximum, size_t bucket_count); | 432 Sample maximum, size_t bucket_count); |
431 Histogram(const std::string& name, TimeDelta minimum, | 433 Histogram(const std::string& name, TimeDelta minimum, |
432 TimeDelta maximum, size_t bucket_count); | 434 TimeDelta maximum, size_t bucket_count); |
433 | 435 |
434 virtual ~Histogram(); | 436 virtual ~Histogram(); |
435 | 437 |
(...skipping 26 matching lines...) Expand all Loading... | |
462 | 464 |
463 //---------------------------------------------------------------------------- | 465 //---------------------------------------------------------------------------- |
464 // Accessors for derived classes. | 466 // Accessors for derived classes. |
465 //---------------------------------------------------------------------------- | 467 //---------------------------------------------------------------------------- |
466 void SetBucketRange(size_t i, Sample value); | 468 void SetBucketRange(size_t i, Sample value); |
467 | 469 |
468 // Validate that ranges_ was created sensibly (top and bottom range | 470 // Validate that ranges_ was created sensibly (top and bottom range |
469 // values relate properly to the declared_min_ and declared_max_).. | 471 // values relate properly to the declared_min_ and declared_max_).. |
470 bool ValidateBucketRanges() const; | 472 bool ValidateBucketRanges() const; |
471 | 473 |
474 virtual uint32 CalculateRangeChecksum() const; | |
475 | |
472 private: | 476 private: |
473 // Allow tests to corrupt our innards for testing purposes. | 477 // Allow tests to corrupt our innards for testing purposes. |
474 FRIEND_TEST(HistogramTest, CorruptBucketBounds); | 478 FRIEND_TEST(HistogramTest, CorruptBucketBounds); |
475 FRIEND_TEST(HistogramTest, CorruptSampleCounts); | 479 FRIEND_TEST(HistogramTest, CorruptSampleCounts); |
480 FRIEND_TEST(HistogramTest, Crc32SampleHash); | |
481 FRIEND_TEST(HistogramTest, Crc32TableTest); | |
476 | 482 |
477 // Post constructor initialization. | 483 // Post constructor initialization. |
478 void Initialize(); | 484 void Initialize(); |
479 | 485 |
480 // Return true iff the range_checksum_ matches current ranges_ vector. | 486 // Checksum function for accumulating range values into a checksum. |
481 bool HasValidRangeChecksum() const; | 487 static uint32 Crc32(uint32 sum, Sample range); |
482 | |
483 Sample CalculateRangeChecksum() const; | |
484 | 488 |
485 //---------------------------------------------------------------------------- | 489 //---------------------------------------------------------------------------- |
486 // Helpers for emitting Ascii graphic. Each method appends data to output. | 490 // Helpers for emitting Ascii graphic. Each method appends data to output. |
487 | 491 |
488 // Find out how large the (graphically) the largest bucket will appear to be. | 492 // Find out how large the (graphically) the largest bucket will appear to be. |
489 double GetPeakBucketSize(const SampleSet& snapshot) const; | 493 double GetPeakBucketSize(const SampleSet& snapshot) const; |
490 | 494 |
491 // Write a common header message describing this histogram. | 495 // Write a common header message describing this histogram. |
492 void WriteAsciiHeader(const SampleSet& snapshot, | 496 void WriteAsciiHeader(const SampleSet& snapshot, |
493 Count sample_count, std::string* output) const; | 497 Count sample_count, std::string* output) const; |
494 | 498 |
495 // Write information about previous, current, and next buckets. | 499 // Write information about previous, current, and next buckets. |
496 // Information such as cumulative percentage, etc. | 500 // Information such as cumulative percentage, etc. |
497 void WriteAsciiBucketContext(const int64 past, const Count current, | 501 void WriteAsciiBucketContext(const int64 past, const Count current, |
498 const int64 remaining, const size_t i, | 502 const int64 remaining, const size_t i, |
499 std::string* output) const; | 503 std::string* output) const; |
500 | 504 |
501 // Write textual description of the bucket contents (relative to histogram). | 505 // Write textual description of the bucket contents (relative to histogram). |
502 // Output is the count in the buckets, as well as the percentage. | 506 // Output is the count in the buckets, as well as the percentage. |
503 void WriteAsciiBucketValue(Count current, double scaled_sum, | 507 void WriteAsciiBucketValue(Count current, double scaled_sum, |
504 std::string* output) const; | 508 std::string* output) const; |
505 | 509 |
506 // Produce actual graph (set of blank vs non blank char's) for a bucket. | 510 // Produce actual graph (set of blank vs non blank char's) for a bucket. |
507 void WriteAsciiBucketGraph(double current_size, double max_size, | 511 void WriteAsciiBucketGraph(double current_size, double max_size, |
508 std::string* output) const; | 512 std::string* output) const; |
509 | 513 |
510 //---------------------------------------------------------------------------- | 514 //---------------------------------------------------------------------------- |
515 // Table for generating Crc32 values. | |
516 static const uint32 kCrcTable[256]; | |
517 //---------------------------------------------------------------------------- | |
511 // Invariant values set at/near construction time | 518 // Invariant values set at/near construction time |
512 | 519 |
513 // ASCII version of original name given to the constructor. All identically | 520 // ASCII version of original name given to the constructor. All identically |
514 // named instances will be coalesced cross-project. | 521 // named instances will be coalesced cross-project. |
515 const std::string histogram_name_; | 522 const std::string histogram_name_; |
516 Sample declared_min_; // Less than this goes into counts_[0] | 523 Sample declared_min_; // Less than this goes into counts_[0] |
517 Sample declared_max_; // Over this goes into counts_[bucket_count_ - 1]. | 524 Sample declared_max_; // Over this goes into counts_[bucket_count_ - 1]. |
518 size_t bucket_count_; // Dimension of counts_[]. | 525 size_t bucket_count_; // Dimension of counts_[]. |
519 | 526 |
520 // Flag the histogram for recording by UMA via metric_services.h. | 527 // Flag the histogram for recording by UMA via metric_services.h. |
521 Flags flags_; | 528 Flags flags_; |
522 | 529 |
523 // For each index, show the least value that can be stored in the | 530 // For each index, show the least value that can be stored in the |
524 // corresponding bucket. We also append one extra element in this array, | 531 // corresponding bucket. We also append one extra element in this array, |
525 // containing kSampleType_MAX, to make calculations easy. | 532 // containing kSampleType_MAX, to make calculations easy. |
526 // The dimension of ranges_ is bucket_count + 1. | 533 // The dimension of ranges_ is bucket_count + 1. |
527 Ranges ranges_; | 534 Ranges ranges_; |
528 | 535 |
529 // For redundancy, we store the sum of all the sample ranges when ranges are | 536 // For redundancy, we store a checksum of all the sample ranges when ranges |
530 // generated. If ever there is ever a difference, then the histogram must | 537 // are generated. If ever there is ever a difference, then the histogram must |
531 // have been corrupted. | 538 // have been corrupted. |
532 Sample range_checksum_; | 539 uint32 range_checksum_; |
533 | 540 |
534 // Finally, provide the state that changes with the addition of each new | 541 // Finally, provide the state that changes with the addition of each new |
535 // sample. | 542 // sample. |
536 SampleSet sample_; | 543 SampleSet sample_; |
537 | 544 |
538 DISALLOW_COPY_AND_ASSIGN(Histogram); | 545 DISALLOW_COPY_AND_ASSIGN(Histogram); |
539 }; | 546 }; |
540 | 547 |
541 //------------------------------------------------------------------------------ | 548 //------------------------------------------------------------------------------ |
542 | 549 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
618 const std::vector<Sample>& custom_ranges, Flags flags); | 625 const std::vector<Sample>& custom_ranges, Flags flags); |
619 | 626 |
620 // Overridden from Histogram: | 627 // Overridden from Histogram: |
621 virtual ClassType histogram_type() const; | 628 virtual ClassType histogram_type() const; |
622 | 629 |
623 protected: | 630 protected: |
624 CustomHistogram(const std::string& name, | 631 CustomHistogram(const std::string& name, |
625 const std::vector<Sample>& custom_ranges); | 632 const std::vector<Sample>& custom_ranges); |
626 | 633 |
627 // Initialize ranges_ mapping. | 634 // Initialize ranges_ mapping. |
628 virtual void InitializeBucketRange(); | 635 virtual void InitializeBucketRange(const std::vector<Sample>& custom_ranges); |
629 virtual double GetBucketSize(Count current, size_t i) const; | 636 virtual double GetBucketSize(Count current, size_t i) const; |
630 | 637 |
631 private: | |
632 // Temporary pointer used during construction/initialization, and then NULLed. | |
633 const std::vector<Sample>* ranges_vector_; | |
634 | |
635 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); | 638 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); |
636 }; | 639 }; |
637 | 640 |
638 //------------------------------------------------------------------------------ | 641 //------------------------------------------------------------------------------ |
639 // StatisticsRecorder handles all histograms in the system. It provides a | 642 // StatisticsRecorder handles all histograms in the system. It provides a |
640 // general place for histograms to register, and supports a global API for | 643 // general place for histograms to register, and supports a global API for |
641 // accessing (i.e., dumping, or graphing) the data in all the histograms. | 644 // accessing (i.e., dumping, or graphing) the data in all the histograms. |
642 | 645 |
643 class StatisticsRecorder { | 646 class StatisticsRecorder { |
644 public: | 647 public: |
645 typedef std::vector<scoped_refptr<Histogram> > Histograms; | 648 typedef std::vector<scoped_refptr<Histogram> > Histograms; |
646 | 649 |
647 StatisticsRecorder(); | 650 StatisticsRecorder(); |
648 | 651 |
649 ~StatisticsRecorder(); | 652 ~StatisticsRecorder(); |
650 | 653 |
651 // Find out if histograms can now be registered into our list. | 654 // Find out if histograms can now be registered into our list. |
652 static bool IsActive(); | 655 static bool IsActive(); |
653 | 656 |
654 // Register, or add a new histogram to the collection of statistics. | 657 // Register, or add a new histogram to the collection of statistics. If an |
655 static void Register(Histogram* histogram); | 658 // identically named histogram is already registered, then the argument |
659 // |histogram| will be replaced by the previously registered value. | |
660 static void Register(scoped_refptr<Histogram>* histogram); | |
rvargas (doing something else)
2011/02/28 19:50:53
In general, I would consider this to be unexpected
jar (doing other things)
2011/02/28 22:11:44
I landed the code this morning... so....
I've cha
| |
656 | 661 |
657 // Methods for printing histograms. Only histograms which have query as | 662 // Methods for printing histograms. Only histograms which have query as |
658 // a substring are written to output (an empty string will process all | 663 // a substring are written to output (an empty string will process all |
659 // registered histograms). | 664 // registered histograms). |
660 static void WriteHTMLGraph(const std::string& query, std::string* output); | 665 static void WriteHTMLGraph(const std::string& query, std::string* output); |
661 static void WriteGraph(const std::string& query, std::string* output); | 666 static void WriteGraph(const std::string& query, std::string* output); |
662 | 667 |
663 // Method for extracting histograms which were marked for use by UMA. | 668 // Method for extracting histograms which were marked for use by UMA. |
664 static void GetHistograms(Histograms* output); | 669 static void GetHistograms(Histograms* output); |
665 | 670 |
(...skipping 25 matching lines...) Expand all Loading... | |
691 | 696 |
692 // Dump all known histograms to log. | 697 // Dump all known histograms to log. |
693 static bool dump_on_exit_; | 698 static bool dump_on_exit_; |
694 | 699 |
695 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); | 700 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); |
696 }; | 701 }; |
697 | 702 |
698 } // namespace base | 703 } // namespace base |
699 | 704 |
700 #endif // BASE_METRICS_HISTOGRAM_H_ | 705 #endif // BASE_METRICS_HISTOGRAM_H_ |
OLD | NEW |