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 #ifndef BASE_METRICS_HISTOGRAM_BASE_H_ | 5 #ifndef BASE_METRICS_HISTOGRAM_BASE_H_ |
6 #define BASE_METRICS_HISTOGRAM_BASE_H_ | 6 #define BASE_METRICS_HISTOGRAM_BASE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/atomicops.h" | 13 #include "base/atomicops.h" |
14 #include "base/base_export.h" | 14 #include "base/base_export.h" |
15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
16 #include "base/memory/persistent_memory_allocator.h" | |
16 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
17 #include "base/strings/string_piece.h" | 18 #include "base/strings/string_piece.h" |
18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 | 22 |
23 class BucketRanges; | |
22 class DictionaryValue; | 24 class DictionaryValue; |
23 class HistogramBase; | 25 class HistogramBase; |
24 class HistogramSamples; | 26 class HistogramSamples; |
25 class ListValue; | 27 class ListValue; |
26 class Pickle; | 28 class Pickle; |
27 class PickleIterator; | 29 class PickleIterator; |
28 | 30 |
29 //////////////////////////////////////////////////////////////////////////////// | 31 //////////////////////////////////////////////////////////////////////////////// |
30 // These enums are used to facilitate deserialization of histograms from other | 32 // These enums are used to facilitate deserialization of histograms from other |
31 // processes into the browser. If you create another class that inherits from | 33 // processes into the browser. If you create another class that inherits from |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 // aggregation should not take place (as we would be aggregating back into | 74 // aggregation should not take place (as we would be aggregating back into |
73 // the source histogram!). | 75 // the source histogram!). |
74 kIPCSerializationSourceFlag = 0x10, | 76 kIPCSerializationSourceFlag = 0x10, |
75 | 77 |
76 // Indicates that a callback exists for when a new sample is recorded on | 78 // Indicates that a callback exists for when a new sample is recorded on |
77 // this histogram. We store this as a flag with the histogram since | 79 // this histogram. We store this as a flag with the histogram since |
78 // histograms can be in performance critical code, and this allows us | 80 // histograms can be in performance critical code, and this allows us |
79 // to shortcut looking up the callback if it doesn't exist. | 81 // to shortcut looking up the callback if it doesn't exist. |
80 kCallbackExists = 0x20, | 82 kCallbackExists = 0x20, |
81 | 83 |
84 // Indicates that the histogram is held in "persistent" memory and may | |
85 // be accessible between processes. This is only possible if such a | |
86 // memory segment has been created/attached, used to create a Persistent- | |
87 // MemoryAllocator, and that loaded into the Histogram module before this | |
88 // histogram is created. | |
89 kIsPersistent = 0x40, | |
90 | |
82 // Only for Histogram and its sub classes: fancy bucket-naming support. | 91 // Only for Histogram and its sub classes: fancy bucket-naming support. |
83 kHexRangePrintingFlag = 0x8000, | 92 kHexRangePrintingFlag = 0x8000, |
84 }; | 93 }; |
85 | 94 |
86 // Histogram data inconsistency types. | 95 // Histogram data inconsistency types. |
87 enum Inconsistency { | 96 enum Inconsistency { |
88 NO_INCONSISTENCIES = 0x0, | 97 NO_INCONSISTENCIES = 0x0, |
89 RANGE_CHECKSUM_ERROR = 0x1, | 98 RANGE_CHECKSUM_ERROR = 0x1, |
90 BUCKET_ORDER_ERROR = 0x2, | 99 BUCKET_ORDER_ERROR = 0x2, |
91 COUNT_HIGH_ERROR = 0x4, | 100 COUNT_HIGH_ERROR = 0x4, |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 | 160 |
152 // The following methods provide graphical histogram displays. | 161 // The following methods provide graphical histogram displays. |
153 virtual void WriteHTMLGraph(std::string* output) const = 0; | 162 virtual void WriteHTMLGraph(std::string* output) const = 0; |
154 virtual void WriteAscii(std::string* output) const = 0; | 163 virtual void WriteAscii(std::string* output) const = 0; |
155 | 164 |
156 // Produce a JSON representation of the histogram. This is implemented with | 165 // Produce a JSON representation of the histogram. This is implemented with |
157 // the help of GetParameters and GetCountAndBucketData; overwrite them to | 166 // the help of GetParameters and GetCountAndBucketData; overwrite them to |
158 // customize the output. | 167 // customize the output. |
159 void WriteJSON(std::string* output) const; | 168 void WriteJSON(std::string* output) const; |
160 | 169 |
170 // Access a PersistentMemoryAllocator for storing histograms in space that | |
171 // can be persisted or shared between processes. There is only ever one | |
172 // allocator for all such histograms created by a single process though one | |
173 // process may access the histograms created by other processes if it has a | |
174 // handle on its memory segment. This takes ownership of the object and | |
175 // should not be changed without great care as it is likely that there will | |
176 // be pointers to data held in that space. | |
177 static void SetDefaultPersistentMemoryAllocator(PersistentMemoryAllocator* a); | |
178 static PersistentMemoryAllocator* GetDefaultPersistentMemoryAllocator() { | |
179 return allocator_.get(); | |
180 } | |
181 | |
182 // This access to the persistent allocator is only for testing; it extracts | |
183 // the current allocator completely. This allows easy creation of histograms | |
184 // within persistent memory segments which can then be extracted and used | |
185 // in other ways. | |
186 static PersistentMemoryAllocator* ReleaseDefaultPersistentMemoryAllocator() { | |
187 return allocator_.release(); | |
188 }; | |
189 | |
190 // Recreate a Histogram from data held in persistent memory. Though this | |
191 // object will be local to the current process, the sample data will be | |
192 // shared with all other threads referencing it. This method takes a |ref| | |
193 // to the top- level histogram data and the |allocator| on which it is found. | |
194 // This method will return nullptr if any problem is detected with the data. | |
195 // The |allocator| may or may not be the same as the PersistentMemoryAllocator | |
196 // set for general use so that this method can be used to extract Histograms | |
197 // from persistent memory segments other than the default place that this | |
198 // process is creating its own histograms. The caller must take ownership of | |
199 // the returned object and destroy it when no longer needed. | |
200 static HistogramBase* GetPersistentHistogram( | |
201 PersistentMemoryAllocator* allocator, | |
202 int32_t ref); | |
203 | |
204 // Get the next histogram in persistent data based on iterator. The caller | |
205 // must take ownership of the returned object and destroy it when no longer | |
206 // needed. | |
207 static HistogramBase* GetNextPersistentHistogram( | |
208 PersistentMemoryAllocator* allocator, | |
209 PersistentMemoryAllocator::Iterator* iter); | |
Alexei Svitkine (slow)
2015/12/04 18:27:36
Can you put these in a separate file? HistogramBas
bcwhite
2015/12/07 23:27:19
Done.
| |
210 | |
161 protected: | 211 protected: |
212 struct PersistentHistogramData; | |
213 | |
214 // Global allocator used when histograms should be persistent. | |
215 static scoped_ptr<PersistentMemoryAllocator> allocator_; | |
216 | |
162 // Subclasses should implement this function to make SerializeInfo work. | 217 // Subclasses should implement this function to make SerializeInfo work. |
163 virtual bool SerializeInfoImpl(base::Pickle* pickle) const = 0; | 218 virtual bool SerializeInfoImpl(base::Pickle* pickle) const = 0; |
164 | 219 |
165 // Writes information about the construction parameters in |params|. | 220 // Writes information about the construction parameters in |params|. |
166 virtual void GetParameters(DictionaryValue* params) const = 0; | 221 virtual void GetParameters(DictionaryValue* params) const = 0; |
167 | 222 |
168 // Writes information about the current (non-empty) buckets and their sample | 223 // Writes information about the current (non-empty) buckets and their sample |
169 // counts to |buckets|, the total sample count to |count| and the total sum | 224 // counts to |buckets|, the total sample count to |count| and the total sum |
170 // to |sum|. | 225 // to |sum|. |
171 virtual void GetCountAndBucketData(Count* count, | 226 virtual void GetCountAndBucketData(Count* count, |
(...skipping 11 matching lines...) Expand all Loading... | |
183 // Write textual description of the bucket contents (relative to histogram). | 238 // Write textual description of the bucket contents (relative to histogram). |
184 // Output is the count in the buckets, as well as the percentage. | 239 // Output is the count in the buckets, as well as the percentage. |
185 void WriteAsciiBucketValue(Count current, | 240 void WriteAsciiBucketValue(Count current, |
186 double scaled_sum, | 241 double scaled_sum, |
187 std::string* output) const; | 242 std::string* output) const; |
188 | 243 |
189 // Retrieves the callback for this histogram, if one exists, and runs it | 244 // Retrieves the callback for this histogram, if one exists, and runs it |
190 // passing |sample| as the parameter. | 245 // passing |sample| as the parameter. |
191 void FindAndRunCallback(Sample sample) const; | 246 void FindAndRunCallback(Sample sample) const; |
192 | 247 |
248 // Create a histogram based on persistent data. | |
249 static HistogramBase* CreatePersistentHistogram( | |
250 PersistentMemoryAllocator* allocator, | |
251 PersistentHistogramData* histogram_data); | |
252 | |
253 // Allocate a new persistent histogram. This does *not* make the object | |
254 // iterable in the allocator; call MakeIterable(ref) directly if that is | |
255 // desired. | |
256 static HistogramBase* AllocatePersistentHistogram( | |
257 PersistentMemoryAllocator* allocator, | |
258 HistogramType histogram_type, | |
259 const std::string& name, | |
260 int minimum, | |
261 int maximum, | |
262 const BucketRanges* bucket_ranges, | |
263 int32 flags, | |
264 PersistentMemoryAllocator::Reference* ref_ptr); | |
265 | |
266 // Import new histograms from attached PersistentMemoryAllocator. It's | |
267 // possible for other processes to create histograms in the attached memory | |
268 // segment; this adds those to the internal list of known histograms to | |
269 // avoid creating duplicates that would have to merged during reporting. | |
270 // Every call to this method resumes from the last entry it saw so it costs | |
271 // nothing if nothing new has been added. | |
272 static void ImportPersistentHistograms(); | |
273 | |
193 private: | 274 private: |
194 const std::string histogram_name_; | 275 const std::string histogram_name_; |
195 AtomicCount flags_; | 276 AtomicCount flags_; |
196 | 277 |
197 DISALLOW_COPY_AND_ASSIGN(HistogramBase); | 278 DISALLOW_COPY_AND_ASSIGN(HistogramBase); |
198 }; | 279 }; |
199 | 280 |
200 } // namespace base | 281 } // namespace base |
201 | 282 |
202 #endif // BASE_METRICS_HISTOGRAM_BASE_H_ | 283 #endif // BASE_METRICS_HISTOGRAM_BASE_H_ |
OLD | NEW |