Coverage Report

Created: 2026-11-12 08:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/monitoring/histogram.h
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under both the GPLv2 (found in the
3
//  COPYING file in the root directory) and Apache 2.0 License
4
//  (found in the LICENSE.Apache file in the root directory).
5
//
6
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7
// Use of this source code is governed by a BSD-style license that can be
8
// found in the LICENSE file. See the AUTHORS file for names of contributors.
9
10
#pragma once
11
#include <cassert>
12
#include <map>
13
#include <mutex>
14
#include <string>
15
#include <vector>
16
17
#include "rocksdb/statistics.h"
18
19
namespace ROCKSDB_NAMESPACE {
20
21
class HistogramBucketMapper {
22
 public:
23
  HistogramBucketMapper();
24
25
  // converts a value to the bucket index.
26
  size_t IndexForValue(uint64_t value) const;
27
  // number of buckets required.
28
29
700k
  size_t BucketCount() const { return bucketValues_.size(); }
30
31
1.40M
  uint64_t LastValue() const { return maxBucketValue_; }
32
33
0
  uint64_t FirstValue() const { return minBucketValue_; }
34
35
0
  uint64_t BucketLimit(const size_t bucketNumber) const {
36
0
    assert(bucketNumber < BucketCount());
37
0
    return bucketValues_[bucketNumber];
38
0
  }
39
40
 private:
41
  std::vector<uint64_t> bucketValues_;
42
  uint64_t maxBucketValue_;
43
  uint64_t minBucketValue_;
44
};
45
46
struct HistogramStat {
47
  HistogramStat();
48
700k
  ~HistogramStat() {}
49
50
  HistogramStat(const HistogramStat&) = delete;
51
  HistogramStat& operator=(const HistogramStat&) = delete;
52
  HistogramStat(HistogramStat&&) = delete;
53
  HistogramStat& operator=(HistogramStat&&) = delete;
54
55
  void Clear();
56
  bool Empty() const;
57
  void Add(uint64_t value);
58
  void Merge(const HistogramStat& other);
59
60
0
  inline uint64_t min() const { return min_.load(std::memory_order_relaxed); }
61
0
  inline uint64_t max() const { return max_.load(std::memory_order_relaxed); }
62
2.83k
  inline uint64_t num() const { return num_.load(std::memory_order_relaxed); }
63
0
  inline uint64_t sum() const { return sum_.load(std::memory_order_relaxed); }
64
0
  inline uint64_t sum_squares() const {
65
0
    return sum_squares_.load(std::memory_order_relaxed);
66
0
  }
67
0
  inline uint64_t bucket_at(size_t b) const {
68
0
    return buckets_[b].load(std::memory_order_relaxed);
69
0
  }
70
71
  double Median() const;
72
  double Percentile(double p) const;
73
  double Average() const;
74
  double StandardDeviation() const;
75
  void Data(HistogramData* const data) const;
76
  std::string ToString() const;
77
78
  // To be able to use HistogramStat as thread local variable, it
79
  // cannot have dynamic allocated member. That's why we're
80
  // using manually values from BucketMapper
81
  std::atomic_uint_fast64_t min_;
82
  std::atomic_uint_fast64_t max_;
83
  std::atomic_uint_fast64_t num_;
84
  std::atomic_uint_fast64_t sum_;
85
  std::atomic_uint_fast64_t sum_squares_;
86
  std::atomic_uint_fast64_t buckets_[109];  // 109==BucketMapper::BucketCount()
87
  const uint64_t num_buckets_;
88
};
89
90
class Histogram {
91
 public:
92
700k
  Histogram() {}
93
700k
  virtual ~Histogram() = default;
94
95
  virtual void Clear() = 0;
96
  virtual bool Empty() const = 0;
97
  virtual void Add(uint64_t value) = 0;
98
  virtual void Merge(const Histogram&) = 0;
99
100
  virtual std::string ToString() const = 0;
101
  virtual const char* Name() const = 0;
102
  virtual uint64_t min() const = 0;
103
  virtual uint64_t max() const = 0;
104
  virtual uint64_t num() const = 0;
105
  virtual double Median() const = 0;
106
  virtual double Percentile(double p) const = 0;
107
  virtual double Average() const = 0;
108
  virtual double StandardDeviation() const = 0;
109
  virtual void Data(HistogramData* const data) const = 0;
110
};
111
112
class HistogramImpl : public Histogram {
113
 public:
114
700k
  HistogramImpl() { HistogramImpl::Clear(); }
115
116
  HistogramImpl(const HistogramImpl&) = delete;
117
  HistogramImpl& operator=(const HistogramImpl&) = delete;
118
  HistogramImpl(HistogramImpl&&) = delete;
119
  HistogramImpl& operator=(HistogramImpl&&) = delete;
120
121
  void Clear() override;
122
  bool Empty() const override;
123
  void Add(uint64_t value) override;
124
  void Merge(const Histogram& other) override;
125
  void Merge(const HistogramImpl& other);
126
127
  std::string ToString() const override;
128
0
  const char* Name() const override { return "HistogramImpl"; }
129
0
  uint64_t min() const override { return stats_.min(); }
130
0
  uint64_t max() const override { return stats_.max(); }
131
0
  uint64_t num() const override { return stats_.num(); }
132
  double Median() const override;
133
  double Percentile(double p) const override;
134
  double Average() const override;
135
  double StandardDeviation() const override;
136
  void Data(HistogramData* const data) const override;
137
138
700k
  ~HistogramImpl() override {}
139
140
0
  inline HistogramStat& TEST_GetStats() { return stats_; }
141
142
 private:
143
  HistogramStat stats_;
144
  std::mutex mutex_;
145
};
146
147
}  // namespace ROCKSDB_NAMESPACE