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