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. Calls to ImportProvidedHistograms() will |
| 116 // fetch from registered providers. |
| 117 static void RegisterHistogramProvider( |
| 118 const WeakPtr<HistogramProvider>& provider); |
| 119 |
104 // Register, or add a new histogram to the collection of statistics. If an | 120 // Register, or add a new histogram to the collection of statistics. If an |
105 // identically named histogram is already registered, then the argument | 121 // identically named histogram is already registered, then the argument |
106 // |histogram| will deleted. The returned value is always the registered | 122 // |histogram| will deleted. The returned value is always the registered |
107 // histogram (either the argument, or the pre-existing registered histogram). | 123 // histogram (either the argument, or the pre-existing registered histogram). |
108 static HistogramBase* RegisterOrDeleteDuplicate(HistogramBase* histogram); | 124 static HistogramBase* RegisterOrDeleteDuplicate(HistogramBase* histogram); |
109 | 125 |
110 // Register, or add a new BucketRanges. If an identically BucketRanges is | 126 // Register, or add a new BucketRanges. If an identically BucketRanges is |
111 // already registered, then the argument |ranges| will deleted. The returned | 127 // already registered, then the argument |ranges| will deleted. The returned |
112 // value is always the registered BucketRanges (either the argument, or the | 128 // value is always the registered BucketRanges (either the argument, or the |
113 // pre-existing one). | 129 // pre-existing one). |
(...skipping 13 matching lines...) Expand all Loading... |
127 // Method for extracting histograms which were marked for use by UMA. | 143 // Method for extracting histograms which were marked for use by UMA. |
128 static void GetHistograms(Histograms* output); | 144 static void GetHistograms(Histograms* output); |
129 | 145 |
130 // Method for extracting BucketRanges used by all histograms registered. | 146 // Method for extracting BucketRanges used by all histograms registered. |
131 static void GetBucketRanges(std::vector<const BucketRanges*>* output); | 147 static void GetBucketRanges(std::vector<const BucketRanges*>* output); |
132 | 148 |
133 // Find a histogram by name. It matches the exact name. This method is thread | 149 // 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. | 150 // safe. It returns NULL if a matching histogram is not found. |
135 static HistogramBase* FindHistogram(base::StringPiece name); | 151 static HistogramBase* FindHistogram(base::StringPiece name); |
136 | 152 |
| 153 // Imports histograms from providers. This must be called on the UI thread. |
| 154 static void ImportProvidedHistograms(); |
| 155 |
137 // Support for iterating over known histograms. | 156 // Support for iterating over known histograms. |
138 static HistogramIterator begin(bool include_persistent); | 157 static HistogramIterator begin(bool include_persistent); |
139 static HistogramIterator end(); | 158 static HistogramIterator end(); |
140 | 159 |
141 // GetSnapshot copies some of the pointers to registered histograms into the | 160 // GetSnapshot copies some of the pointers to registered histograms into the |
142 // caller supplied vector (Histograms). Only histograms which have |query| as | 161 // caller supplied vector (Histograms). Only histograms which have |query| as |
143 // a substring are copied (an empty string will process all registered | 162 // a substring are copied (an empty string will process all registered |
144 // histograms). | 163 // histograms). |
145 static void GetSnapshot(const std::string& query, Histograms* snapshot); | 164 static void GetSnapshot(const std::string& query, Histograms* snapshot); |
146 | 165 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 // Initialize implementation but without lock. Caller should guard | 232 // Initialize implementation but without lock. Caller should guard |
214 // StatisticsRecorder by itself if needed (it isn't in unit tests). | 233 // StatisticsRecorder by itself if needed (it isn't in unit tests). |
215 void InitLogOnShutdownWithoutLock(); | 234 void InitLogOnShutdownWithoutLock(); |
216 | 235 |
217 // These are copies of everything that existed when the (test) Statistics- | 236 // 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 | 237 // Recorder was created. The global ones have to be moved aside to create a |
219 // clean environment. | 238 // clean environment. |
220 std::unique_ptr<HistogramMap> existing_histograms_; | 239 std::unique_ptr<HistogramMap> existing_histograms_; |
221 std::unique_ptr<CallbackMap> existing_callbacks_; | 240 std::unique_ptr<CallbackMap> existing_callbacks_; |
222 std::unique_ptr<RangesMap> existing_ranges_; | 241 std::unique_ptr<RangesMap> existing_ranges_; |
| 242 std::unique_ptr<HistogramProviders> existing_providers_; |
223 | 243 |
224 bool vlog_initialized_ = false; | 244 bool vlog_initialized_ = false; |
225 | 245 |
226 static void Reset(); | 246 static void Reset(); |
227 static void DumpHistogramsToVlog(void* instance); | 247 static void DumpHistogramsToVlog(void* instance); |
228 | 248 |
229 static HistogramMap* histograms_; | 249 static HistogramMap* histograms_; |
230 static CallbackMap* callbacks_; | 250 static CallbackMap* callbacks_; |
231 static RangesMap* ranges_; | 251 static RangesMap* ranges_; |
| 252 static HistogramProviders* providers_; |
232 | 253 |
233 // Lock protects access to above maps. This is a LazyInstance to avoid races | 254 // 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 | 255 // when the above methods are used before Initialize(). Previously each method |
235 // would do |if (!lock_) return;| which would race with | 256 // would do |if (!lock_) return;| which would race with |
236 // |lock_ = new Lock;| in StatisticsRecorder(). http://crbug.com/672852. | 257 // |lock_ = new Lock;| in StatisticsRecorder(). http://crbug.com/672852. |
237 static base::LazyInstance<base::Lock>::Leaky lock_; | 258 static base::LazyInstance<base::Lock>::Leaky lock_; |
238 | 259 |
239 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); | 260 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); |
240 }; | 261 }; |
241 | 262 |
242 } // namespace base | 263 } // namespace base |
243 | 264 |
244 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ | 265 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ |
OLD | NEW |