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
20#include <vector>
21
22#include "base/base_export.h"
23#include "base/basictypes.h"
24#include "base/gtest_prod_util.h"
25#include "base/metrics/histogram_base.h"
26
27namespace base {
28
29class BASE_EXPORT BucketRanges {
30 public:
31  typedef std::vector<HistogramBase::Sample> Ranges;
32
33  explicit BucketRanges(size_t num_ranges);
34  ~BucketRanges();
35
36  size_t size() const { return ranges_.size(); }
37  HistogramBase::Sample range(size_t i) const { return ranges_[i]; }
38  void set_range(size_t i, HistogramBase::Sample value);
39  uint32 checksum() const { return checksum_; }
40  void set_checksum(uint32 checksum) { checksum_ = checksum; }
41
42  // A bucket is defined by a consecutive pair of entries in |ranges|, so there
43  // is one fewer bucket than there are ranges.  For example, if |ranges| is
44  // [0, 1, 3, 7, INT_MAX], then the buckets in this histogram are
45  // [0, 1), [1, 3), [3, 7), and [7, INT_MAX).
46  size_t bucket_count() const { return ranges_.size() - 1; }
47
48  // Checksum methods to verify whether the ranges are corrupted (e.g. bad
49  // memory access).
50  uint32 CalculateChecksum() const;
51  bool HasValidChecksum() const;
52  void ResetChecksum();
53
54  // Return true iff |other| object has same ranges_ as |this| object's ranges_.
55  bool Equals(const BucketRanges* other) const;
56
57 private:
58  // A monotonically increasing list of values which determine which bucket to
59  // put a sample into.  For each index, show the smallest sample that can be
60  // added to the corresponding bucket.
61  Ranges ranges_;
62
63  // Checksum for the conntents of ranges_.  Used to detect random over-writes
64  // of our data, and to quickly see if some other BucketRanges instance is
65  // possibly Equal() to this instance.
66  // TODO(kaiwang): Consider change this to uint64. Because we see a lot of
67  // noise on UMA dashboard.
68  uint32 checksum_;
69
70  DISALLOW_COPY_AND_ASSIGN(BucketRanges);
71};
72
73//////////////////////////////////////////////////////////////////////////////
74// Expose only for test.
75BASE_EXPORT_PRIVATE extern const uint32 kCrcTable[256];
76
77}  // namespace base
78
79#endif  // BASE_METRICS_BUCKET_RANGES_H_
80