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).
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.
11 #include "rocksdb/statistics.h"
21 class HistogramBucketMapper
{
24 HistogramBucketMapper();
26 // converts a value to the bucket index.
27 size_t IndexForValue(uint64_t value
) const;
28 // number of buckets required.
30 size_t BucketCount() const {
31 return bucketValues_
.size();
34 uint64_t LastValue() const {
35 return maxBucketValue_
;
38 uint64_t FirstValue() const {
39 return minBucketValue_
;
42 uint64_t BucketLimit(const size_t bucketNumber
) const {
43 assert(bucketNumber
< BucketCount());
44 return bucketValues_
[bucketNumber
];
48 std::vector
<uint64_t> bucketValues_
;
49 uint64_t maxBucketValue_
;
50 uint64_t minBucketValue_
;
51 std::map
<uint64_t, uint64_t> valueIndexMap_
;
54 struct HistogramStat
{
58 HistogramStat(const HistogramStat
&) = delete;
59 HistogramStat
& operator=(const HistogramStat
&) = delete;
63 void Add(uint64_t value
);
64 void Merge(const HistogramStat
& other
);
66 inline uint64_t min() const { return min_
.load(std::memory_order_relaxed
); }
67 inline uint64_t max() const { return max_
.load(std::memory_order_relaxed
); }
68 inline uint64_t num() const { return num_
.load(std::memory_order_relaxed
); }
69 inline uint64_t sum() const { return sum_
.load(std::memory_order_relaxed
); }
70 inline uint64_t sum_squares() const {
71 return sum_squares_
.load(std::memory_order_relaxed
);
73 inline uint64_t bucket_at(size_t b
) const {
74 return buckets_
[b
].load(std::memory_order_relaxed
);
77 double Median() const;
78 double Percentile(double p
) const;
79 double Average() const;
80 double StandardDeviation() const;
81 void Data(HistogramData
* const data
) const;
82 std::string
ToString() const;
84 // To be able to use HistogramStat as thread local variable, it
85 // cannot have dynamic allocated member. That's why we're
86 // using manually values from BucketMapper
87 std::atomic_uint_fast64_t min_
;
88 std::atomic_uint_fast64_t max_
;
89 std::atomic_uint_fast64_t num_
;
90 std::atomic_uint_fast64_t sum_
;
91 std::atomic_uint_fast64_t sum_squares_
;
92 std::atomic_uint_fast64_t buckets_
[109]; // 109==BucketMapper::BucketCount()
93 const uint64_t num_buckets_
;
99 virtual ~Histogram() {};
101 virtual void Clear() = 0;
102 virtual bool Empty() const = 0;
103 virtual void Add(uint64_t value
) = 0;
104 virtual void Merge(const Histogram
&) = 0;
106 virtual std::string
ToString() const = 0;
107 virtual const char* Name() const = 0;
108 virtual uint64_t min() const = 0;
109 virtual uint64_t max() const = 0;
110 virtual uint64_t num() const = 0;
111 virtual double Median() const = 0;
112 virtual double Percentile(double p
) const = 0;
113 virtual double Average() const = 0;
114 virtual double StandardDeviation() const = 0;
115 virtual void Data(HistogramData
* const data
) const = 0;
118 class HistogramImpl
: public Histogram
{
120 HistogramImpl() { Clear(); }
122 HistogramImpl(const HistogramImpl
&) = delete;
123 HistogramImpl
& operator=(const HistogramImpl
&) = delete;
125 virtual void Clear() override
;
126 virtual bool Empty() const override
;
127 virtual void Add(uint64_t value
) override
;
128 virtual void Merge(const Histogram
& other
) override
;
129 void Merge(const HistogramImpl
& other
);
131 virtual std::string
ToString() const override
;
132 virtual const char* Name() const override
{ return "HistogramImpl"; }
133 virtual uint64_t min() const override
{ return stats_
.min(); }
134 virtual uint64_t max() const override
{ return stats_
.max(); }
135 virtual uint64_t num() const override
{ return stats_
.num(); }
136 virtual double Median() const override
;
137 virtual double Percentile(double p
) const override
;
138 virtual double Average() const override
;
139 virtual double StandardDeviation() const override
;
140 virtual void Data(HistogramData
* const data
) const override
;
142 virtual ~HistogramImpl() {}
145 HistogramStat stats_
;
149 } // namespace rocksdb