[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | // |
| 5 | // BucketRanges stores the vector of ranges that delimit what samples are |
| 6 | // tallied in the corresponding buckets of a histogram. Histograms that have |
| 7 | // same ranges for all their corresponding buckets should share the same |
| 8 | // BucketRanges object. |
| 9 | // |
| 10 | // E.g. A 5 buckets LinearHistogram with 1 as minimal value and 4 as maximal |
| 11 | // value will need a BucketRanges with 6 ranges: |
| 12 | // 0, 1, 2, 3, 4, INT_MAX |
| 13 | // |
| 14 | // TODO(kaiwang): Currently we keep all negative values in 0~1 bucket. Consider |
| 15 | // changing 0 to INT_MIN. |
| 16 | |
| 17 | #ifndef BASE_METRICS_BUCKET_RANGES_H_ |
| 18 | #define BASE_METRICS_BUCKET_RANGES_H_ |
| 19 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 25 | #include <limits.h> |
| 26 | |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 27 | #include "base/base_export.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 28 | #include "base/macros.h" |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 29 | #include "base/metrics/histogram_base.h" |
| 30 | |
| 31 | namespace base { |
| 32 | |
[email protected] | 2f7d9cd | 2012-09-22 03:42:12 | [diff] [blame] | 33 | class BASE_EXPORT BucketRanges { |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 34 | public: |
| 35 | typedef std::vector<HistogramBase::Sample> Ranges; |
| 36 | |
[email protected] | f3c697c5 | 2013-01-15 10:52:11 | [diff] [blame] | 37 | explicit BucketRanges(size_t num_ranges); |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 38 | ~BucketRanges(); |
| 39 | |
| 40 | size_t size() const { return ranges_.size(); } |
| 41 | HistogramBase::Sample range(size_t i) const { return ranges_[i]; } |
| 42 | void set_range(size_t i, HistogramBase::Sample value); |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 43 | uint32_t checksum() const { return checksum_; } |
| 44 | void set_checksum(uint32_t checksum) { checksum_ = checksum; } |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 45 | |
[email protected] | 15ce384 | 2013-06-27 14:38:45 | [diff] [blame] | 46 | // A bucket is defined by a consecutive pair of entries in |ranges|, so there |
| 47 | // is one fewer bucket than there are ranges. For example, if |ranges| is |
| 48 | // [0, 1, 3, 7, INT_MAX], then the buckets in this histogram are |
| 49 | // [0, 1), [1, 3), [3, 7), and [7, INT_MAX). |
| 50 | size_t bucket_count() const { return ranges_.size() - 1; } |
| 51 | |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 52 | // Checksum methods to verify whether the ranges are corrupted (e.g. bad |
| 53 | // memory access). |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 54 | uint32_t CalculateChecksum() const; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 55 | bool HasValidChecksum() const; |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 56 | void ResetChecksum(); |
| 57 | |
| 58 | // Return true iff |other| object has same ranges_ as |this| object's ranges_. |
| 59 | bool Equals(const BucketRanges* other) const; |
| 60 | |
| 61 | private: |
| 62 | // A monotonically increasing list of values which determine which bucket to |
| 63 | // put a sample into. For each index, show the smallest sample that can be |
| 64 | // added to the corresponding bucket. |
| 65 | Ranges ranges_; |
| 66 | |
| 67 | // Checksum for the conntents of ranges_. Used to detect random over-writes |
| 68 | // of our data, and to quickly see if some other BucketRanges instance is |
| 69 | // possibly Equal() to this instance. |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 70 | // TODO(kaiwang): Consider change this to uint64_t. Because we see a lot of |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 71 | // noise on UMA dashboard. |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 72 | uint32_t checksum_; |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 73 | |
| 74 | DISALLOW_COPY_AND_ASSIGN(BucketRanges); |
| 75 | }; |
| 76 | |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 77 | ////////////////////////////////////////////////////////////////////////////// |
| 78 | // Expose only for test. |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 79 | BASE_EXPORT extern const uint32_t kCrcTable[256]; |
[email protected] | 34d06232 | 2012-08-01 21:34:08 | [diff] [blame] | 80 | |
[email protected] | 4a32f12 | 2012-07-25 20:02:48 | [diff] [blame] | 81 | } // namespace base |
| 82 | |
| 83 | #endif // BASE_METRICS_BUCKET_RANGES_H_ |