Chromium Code Reviews| 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 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 static void ClearCallback(const std::string& histogram_name); | 160 static void ClearCallback(const std::string& histogram_name); |
| 161 | 161 |
| 162 // FindCallback retrieves the callback for the histogram referred to by | 162 // FindCallback retrieves the callback for the histogram referred to by |
| 163 // |histogram_name|, or a null callback if no callback exists for this | 163 // |histogram_name|, or a null callback if no callback exists for this |
| 164 // histogram. This method is thread safe. | 164 // histogram. This method is thread safe. |
| 165 static OnSampleCallback FindCallback(const std::string& histogram_name); | 165 static OnSampleCallback FindCallback(const std::string& histogram_name); |
| 166 | 166 |
| 167 // Returns the number of known histograms. | 167 // Returns the number of known histograms. |
| 168 static size_t GetHistogramCount(); | 168 static size_t GetHistogramCount(); |
| 169 | 169 |
| 170 // Initializes logging histograms with --v=1. Safe to call several times. | |
|
Lei Zhang
2016/07/04 21:03:31
s/several/multiple/
| |
| 171 // Is called from ctor but for browser it seems that it is more useful to | |
| 172 // start logging after statistics recorder, so we need to init log-on-shutdown | |
| 173 // later. | |
| 174 static void InitLogOnShutdown(); | |
| 175 | |
| 170 // Removes a histogram from the internal set of known ones. This can be | 176 // Removes a histogram from the internal set of known ones. This can be |
| 171 // necessary during testing persistent histograms where the underlying | 177 // necessary during testing persistent histograms where the underlying |
| 172 // memory is being released. | 178 // memory is being released. |
| 173 static void ForgetHistogramForTesting(base::StringPiece name); | 179 static void ForgetHistogramForTesting(base::StringPiece name); |
| 174 | 180 |
| 175 // Creates a local StatisticsRecorder object for testing purposes. All new | 181 // Creates a local StatisticsRecorder object for testing purposes. All new |
| 176 // histograms will be registered in it until it is destructed or pushed | 182 // histograms will be registered in it until it is destructed or pushed |
| 177 // aside for the lifetime of yet another SR object. The destruction of the | 183 // aside for the lifetime of yet another SR object. The destruction of the |
| 178 // returned object will re-activate the previous one. Always release SR | 184 // returned object will re-activate the previous one. Always release SR |
| 179 // objects in the opposite order to which they're created. | 185 // objects in the opposite order to which they're created. |
| 180 static std::unique_ptr<StatisticsRecorder> CreateTemporaryForTesting() | 186 static std::unique_ptr<StatisticsRecorder> CreateTemporaryForTesting() |
| 181 WARN_UNUSED_RESULT; | 187 WARN_UNUSED_RESULT; |
| 182 | 188 |
| 183 // Resets any global instance of the statistics-recorder that was created | 189 // Resets any global instance of the statistics-recorder that was created |
| 184 // by a call to Initialize(). | 190 // by a call to Initialize(). |
| 185 static void UninitializeForTesting(); | 191 static void UninitializeForTesting(); |
| 186 | 192 |
| 187 private: | 193 private: |
| 188 // We keep a map of callbacks to histograms, so that as histograms are | 194 // We keep a map of callbacks to histograms, so that as histograms are |
| 189 // created, we can set the callback properly. | 195 // created, we can set the callback properly. |
| 190 typedef std::map<std::string, OnSampleCallback> CallbackMap; | 196 typedef std::map<std::string, OnSampleCallback> CallbackMap; |
| 191 | 197 |
| 192 // We keep all |bucket_ranges_| in a map, from checksum to a list of | 198 // We keep all |bucket_ranges_| in a map, from checksum to a list of |
| 193 // |bucket_ranges_|. Checksum is calculated from the |ranges_| in | 199 // |bucket_ranges_|. Checksum is calculated from the |ranges_| in |
| 194 // |bucket_ranges_|. | 200 // |bucket_ranges_|. |
| 195 typedef std::map<uint32_t, std::list<const BucketRanges*>*> RangesMap; | 201 typedef std::map<uint32_t, std::list<const BucketRanges*>*> RangesMap; |
| 196 | 202 |
| 197 friend struct DefaultLazyInstanceTraits<StatisticsRecorder>; | 203 friend struct DefaultLazyInstanceTraits<StatisticsRecorder>; |
| 204 friend class StatisticsRecorderTest; | |
| 198 | 205 |
| 199 // Imports histograms from global persistent memory. The global lock must | 206 // Imports histograms from global persistent memory. The global lock must |
| 200 // not be held during this call. | 207 // not be held during this call. |
| 201 static void ImportGlobalPersistentHistograms(); | 208 static void ImportGlobalPersistentHistograms(); |
| 202 | 209 |
| 203 // The constructor just initializes static members. Usually client code should | 210 // The constructor just initializes static members. Usually client code should |
| 204 // use Initialize to do this. But in test code, you can friend this class and | 211 // use Initialize to do this. But in test code, you can friend this class and |
| 205 // call the constructor to get a clean StatisticsRecorder. | 212 // call the constructor to get a clean StatisticsRecorder. |
| 206 StatisticsRecorder(); | 213 StatisticsRecorder(); |
| 207 | 214 |
| 215 // Initialize implementation but without lock. Caller should guard | |
| 216 // StatisticsRecorder by itlsef if needed (it isn't in unit tests). | |
|
Lei Zhang
2016/07/04 21:03:31
type: "itself"
| |
| 217 void InitLogOnShutdownWithoutLock(); | |
| 218 | |
| 208 // These are copies of everything that existed when the (test) Statistics- | 219 // These are copies of everything that existed when the (test) Statistics- |
| 209 // Recorder was created. The global ones have to be moved aside to create a | 220 // Recorder was created. The global ones have to be moved aside to create a |
| 210 // clean environment. | 221 // clean environment. |
| 211 std::unique_ptr<HistogramMap> existing_histograms_; | 222 std::unique_ptr<HistogramMap> existing_histograms_; |
| 212 std::unique_ptr<CallbackMap> existing_callbacks_; | 223 std::unique_ptr<CallbackMap> existing_callbacks_; |
| 213 std::unique_ptr<RangesMap> existing_ranges_; | 224 std::unique_ptr<RangesMap> existing_ranges_; |
| 214 | 225 |
| 226 bool vlog_initialized_ = false; | |
| 227 | |
| 215 static void Reset(); | 228 static void Reset(); |
| 216 static void DumpHistogramsToVlog(void* instance); | 229 static void DumpHistogramsToVlog(void* instance); |
| 217 | 230 |
| 218 static HistogramMap* histograms_; | 231 static HistogramMap* histograms_; |
| 219 static CallbackMap* callbacks_; | 232 static CallbackMap* callbacks_; |
| 220 static RangesMap* ranges_; | 233 static RangesMap* ranges_; |
| 221 | 234 |
| 222 // Lock protects access to above maps. | 235 // Lock protects access to above maps. |
| 223 static base::Lock* lock_; | 236 static base::Lock* lock_; |
| 224 | 237 |
| 225 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); | 238 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); |
| 226 }; | 239 }; |
| 227 | 240 |
| 228 } // namespace base | 241 } // namespace base |
| 229 | 242 |
| 230 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ | 243 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ |
| OLD | NEW |