2016-03-12 00:54:25 +00:00
|
|
|
// Copyright (c) 2013, 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).
|
2016-03-12 00:54:25 +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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "monitoring/histogram.h"
|
2016-03-12 00:54:25 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2021-01-26 06:07:26 +00:00
|
|
|
class SystemClock;
|
2016-03-12 00:54:25 +00:00
|
|
|
|
2022-10-25 03:45:54 +00:00
|
|
|
class HistogramWindowingImpl : public Histogram {
|
|
|
|
public:
|
2016-03-12 00:54:25 +00:00
|
|
|
HistogramWindowingImpl();
|
2022-10-25 03:45:54 +00:00
|
|
|
HistogramWindowingImpl(uint64_t num_windows, uint64_t micros_per_window,
|
2016-03-12 00:54:25 +00:00
|
|
|
uint64_t min_num_per_window);
|
|
|
|
|
2017-09-01 17:58:04 +00:00
|
|
|
HistogramWindowingImpl(const HistogramWindowingImpl&) = delete;
|
|
|
|
HistogramWindowingImpl& operator=(const HistogramWindowingImpl&) = delete;
|
2016-03-12 00:54:25 +00:00
|
|
|
|
|
|
|
~HistogramWindowingImpl();
|
|
|
|
|
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 HistogramWindowingImpl& other);
|
|
|
|
|
2024-01-31 21:14:42 +00:00
|
|
|
std::string ToString() const override;
|
|
|
|
const char* Name() const override { return "HistogramWindowingImpl"; }
|
|
|
|
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;
|
2016-03-12 00:54:25 +00:00
|
|
|
|
2021-01-04 21:11:31 +00:00
|
|
|
#ifndef NDEBUG
|
2021-01-26 06:07:26 +00:00
|
|
|
void TEST_UpdateClock(const std::shared_ptr<SystemClock>& clock) {
|
|
|
|
clock_ = clock;
|
|
|
|
}
|
2021-01-04 21:11:31 +00:00
|
|
|
#endif // NDEBUG
|
|
|
|
|
|
|
|
private:
|
2016-03-12 00:54:25 +00:00
|
|
|
void TimerTick();
|
|
|
|
void SwapHistoryBucket();
|
|
|
|
inline uint64_t current_window() const {
|
|
|
|
return current_window_.load(std::memory_order_relaxed);
|
|
|
|
}
|
2022-10-25 03:45:54 +00:00
|
|
|
inline uint64_t last_swap_time() const {
|
2016-03-12 00:54:25 +00:00
|
|
|
return last_swap_time_.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2021-01-26 06:07:26 +00:00
|
|
|
std::shared_ptr<SystemClock> clock_;
|
2016-03-12 00:54:25 +00:00
|
|
|
std::mutex mutex_;
|
|
|
|
|
|
|
|
// Aggregated stats over windows_stats_, all the computation is done
|
|
|
|
// upon aggregated values
|
|
|
|
HistogramStat stats_;
|
|
|
|
|
|
|
|
// This is a circular array representing the latest N time-windows.
|
|
|
|
// Each entry stores a time-window of data. Expiration is done
|
|
|
|
// on window-based.
|
|
|
|
std::unique_ptr<HistogramStat[]> window_stats_;
|
|
|
|
|
|
|
|
std::atomic_uint_fast64_t current_window_;
|
|
|
|
std::atomic_uint_fast64_t last_swap_time_;
|
|
|
|
|
|
|
|
// Following parameters are configuable
|
|
|
|
uint64_t num_windows_ = 5;
|
|
|
|
uint64_t micros_per_window_ = 60000000;
|
|
|
|
// By default, don't care about the number of values in current window
|
|
|
|
// when decide whether to swap windows or not.
|
|
|
|
uint64_t min_num_per_window_ = 0;
|
|
|
|
};
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|