| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 // that detects a uninitialized (NULL) pointer. The potentially racy | 59 // that detects a uninitialized (NULL) pointer. The potentially racy |
| 60 // initialization is not a problem as it is always set to point to the same | 60 // initialization is not a problem as it is always set to point to the same |
| 61 // value (i.e., the FactoryGet always returns the same value). FactoryGet | 61 // value (i.e., the FactoryGet always returns the same value). FactoryGet |
| 62 // is also completely thread safe, which results in a completely thread safe, | 62 // is also completely thread safe, which results in a completely thread safe, |
| 63 // and relatively fast, set of counters. To avoid races at shutdown, the static | 63 // and relatively fast, set of counters. To avoid races at shutdown, the static |
| 64 // pointer is NOT deleted, and we leak the histograms at process termination. | 64 // pointer is NOT deleted, and we leak the histograms at process termination. |
| 65 | 65 |
| 66 #ifndef BASE_METRICS_HISTOGRAM_H_ | 66 #ifndef BASE_METRICS_HISTOGRAM_H_ |
| 67 #define BASE_METRICS_HISTOGRAM_H_ | 67 #define BASE_METRICS_HISTOGRAM_H_ |
| 68 | 68 |
| 69 #include <stddef.h> |
| 70 #include <stdint.h> |
| 71 |
| 69 #include <map> | 72 #include <map> |
| 70 #include <string> | 73 #include <string> |
| 71 #include <vector> | 74 #include <vector> |
| 72 | 75 |
| 73 #include "base/base_export.h" | 76 #include "base/base_export.h" |
| 74 #include "base/basictypes.h" | |
| 75 #include "base/compiler_specific.h" | 77 #include "base/compiler_specific.h" |
| 76 #include "base/gtest_prod_util.h" | 78 #include "base/gtest_prod_util.h" |
| 77 #include "base/logging.h" | 79 #include "base/logging.h" |
| 80 #include "base/macros.h" |
| 78 #include "base/memory/scoped_ptr.h" | 81 #include "base/memory/scoped_ptr.h" |
| 79 #include "base/metrics/bucket_ranges.h" | 82 #include "base/metrics/bucket_ranges.h" |
| 80 #include "base/metrics/histogram_base.h" | 83 #include "base/metrics/histogram_base.h" |
| 81 // TODO(asvitkine): Migrate callers to to include this directly and remove this. | 84 // TODO(asvitkine): Migrate callers to to include this directly and remove this. |
| 82 #include "base/metrics/histogram_macros.h" | 85 #include "base/metrics/histogram_macros.h" |
| 83 #include "base/metrics/histogram_samples.h" | 86 #include "base/metrics/histogram_samples.h" |
| 84 #include "base/time/time.h" | 87 #include "base/time/time.h" |
| 85 | 88 |
| 86 namespace base { | 89 namespace base { |
| 87 | 90 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 107 // maximum > minimum | 110 // maximum > minimum |
| 108 // buckets > 2 [minimum buckets needed: underflow, overflow and the range] | 111 // buckets > 2 [minimum buckets needed: underflow, overflow and the range] |
| 109 // Additionally, | 112 // Additionally, |
| 110 // buckets <= (maximum - minimum + 2) - this is to ensure that we don't have | 113 // buckets <= (maximum - minimum + 2) - this is to ensure that we don't have |
| 111 // more buckets than the range of numbers; having more buckets than 1 per | 114 // more buckets than the range of numbers; having more buckets than 1 per |
| 112 // value in the range would be nonsensical. | 115 // value in the range would be nonsensical. |
| 113 static HistogramBase* FactoryGet(const std::string& name, | 116 static HistogramBase* FactoryGet(const std::string& name, |
| 114 Sample minimum, | 117 Sample minimum, |
| 115 Sample maximum, | 118 Sample maximum, |
| 116 size_t bucket_count, | 119 size_t bucket_count, |
| 117 int32 flags); | 120 int32_t flags); |
| 118 static HistogramBase* FactoryTimeGet(const std::string& name, | 121 static HistogramBase* FactoryTimeGet(const std::string& name, |
| 119 base::TimeDelta minimum, | 122 base::TimeDelta minimum, |
| 120 base::TimeDelta maximum, | 123 base::TimeDelta maximum, |
| 121 size_t bucket_count, | 124 size_t bucket_count, |
| 122 int32 flags); | 125 int32_t flags); |
| 123 | 126 |
| 124 // Overloads of the above two functions that take a const char* |name| param, | 127 // Overloads of the above two functions that take a const char* |name| param, |
| 125 // to avoid code bloat from the std::string constructor being inlined into | 128 // to avoid code bloat from the std::string constructor being inlined into |
| 126 // call sites. | 129 // call sites. |
| 127 static HistogramBase* FactoryGet(const char* name, | 130 static HistogramBase* FactoryGet(const char* name, |
| 128 Sample minimum, | 131 Sample minimum, |
| 129 Sample maximum, | 132 Sample maximum, |
| 130 size_t bucket_count, | 133 size_t bucket_count, |
| 131 int32 flags); | 134 int32_t flags); |
| 132 static HistogramBase* FactoryTimeGet(const char* name, | 135 static HistogramBase* FactoryTimeGet(const char* name, |
| 133 base::TimeDelta minimum, | 136 base::TimeDelta minimum, |
| 134 base::TimeDelta maximum, | 137 base::TimeDelta maximum, |
| 135 size_t bucket_count, | 138 size_t bucket_count, |
| 136 int32 flags); | 139 int32_t flags); |
| 137 | 140 |
| 138 static void InitializeBucketRanges(Sample minimum, | 141 static void InitializeBucketRanges(Sample minimum, |
| 139 Sample maximum, | 142 Sample maximum, |
| 140 BucketRanges* ranges); | 143 BucketRanges* ranges); |
| 141 | 144 |
| 142 // This constant if for FindCorruption. Since snapshots of histograms are | 145 // This constant if for FindCorruption. Since snapshots of histograms are |
| 143 // taken asynchronously relative to sampling, and our counting code currently | 146 // taken asynchronously relative to sampling, and our counting code currently |
| 144 // does not prevent race conditions, it is pretty likely that we'll catch a | 147 // does not prevent race conditions, it is pretty likely that we'll catch a |
| 145 // redundant count that doesn't match the sample count. We allow for a | 148 // redundant count that doesn't match the sample count. We allow for a |
| 146 // certain amount of slop before flagging this as an inconsistency. Even with | 149 // certain amount of slop before flagging this as an inconsistency. Even with |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 // Find out how large (graphically) the largest bucket will appear to be. | 245 // Find out how large (graphically) the largest bucket will appear to be. |
| 243 double GetPeakBucketSize(const SampleVector& samples) const; | 246 double GetPeakBucketSize(const SampleVector& samples) const; |
| 244 | 247 |
| 245 // Write a common header message describing this histogram. | 248 // Write a common header message describing this histogram. |
| 246 void WriteAsciiHeader(const SampleVector& samples, | 249 void WriteAsciiHeader(const SampleVector& samples, |
| 247 Count sample_count, | 250 Count sample_count, |
| 248 std::string* output) const; | 251 std::string* output) const; |
| 249 | 252 |
| 250 // Write information about previous, current, and next buckets. | 253 // Write information about previous, current, and next buckets. |
| 251 // Information such as cumulative percentage, etc. | 254 // Information such as cumulative percentage, etc. |
| 252 void WriteAsciiBucketContext(const int64 past, const Count current, | 255 void WriteAsciiBucketContext(const int64_t past, |
| 253 const int64 remaining, const size_t i, | 256 const Count current, |
| 257 const int64_t remaining, |
| 258 const size_t i, |
| 254 std::string* output) const; | 259 std::string* output) const; |
| 255 | 260 |
| 256 // WriteJSON calls these. | 261 // WriteJSON calls these. |
| 257 void GetParameters(DictionaryValue* params) const override; | 262 void GetParameters(DictionaryValue* params) const override; |
| 258 | 263 |
| 259 void GetCountAndBucketData(Count* count, | 264 void GetCountAndBucketData(Count* count, |
| 260 int64* sum, | 265 int64_t* sum, |
| 261 ListValue* buckets) const override; | 266 ListValue* buckets) const override; |
| 262 | 267 |
| 263 // Does not own this object. Should get from StatisticsRecorder. | 268 // Does not own this object. Should get from StatisticsRecorder. |
| 264 const BucketRanges* bucket_ranges_; | 269 const BucketRanges* bucket_ranges_; |
| 265 | 270 |
| 266 Sample declared_min_; // Less than this goes into the first bucket. | 271 Sample declared_min_; // Less than this goes into the first bucket. |
| 267 Sample declared_max_; // Over this goes into the last bucket. | 272 Sample declared_max_; // Over this goes into the last bucket. |
| 268 | 273 |
| 269 // Finally, provide the state that changes with the addition of each new | 274 // Finally, provide the state that changes with the addition of each new |
| 270 // sample. | 275 // sample. |
| 271 scoped_ptr<SampleVector> samples_; | 276 scoped_ptr<SampleVector> samples_; |
| 272 | 277 |
| 273 DISALLOW_COPY_AND_ASSIGN(Histogram); | 278 DISALLOW_COPY_AND_ASSIGN(Histogram); |
| 274 }; | 279 }; |
| 275 | 280 |
| 276 //------------------------------------------------------------------------------ | 281 //------------------------------------------------------------------------------ |
| 277 | 282 |
| 278 // LinearHistogram is a more traditional histogram, with evenly spaced | 283 // LinearHistogram is a more traditional histogram, with evenly spaced |
| 279 // buckets. | 284 // buckets. |
| 280 class BASE_EXPORT LinearHistogram : public Histogram { | 285 class BASE_EXPORT LinearHistogram : public Histogram { |
| 281 public: | 286 public: |
| 282 ~LinearHistogram() override; | 287 ~LinearHistogram() override; |
| 283 | 288 |
| 284 /* minimum should start from 1. 0 is as minimum is invalid. 0 is an implicit | 289 /* minimum should start from 1. 0 is as minimum is invalid. 0 is an implicit |
| 285 default underflow bucket. */ | 290 default underflow bucket. */ |
| 286 static HistogramBase* FactoryGet(const std::string& name, | 291 static HistogramBase* FactoryGet(const std::string& name, |
| 287 Sample minimum, | 292 Sample minimum, |
| 288 Sample maximum, | 293 Sample maximum, |
| 289 size_t bucket_count, | 294 size_t bucket_count, |
| 290 int32 flags); | 295 int32_t flags); |
| 291 static HistogramBase* FactoryTimeGet(const std::string& name, | 296 static HistogramBase* FactoryTimeGet(const std::string& name, |
| 292 TimeDelta minimum, | 297 TimeDelta minimum, |
| 293 TimeDelta maximum, | 298 TimeDelta maximum, |
| 294 size_t bucket_count, | 299 size_t bucket_count, |
| 295 int32 flags); | 300 int32_t flags); |
| 296 | 301 |
| 297 // Overloads of the above two functions that take a const char* |name| param, | 302 // Overloads of the above two functions that take a const char* |name| param, |
| 298 // to avoid code bloat from the std::string constructor being inlined into | 303 // to avoid code bloat from the std::string constructor being inlined into |
| 299 // call sites. | 304 // call sites. |
| 300 static HistogramBase* FactoryGet(const char* name, | 305 static HistogramBase* FactoryGet(const char* name, |
| 301 Sample minimum, | 306 Sample minimum, |
| 302 Sample maximum, | 307 Sample maximum, |
| 303 size_t bucket_count, | 308 size_t bucket_count, |
| 304 int32 flags); | 309 int32_t flags); |
| 305 static HistogramBase* FactoryTimeGet(const char* name, | 310 static HistogramBase* FactoryTimeGet(const char* name, |
| 306 TimeDelta minimum, | 311 TimeDelta minimum, |
| 307 TimeDelta maximum, | 312 TimeDelta maximum, |
| 308 size_t bucket_count, | 313 size_t bucket_count, |
| 309 int32 flags); | 314 int32_t flags); |
| 310 | 315 |
| 311 struct DescriptionPair { | 316 struct DescriptionPair { |
| 312 Sample sample; | 317 Sample sample; |
| 313 const char* description; // Null means end of a list of pairs. | 318 const char* description; // Null means end of a list of pairs. |
| 314 }; | 319 }; |
| 315 | 320 |
| 316 // Create a LinearHistogram and store a list of number/text values for use in | 321 // Create a LinearHistogram and store a list of number/text values for use in |
| 317 // writing the histogram graph. | 322 // writing the histogram graph. |
| 318 // |descriptions| can be NULL, which means no special descriptions to set. If | 323 // |descriptions| can be NULL, which means no special descriptions to set. If |
| 319 // it's not NULL, the last element in the array must has a NULL in its | 324 // it's not NULL, the last element in the array must has a NULL in its |
| 320 // "description" field. | 325 // "description" field. |
| 321 static HistogramBase* FactoryGetWithRangeDescription( | 326 static HistogramBase* FactoryGetWithRangeDescription( |
| 322 const std::string& name, | 327 const std::string& name, |
| 323 Sample minimum, | 328 Sample minimum, |
| 324 Sample maximum, | 329 Sample maximum, |
| 325 size_t bucket_count, | 330 size_t bucket_count, |
| 326 int32 flags, | 331 int32_t flags, |
| 327 const DescriptionPair descriptions[]); | 332 const DescriptionPair descriptions[]); |
| 328 | 333 |
| 329 static void InitializeBucketRanges(Sample minimum, | 334 static void InitializeBucketRanges(Sample minimum, |
| 330 Sample maximum, | 335 Sample maximum, |
| 331 BucketRanges* ranges); | 336 BucketRanges* ranges); |
| 332 | 337 |
| 333 // Overridden from Histogram: | 338 // Overridden from Histogram: |
| 334 HistogramType GetHistogramType() const override; | 339 HistogramType GetHistogramType() const override; |
| 335 | 340 |
| 336 protected: | 341 protected: |
| (...skipping 24 matching lines...) Expand all Loading... |
| 361 BucketDescriptionMap bucket_description_; | 366 BucketDescriptionMap bucket_description_; |
| 362 | 367 |
| 363 DISALLOW_COPY_AND_ASSIGN(LinearHistogram); | 368 DISALLOW_COPY_AND_ASSIGN(LinearHistogram); |
| 364 }; | 369 }; |
| 365 | 370 |
| 366 //------------------------------------------------------------------------------ | 371 //------------------------------------------------------------------------------ |
| 367 | 372 |
| 368 // BooleanHistogram is a histogram for booleans. | 373 // BooleanHistogram is a histogram for booleans. |
| 369 class BASE_EXPORT BooleanHistogram : public LinearHistogram { | 374 class BASE_EXPORT BooleanHistogram : public LinearHistogram { |
| 370 public: | 375 public: |
| 371 static HistogramBase* FactoryGet(const std::string& name, int32 flags); | 376 static HistogramBase* FactoryGet(const std::string& name, int32_t flags); |
| 372 | 377 |
| 373 // Overload of the above function that takes a const char* |name| param, | 378 // 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 | 379 // to avoid code bloat from the std::string constructor being inlined into |
| 375 // call sites. | 380 // call sites. |
| 376 static HistogramBase* FactoryGet(const char* name, int32 flags); | 381 static HistogramBase* FactoryGet(const char* name, int32_t flags); |
| 377 | 382 |
| 378 HistogramType GetHistogramType() const override; | 383 HistogramType GetHistogramType() const override; |
| 379 | 384 |
| 380 private: | 385 private: |
| 381 BooleanHistogram(const std::string& name, const BucketRanges* ranges); | 386 BooleanHistogram(const std::string& name, const BucketRanges* ranges); |
| 382 | 387 |
| 383 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( | 388 friend BASE_EXPORT HistogramBase* DeserializeHistogramInfo( |
| 384 base::PickleIterator* iter); | 389 base::PickleIterator* iter); |
| 385 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); | 390 static HistogramBase* DeserializeInfoImpl(base::PickleIterator* iter); |
| 386 | 391 |
| 387 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram); | 392 DISALLOW_COPY_AND_ASSIGN(BooleanHistogram); |
| 388 }; | 393 }; |
| 389 | 394 |
| 390 //------------------------------------------------------------------------------ | 395 //------------------------------------------------------------------------------ |
| 391 | 396 |
| 392 // CustomHistogram is a histogram for a set of custom integers. | 397 // CustomHistogram is a histogram for a set of custom integers. |
| 393 class BASE_EXPORT CustomHistogram : public Histogram { | 398 class BASE_EXPORT CustomHistogram : public Histogram { |
| 394 public: | 399 public: |
| 395 // |custom_ranges| contains a vector of limits on ranges. Each limit should be | 400 // |custom_ranges| contains a vector of limits on ranges. Each limit should be |
| 396 // > 0 and < kSampleType_MAX. (Currently 0 is still accepted for backward | 401 // > 0 and < kSampleType_MAX. (Currently 0 is still accepted for backward |
| 397 // compatibility). The limits can be unordered or contain duplication, but | 402 // compatibility). The limits can be unordered or contain duplication, but |
| 398 // client should not depend on this. | 403 // client should not depend on this. |
| 399 static HistogramBase* FactoryGet(const std::string& name, | 404 static HistogramBase* FactoryGet(const std::string& name, |
| 400 const std::vector<Sample>& custom_ranges, | 405 const std::vector<Sample>& custom_ranges, |
| 401 int32 flags); | 406 int32_t flags); |
| 402 | 407 |
| 403 // 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, |
| 404 // 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 |
| 405 // call sites. | 410 // call sites. |
| 406 static HistogramBase* FactoryGet(const char* name, | 411 static HistogramBase* FactoryGet(const char* name, |
| 407 const std::vector<Sample>& custom_ranges, | 412 const std::vector<Sample>& custom_ranges, |
| 408 int32 flags); | 413 int32_t flags); |
| 409 | 414 |
| 410 // Overridden from Histogram: | 415 // Overridden from Histogram: |
| 411 HistogramType GetHistogramType() const override; | 416 HistogramType GetHistogramType() const override; |
| 412 | 417 |
| 413 // Helper method for transforming an array of valid enumeration values | 418 // Helper method for transforming an array of valid enumeration values |
| 414 // to the std::vector<int> expected by UMA_HISTOGRAM_CUSTOM_ENUMERATION. | 419 // to the std::vector<int> expected by UMA_HISTOGRAM_CUSTOM_ENUMERATION. |
| 415 // This function ensures that a guard bucket exists right after any | 420 // 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), | 421 // 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. | 422 // so that invalid samples never fall into the same bucket as valid samples. |
| 418 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges. | 423 // TODO(kaiwang): Change name to ArrayToCustomEnumRanges. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 435 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); | 440 static bool ValidateCustomRanges(const std::vector<Sample>& custom_ranges); |
| 436 static BucketRanges* CreateBucketRangesFromCustomRanges( | 441 static BucketRanges* CreateBucketRangesFromCustomRanges( |
| 437 const std::vector<Sample>& custom_ranges); | 442 const std::vector<Sample>& custom_ranges); |
| 438 | 443 |
| 439 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); | 444 DISALLOW_COPY_AND_ASSIGN(CustomHistogram); |
| 440 }; | 445 }; |
| 441 | 446 |
| 442 } // namespace base | 447 } // namespace base |
| 443 | 448 |
| 444 #endif // BASE_METRICS_HISTOGRAM_H_ | 449 #endif // BASE_METRICS_HISTOGRAM_H_ |
| OLD | NEW |