fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
4.5 KiB
149 lines
4.5 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
11 years ago
|
// This source code is licensed under the BSD-style license found in the
|
||
|
// LICENSE file in the root directory of this source tree. An additional grant
|
||
|
// of patent rights can be found in the PATENTS file in the same directory.
|
||
|
//
|
||
14 years ago
|
// 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.
|
||
|
|
||
11 years ago
|
#pragma once
|
||
11 years ago
|
#include "rocksdb/statistics.h"
|
||
12 years ago
|
|
||
12 years ago
|
#include <cassert>
|
||
14 years ago
|
#include <string>
|
||
12 years ago
|
#include <vector>
|
||
|
#include <map>
|
||
9 years ago
|
#include <mutex>
|
||
10 years ago
|
|
||
11 years ago
|
namespace rocksdb {
|
||
14 years ago
|
|
||
12 years ago
|
class HistogramBucketMapper {
|
||
|
public:
|
||
|
|
||
|
HistogramBucketMapper();
|
||
|
|
||
|
// converts a value to the bucket index.
|
||
9 years ago
|
size_t IndexForValue(uint64_t value) const;
|
||
12 years ago
|
// number of buckets required.
|
||
|
|
||
10 years ago
|
size_t BucketCount() const {
|
||
12 years ago
|
return bucketValues_.size();
|
||
|
}
|
||
|
|
||
|
uint64_t LastValue() const {
|
||
|
return maxBucketValue_;
|
||
|
}
|
||
|
|
||
|
uint64_t FirstValue() const {
|
||
|
return minBucketValue_;
|
||
|
}
|
||
|
|
||
10 years ago
|
uint64_t BucketLimit(const size_t bucketNumber) const {
|
||
12 years ago
|
assert(bucketNumber < BucketCount());
|
||
|
return bucketValues_[bucketNumber];
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
const std::vector<uint64_t> bucketValues_;
|
||
|
const uint64_t maxBucketValue_;
|
||
|
const uint64_t minBucketValue_;
|
||
|
std::map<uint64_t, uint64_t> valueIndexMap_;
|
||
|
};
|
||
|
|
||
9 years ago
|
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_;
|
||
|
std::atomic_uint_fast64_t buckets_[138]; // 138==BucketMapper::BucketCount()
|
||
|
const uint64_t num_buckets_;
|
||
|
};
|
||
|
|
||
|
class Histogram {
|
||
|
public:
|
||
|
Histogram() {}
|
||
|
virtual ~Histogram() {};
|
||
|
|
||
|
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 {
|
||
14 years ago
|
public:
|
||
9 years ago
|
HistogramImpl() { Clear(); }
|
||
|
|
||
|
HistogramImpl(const HistogramImpl&) = delete;
|
||
|
HistogramImpl& operator=(const HistogramImpl&) = delete;
|
||
12 years ago
|
|
||
9 years ago
|
virtual void Clear() override;
|
||
|
virtual bool Empty() const override;
|
||
|
virtual void Add(uint64_t value) override;
|
||
|
virtual void Merge(const Histogram& other) override;
|
||
|
void Merge(const HistogramImpl& other);
|
||
12 years ago
|
|
||
9 years ago
|
virtual std::string ToString() const override;
|
||
|
virtual const char* Name() const override { return "HistogramImpl"; }
|
||
|
virtual uint64_t min() const override { return stats_.min(); }
|
||
|
virtual uint64_t max() const override { return stats_.max(); }
|
||
|
virtual uint64_t num() const override { return stats_.num(); }
|
||
|
virtual double Median() const override;
|
||
|
virtual double Percentile(double p) const override;
|
||
|
virtual double Average() const override;
|
||
|
virtual double StandardDeviation() const override;
|
||
|
virtual void Data(HistogramData* const data) const override;
|
||
12 years ago
|
|
||
10 years ago
|
virtual ~HistogramImpl() {}
|
||
|
|
||
14 years ago
|
private:
|
||
9 years ago
|
HistogramStat stats_;
|
||
|
std::mutex mutex_;
|
||
14 years ago
|
};
|
||
|
|
||
9 years ago
|
} // namespace rocksdb
|