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_ |
| 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 <memory> |
| 18 #include <string> | 18 #include <string> |
| 19 #include <vector> | 19 #include <vector> |
| 20 | 20 |
| 21 #include "base/base_export.h" | 21 #include "base/base_export.h" |
| 22 #include "base/callback.h" | 22 #include "base/callback.h" |
| 23 #include "base/gtest_prod_util.h" | 23 #include "base/gtest_prod_util.h" |
| 24 #include "base/lazy_instance.h" | 24 #include "base/lazy_instance.h" |
| 25 #include "base/macros.h" | 25 #include "base/macros.h" |
| 26 #include "base/memory/weak_ptr.h" | |
| 26 #include "base/metrics/histogram_base.h" | 27 #include "base/metrics/histogram_base.h" |
| 27 #include "base/strings/string_piece.h" | 28 #include "base/strings/string_piece.h" |
| 28 #include "base/synchronization/lock.h" | 29 #include "base/synchronization/lock.h" |
| 29 | 30 |
| 30 namespace base { | 31 namespace base { |
| 31 | 32 |
| 32 class BucketRanges; | 33 class BucketRanges; |
| 33 | 34 |
| 34 class BASE_EXPORT StatisticsRecorder { | 35 class BASE_EXPORT StatisticsRecorder { |
| 35 public: | 36 public: |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 55 return false; | 56 return false; |
| 56 | 57 |
| 57 // Fall back to an actual string comparison. The lengths are the same | 58 // Fall back to an actual string comparison. The lengths are the same |
| 58 // so a simple memory-compare is sufficient. This is slightly more | 59 // so a simple memory-compare is sufficient. This is slightly more |
| 59 // efficient than calling operator<() for StringPiece which would | 60 // efficient than calling operator<() for StringPiece which would |
| 60 // again have to check lengths before calling wordmemcmp(). | 61 // again have to check lengths before calling wordmemcmp(). |
| 61 return wordmemcmp(data(), rhs.data(), length()) < 0; | 62 return wordmemcmp(data(), rhs.data(), length()) < 0; |
| 62 } | 63 } |
| 63 }; | 64 }; |
| 64 | 65 |
| 66 // An interface class that allows the StatisticsRecorder to forcibly merge | |
| 67 // histograms from providers when necessary. | |
| 68 class HistogramProvider { | |
| 69 public: | |
| 70 // Merges all histogram information into the global versions. | |
| 71 virtual void MergeHistogramDeltas() = 0; | |
| 72 }; | |
| 73 | |
| 65 typedef std::map<StringKey, HistogramBase*> HistogramMap; | 74 typedef std::map<StringKey, HistogramBase*> HistogramMap; |
| 66 typedef std::vector<HistogramBase*> Histograms; | 75 typedef std::vector<HistogramBase*> Histograms; |
| 76 typedef std::vector<WeakPtr<HistogramProvider>> HistogramProviders; | |
| 67 | 77 |
| 68 // A class for iterating over the histograms held within this global resource. | 78 // A class for iterating over the histograms held within this global resource. |
| 69 class BASE_EXPORT HistogramIterator { | 79 class BASE_EXPORT HistogramIterator { |
| 70 public: | 80 public: |
| 71 HistogramIterator(const HistogramMap::iterator& iter, | 81 HistogramIterator(const HistogramMap::iterator& iter, |
| 72 bool include_persistent); | 82 bool include_persistent); |
| 73 HistogramIterator(const HistogramIterator& rhs); // Must be copyable. | 83 HistogramIterator(const HistogramIterator& rhs); // Must be copyable. |
| 74 ~HistogramIterator(); | 84 ~HistogramIterator(); |
| 75 | 85 |
| 76 HistogramIterator& operator++(); | 86 HistogramIterator& operator++(); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 94 }; | 104 }; |
| 95 | 105 |
| 96 ~StatisticsRecorder(); | 106 ~StatisticsRecorder(); |
| 97 | 107 |
| 98 // Initializes the StatisticsRecorder system. Safe to call multiple times. | 108 // Initializes the StatisticsRecorder system. Safe to call multiple times. |
| 99 static void Initialize(); | 109 static void Initialize(); |
| 100 | 110 |
| 101 // Find out if histograms can now be registered into our list. | 111 // Find out if histograms can now be registered into our list. |
| 102 static bool IsActive(); | 112 static bool IsActive(); |
| 103 | 113 |
| 114 // Register a provider of histograms that can be called to merge those into | |
| 115 // the global StatisticsRecorder. | |
|
Alexei Svitkine (slow)
2017/01/27 16:22:03
Nit: Mention ImportProvidedHistograms() here.
bcwhite
2017/01/27 18:05:31
Done.
| |
| 116 static void RegisterHistogramProvider( | |
| 117 const WeakPtr<HistogramProvider>& provider); | |
| 118 | |
| 104 // Register, or add a new histogram to the collection of statistics. If an | 119 // Register, or add a new histogram to the collection of statistics. If an |
| 105 // identically named histogram is already registered, then the argument | 120 // identically named histogram is already registered, then the argument |
| 106 // |histogram| will deleted. The returned value is always the registered | 121 // |histogram| will deleted. The returned value is always the registered |
| 107 // histogram (either the argument, or the pre-existing registered histogram). | 122 // histogram (either the argument, or the pre-existing registered histogram). |
| 108 static HistogramBase* RegisterOrDeleteDuplicate(HistogramBase* histogram); | 123 static HistogramBase* RegisterOrDeleteDuplicate(HistogramBase* histogram); |
| 109 | 124 |
| 110 // Register, or add a new BucketRanges. If an identically BucketRanges is | 125 // Register, or add a new BucketRanges. If an identically BucketRanges is |
| 111 // already registered, then the argument |ranges| will deleted. The returned | 126 // already registered, then the argument |ranges| will deleted. The returned |
| 112 // value is always the registered BucketRanges (either the argument, or the | 127 // value is always the registered BucketRanges (either the argument, or the |
| 113 // pre-existing one). | 128 // pre-existing one). |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 127 // Method for extracting histograms which were marked for use by UMA. | 142 // Method for extracting histograms which were marked for use by UMA. |
| 128 static void GetHistograms(Histograms* output); | 143 static void GetHistograms(Histograms* output); |
| 129 | 144 |
| 130 // Method for extracting BucketRanges used by all histograms registered. | 145 // Method for extracting BucketRanges used by all histograms registered. |
| 131 static void GetBucketRanges(std::vector<const BucketRanges*>* output); | 146 static void GetBucketRanges(std::vector<const BucketRanges*>* output); |
| 132 | 147 |
| 133 // Find a histogram by name. It matches the exact name. This method is thread | 148 // Find a histogram by name. It matches the exact name. This method is thread |
| 134 // safe. It returns NULL if a matching histogram is not found. | 149 // safe. It returns NULL if a matching histogram is not found. |
| 135 static HistogramBase* FindHistogram(base::StringPiece name); | 150 static HistogramBase* FindHistogram(base::StringPiece name); |
| 136 | 151 |
| 152 // Imports histograms from providers. This must be called on the UI thread. | |
| 153 static void ImportProvidedHistograms(); | |
| 154 | |
| 137 // Support for iterating over known histograms. | 155 // Support for iterating over known histograms. |
| 138 static HistogramIterator begin(bool include_persistent); | 156 static HistogramIterator begin(bool include_persistent); |
| 139 static HistogramIterator end(); | 157 static HistogramIterator end(); |
| 140 | 158 |
| 141 // GetSnapshot copies some of the pointers to registered histograms into the | 159 // GetSnapshot copies some of the pointers to registered histograms into the |
| 142 // caller supplied vector (Histograms). Only histograms which have |query| as | 160 // caller supplied vector (Histograms). Only histograms which have |query| as |
| 143 // a substring are copied (an empty string will process all registered | 161 // a substring are copied (an empty string will process all registered |
| 144 // histograms). | 162 // histograms). |
| 145 static void GetSnapshot(const std::string& query, Histograms* snapshot); | 163 static void GetSnapshot(const std::string& query, Histograms* snapshot); |
| 146 | 164 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 // Initialize implementation but without lock. Caller should guard | 231 // Initialize implementation but without lock. Caller should guard |
| 214 // StatisticsRecorder by itself if needed (it isn't in unit tests). | 232 // StatisticsRecorder by itself if needed (it isn't in unit tests). |
| 215 void InitLogOnShutdownWithoutLock(); | 233 void InitLogOnShutdownWithoutLock(); |
| 216 | 234 |
| 217 // These are copies of everything that existed when the (test) Statistics- | 235 // These are copies of everything that existed when the (test) Statistics- |
| 218 // Recorder was created. The global ones have to be moved aside to create a | 236 // Recorder was created. The global ones have to be moved aside to create a |
| 219 // clean environment. | 237 // clean environment. |
| 220 std::unique_ptr<HistogramMap> existing_histograms_; | 238 std::unique_ptr<HistogramMap> existing_histograms_; |
| 221 std::unique_ptr<CallbackMap> existing_callbacks_; | 239 std::unique_ptr<CallbackMap> existing_callbacks_; |
| 222 std::unique_ptr<RangesMap> existing_ranges_; | 240 std::unique_ptr<RangesMap> existing_ranges_; |
| 241 std::unique_ptr<HistogramProviders> existing_providers_; | |
| 223 | 242 |
| 224 bool vlog_initialized_ = false; | 243 bool vlog_initialized_ = false; |
| 225 | 244 |
| 226 static void Reset(); | 245 static void Reset(); |
| 227 static void DumpHistogramsToVlog(void* instance); | 246 static void DumpHistogramsToVlog(void* instance); |
| 228 | 247 |
| 229 static HistogramMap* histograms_; | 248 static HistogramMap* histograms_; |
| 230 static CallbackMap* callbacks_; | 249 static CallbackMap* callbacks_; |
| 231 static RangesMap* ranges_; | 250 static RangesMap* ranges_; |
| 251 static HistogramProviders* providers_; | |
| 232 | 252 |
| 233 // Lock protects access to above maps. This is a LazyInstance to avoid races | 253 // Lock protects access to above maps. This is a LazyInstance to avoid races |
| 234 // when the above methods are used before Initialize(). Previously each method | 254 // when the above methods are used before Initialize(). Previously each method |
| 235 // would do |if (!lock_) return;| which would race with | 255 // would do |if (!lock_) return;| which would race with |
| 236 // |lock_ = new Lock;| in StatisticsRecorder(). http://crbug.com/672852. | 256 // |lock_ = new Lock;| in StatisticsRecorder(). http://crbug.com/672852. |
| 237 static base::LazyInstance<base::Lock>::Leaky lock_; | 257 static base::LazyInstance<base::Lock>::Leaky lock_; |
| 238 | 258 |
| 239 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); | 259 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); |
| 240 }; | 260 }; |
| 241 | 261 |
| 242 } // namespace base | 262 } // namespace base |
| 243 | 263 |
| 244 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ | 264 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ |
| OLD | NEW |