Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: base/metrics/statistics_recorder.h

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

Powered by Google App Engine
This is Rietveld 408576698