File: bucket_ranges.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (104 lines) | stat: -rw-r--r-- 3,820 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// BucketRanges stores the vector of ranges that delimit what samples are
// tallied in the corresponding buckets of a histogram. Histograms that have
// same ranges for all their corresponding buckets should share the same
// BucketRanges object.
//
// E.g. A 5 buckets LinearHistogram with 1 as minimal value and 4 as maximal
// value will need a BucketRanges with 6 ranges:
// 0, 1, 2, 3, 4, INT_MAX
//
// TODO(kaiwang): Currently we keep all negative values in 0~1 bucket. Consider
// changing 0 to INT_MIN.

#ifndef BASE_METRICS_BUCKET_RANGES_H_
#define BASE_METRICS_BUCKET_RANGES_H_

#include <limits.h>
#include <stddef.h>
#include <stdint.h>

#include <atomic>
#include <vector>

#include "base/base_export.h"
#include "base/check_op.h"
#include "base/containers/span.h"
#include "base/metrics/histogram_base.h"

namespace base {

class BASE_EXPORT BucketRanges {
 public:
  typedef std::vector<HistogramBase::Sample32> Ranges;

  explicit BucketRanges(size_t num_ranges);
  explicit BucketRanges(base::span<const HistogramBase::Sample32> data);

  BucketRanges(const BucketRanges&) = delete;
  BucketRanges& operator=(const BucketRanges&) = delete;

  ~BucketRanges();

  size_t size() const { return ranges_.size(); }
  HistogramBase::Sample32 range(size_t i) const { return ranges_[i]; }
  void set_range(size_t i, HistogramBase::Sample32 value) {
    DCHECK_LT(i, ranges_.size());
    DCHECK_GE(value, 0);
    ranges_[i] = value;
  }
  uint32_t checksum() const { return checksum_; }
  void set_checksum(uint32_t checksum) { checksum_ = checksum; }

  // A bucket is defined by a consecutive pair of entries in |ranges|, so there
  // is one fewer bucket than there are ranges.  For example, if |ranges| is
  // [0, 1, 3, 7, INT_MAX], then the buckets in this histogram are
  // [0, 1), [1, 3), [3, 7), and [7, INT_MAX).
  size_t bucket_count() const { return ranges_.size() - 1; }

  // Checksum methods to verify whether the ranges are corrupted (e.g. bad
  // memory access).
  uint32_t CalculateChecksum() const;
  bool HasValidChecksum() const;
  void ResetChecksum();

  // Return true iff |other| object has same ranges_ as |this| object's ranges_.
  bool Equals(const BucketRanges* other) const;

  // Set and get a reference into persistent memory where this bucket data
  // can be found (and re-used). These calls are internally atomic with no
  // safety against overwriting an existing value since though it is wasteful
  // to have multiple identical persistent records, it is still safe.
  void set_persistent_reference(uint32_t ref) const {
    persistent_reference_.store(ref, std::memory_order_release);
  }
  uint32_t persistent_reference() const {
    return persistent_reference_.load(std::memory_order_acquire);
  }

 private:
  // A monotonically increasing list of values which determine which bucket to
  // put a sample into.  For each index, show the smallest sample that can be
  // added to the corresponding bucket.
  Ranges ranges_;

  // Checksum for the conntents of ranges_.  Used to detect random over-writes
  // of our data, and to quickly see if some other BucketRanges instance is
  // possibly Equal() to this instance.
  // TODO(kaiwang): Consider change this to uint64_t. Because we see a lot of
  // noise on UMA dashboard.
  uint32_t checksum_;

  // A reference into a global PersistentMemoryAllocator where the ranges
  // information is stored. This allows for the record to be created once and
  // re-used simply by having all histograms with the same ranges use the
  // same reference.
  mutable std::atomic<uint32_t> persistent_reference_{0};
};

}  // namespace base

#endif  // BASE_METRICS_BUCKET_RANGES_H_