2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2013-10-05 05:32:05 +00:00
|
|
|
#pragma once
|
2013-01-29 20:23:31 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <map>
|
2016-03-12 00:54:25 +00:00
|
|
|
#include <mutex>
|
2022-10-25 03:45:54 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "rocksdb/statistics.h"
|
2015-07-02 00:21:38 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-01-29 20:23:31 +00:00
|
|
|
class HistogramBucketMapper {
|
|
|
|
public:
|
|
|
|
HistogramBucketMapper();
|
|
|
|
|
|
|
|
// converts a value to the bucket index.
|
2016-03-12 00:54:25 +00:00
|
|
|
size_t IndexForValue(uint64_t value) const;
|
2013-01-29 20:23:31 +00:00
|
|
|
// number of buckets required.
|
|
|
|
|
2022-10-25 03:45:54 +00:00
|
|
|
size_t BucketCount() const { return bucketValues_.size(); }
|
2013-01-29 20:23:31 +00:00
|
|
|
|
2022-10-25 03:45:54 +00:00
|
|
|
uint64_t LastValue() const { return maxBucketValue_; }
|
2013-01-29 20:23:31 +00:00
|
|
|
|
2022-10-25 03:45:54 +00:00
|
|
|
uint64_t FirstValue() const { return minBucketValue_; }
|
2013-01-29 20:23:31 +00:00
|
|
|
|
2014-11-13 19:39:30 +00:00
|
|
|
uint64_t BucketLimit(const size_t bucketNumber) const {
|
2013-01-29 20:23:31 +00:00
|
|
|
assert(bucketNumber < BucketCount());
|
|
|
|
return bucketValues_[bucketNumber];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-08-22 00:09:20 +00:00
|
|
|
std::vector<uint64_t> bucketValues_;
|
|
|
|
uint64_t maxBucketValue_;
|
|
|
|
uint64_t minBucketValue_;
|
2013-01-29 20:23:31 +00:00
|
|
|
};
|
|
|
|
|
2016-03-12 00:54:25 +00:00
|
|
|
struct HistogramStat {
|
|
|
|
HistogramStat();
|
|
|
|
~HistogramStat() {}
|
|
|
|
|
|
|
|
HistogramStat(const HistogramStat&) = delete;
|
|
|
|
HistogramStat& operator=(const HistogramStat&) = delete;
|
|
|
|
|
|
|
|
void Clear();
|
|
|
|
bool Empty() const;
|
|
|
|
void Add(uint64_t value);
|
|
|
|
void Merge(const HistogramStat& other);
|
|
|
|
|
|
|
|
inline uint64_t min() const { return min_.load(std::memory_order_relaxed); }
|
|
|
|
inline uint64_t max() const { return max_.load(std::memory_order_relaxed); }
|
|
|
|
inline uint64_t num() const { return num_.load(std::memory_order_relaxed); }
|
|
|
|
inline uint64_t sum() const { return sum_.load(std::memory_order_relaxed); }
|
|
|
|
inline uint64_t sum_squares() const {
|
|
|
|
return sum_squares_.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
inline uint64_t bucket_at(size_t b) const {
|
|
|
|
return buckets_[b].load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Median() const;
|
|
|
|
double Percentile(double p) const;
|
|
|
|
double Average() const;
|
|
|
|
double StandardDeviation() const;
|
|
|
|
void Data(HistogramData* const data) const;
|
|
|
|
std::string ToString() const;
|
|
|
|
|
|
|
|
// To be able to use HistogramStat as thread local variable, it
|
|
|
|
// cannot have dynamic allocated member. That's why we're
|
|
|
|
// using manually values from BucketMapper
|
|
|
|
std::atomic_uint_fast64_t min_;
|
|
|
|
std::atomic_uint_fast64_t max_;
|
|
|
|
std::atomic_uint_fast64_t num_;
|
|
|
|
std::atomic_uint_fast64_t sum_;
|
|
|
|
std::atomic_uint_fast64_t sum_squares_;
|
2022-10-25 03:45:54 +00:00
|
|
|
std::atomic_uint_fast64_t buckets_[109]; // 109==BucketMapper::BucketCount()
|
2016-03-12 00:54:25 +00:00
|
|
|
const uint64_t num_buckets_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Histogram {
|
2022-10-25 03:45:54 +00:00
|
|
|
public:
|
2016-03-12 00:54:25 +00:00
|
|
|
Histogram() {}
|
2024-01-23 16:42:15 +00:00
|
|
|
virtual ~Histogram(){}
|
2016-03-12 00:54:25 +00:00
|
|
|
|
|
|
|
virtual void Clear() = 0;
|
|
|
|
virtual bool Empty() const = 0;
|
|
|
|
virtual void Add(uint64_t value) = 0;
|
|
|
|
virtual void Merge(const Histogram&) = 0;
|
|
|
|
|
|
|
|
virtual std::string ToString() const = 0;
|
|
|
|
virtual const char* Name() const = 0;
|
|
|
|
virtual uint64_t min() const = 0;
|
|
|
|
virtual uint64_t max() const = 0;
|
|
|
|
virtual uint64_t num() const = 0;
|
|
|
|
virtual double Median() const = 0;
|
|
|
|
virtual double Percentile(double p) const = 0;
|
|
|
|
virtual double Average() const = 0;
|
|
|
|
virtual double StandardDeviation() const = 0;
|
|
|
|
virtual void Data(HistogramData* const data) const = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class HistogramImpl : public Histogram {
|
2011-03-18 22:37:00 +00:00
|
|
|
public:
|
2016-03-12 00:54:25 +00:00
|
|
|
HistogramImpl() { Clear(); }
|
|
|
|
|
|
|
|
HistogramImpl(const HistogramImpl&) = delete;
|
|
|
|
HistogramImpl& operator=(const HistogramImpl&) = delete;
|
2013-02-15 19:53:17 +00:00
|
|
|
|
2024-01-31 21:14:42 +00:00
|
|
|
void Clear() override;
|
|
|
|
bool Empty() const override;
|
|
|
|
void Add(uint64_t value) override;
|
|
|
|
void Merge(const Histogram& other) override;
|
2016-03-12 00:54:25 +00:00
|
|
|
void Merge(const HistogramImpl& other);
|
2013-02-15 19:53:17 +00:00
|
|
|
|
2024-01-31 21:14:42 +00:00
|
|
|
std::string ToString() const override;
|
|
|
|
const char* Name() const override { return "HistogramImpl"; }
|
|
|
|
uint64_t min() const override { return stats_.min(); }
|
|
|
|
uint64_t max() const override { return stats_.max(); }
|
|
|
|
uint64_t num() const override { return stats_.num(); }
|
|
|
|
double Median() const override;
|
|
|
|
double Percentile(double p) const override;
|
|
|
|
double Average() const override;
|
|
|
|
double StandardDeviation() const override;
|
|
|
|
void Data(HistogramData* const data) const override;
|
2013-01-29 20:23:31 +00:00
|
|
|
|
2014-09-04 14:04:37 +00:00
|
|
|
virtual ~HistogramImpl() {}
|
|
|
|
|
2022-09-02 00:46:30 +00:00
|
|
|
inline HistogramStat& TEST_GetStats() { return stats_; }
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
2016-03-12 00:54:25 +00:00
|
|
|
HistogramStat stats_;
|
|
|
|
std::mutex mutex_;
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|