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 // StatisticsRecorder holds all Histograms and BucketRanges that are used by | 5 // StatisticsRecorder holds all Histograms and BucketRanges that are used by |
6 // Histograms in the system. It provides a general place for | 6 // Histograms in the system. It provides a general place for |
7 // Histograms/BucketRanges to register, and supports a global API for accessing | 7 // Histograms/BucketRanges to register, and supports a global API for accessing |
8 // (i.e., dumping, or graphing) the data. | 8 // (i.e., dumping, or graphing) the data. |
9 | 9 |
10 #ifndef BASE_METRICS_STATISTICS_RECORDER_H_ | 10 #ifndef BASE_METRICS_STATISTICS_RECORDER_H_ |
(...skipping 16 matching lines...) Expand all Loading... |
27 class BucketRanges; | 27 class BucketRanges; |
28 class Lock; | 28 class Lock; |
29 | 29 |
30 class BASE_EXPORT StatisticsRecorder { | 30 class BASE_EXPORT StatisticsRecorder { |
31 // We keep all registered histograms in a map, from name to histogram. | 31 // We keep all registered histograms in a map, from name to histogram. |
32 typedef std::map<uint64_t, HistogramBase*> HistogramMap; | 32 typedef std::map<uint64_t, HistogramBase*> HistogramMap; |
33 | 33 |
34 public: | 34 public: |
35 typedef std::vector<HistogramBase*> Histograms; | 35 typedef std::vector<HistogramBase*> Histograms; |
36 | 36 |
| 37 // A class for iterating over the histograms held within this global resource. |
| 38 class BASE_EXPORT HistogramIterator { |
| 39 public: |
| 40 HistogramIterator(HistogramMap::iterator iter, bool include_persistent) |
| 41 : iter_(iter), |
| 42 include_persistent_(include_persistent) {} |
| 43 |
| 44 HistogramIterator& operator++(); |
| 45 HistogramIterator operator++(int) { |
| 46 HistogramIterator tmp(*this); |
| 47 operator++(); |
| 48 return tmp; |
| 49 } |
| 50 |
| 51 bool operator==(const HistogramIterator& rhs) const { |
| 52 return iter_ == rhs.iter_; |
| 53 } |
| 54 bool operator!=(const HistogramIterator& rhs) const { |
| 55 return iter_ != rhs.iter_; |
| 56 } |
| 57 HistogramBase* operator*() { return iter_->second; } |
| 58 |
| 59 private: |
| 60 HistogramMap::iterator iter_; |
| 61 const bool include_persistent_; |
| 62 }; |
| 63 |
37 // Initializes the StatisticsRecorder system. Safe to call multiple times. | 64 // Initializes the StatisticsRecorder system. Safe to call multiple times. |
38 static void Initialize(); | 65 static void Initialize(); |
39 | 66 |
40 // Find out if histograms can now be registered into our list. | 67 // Find out if histograms can now be registered into our list. |
41 static bool IsActive(); | 68 static bool IsActive(); |
42 | 69 |
43 // Register, or add a new histogram to the collection of statistics. If an | 70 // Register, or add a new histogram to the collection of statistics. If an |
44 // identically named histogram is already registered, then the argument | 71 // identically named histogram is already registered, then the argument |
45 // |histogram| will deleted. The returned value is always the registered | 72 // |histogram| will deleted. The returned value is always the registered |
46 // histogram (either the argument, or the pre-existing registered histogram). | 73 // histogram (either the argument, or the pre-existing registered histogram). |
(...skipping 19 matching lines...) Expand all Loading... |
66 // Method for extracting histograms which were marked for use by UMA. | 93 // Method for extracting histograms which were marked for use by UMA. |
67 static void GetHistograms(Histograms* output); | 94 static void GetHistograms(Histograms* output); |
68 | 95 |
69 // Method for extracting BucketRanges used by all histograms registered. | 96 // Method for extracting BucketRanges used by all histograms registered. |
70 static void GetBucketRanges(std::vector<const BucketRanges*>* output); | 97 static void GetBucketRanges(std::vector<const BucketRanges*>* output); |
71 | 98 |
72 // Find a histogram by name. It matches the exact name. This method is thread | 99 // Find a histogram by name. It matches the exact name. This method is thread |
73 // safe. It returns NULL if a matching histogram is not found. | 100 // safe. It returns NULL if a matching histogram is not found. |
74 static HistogramBase* FindHistogram(const std::string& name); | 101 static HistogramBase* FindHistogram(const std::string& name); |
75 | 102 |
| 103 // Support for iterating over known histograms. |
| 104 static HistogramIterator begin(bool include_persistent); |
| 105 static HistogramIterator end(); |
| 106 |
76 // GetSnapshot copies some of the pointers to registered histograms into the | 107 // GetSnapshot copies some of the pointers to registered histograms into the |
77 // caller supplied vector (Histograms). Only histograms which have |query| as | 108 // caller supplied vector (Histograms). Only histograms which have |query| as |
78 // a substring are copied (an empty string will process all registered | 109 // a substring are copied (an empty string will process all registered |
79 // histograms). | 110 // histograms). |
80 static void GetSnapshot(const std::string& query, Histograms* snapshot); | 111 static void GetSnapshot(const std::string& query, Histograms* snapshot); |
81 | 112 |
82 typedef base::Callback<void(HistogramBase::Sample)> OnSampleCallback; | 113 typedef base::Callback<void(HistogramBase::Sample)> OnSampleCallback; |
83 | 114 |
84 // SetCallback sets the callback to notify when a new sample is recorded on | 115 // SetCallback sets the callback to notify when a new sample is recorded on |
85 // the histogram referred to by |histogram_name|. The call to this method can | 116 // the histogram referred to by |histogram_name|. The call to this method can |
(...skipping 19 matching lines...) Expand all Loading... |
105 // We keep all |bucket_ranges_| in a map, from checksum to a list of | 136 // We keep all |bucket_ranges_| in a map, from checksum to a list of |
106 // |bucket_ranges_|. Checksum is calculated from the |ranges_| in | 137 // |bucket_ranges_|. Checksum is calculated from the |ranges_| in |
107 // |bucket_ranges_|. | 138 // |bucket_ranges_|. |
108 typedef std::map<uint32, std::list<const BucketRanges*>*> RangesMap; | 139 typedef std::map<uint32, std::list<const BucketRanges*>*> RangesMap; |
109 | 140 |
110 friend struct DefaultLazyInstanceTraits<StatisticsRecorder>; | 141 friend struct DefaultLazyInstanceTraits<StatisticsRecorder>; |
111 friend class HistogramBaseTest; | 142 friend class HistogramBaseTest; |
112 friend class HistogramSnapshotManagerTest; | 143 friend class HistogramSnapshotManagerTest; |
113 friend class HistogramTest; | 144 friend class HistogramTest; |
114 friend class JsonPrefStoreTest; | 145 friend class JsonPrefStoreTest; |
| 146 friend class SharedHistogramTest; |
115 friend class SparseHistogramTest; | 147 friend class SparseHistogramTest; |
116 friend class StatisticsRecorderTest; | 148 friend class StatisticsRecorderTest; |
117 FRIEND_TEST_ALL_PREFIXES(HistogramDeltaSerializationTest, | 149 FRIEND_TEST_ALL_PREFIXES(HistogramDeltaSerializationTest, |
118 DeserializeHistogramAndAddSamples); | 150 DeserializeHistogramAndAddSamples); |
119 | 151 |
120 // The constructor just initializes static members. Usually client code should | 152 // The constructor just initializes static members. Usually client code should |
121 // use Initialize to do this. But in test code, you can friend this class and | 153 // use Initialize to do this. But in test code, you can friend this class and |
122 // call destructor/constructor to get a clean StatisticsRecorder. | 154 // call destructor/constructor to get a clean StatisticsRecorder. |
123 StatisticsRecorder(); | 155 StatisticsRecorder(); |
124 ~StatisticsRecorder(); | 156 ~StatisticsRecorder(); |
125 | 157 |
126 static void DumpHistogramsToVlog(void* instance); | 158 static void DumpHistogramsToVlog(void* instance); |
127 | 159 |
128 static HistogramMap* histograms_; | 160 static HistogramMap* histograms_; |
129 static CallbackMap* callbacks_; | 161 static CallbackMap* callbacks_; |
130 static RangesMap* ranges_; | 162 static RangesMap* ranges_; |
131 | 163 |
132 // Lock protects access to above maps. | 164 // Lock protects access to above maps. |
133 static base::Lock* lock_; | 165 static base::Lock* lock_; |
134 | 166 |
135 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); | 167 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); |
136 }; | 168 }; |
137 | 169 |
138 } // namespace base | 170 } // namespace base |
139 | 171 |
140 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ | 172 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ |
OLD | NEW |