bucket_ranges.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #ifndef BASE_METRICS_BUCKET_RANGES_H_
  17. #define BASE_METRICS_BUCKET_RANGES_H_
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <vector>
  21. #include <limits.h>
  22. #include "base/atomicops.h"
  23. #include "base/base_export.h"
  24. #include "base/macros.h"
  25. #include "base/metrics/histogram_base.h"
  26. namespace base {
  27. class BASE_EXPORT BucketRanges {
  28. public:
  29. typedef std::vector<HistogramBase::Sample> Ranges;
  30. explicit BucketRanges(size_t num_ranges);
  31. ~BucketRanges();
  32. size_t size() const { return ranges_.size(); }
  33. HistogramBase::Sample range(size_t i) const { return ranges_[i]; }
  34. void set_range(size_t i, HistogramBase::Sample value) {
  35. DCHECK_LT(i, ranges_.size());
  36. DCHECK_GE(value, 0);
  37. ranges_[i] = value;
  38. }
  39. uint32_t checksum() const { return checksum_; }
  40. void set_checksum(uint32_t checksum) { checksum_ = checksum; }
  41. // A bucket is defined by a consecutive pair of entries in |ranges|, so there
  42. // is one fewer bucket than there are ranges. For example, if |ranges| is
  43. // [0, 1, 3, 7, INT_MAX], then the buckets in this histogram are
  44. // [0, 1), [1, 3), [3, 7), and [7, INT_MAX).
  45. size_t bucket_count() const { return ranges_.size() - 1; }
  46. // Checksum methods to verify whether the ranges are corrupted (e.g. bad
  47. // memory access).
  48. uint32_t CalculateChecksum() const;
  49. bool HasValidChecksum() const;
  50. void ResetChecksum();
  51. // Return true iff |other| object has same ranges_ as |this| object's ranges_.
  52. bool Equals(const BucketRanges* other) const;
  53. // Set and get a reference into persistent memory where this bucket data
  54. // can be found (and re-used). These calls are internally atomic with no
  55. // safety against overwriting an existing value since though it is wasteful
  56. // to have multiple identical persistent records, it is still safe.
  57. void set_persistent_reference(uint32_t ref) const {
  58. subtle::Release_Store(&persistent_reference_, ref);
  59. }
  60. uint32_t persistent_reference() const {
  61. return subtle::Acquire_Load(&persistent_reference_);
  62. }
  63. private:
  64. // A monotonically increasing list of values which determine which bucket to
  65. // put a sample into. For each index, show the smallest sample that can be
  66. // added to the corresponding bucket.
  67. Ranges ranges_;
  68. // Checksum for the conntents of ranges_. Used to detect random over-writes
  69. // of our data, and to quickly see if some other BucketRanges instance is
  70. // possibly Equal() to this instance.
  71. // TODO(kaiwang): Consider change this to uint64_t. Because we see a lot of
  72. // noise on UMA dashboard.
  73. uint32_t checksum_;
  74. // A reference into a global PersistentMemoryAllocator where the ranges
  75. // information is stored. This allows for the record to be created once and
  76. // re-used simply by having all histograms with the same ranges use the
  77. // same reference.
  78. mutable subtle::Atomic32 persistent_reference_ = 0;
  79. DISALLOW_COPY_AND_ASSIGN(BucketRanges);
  80. };
  81. } // namespace base
  82. #endif // BASE_METRICS_BUCKET_RANGES_H_