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

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

Issue 1779503002: Fix StatisticsRecorder to handle re-entry during tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months 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
« no previous file with comments | « base/metrics/histogram_unittest.cc ('k') | base/metrics/statistics_recorder.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 123
124 // ClearCallback clears any callback set on the histogram referred to by 124 // ClearCallback clears any callback set on the histogram referred to by
125 // |histogram_name|. This method is thread safe. 125 // |histogram_name|. This method is thread safe.
126 static void ClearCallback(const std::string& histogram_name); 126 static void ClearCallback(const std::string& histogram_name);
127 127
128 // FindCallback retrieves the callback for the histogram referred to by 128 // FindCallback retrieves the callback for the histogram referred to by
129 // |histogram_name|, or a null callback if no callback exists for this 129 // |histogram_name|, or a null callback if no callback exists for this
130 // histogram. This method is thread safe. 130 // histogram. This method is thread safe.
131 static OnSampleCallback FindCallback(const std::string& histogram_name); 131 static OnSampleCallback FindCallback(const std::string& histogram_name);
132 132
133 // Clears all of the known histograms and resets static variables to a
134 // state that allows a new initialization.
135 static void ResetForTesting();
136
137 // Removes a histogram from the internal set of known ones. This can be 133 // Removes a histogram from the internal set of known ones. This can be
138 // necessary during testing persistent histograms where the underlying 134 // necessary during testing persistent histograms where the underlying
139 // memory is being released. 135 // memory is being released.
140 static void ForgetHistogramForTesting(base::StringPiece name); 136 static void ForgetHistogramForTesting(base::StringPiece name);
141 137
142 private: 138 private:
143 // We keep a map of callbacks to histograms, so that as histograms are 139 // We keep a map of callbacks to histograms, so that as histograms are
144 // created, we can set the callback properly. 140 // created, we can set the callback properly.
145 typedef std::map<std::string, OnSampleCallback> CallbackMap; 141 typedef std::map<std::string, OnSampleCallback> CallbackMap;
146 142
(...skipping 12 matching lines...) Expand all
159 friend class StatisticsRecorderTest; 155 friend class StatisticsRecorderTest;
160 FRIEND_TEST_ALL_PREFIXES(HistogramDeltaSerializationTest, 156 FRIEND_TEST_ALL_PREFIXES(HistogramDeltaSerializationTest,
161 DeserializeHistogramAndAddSamples); 157 DeserializeHistogramAndAddSamples);
162 158
163 // The constructor just initializes static members. Usually client code should 159 // The constructor just initializes static members. Usually client code should
164 // use Initialize to do this. But in test code, you can friend this class and 160 // use Initialize to do this. But in test code, you can friend this class and
165 // call destructor/constructor to get a clean StatisticsRecorder. 161 // call destructor/constructor to get a clean StatisticsRecorder.
166 StatisticsRecorder(); 162 StatisticsRecorder();
167 ~StatisticsRecorder(); 163 ~StatisticsRecorder();
168 164
165 // These are copies of everything that existed when the (test) Statistics-
166 // Recorder was created. The global ones have to be moved aside to create a
167 // clean environment.
168 scoped_ptr<HistogramMap> existing_histograms_;
169 scoped_ptr<CallbackMap> existing_callbacks_;
170 scoped_ptr<RangesMap> existing_ranges_;
Alexei Svitkine (slow) 2016/03/08 20:04:12 Can this be done in a test-only subclass?
bcwhite 2016/03/08 20:23:27 Potentially. It'll require turning private member
171
169 static void Reset(); 172 static void Reset();
170 static void DumpHistogramsToVlog(void* instance); 173 static void DumpHistogramsToVlog(void* instance);
171 174
172 static HistogramMap* histograms_; 175 static HistogramMap* histograms_;
173 static CallbackMap* callbacks_; 176 static CallbackMap* callbacks_;
174 static RangesMap* ranges_; 177 static RangesMap* ranges_;
175 178
176 // Lock protects access to above maps. 179 // Lock protects access to above maps.
177 static base::Lock* lock_; 180 static base::Lock* lock_;
178 181
179 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder); 182 DISALLOW_COPY_AND_ASSIGN(StatisticsRecorder);
180 }; 183 };
181 184
182 } // namespace base 185 } // namespace base
183 186
184 #endif // BASE_METRICS_STATISTICS_RECORDER_H_ 187 #endif // BASE_METRICS_STATISTICS_RECORDER_H_
OLDNEW
« no previous file with comments | « base/metrics/histogram_unittest.cc ('k') | base/metrics/statistics_recorder.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698