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_ |
11 #define BASE_METRICS_STATISTICS_RECORDER_H_ | 11 #define BASE_METRICS_STATISTICS_RECORDER_H_ |
12 | 12 |
13 #include <stdint.h> | 13 #include <stdint.h> |
14 | 14 |
15 #include <list> | 15 #include <list> |
16 #include <map> | 16 #include <map> |
| 17 #include <memory> |
17 #include <string> | 18 #include <string> |
18 #include <vector> | 19 #include <vector> |
19 | 20 |
20 #include "base/base_export.h" | 21 #include "base/base_export.h" |
21 #include "base/callback.h" | 22 #include "base/callback.h" |
22 #include "base/gtest_prod_util.h" | 23 #include "base/gtest_prod_util.h" |
23 #include "base/lazy_instance.h" | 24 #include "base/lazy_instance.h" |
24 #include "base/macros.h" | 25 #include "base/macros.h" |
25 #include "base/metrics/histogram_base.h" | 26 #include "base/metrics/histogram_base.h" |
26 #include "base/strings/string_piece.h" | 27 #include "base/strings/string_piece.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 static void ClearCallback(const std::string& histogram_name); | 158 static void ClearCallback(const std::string& histogram_name); |
158 | 159 |
159 // FindCallback retrieves the callback for the histogram referred to by | 160 // FindCallback retrieves the callback for the histogram referred to by |
160 // |histogram_name|, or a null callback if no callback exists for this | 161 // |histogram_name|, or a null callback if no callback exists for this |
161 // histogram. This method is thread safe. | 162 // histogram. This method is thread safe. |
162 static OnSampleCallback FindCallback(const std::string& histogram_name); | 163 static OnSampleCallback FindCallback(const std::string& histogram_name); |
163 | 164 |
164 // Returns the number of known histograms. | 165 // Returns the number of known histograms. |
165 static size_t GetHistogramCount(); | 166 static size_t GetHistogramCount(); |
166 | 167 |
167 // Clears all of the known histograms and resets static variables to a | |
168 // state that allows a new initialization. | |
169 static void ResetForTesting(); | |
170 | |
171 // Removes a histogram from the internal set of known ones. This can be | 168 // Removes a histogram from the internal set of known ones. This can be |
172 // necessary during testing persistent histograms where the underlying | 169 // necessary during testing persistent histograms where the underlying |
173 // memory is being released. | 170 // memory is being released. |
174 static void ForgetHistogramForTesting(base::StringPiece name); | 171 static void ForgetHistogramForTesting(base::StringPiece name); |
175 | 172 |
| 173 // Reset any global instance of the statistics-recorder that was created |
| 174 // by a call to Initialize(). |
| 175 static void UninitializeForTesting(); |
| 176 |
176 private: | 177 private: |
177 // We keep a map of callbacks to histograms, so that as histograms are | 178 // We keep a map of callbacks to histograms, so that as histograms are |
178 // created, we can set the callback properly. | 179 // created, we can set the callback properly. |
179 typedef std::map<std::string, OnSampleCallback> CallbackMap; | 180 typedef std::map<std::string, OnSampleCallback> CallbackMap; |
180 | 181 |
181 // We keep all |bucket_ranges_| in a map, from checksum to a list of | 182 // We keep all |bucket_ranges_| in a map, from checksum to a list of |
182 // |bucket_ranges_|. Checksum is calculated from the |ranges_| in | 183 // |bucket_ranges_|. Checksum is calculated from the |ranges_| in |
183 // |bucket_ranges_|. | 184 // |bucket_ranges_|. |
184 typedef std::map<uint32_t, std::list<const BucketRanges*>*> RangesMap; | 185 typedef std::map<uint32_t, std::list<const BucketRanges*>*> RangesMap; |
185 | 186 |
186 friend struct DefaultLazyInstanceTraits<StatisticsRecorder>; | 187 friend struct DefaultLazyInstanceTraits<StatisticsRecorder>; |
187 friend class HistogramBaseTest; | 188 friend class HistogramBaseTest; |
188 friend class HistogramSnapshotManagerTest; | 189 friend class HistogramSnapshotManagerTest; |
189 friend class HistogramTest; | 190 friend class HistogramTest; |
190 friend class JsonPrefStoreTest; | 191 friend class JsonPrefStoreTest; |
191 friend class SharedHistogramTest; | 192 friend class SharedHistogramTest; |
192 friend class SparseHistogramTest; | 193 friend class SparseHistogramTest; |
193 friend class StatisticsRecorderTest; | 194 friend class StatisticsRecorderTest; |
194 FRIEND_TEST_ALL_PREFIXES(HistogramDeltaSerializationTest, | 195 FRIEND_TEST_ALL_PREFIXES(HistogramDeltaSerializationTest, |
195 DeserializeHistogramAndAddSamples); | 196 DeserializeHistogramAndAddSamples); |
196 | 197 |
197 // The constructor just initializes static members. Usually client code should | 198 // The constructor just initializes static members. Usually client code should |
198 // use Initialize to do this. But in test code, you can friend this class and | 199 // use Initialize to do this. But in test code, you can friend this class and |
199 // call the constructor to get a clean StatisticsRecorder. | 200 // call the constructor to get a clean StatisticsRecorder. |
200 StatisticsRecorder(); | 201 StatisticsRecorder(); |
201 | 202 |
| 203 // These are copies of everything that existed when the (test) Statistics- |
| 204 // Recorder was created. The global ones have to be moved aside to create a |
| 205 // clean environment. |
| 206 std::unique_ptr<HistogramMap> existing_histograms_; |
| 207 std::unique_ptr<CallbackMap> existing_callbacks_; |
| 208 std::unique_ptr<RangesMap> existing_ranges_; |
| 209 |
202 static void Reset(); | 210 static void Reset(); |
203 static void DumpHistogramsToVlog(void* instance); | 211 static void DumpHistogramsToVlog(void* instance); |
204 | 212 |
205 static HistogramMap* histograms_; | 213 static HistogramMap* histograms_; |
206 static CallbackMap* callbacks_; | 214 static CallbackMap* callbacks_; |
207 static RangesMap* ranges_; | 215 static RangesMap* ranges_; |
208 | 216 |
209 // Lock protects access to above maps. | 217 // Lock protects access to above maps. |
210 static base::Lock* lock_; | 218 static base::Lock* lock_; |
211 | 219 |
212 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); | 220 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); |
213 }; | 221 }; |
214 | 222 |
215 } // namespace base | 223 } // namespace base |
216 | 224 |
217 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ | 225 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ |
OLD | NEW |