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