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 // Histogram is an object that aggregates statistics, and can summarize them in | 5 // Histogram is an object that aggregates statistics, and can summarize them in |
6 // various forms, including ASCII graphical, HTML, and numerically (as a | 6 // various forms, including ASCII graphical, HTML, and numerically (as a |
7 // vector of numbers corresponding to each of the aggregating buckets). | 7 // vector of numbers corresponding to each of the aggregating buckets). |
8 | 8 |
9 // It supports calls to accumulate either time intervals (which are processed | 9 // It supports calls to accumulate either time intervals (which are processed |
10 // as integral number of milliseconds), or arbitrary integral units. | 10 // as integral number of milliseconds), or arbitrary integral units. |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 #include "base/metrics/histogram_macros.h" | 82 #include "base/metrics/histogram_macros.h" |
83 #include "base/metrics/histogram_samples.h" | 83 #include "base/metrics/histogram_samples.h" |
84 #include "base/time/time.h" | 84 #include "base/time/time.h" |
85 | 85 |
86 namespace base { | 86 namespace base { |
87 | 87 |
88 class BooleanHistogram; | 88 class BooleanHistogram; |
89 class CustomHistogram; | 89 class CustomHistogram; |
90 class Histogram; | 90 class Histogram; |
91 class LinearHistogram; | 91 class LinearHistogram; |
92 class PersistentMemoryAllocator; | |
92 class Pickle; | 93 class Pickle; |
93 class PickleIterator; | 94 class PickleIterator; |
94 class SampleVector; | 95 class SampleVector; |
95 | 96 |
97 struct PersistentHistogramData; | |
98 | |
96 class BASE_EXPORT Histogram : public HistogramBase { | 99 class BASE_EXPORT Histogram : public HistogramBase { |
97 public: | 100 public: |
98 // Initialize maximum number of buckets in histograms as 16,384. | 101 // Initialize maximum number of buckets in histograms as 16,384. |
99 static const size_t kBucketCount_MAX; | 102 static const size_t kBucketCount_MAX; |
100 | 103 |
101 typedef std::vector<Count> Counts; | 104 typedef std::vector<Count> Counts; |
102 | 105 |
103 //---------------------------------------------------------------------------- | 106 //---------------------------------------------------------------------------- |
104 // For a valid histogram, input should follow these restrictions: | 107 // For a valid histogram, input should follow these restrictions: |
105 // minimum > 0 (if a minimum below 1 is specified, it will implicitly be | 108 // minimum > 0 (if a minimum below 1 is specified, it will implicitly be |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 size_t expected_bucket_count) const override; | 186 size_t expected_bucket_count) const override; |
184 void Add(Sample value) override; | 187 void Add(Sample value) override; |
185 void AddCount(Sample value, int count) override; | 188 void AddCount(Sample value, int count) override; |
186 scoped_ptr<HistogramSamples> SnapshotSamples() const override; | 189 scoped_ptr<HistogramSamples> SnapshotSamples() const override; |
187 void AddSamples(const HistogramSamples& samples) override; | 190 void AddSamples(const HistogramSamples& samples) override; |
188 bool AddSamplesFromPickle(base::PickleIterator* iter) override; | 191 bool AddSamplesFromPickle(base::PickleIterator* iter) override; |
189 void WriteHTMLGraph(std::string* output) const override; | 192 void WriteHTMLGraph(std::string* output) const override; |
190 void WriteAscii(std::string* output) const override; | 193 void WriteAscii(std::string* output) const override; |
191 | 194 |
192 protected: | 195 protected: |
196 class Factory; | |
197 | |
193 // |ranges| should contain the underflow and overflow buckets. See top | 198 // |ranges| should contain the underflow and overflow buckets. See top |
194 // comments for example. | 199 // comments for example. |
195 Histogram(const std::string& name, | 200 Histogram(const std::string& name, |
196 Sample minimum, | 201 Sample minimum, |
197 Sample maximum, | 202 Sample maximum, |
198 const BucketRanges* ranges); | 203 const BucketRanges* ranges); |
199 | 204 |
205 // Traditionally, histograms allocate their own memory for the bucket | |
206 // vector but "shared" histograms use memory regions allocated from a | |
207 // special memory segment that is passed in here. It is assumed that | |
208 // the life of this memory is managed externally and exceeds the lifetime | |
209 // of this object. Practically, this memory is never released until the | |
210 // process exits and the OS cleans it up. | |
211 Histogram(const std::string& name, | |
212 Sample minimum, | |
213 Sample maximum, | |
214 const BucketRanges* ranges, | |
215 HistogramBase::AtomicCount* counts, | |
216 size_t counts_size, | |
217 HistogramSamples::Metadata* meta); | |
218 | |
200 ~Histogram() override; | 219 ~Histogram() override; |
201 | 220 |
202 // HistogramBase implementation: | 221 // HistogramBase implementation: |
203 bool SerializeInfoImpl(base::Pickle* pickle) const override; | 222 bool SerializeInfoImpl(base::Pickle* pickle) const override; |
204 | 223 |
205 // Method to override to skip the display of the i'th bucket if it's empty. | 224 // Method to override to skip the display of the i'th bucket if it's empty. |
206 virtual bool PrintEmptyBucket(size_t index) const; | 225 virtual bool PrintEmptyBucket(size_t index) const; |
207 | 226 |
208 // Get normalized size, relative to the ranges(i). | 227 // Get normalized size, relative to the ranges(i). |
209 virtual double GetBucketSize(Count current, size_t i) const; | 228 virtual double GetBucketSize(Count current, size_t i) const; |
210 | 229 |
211 // Return a string description of what goes in a given bucket. | 230 // Return a string description of what goes in a given bucket. |
212 // Most commonly this is the numeric value, but in derived classes it may | 231 // Most commonly this is the numeric value, but in derived classes it may |
213 // be a name (or string description) given to the bucket. | 232 // be a name (or string description) given to the bucket. |
214 virtual const std::string GetAsciiBucketRange(size_t it) const; | 233 virtual const std::string GetAsciiBucketRange(size_t it) const; |
215 | 234 |
216 private: | 235 private: |
217 // Allow tests to corrupt our innards for testing purposes. | 236 // Allow tests to corrupt our innards for testing purposes. |
218 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BoundsTest); | 237 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BoundsTest); |
219 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BucketPlacementTest); | 238 FRIEND_TEST_ALL_PREFIXES(HistogramTest, BucketPlacementTest); |
220 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptBucketBounds); | 239 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptBucketBounds); |
221 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts); | 240 FRIEND_TEST_ALL_PREFIXES(HistogramTest, CorruptSampleCounts); |
222 FRIEND_TEST_ALL_PREFIXES(HistogramTest, NameMatchTest); | 241 FRIEND_TEST_ALL_PREFIXES(HistogramTest, NameMatchTest); |
223 FRIEND_TEST_ALL_PREFIXES(HistogramTest, AddCountTest); | 242 FRIEND_TEST_ALL_PREFIXES(HistogramTest, AddCountTest); |
224 | 243 |
225 friend class StatisticsRecorder; // To allow it to delete duplicates. | 244 friend class StatisticsRecorder; // To allow it to delete duplicates. |
226 friend class StatisticsRecorderTest; | 245 friend class StatisticsRecorderTest; |
227 | 246 |
247 friend BASE_EXPORT HistogramBase* CreatePersistentHistogram( | |
248 PersistentMemoryAllocator* allocator, | |
249 PersistentHistogramData* histogram_data); | |
228 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( | 250 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( |
229 base::PickleIterator* iter); | 251 base::PickleIterator* iter); |
230 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); | 252 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); |
231 | 253 |
232 // Implementation of SnapshotSamples function. | 254 // Implementation of SnapshotSamples function. |
233 scoped_ptr<SampleVector> SnapshotSampleVector() const; | 255 scoped_ptr<SampleVector> SnapshotSampleVector() const; |
234 | 256 |
235 //---------------------------------------------------------------------------- | 257 //---------------------------------------------------------------------------- |
236 // Helpers for emitting Ascii graphic. Each method appends data to output. | 258 // Helpers for emitting Ascii graphic. Each method appends data to output. |
237 | 259 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
327 const DescriptionPair descriptions[]); | 349 const DescriptionPair descriptions[]); |
328 | 350 |
329 static void InitializeBucketRanges(Sample minimum, | 351 static void InitializeBucketRanges(Sample minimum, |
330 Sample maximum, | 352 Sample maximum, |
331 BucketRanges* ranges); | 353 BucketRanges* ranges); |
332 | 354 |
333 // Overridden from Histogram: | 355 // Overridden from Histogram: |
334 HistogramType GetHistogramType() const override; | 356 HistogramType GetHistogramType() const override; |
335 | 357 |
336 protected: | 358 protected: |
359 class Factory; | |
360 | |
337 LinearHistogram(const std::string& name, | 361 LinearHistogram(const std::string& name, |
338 Sample minimum, | 362 Sample minimum, |
339 Sample maximum, | 363 Sample maximum, |
340 const BucketRanges* ranges); | 364 const BucketRanges* ranges); |
341 | 365 |
366 LinearHistogram(const std::string& name, | |
367 Sample minimum, | |
368 Sample maximum, | |
369 const BucketRanges* ranges, | |
370 HistogramBase::AtomicCount* counts, | |
371 size_t counts_size, | |
372 HistogramSamples::Metadata* meta); | |
373 | |
342 double GetBucketSize(Count current, size_t i) const override; | 374 double GetBucketSize(Count current, size_t i) const override; |
343 | 375 |
344 // If we have a description for a bucket, then return that. Otherwise | 376 // If we have a description for a bucket, then return that. Otherwise |
345 // let parent class provide a (numeric) description. | 377 // let parent class provide a (numeric) description. |
346 const std::string GetAsciiBucketRange(size_t i) const override; | 378 const std::string GetAsciiBucketRange(size_t i) const override; |
347 | 379 |
348 // Skip printing of name for numeric range if we have a name (and if this is | 380 // Skip printing of name for numeric range if we have a name (and if this is |
349 // an empty bucket). | 381 // an empty bucket). |
350 bool PrintEmptyBucket(size_t index) const override; | 382 bool PrintEmptyBucket(size_t index) const override; |
351 | 383 |
352 private: | 384 private: |
385 friend BASE_EXPORT HistogramBase* CreatePersistentHistogram( | |
386 PersistentMemoryAllocator* allocator, | |
387 PersistentHistogramData* histogram_data); | |
353 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( | 388 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( |
354 base::PickleIterator* iter); | 389 base::PickleIterator* iter); |
355 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); | 390 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); |
356 | 391 |
357 // For some ranges, we store a printable description of a bucket range. | 392 // For some ranges, we store a printable description of a bucket range. |
358 // If there is no description, then GetAsciiBucketRange() uses parent class | 393 // If there is no description, then GetAsciiBucketRange() uses parent class |
359 // to provide a description. | 394 // to provide a description. |
360 typedef std::map<Sample, std::string> BucketDescriptionMap; | 395 typedef std::map<Sample, std::string> BucketDescriptionMap; |
361 BucketDescriptionMap bucket_description_; | 396 BucketDescriptionMap bucket_description_; |
362 | 397 |
363 DISALLOW_COPY_AND_ASSIGN(LinearHistogram); | 398 DISALLOW_COPY_AND_ASSIGN(LinearHistogram); |
364 }; | 399 }; |
365 | 400 |
366 //------------------------------------------------------------------------------ | 401 //------------------------------------------------------------------------------ |
367 | 402 |
368 // BooleanHistogram is a histogram for booleans. | 403 // BooleanHistogram is a histogram for booleans. |
369 class BASE_EXPORT BooleanHistogram : public LinearHistogram { | 404 class BASE_EXPORT BooleanHistogram : public LinearHistogram { |
370 public: | 405 public: |
371 static HistogramBase* FactoryGet(const std::string& name, int32 flags); | 406 static HistogramBase* FactoryGet(const std::string& name, int32 flags); |
372 | 407 |
373 // Overload of the above function that takes a const char* |name| param, | 408 // Overload of the above function that takes a const char* |name| param, |
374 // to avoid code bloat from the std::string constructor being inlined into | 409 // to avoid code bloat from the std::string constructor being inlined into |
375 // call sites. | 410 // call sites. |
376 static HistogramBase* FactoryGet(const char* name, int32 flags); | 411 static HistogramBase* FactoryGet(const char* name, int32 flags); |
377 | 412 |
378 HistogramType GetHistogramType() const override; | 413 HistogramType GetHistogramType() const override; |
379 | 414 |
415 protected: | |
416 class Factory; | |
417 | |
380 private: | 418 private: |
381 BooleanHistogram(const std::string& name, const BucketRanges* ranges); | 419 BooleanHistogram(const std::string& name, const BucketRanges* ranges); |
420 BooleanHistogram(const std::string& name, | |
421 const BucketRanges* ranges, | |
422 HistogramBase::AtomicCount* counts, | |
423 HistogramSamples::Metadata* meta); | |
382 | 424 |
425 friend BASE_EXPORT HistogramBase* CreatePersistentHistogram( | |
426 PersistentMemoryAllocator* allocator, | |
427 PersistentHistogramData* histogram_data); | |
383 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( | 428 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( |
384 base::PickleIterator* iter); | 429 base::PickleIterator* iter); |
385 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); | 430 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); |
386 | 431 |
387 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram); | 432 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram); |
388 }; | 433 }; |
389 | 434 |
390 //------------------------------------------------------------------------------ | 435 //------------------------------------------------------------------------------ |
391 | 436 |
392 // CustomHistogram is a histogram for a set of custom integers. | 437 // CustomHistogram is a histogram for a set of custom integers. |
(...skipping 19 matching lines...) Expand all Loading... | |
412 | 457 |
413 // Helper method for transforming an array of valid enumeration values | 458 // Helper method for transforming an array of valid enumeration values |
414 // to the std::vector<int> expected by UMA_HISTOGRAM_CUSTOM_ENUMERATION. | 459 // to the std::vector<int> expected by UMA_HISTOGRAM_CUSTOM_ENUMERATION. |
415 // This function ensures that a guard bucket exists right after any | 460 // This function ensures that a guard bucket exists right after any |
416 // valid sample value (unless the next higher sample is also a valid value), | 461 // valid sample value (unless the next higher sample is also a valid value), |
417 // so that invalid samples never fall into the same bucket as valid samples. | 462 // so that invalid samples never fall into the same bucket as valid samples. |
418 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges. | 463 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges. |
419 static std::vector<Sample> ArrayToCustomRanges(const Sample* values, | 464 static std::vector<Sample> ArrayToCustomRanges(const Sample* values, |
420 size_t num_values); | 465 size_t num_values); |
421 protected: | 466 protected: |
467 class Factory; | |
Alexei Svitkine (slow)
2015/12/15 23:03:37
It's not clear to me why these have to be declared
bcwhite
2015/12/16 13:36:34
Everything related to a class has to be declared w
Alexei Svitkine (slow)
2015/12/16 16:33:49
I guess my point is that it could just be a free-s
bcwhite
2015/12/17 15:55:48
I tried that originally but such cannot access the
| |
468 | |
422 CustomHistogram(const std::string& name, | 469 CustomHistogram(const std::string& name, |
423 const BucketRanges* ranges); | 470 const BucketRanges* ranges); |
424 | 471 |
472 CustomHistogram(const std::string& name, | |
473 const BucketRanges* ranges, | |
474 HistogramBase::AtomicCount* counts, | |
475 size_t counts_size, | |
476 HistogramSamples::Metadata* meta); | |
477 | |
425 // HistogramBase implementation: | 478 // HistogramBase implementation: |
426 bool SerializeInfoImpl(base::Pickle* pickle) const override; | 479 bool SerializeInfoImpl(base::Pickle* pickle) const override; |
427 | 480 |
428 double GetBucketSize(Count current, size_t i) const override; | 481 double GetBucketSize(Count current, size_t i) const override; |
429 | 482 |
430 private: | 483 private: |
484 friend BASE_EXPORT HistogramBase* CreatePersistentHistogram( | |
485 PersistentMemoryAllocator* allocator, | |
486 PersistentHistogramData* histogram_data); | |
431 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( | 487 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( |
432 base::PickleIterator* iter); | 488 base::PickleIterator* iter); |
433 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); | 489 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); |
434 | 490 |
435 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); | 491 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); |
436 static BucketRanges* CreateBucketRangesFromCustomRanges( | |
437 const std::vector<Sample>& custom_ranges); | |
438 | 492 |
439 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); | 493 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); |
440 }; | 494 }; |
441 | 495 |
442 } // namespace base | 496 } // namespace base |
443 | 497 |
444 #endif // BASE_METRICS_HISTOGRAM_H_ | 498 #endif // BASE_METRICS_HISTOGRAM_H_ |
OLD | NEW |