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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 #include "base/metrics/histogram_macros.h" | 85 #include "base/metrics/histogram_macros.h" |
86 #include "base/metrics/histogram_samples.h" | 86 #include "base/metrics/histogram_samples.h" |
87 #include "base/time/time.h" | 87 #include "base/time/time.h" |
88 | 88 |
89 namespace base { | 89 namespace base { |
90 | 90 |
91 class BooleanHistogram; | 91 class BooleanHistogram; |
92 class CustomHistogram; | 92 class CustomHistogram; |
93 class Histogram; | 93 class Histogram; |
94 class LinearHistogram; | 94 class LinearHistogram; |
| 95 class PersistentMemoryAllocator; |
95 class Pickle; | 96 class Pickle; |
96 class PickleIterator; | 97 class PickleIterator; |
97 class SampleVector; | 98 class SampleVector; |
98 | 99 |
99 class BASE_EXPORT Histogram : public HistogramBase { | 100 class BASE_EXPORT Histogram : public HistogramBase { |
100 public: | 101 public: |
101 // Initialize maximum number of buckets in histograms as 16,384. | 102 // Initialize maximum number of buckets in histograms as 16,384. |
102 static const size_t kBucketCount_MAX; | 103 static const size_t kBucketCount_MAX; |
103 | 104 |
104 typedef std::vector<Count> Counts; | 105 typedef std::vector<Count> Counts; |
(...skipping 26 matching lines...) Expand all Loading... |
131 Sample minimum, | 132 Sample minimum, |
132 Sample maximum, | 133 Sample maximum, |
133 size_t bucket_count, | 134 size_t bucket_count, |
134 int32_t flags); | 135 int32_t flags); |
135 static HistogramBase* FactoryTimeGet(const char* name, | 136 static HistogramBase* FactoryTimeGet(const char* name, |
136 base::TimeDelta minimum, | 137 base::TimeDelta minimum, |
137 base::TimeDelta maximum, | 138 base::TimeDelta maximum, |
138 size_t bucket_count, | 139 size_t bucket_count, |
139 int32_t flags); | 140 int32_t flags); |
140 | 141 |
| 142 // Get a histogram using data in persistent storage. |
| 143 static HistogramBase* PersistentGet(const char* name, |
| 144 Sample minimum, |
| 145 Sample maximum, |
| 146 const BucketRanges* ranges, |
| 147 HistogramBase::AtomicCount* counts, |
| 148 size_t counts_size, |
| 149 HistogramSamples::Metadata* meta); |
| 150 |
141 static void InitializeBucketRanges(Sample minimum, | 151 static void InitializeBucketRanges(Sample minimum, |
142 Sample maximum, | 152 Sample maximum, |
143 BucketRanges* ranges); | 153 BucketRanges* ranges); |
144 | 154 |
145 // This constant if for FindCorruption. Since snapshots of histograms are | 155 // This constant if for FindCorruption. Since snapshots of histograms are |
146 // taken asynchronously relative to sampling, and our counting code currently | 156 // taken asynchronously relative to sampling, and our counting code currently |
147 // does not prevent race conditions, it is pretty likely that we'll catch a | 157 // does not prevent race conditions, it is pretty likely that we'll catch a |
148 // redundant count that doesn't match the sample count. We allow for a | 158 // redundant count that doesn't match the sample count. We allow for a |
149 // certain amount of slop before flagging this as an inconsistency. Even with | 159 // certain amount of slop before flagging this as an inconsistency. Even with |
150 // an inconsistency, we'll snapshot it again (for UMA in about a half hour), | 160 // an inconsistency, we'll snapshot it again (for UMA in about a half hour), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 size_t expected_bucket_count) const override; | 196 size_t expected_bucket_count) const override; |
187 void Add(Sample value) override; | 197 void Add(Sample value) override; |
188 void AddCount(Sample value, int count) override; | 198 void AddCount(Sample value, int count) override; |
189 scoped_ptr<HistogramSamples> SnapshotSamples() const override; | 199 scoped_ptr<HistogramSamples> SnapshotSamples() const override; |
190 void AddSamples(const HistogramSamples& samples) override; | 200 void AddSamples(const HistogramSamples& samples) override; |
191 bool AddSamplesFromPickle(base::PickleIterator* iter) override; | 201 bool AddSamplesFromPickle(base::PickleIterator* iter) override; |
192 void WriteHTMLGraph(std::string* output) const override; | 202 void WriteHTMLGraph(std::string* output) const override; |
193 void WriteAscii(std::string* output) const override; | 203 void WriteAscii(std::string* output) const override; |
194 | 204 |
195 protected: | 205 protected: |
| 206 // This class, defined entirely within the .cc file, contains all the |
| 207 // common logic for building a Histogram and can be overridden by more |
| 208 // specific types to alter details of how the creation is done. It is |
| 209 // defined as an embedded class (rather than an anonymous one) so it |
| 210 // can access the protected constructors. |
| 211 class Factory; |
| 212 |
196 // |ranges| should contain the underflow and overflow buckets. See top | 213 // |ranges| should contain the underflow and overflow buckets. See top |
197 // comments for example. | 214 // comments for example. |
198 Histogram(const std::string& name, | 215 Histogram(const std::string& name, |
199 Sample minimum, | 216 Sample minimum, |
200 Sample maximum, | 217 Sample maximum, |
201 const BucketRanges* ranges); | 218 const BucketRanges* ranges); |
202 | 219 |
| 220 // Traditionally, histograms allocate their own memory for the bucket |
| 221 // vector but "shared" histograms use memory regions allocated from a |
| 222 // special memory segment that is passed in here. It is assumed that |
| 223 // the life of this memory is managed externally and exceeds the lifetime |
| 224 // of this object. Practically, this memory is never released until the |
| 225 // process exits and the OS cleans it up. |
| 226 Histogram(const std::string& name, |
| 227 Sample minimum, |
| 228 Sample maximum, |
| 229 const BucketRanges* ranges, |
| 230 HistogramBase::AtomicCount* counts, |
| 231 size_t counts_size, |
| 232 HistogramSamples::Metadata* meta); |
| 233 |
203 ~Histogram() override; | 234 ~Histogram() override; |
204 | 235 |
205 // HistogramBase implementation: | 236 // HistogramBase implementation: |
206 bool SerializeInfoImpl(base::Pickle* pickle) const override; | 237 bool SerializeInfoImpl(base::Pickle* pickle) const override; |
207 | 238 |
208 // Method to override to skip the display of the i'th bucket if it's empty. | 239 // Method to override to skip the display of the i'th bucket if it's empty. |
209 virtual bool PrintEmptyBucket(size_t index) const; | 240 virtual bool PrintEmptyBucket(size_t index) const; |
210 | 241 |
211 // Get normalized size, relative to the ranges(i). | 242 // Get normalized size, relative to the ranges(i). |
212 virtual double GetBucketSize(Count current, size_t i) const; | 243 virtual double GetBucketSize(Count current, size_t i) const; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 Sample minimum, | 337 Sample minimum, |
307 Sample maximum, | 338 Sample maximum, |
308 size_t bucket_count, | 339 size_t bucket_count, |
309 int32_t flags); | 340 int32_t flags); |
310 static HistogramBase* FactoryTimeGet(const char* name, | 341 static HistogramBase* FactoryTimeGet(const char* name, |
311 TimeDelta minimum, | 342 TimeDelta minimum, |
312 TimeDelta maximum, | 343 TimeDelta maximum, |
313 size_t bucket_count, | 344 size_t bucket_count, |
314 int32_t flags); | 345 int32_t flags); |
315 | 346 |
| 347 // Get a histogram using data in persistent storage. |
| 348 static HistogramBase* PersistentGet(const char* name, |
| 349 Sample minimum, |
| 350 Sample maximum, |
| 351 const BucketRanges* ranges, |
| 352 HistogramBase::AtomicCount* counts, |
| 353 size_t counts_size, |
| 354 HistogramSamples::Metadata* meta); |
| 355 |
316 struct DescriptionPair { | 356 struct DescriptionPair { |
317 Sample sample; | 357 Sample sample; |
318 const char* description; // Null means end of a list of pairs. | 358 const char* description; // Null means end of a list of pairs. |
319 }; | 359 }; |
320 | 360 |
321 // Create a LinearHistogram and store a list of number/text values for use in | 361 // Create a LinearHistogram and store a list of number/text values for use in |
322 // writing the histogram graph. | 362 // writing the histogram graph. |
323 // |descriptions| can be NULL, which means no special descriptions to set. If | 363 // |descriptions| can be NULL, which means no special descriptions to set. If |
324 // it's not NULL, the last element in the array must has a NULL in its | 364 // it's not NULL, the last element in the array must has a NULL in its |
325 // "description" field. | 365 // "description" field. |
326 static HistogramBase* FactoryGetWithRangeDescription( | 366 static HistogramBase* FactoryGetWithRangeDescription( |
327 const std::string& name, | 367 const std::string& name, |
328 Sample minimum, | 368 Sample minimum, |
329 Sample maximum, | 369 Sample maximum, |
330 size_t bucket_count, | 370 size_t bucket_count, |
331 int32_t flags, | 371 int32_t flags, |
332 const DescriptionPair descriptions[]); | 372 const DescriptionPair descriptions[]); |
333 | 373 |
334 static void InitializeBucketRanges(Sample minimum, | 374 static void InitializeBucketRanges(Sample minimum, |
335 Sample maximum, | 375 Sample maximum, |
336 BucketRanges* ranges); | 376 BucketRanges* ranges); |
337 | 377 |
338 // Overridden from Histogram: | 378 // Overridden from Histogram: |
339 HistogramType GetHistogramType() const override; | 379 HistogramType GetHistogramType() const override; |
340 | 380 |
341 protected: | 381 protected: |
| 382 class Factory; |
| 383 |
342 LinearHistogram(const std::string& name, | 384 LinearHistogram(const std::string& name, |
343 Sample minimum, | 385 Sample minimum, |
344 Sample maximum, | 386 Sample maximum, |
345 const BucketRanges* ranges); | 387 const BucketRanges* ranges); |
346 | 388 |
| 389 LinearHistogram(const std::string& name, |
| 390 Sample minimum, |
| 391 Sample maximum, |
| 392 const BucketRanges* ranges, |
| 393 HistogramBase::AtomicCount* counts, |
| 394 size_t counts_size, |
| 395 HistogramSamples::Metadata* meta); |
| 396 |
347 double GetBucketSize(Count current, size_t i) const override; | 397 double GetBucketSize(Count current, size_t i) const override; |
348 | 398 |
349 // If we have a description for a bucket, then return that. Otherwise | 399 // If we have a description for a bucket, then return that. Otherwise |
350 // let parent class provide a (numeric) description. | 400 // let parent class provide a (numeric) description. |
351 const std::string GetAsciiBucketRange(size_t i) const override; | 401 const std::string GetAsciiBucketRange(size_t i) const override; |
352 | 402 |
353 // Skip printing of name for numeric range if we have a name (and if this is | 403 // Skip printing of name for numeric range if we have a name (and if this is |
354 // an empty bucket). | 404 // an empty bucket). |
355 bool PrintEmptyBucket(size_t index) const override; | 405 bool PrintEmptyBucket(size_t index) const override; |
356 | 406 |
(...skipping 16 matching lines...) Expand all Loading... |
373 // BooleanHistogram is a histogram for booleans. | 423 // BooleanHistogram is a histogram for booleans. |
374 class BASE_EXPORT BooleanHistogram : public LinearHistogram { | 424 class BASE_EXPORT BooleanHistogram : public LinearHistogram { |
375 public: | 425 public: |
376 static HistogramBase* FactoryGet(const std::string& name, int32_t flags); | 426 static HistogramBase* FactoryGet(const std::string& name, int32_t flags); |
377 | 427 |
378 // Overload of the above function that takes a const char* |name| param, | 428 // Overload of the above function that takes a const char* |name| param, |
379 // to avoid code bloat from the std::string constructor being inlined into | 429 // to avoid code bloat from the std::string constructor being inlined into |
380 // call sites. | 430 // call sites. |
381 static HistogramBase* FactoryGet(const char* name, int32_t flags); | 431 static HistogramBase* FactoryGet(const char* name, int32_t flags); |
382 | 432 |
| 433 // Get a histogram using data in persistent storage. |
| 434 static HistogramBase* PersistentGet(const char* name, |
| 435 const BucketRanges* ranges, |
| 436 HistogramBase::AtomicCount* counts, |
| 437 HistogramSamples::Metadata* meta); |
| 438 |
383 HistogramType GetHistogramType() const override; | 439 HistogramType GetHistogramType() const override; |
384 | 440 |
| 441 protected: |
| 442 class Factory; |
| 443 |
385 private: | 444 private: |
386 BooleanHistogram(const std::string& name, const BucketRanges* ranges); | 445 BooleanHistogram(const std::string& name, const BucketRanges* ranges); |
| 446 BooleanHistogram(const std::string& name, |
| 447 const BucketRanges* ranges, |
| 448 HistogramBase::AtomicCount* counts, |
| 449 HistogramSamples::Metadata* meta); |
387 | 450 |
388 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( | 451 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( |
389 base::PickleIterator* iter); | 452 base::PickleIterator* iter); |
390 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); | 453 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); |
391 | 454 |
392 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram); | 455 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram); |
393 }; | 456 }; |
394 | 457 |
395 //------------------------------------------------------------------------------ | 458 //------------------------------------------------------------------------------ |
396 | 459 |
397 // CustomHistogram is a histogram for a set of custom integers. | 460 // CustomHistogram is a histogram for a set of custom integers. |
398 class BASE_EXPORT CustomHistogram : public Histogram { | 461 class BASE_EXPORT CustomHistogram : public Histogram { |
399 public: | 462 public: |
400 // |custom_ranges| contains a vector of limits on ranges. Each limit should be | 463 // |custom_ranges| contains a vector of limits on ranges. Each limit should be |
401 // > 0 and < kSampleType_MAX. (Currently 0 is still accepted for backward | 464 // > 0 and < kSampleType_MAX. (Currently 0 is still accepted for backward |
402 // compatibility). The limits can be unordered or contain duplication, but | 465 // compatibility). The limits can be unordered or contain duplication, but |
403 // client should not depend on this. | 466 // client should not depend on this. |
404 static HistogramBase* FactoryGet(const std::string& name, | 467 static HistogramBase* FactoryGet(const std::string& name, |
405 const std::vector<Sample>& custom_ranges, | 468 const std::vector<Sample>& custom_ranges, |
406 int32_t flags); | 469 int32_t flags); |
407 | 470 |
408 // Overload of the above function that takes a const char* |name| param, | 471 // Overload of the above function that takes a const char* |name| param, |
409 // to avoid code bloat from the std::string constructor being inlined into | 472 // to avoid code bloat from the std::string constructor being inlined into |
410 // call sites. | 473 // call sites. |
411 static HistogramBase* FactoryGet(const char* name, | 474 static HistogramBase* FactoryGet(const char* name, |
412 const std::vector<Sample>& custom_ranges, | 475 const std::vector<Sample>& custom_ranges, |
413 int32_t flags); | 476 int32_t flags); |
414 | 477 |
| 478 // Get a histogram using data in persistent storage. |
| 479 static HistogramBase* PersistentGet(const char* name, |
| 480 const BucketRanges* ranges, |
| 481 HistogramBase::AtomicCount* counts, |
| 482 size_t counts_size, |
| 483 HistogramSamples::Metadata* meta); |
| 484 |
415 // Overridden from Histogram: | 485 // Overridden from Histogram: |
416 HistogramType GetHistogramType() const override; | 486 HistogramType GetHistogramType() const override; |
417 | 487 |
418 // Helper method for transforming an array of valid enumeration values | 488 // Helper method for transforming an array of valid enumeration values |
419 // to the std::vector<int> expected by UMA_HISTOGRAM_CUSTOM_ENUMERATION. | 489 // to the std::vector<int> expected by UMA_HISTOGRAM_CUSTOM_ENUMERATION. |
420 // This function ensures that a guard bucket exists right after any | 490 // This function ensures that a guard bucket exists right after any |
421 // valid sample value (unless the next higher sample is also a valid value), | 491 // valid sample value (unless the next higher sample is also a valid value), |
422 // so that invalid samples never fall into the same bucket as valid samples. | 492 // so that invalid samples never fall into the same bucket as valid samples. |
423 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges. | 493 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges. |
424 static std::vector<Sample> ArrayToCustomRanges(const Sample* values, | 494 static std::vector<Sample> ArrayToCustomRanges(const Sample* values, |
425 size_t num_values); | 495 size_t num_values); |
426 protected: | 496 protected: |
| 497 class Factory; |
| 498 |
427 CustomHistogram(const std::string& name, | 499 CustomHistogram(const std::string& name, |
428 const BucketRanges* ranges); | 500 const BucketRanges* ranges); |
429 | 501 |
| 502 CustomHistogram(const std::string& name, |
| 503 const BucketRanges* ranges, |
| 504 HistogramBase::AtomicCount* counts, |
| 505 size_t counts_size, |
| 506 HistogramSamples::Metadata* meta); |
| 507 |
430 // HistogramBase implementation: | 508 // HistogramBase implementation: |
431 bool SerializeInfoImpl(base::Pickle* pickle) const override; | 509 bool SerializeInfoImpl(base::Pickle* pickle) const override; |
432 | 510 |
433 double GetBucketSize(Count current, size_t i) const override; | 511 double GetBucketSize(Count current, size_t i) const override; |
434 | 512 |
435 private: | 513 private: |
436 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( | 514 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( |
437 base::PickleIterator* iter); | 515 base::PickleIterator* iter); |
438 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); | 516 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); |
439 | 517 |
440 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); | 518 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); |
441 static BucketRanges* CreateBucketRangesFromCustomRanges( | |
442 const std::vector<Sample>& custom_ranges); | |
443 | 519 |
444 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); | 520 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); |
445 }; | 521 }; |
446 | 522 |
447 } // namespace base | 523 } // namespace base |
448 | 524 |
449 #endif // BASE_METRICS_HISTOGRAM_H_ | 525 #endif // BASE_METRICS_HISTOGRAM_H_ |
OLD | NEW |