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 <list> | 13 #include <list> |
14 #include <map> | 14 #include <map> |
15 #include <string> | 15 #include <string> |
16 #include <vector> | 16 #include <vector> |
17 | 17 |
18 #include "base/base_export.h" | 18 #include "base/base_export.h" |
19 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
| 20 #include "base/callback.h" |
20 #include "base/gtest_prod_util.h" | 21 #include "base/gtest_prod_util.h" |
21 #include "base/lazy_instance.h" | 22 #include "base/lazy_instance.h" |
| 23 #include "base/metrics/histogram_base.h" |
22 | 24 |
23 namespace base { | 25 namespace base { |
24 | 26 |
25 class BucketRanges; | 27 class BucketRanges; |
26 class HistogramBase; | |
27 class Lock; | 28 class Lock; |
28 | 29 |
29 class BASE_EXPORT StatisticsRecorder { | 30 class BASE_EXPORT StatisticsRecorder { |
30 public: | 31 public: |
31 typedef std::vector<HistogramBase*> Histograms; | 32 typedef std::vector<HistogramBase*> Histograms; |
32 | 33 |
33 // Initializes the StatisticsRecorder system. Safe to call multiple times. | 34 // Initializes the StatisticsRecorder system. Safe to call multiple times. |
34 static void Initialize(); | 35 static void Initialize(); |
35 | 36 |
36 // Find out if histograms can now be registered into our list. | 37 // Find out if histograms can now be registered into our list. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 // Find a histogram by name. It matches the exact name. This method is thread | 69 // Find a histogram by name. It matches the exact name. This method is thread |
69 // safe. It returns NULL if a matching histogram is not found. | 70 // safe. It returns NULL if a matching histogram is not found. |
70 static HistogramBase* FindHistogram(const std::string& name); | 71 static HistogramBase* FindHistogram(const std::string& name); |
71 | 72 |
72 // GetSnapshot copies some of the pointers to registered histograms into the | 73 // GetSnapshot copies some of the pointers to registered histograms into the |
73 // caller supplied vector (Histograms). Only histograms which have |query| as | 74 // caller supplied vector (Histograms). Only histograms which have |query| as |
74 // a substring are copied (an empty string will process all registered | 75 // a substring are copied (an empty string will process all registered |
75 // histograms). | 76 // histograms). |
76 static void GetSnapshot(const std::string& query, Histograms* snapshot); | 77 static void GetSnapshot(const std::string& query, Histograms* snapshot); |
77 | 78 |
| 79 typedef base::Callback<void(HistogramBase::Sample)> OnSampleCallback; |
| 80 |
| 81 // SetCallback sets the callback to notify when a new sample is recorded on |
| 82 // the histogram referred to by |histogram_name|. The call to this method can |
| 83 // be be done before or after the histogram is created. This method is thread |
| 84 // safe. The return value is whether or not the callback was successfully set. |
| 85 static bool SetCallback(const std::string& histogram_name, |
| 86 const OnSampleCallback& callback); |
| 87 |
| 88 // ClearCallback clears any callback set on the histogram referred to by |
| 89 // |histogram_name|. This method is thread safe. |
| 90 static void ClearCallback(const std::string& histogram_name); |
| 91 |
| 92 // FindCallback retrieves the callback for the histogram referred to by |
| 93 // |histogram_name|, or a null callback if no callback exists for this |
| 94 // histogram. This method is thread safe. |
| 95 static OnSampleCallback FindCallback(const std::string& histogram_name); |
| 96 |
78 private: | 97 private: |
| 98 // HistogramNameRef holds a weak const ref to the name field of the associated |
| 99 // Histogram object, allowing re-use of the underlying string storage for the |
| 100 // map keys. The wrapper is required as using "const std::string&" as the key |
| 101 // results in compile errors. |
| 102 struct HistogramNameRef { |
| 103 explicit HistogramNameRef(const std::string& name) : name_(name){}; |
| 104 |
| 105 // Operator < is necessary to use this type as a std::map key. |
| 106 bool operator<(const HistogramNameRef& other) const { |
| 107 return name_ < other.name_; |
| 108 } |
| 109 |
| 110 // Weak, owned by the associated Histogram object. |
| 111 const std::string& name_; |
| 112 }; |
| 113 |
79 // We keep all registered histograms in a map, from name to histogram. | 114 // We keep all registered histograms in a map, from name to histogram. |
80 typedef std::map<std::string, HistogramBase*> HistogramMap; | 115 typedef std::map<HistogramNameRef, HistogramBase*> HistogramMap; |
| 116 |
| 117 // We keep a map of callbacks to histograms, so that as histograms are |
| 118 // created, we can set the callback properly. |
| 119 typedef std::map<std::string, OnSampleCallback> CallbackMap; |
81 | 120 |
82 // We keep all |bucket_ranges_| in a map, from checksum to a list of | 121 // We keep all |bucket_ranges_| in a map, from checksum to a list of |
83 // |bucket_ranges_|. Checksum is calculated from the |ranges_| in | 122 // |bucket_ranges_|. Checksum is calculated from the |ranges_| in |
84 // |bucket_ranges_|. | 123 // |bucket_ranges_|. |
85 typedef std::map<uint32, std::list<const BucketRanges*>*> RangesMap; | 124 typedef std::map<uint32, std::list<const BucketRanges*>*> RangesMap; |
86 | 125 |
87 friend struct DefaultLazyInstanceTraits<StatisticsRecorder>; | 126 friend struct DefaultLazyInstanceTraits<StatisticsRecorder>; |
88 friend class HistogramBaseTest; | 127 friend class HistogramBaseTest; |
89 friend class HistogramSnapshotManagerTest; | 128 friend class HistogramSnapshotManagerTest; |
90 friend class HistogramTest; | 129 friend class HistogramTest; |
91 friend class JsonPrefStoreTest; | 130 friend class JsonPrefStoreTest; |
92 friend class SparseHistogramTest; | 131 friend class SparseHistogramTest; |
93 friend class StatisticsRecorderTest; | 132 friend class StatisticsRecorderTest; |
94 FRIEND_TEST_ALL_PREFIXES(HistogramDeltaSerializationTest, | 133 FRIEND_TEST_ALL_PREFIXES(HistogramDeltaSerializationTest, |
95 DeserializeHistogramAndAddSamples); | 134 DeserializeHistogramAndAddSamples); |
96 | 135 |
97 // The constructor just initializes static members. Usually client code should | 136 // The constructor just initializes static members. Usually client code should |
98 // use Initialize to do this. But in test code, you can friend this class and | 137 // use Initialize to do this. But in test code, you can friend this class and |
99 // call destructor/constructor to get a clean StatisticsRecorder. | 138 // call destructor/constructor to get a clean StatisticsRecorder. |
100 StatisticsRecorder(); | 139 StatisticsRecorder(); |
101 ~StatisticsRecorder(); | 140 ~StatisticsRecorder(); |
102 | 141 |
103 static void DumpHistogramsToVlog(void* instance); | 142 static void DumpHistogramsToVlog(void* instance); |
104 | 143 |
105 static HistogramMap* histograms_; | 144 static HistogramMap* histograms_; |
| 145 static CallbackMap* callbacks_; |
106 static RangesMap* ranges_; | 146 static RangesMap* ranges_; |
107 | 147 |
108 // Lock protects access to above maps. | 148 // Lock protects access to above maps. |
109 static base::Lock* lock_; | 149 static base::Lock* lock_; |
110 | 150 |
111 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); | 151 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); |
112 }; | 152 }; |
113 | 153 |
114 } // namespace base | 154 } // namespace base |
115 | 155 |
116 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ | 156 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ |
OLD | NEW |