OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 22 matching lines...) Expand all Loading... |
33 // that detects a uninitialized (NULL) pointer. The potentially racy | 33 // that detects a uninitialized (NULL) pointer. The potentially racy |
34 // initialization is not a problem as it is always set to point to the same | 34 // initialization is not a problem as it is always set to point to the same |
35 // value (i.e., the FactoryGet always returns the same value). FactoryGet | 35 // value (i.e., the FactoryGet always returns the same value). FactoryGet |
36 // is also completely thread safe, which results in a completely thread safe, | 36 // is also completely thread safe, which results in a completely thread safe, |
37 // and relatively fast, set of counters. To avoid races at shutdown, the static | 37 // and relatively fast, set of counters. To avoid races at shutdown, the static |
38 // pointer is NOT deleted, and we leak the histograms at process termination. | 38 // pointer is NOT deleted, and we leak the histograms at process termination. |
39 | 39 |
40 #ifndef BASE_METRICS_HISTOGRAM_H_ | 40 #ifndef BASE_METRICS_HISTOGRAM_H_ |
41 #define BASE_METRICS_HISTOGRAM_H_ | 41 #define BASE_METRICS_HISTOGRAM_H_ |
42 | 42 |
43 #include <list> | |
44 #include <map> | 43 #include <map> |
45 #include <string> | 44 #include <string> |
46 #include <vector> | 45 #include <vector> |
47 | 46 |
48 #include "base/atomicops.h" | 47 #include "base/atomicops.h" |
49 #include "base/base_export.h" | 48 #include "base/base_export.h" |
50 #include "base/compiler_specific.h" | 49 #include "base/compiler_specific.h" |
51 #include "base/gtest_prod_util.h" | 50 #include "base/gtest_prod_util.h" |
52 #include "base/logging.h" | 51 #include "base/logging.h" |
53 #include "base/time.h" | 52 #include "base/time.h" |
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 | 766 |
768 virtual bool SerializeRanges(Pickle* pickle) const OVERRIDE; | 767 virtual bool SerializeRanges(Pickle* pickle) const OVERRIDE; |
769 | 768 |
770 // Initialize ranges_ mapping in cached_ranges_. | 769 // Initialize ranges_ mapping in cached_ranges_. |
771 void InitializedCustomBucketRange(const std::vector<Sample>& custom_ranges); | 770 void InitializedCustomBucketRange(const std::vector<Sample>& custom_ranges); |
772 virtual double GetBucketSize(Count current, size_t i) const OVERRIDE; | 771 virtual double GetBucketSize(Count current, size_t i) const OVERRIDE; |
773 | 772 |
774 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); | 773 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); |
775 }; | 774 }; |
776 | 775 |
777 //------------------------------------------------------------------------------ | |
778 // StatisticsRecorder handles all histograms in the system. It provides a | |
779 // general place for histograms to register, and supports a global API for | |
780 // accessing (i.e., dumping, or graphing) the data in all the histograms. | |
781 | |
782 class BASE_EXPORT StatisticsRecorder { | |
783 public: | |
784 typedef std::vector<Histogram*> Histograms; | |
785 | |
786 StatisticsRecorder(); | |
787 | |
788 ~StatisticsRecorder(); | |
789 | |
790 // Find out if histograms can now be registered into our list. | |
791 static bool IsActive(); | |
792 | |
793 // Register, or add a new histogram to the collection of statistics. If an | |
794 // identically named histogram is already registered, then the argument | |
795 // |histogram| will deleted. The returned value is always the registered | |
796 // histogram (either the argument, or the pre-existing registered histogram). | |
797 static Histogram* RegisterOrDeleteDuplicate(Histogram* histogram); | |
798 | |
799 // Register, or add a new cached_ranges_ of |histogram|. If an identical | |
800 // cached_ranges_ is already registered, then the cached_ranges_ of | |
801 // |histogram| is deleted and the |histogram|'s cached_ranges_ is reset to the | |
802 // registered cached_ranges_. The cached_ranges_ of |histogram| is always the | |
803 // registered CachedRanges (either the argument's cached_ranges_, or the | |
804 // pre-existing registered cached_ranges_). | |
805 static void RegisterOrDeleteDuplicateRanges(Histogram* histogram); | |
806 | |
807 // Method for collecting stats about histograms created in browser and | |
808 // renderer processes. |suffix| is appended to histogram names. |suffix| could | |
809 // be either browser or renderer. | |
810 static void CollectHistogramStats(const std::string& suffix); | |
811 | |
812 // Methods for printing histograms. Only histograms which have query as | |
813 // a substring are written to output (an empty string will process all | |
814 // registered histograms). | |
815 static void WriteHTMLGraph(const std::string& query, std::string* output); | |
816 static void WriteGraph(const std::string& query, std::string* output); | |
817 | |
818 // Method for extracting histograms which were marked for use by UMA. | |
819 static void GetHistograms(Histograms* output); | |
820 | |
821 // Find a histogram by name. It matches the exact name. This method is thread | |
822 // safe. If a matching histogram is not found, then the |histogram| is | |
823 // not changed. | |
824 static bool FindHistogram(const std::string& query, Histogram** histogram); | |
825 | |
826 static bool dump_on_exit() { return dump_on_exit_; } | |
827 | |
828 static void set_dump_on_exit(bool enable) { dump_on_exit_ = enable; } | |
829 | |
830 // GetSnapshot copies some of the pointers to registered histograms into the | |
831 // caller supplied vector (Histograms). Only histograms with names matching | |
832 // query are returned. The query must be a substring of histogram name for its | |
833 // pointer to be copied. | |
834 static void GetSnapshot(const std::string& query, Histograms* snapshot); | |
835 | |
836 | |
837 private: | |
838 // We keep all registered histograms in a map, from name to histogram. | |
839 typedef std::map<std::string, Histogram*> HistogramMap; | |
840 | |
841 // We keep all |cached_ranges_| in a map, from checksum to a list of | |
842 // |cached_ranges_|. Checksum is calculated from the |ranges_| in | |
843 // |cached_ranges_|. | |
844 typedef std::map<uint32, std::list<CachedRanges*>*> RangesMap; | |
845 | |
846 static HistogramMap* histograms_; | |
847 | |
848 static RangesMap* ranges_; | |
849 | |
850 // lock protects access to the above map. | |
851 static base::Lock* lock_; | |
852 | |
853 // Dump all known histograms to log. | |
854 static bool dump_on_exit_; | |
855 | |
856 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); | |
857 }; | |
858 | |
859 //------------------------------------------------------------------------------ | |
860 | |
861 // CachedRanges stores the Ranges vector. Histograms that have same Ranges | 776 // CachedRanges stores the Ranges vector. Histograms that have same Ranges |
862 // vector will use the same CachedRanges object. | 777 // vector will use the same CachedRanges object. |
863 class BASE_EXPORT CachedRanges { | 778 class BASE_EXPORT CachedRanges { |
864 public: | 779 public: |
865 typedef std::vector<Histogram::Sample> Ranges; | 780 typedef std::vector<Histogram::Sample> Ranges; |
866 | 781 |
867 CachedRanges(size_t bucket_count, int initial_value); | 782 CachedRanges(size_t bucket_count, int initial_value); |
868 ~CachedRanges(); | 783 ~CachedRanges(); |
869 | 784 |
870 //---------------------------------------------------------------------------- | 785 //---------------------------------------------------------------------------- |
(...skipping 21 matching lines...) Expand all Loading... |
892 // of our data, and to quickly see if some other CachedRanges instance is | 807 // of our data, and to quickly see if some other CachedRanges instance is |
893 // possibly Equal() to this instance. | 808 // possibly Equal() to this instance. |
894 uint32 range_checksum_; | 809 uint32 range_checksum_; |
895 | 810 |
896 DISALLOW_COPY_AND_ASSIGN(CachedRanges); | 811 DISALLOW_COPY_AND_ASSIGN(CachedRanges); |
897 }; | 812 }; |
898 | 813 |
899 } // namespace base | 814 } // namespace base |
900 | 815 |
901 #endif // BASE_METRICS_HISTOGRAM_H_ | 816 #endif // BASE_METRICS_HISTOGRAM_H_ |
OLD | NEW |