/src/rocksdb/monitoring/histogram.h
Line | Count | Source (jump to first uncovered line) |
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 | 238k | size_t BucketCount() const { return bucketValues_.size(); } |
30 | | |
31 | 476k | 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 | 238k | ~HistogramStat() {} |
49 | | |
50 | | HistogramStat(const HistogramStat&) = delete; |
51 | | HistogramStat& operator=(const HistogramStat&) = delete; |
52 | | |
53 | | void Clear(); |
54 | | bool Empty() const; |
55 | | void Add(uint64_t value); |
56 | | void Merge(const HistogramStat& other); |
57 | | |
58 | 0 | inline uint64_t min() const { return min_.load(std::memory_order_relaxed); } |
59 | 0 | inline uint64_t max() const { return max_.load(std::memory_order_relaxed); } |
60 | 704 | inline uint64_t num() const { return num_.load(std::memory_order_relaxed); } |
61 | 0 | inline uint64_t sum() const { return sum_.load(std::memory_order_relaxed); } |
62 | 0 | inline uint64_t sum_squares() const { |
63 | 0 | return sum_squares_.load(std::memory_order_relaxed); |
64 | 0 | } |
65 | 0 | inline uint64_t bucket_at(size_t b) const { |
66 | 0 | return buckets_[b].load(std::memory_order_relaxed); |
67 | 0 | } |
68 | | |
69 | | double Median() const; |
70 | | double Percentile(double p) const; |
71 | | double Average() const; |
72 | | double StandardDeviation() const; |
73 | | void Data(HistogramData* const data) const; |
74 | | std::string ToString() const; |
75 | | |
76 | | // To be able to use HistogramStat as thread local variable, it |
77 | | // cannot have dynamic allocated member. That's why we're |
78 | | // using manually values from BucketMapper |
79 | | std::atomic_uint_fast64_t min_; |
80 | | std::atomic_uint_fast64_t max_; |
81 | | std::atomic_uint_fast64_t num_; |
82 | | std::atomic_uint_fast64_t sum_; |
83 | | std::atomic_uint_fast64_t sum_squares_; |
84 | | std::atomic_uint_fast64_t buckets_[109]; // 109==BucketMapper::BucketCount() |
85 | | const uint64_t num_buckets_; |
86 | | }; |
87 | | |
88 | | class Histogram { |
89 | | public: |
90 | 238k | Histogram() {} |
91 | 238k | virtual ~Histogram(){} |
92 | | |
93 | | virtual void Clear() = 0; |
94 | | virtual bool Empty() const = 0; |
95 | | virtual void Add(uint64_t value) = 0; |
96 | | virtual void Merge(const Histogram&) = 0; |
97 | | |
98 | | virtual std::string ToString() const = 0; |
99 | | virtual const char* Name() const = 0; |
100 | | virtual uint64_t min() const = 0; |
101 | | virtual uint64_t max() const = 0; |
102 | | virtual uint64_t num() const = 0; |
103 | | virtual double Median() const = 0; |
104 | | virtual double Percentile(double p) const = 0; |
105 | | virtual double Average() const = 0; |
106 | | virtual double StandardDeviation() const = 0; |
107 | | virtual void Data(HistogramData* const data) const = 0; |
108 | | }; |
109 | | |
110 | | class HistogramImpl : public Histogram { |
111 | | public: |
112 | 238k | HistogramImpl() { Clear(); } |
113 | | |
114 | | HistogramImpl(const HistogramImpl&) = delete; |
115 | | HistogramImpl& operator=(const HistogramImpl&) = delete; |
116 | | |
117 | | void Clear() override; |
118 | | bool Empty() const override; |
119 | | void Add(uint64_t value) override; |
120 | | void Merge(const Histogram& other) override; |
121 | | void Merge(const HistogramImpl& other); |
122 | | |
123 | | std::string ToString() const override; |
124 | 0 | const char* Name() const override { return "HistogramImpl"; } |
125 | 0 | uint64_t min() const override { return stats_.min(); } |
126 | 0 | uint64_t max() const override { return stats_.max(); } |
127 | 0 | uint64_t num() const override { return stats_.num(); } |
128 | | double Median() const override; |
129 | | double Percentile(double p) const override; |
130 | | double Average() const override; |
131 | | double StandardDeviation() const override; |
132 | | void Data(HistogramData* const data) const override; |
133 | | |
134 | 238k | virtual ~HistogramImpl() {} |
135 | | |
136 | 0 | inline HistogramStat& TEST_GetStats() { return stats_; } |
137 | | |
138 | | private: |
139 | | HistogramStat stats_; |
140 | | std::mutex mutex_; |
141 | | }; |
142 | | |
143 | | } // namespace ROCKSDB_NAMESPACE |