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.
147 lines
4.6 KiB
147 lines
4.6 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// 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).
|
||
11 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.
|
||
|
|
||
|
#pragma once
|
||
|
|
||
9 years ago
|
#include <algorithm>
|
||
10 years ago
|
#include <atomic>
|
||
7 years ago
|
#include <chrono>
|
||
11 years ago
|
#include <deque>
|
||
4 years ago
|
|
||
10 years ago
|
#include "port/port.h"
|
||
11 years ago
|
#include "rocksdb/env.h"
|
||
11 years ago
|
#include "rocksdb/rate_limiter.h"
|
||
3 years ago
|
#include "rocksdb/status.h"
|
||
4 years ago
|
#include "rocksdb/system_clock.h"
|
||
7 years ago
|
#include "util/mutexlock.h"
|
||
|
#include "util/random.h"
|
||
11 years ago
|
|
||
5 years ago
|
namespace ROCKSDB_NAMESPACE {
|
||
11 years ago
|
|
||
11 years ago
|
class GenericRateLimiter : public RateLimiter {
|
||
11 years ago
|
public:
|
||
2 years ago
|
GenericRateLimiter(int64_t refill_bytes, int64_t refill_period_us,
|
||
|
int32_t fairness, RateLimiter::Mode mode,
|
||
|
const std::shared_ptr<SystemClock>& clock,
|
||
|
bool auto_tuned);
|
||
11 years ago
|
|
||
11 years ago
|
virtual ~GenericRateLimiter();
|
||
11 years ago
|
|
||
10 years ago
|
// This API allows user to dynamically change rate limiter's bytes per second.
|
||
|
virtual void SetBytesPerSecond(int64_t bytes_per_second) override;
|
||
|
|
||
11 years ago
|
// Request for token to write bytes. If this request can not be satisfied,
|
||
|
// the call is blocked. Caller is responsible to make sure
|
||
3 years ago
|
// bytes <= GetSingleBurstBytes() and bytes >= 0. Negative bytes
|
||
|
// passed in will be rounded up to 0.
|
||
8 years ago
|
using RateLimiter::Request;
|
||
|
virtual void Request(const int64_t bytes, const Env::IOPriority pri,
|
||
|
Statistics* stats) override;
|
||
11 years ago
|
|
||
|
virtual int64_t GetSingleBurstBytes() const override {
|
||
10 years ago
|
return refill_bytes_per_period_.load(std::memory_order_relaxed);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
virtual int64_t GetTotalBytesThrough(
|
||
|
const Env::IOPriority pri = Env::IO_TOTAL) const override {
|
||
11 years ago
|
MutexLock g(&request_mutex_);
|
||
|
if (pri == Env::IO_TOTAL) {
|
||
3 years ago
|
int64_t total_bytes_through_sum = 0;
|
||
|
for (int i = Env::IO_LOW; i < Env::IO_TOTAL; ++i) {
|
||
|
total_bytes_through_sum += total_bytes_through_[i];
|
||
|
}
|
||
|
return total_bytes_through_sum;
|
||
11 years ago
|
}
|
||
|
return total_bytes_through_[pri];
|
||
|
}
|
||
|
|
||
11 years ago
|
virtual int64_t GetTotalRequests(
|
||
|
const Env::IOPriority pri = Env::IO_TOTAL) const override {
|
||
11 years ago
|
MutexLock g(&request_mutex_);
|
||
|
if (pri == Env::IO_TOTAL) {
|
||
3 years ago
|
int64_t total_requests_sum = 0;
|
||
|
for (int i = Env::IO_LOW; i < Env::IO_TOTAL; ++i) {
|
||
|
total_requests_sum += total_requests_[i];
|
||
|
}
|
||
|
return total_requests_sum;
|
||
11 years ago
|
}
|
||
|
return total_requests_[pri];
|
||
|
}
|
||
|
|
||
3 years ago
|
virtual Status GetTotalPendingRequests(
|
||
|
int64_t* total_pending_requests,
|
||
3 years ago
|
const Env::IOPriority pri = Env::IO_TOTAL) const override {
|
||
3 years ago
|
assert(total_pending_requests != nullptr);
|
||
3 years ago
|
MutexLock g(&request_mutex_);
|
||
|
if (pri == Env::IO_TOTAL) {
|
||
|
int64_t total_pending_requests_sum = 0;
|
||
|
for (int i = Env::IO_LOW; i < Env::IO_TOTAL; ++i) {
|
||
|
total_pending_requests_sum += static_cast<int64_t>(queue_[i].size());
|
||
|
}
|
||
3 years ago
|
*total_pending_requests = total_pending_requests_sum;
|
||
|
} else {
|
||
|
*total_pending_requests = static_cast<int64_t>(queue_[pri].size());
|
||
3 years ago
|
}
|
||
3 years ago
|
return Status::OK();
|
||
3 years ago
|
}
|
||
|
|
||
8 years ago
|
virtual int64_t GetBytesPerSecond() const override {
|
||
2 years ago
|
return rate_bytes_per_sec_.load(std::memory_order_relaxed);
|
||
8 years ago
|
}
|
||
|
|
||
2 years ago
|
virtual void TEST_SetClock(std::shared_ptr<SystemClock> clock) {
|
||
|
MutexLock g(&request_mutex_);
|
||
|
clock_ = std::move(clock);
|
||
2 years ago
|
next_refill_us_ = NowMicrosMonotonicLocked();
|
||
2 years ago
|
}
|
||
|
|
||
11 years ago
|
private:
|
||
2 years ago
|
void RefillBytesAndGrantRequestsLocked();
|
||
|
std::vector<Env::IOPriority> GeneratePriorityIterationOrderLocked();
|
||
|
int64_t CalculateRefillBytesPerPeriodLocked(int64_t rate_bytes_per_sec);
|
||
|
Status TuneLocked();
|
||
|
void SetBytesPerSecondLocked(int64_t bytes_per_second);
|
||
|
|
||
|
uint64_t NowMicrosMonotonicLocked() {
|
||
|
return clock_->NowNanos() / std::milli::den;
|
||
|
}
|
||
11 years ago
|
|
||
|
// This mutex guard all internal states
|
||
|
mutable port::Mutex request_mutex_;
|
||
|
|
||
2 years ago
|
const int64_t refill_period_us_;
|
||
8 years ago
|
|
||
2 years ago
|
std::atomic<int64_t> rate_bytes_per_sec_;
|
||
10 years ago
|
std::atomic<int64_t> refill_bytes_per_period_;
|
||
2 years ago
|
std::shared_ptr<SystemClock> clock_;
|
||
11 years ago
|
|
||
|
bool stop_;
|
||
|
port::CondVar exit_cv_;
|
||
|
int32_t requests_to_wait_;
|
||
|
|
||
|
int64_t total_requests_[Env::IO_TOTAL];
|
||
|
int64_t total_bytes_through_[Env::IO_TOTAL];
|
||
|
int64_t available_bytes_;
|
||
|
int64_t next_refill_us_;
|
||
|
|
||
2 years ago
|
int32_t fairness_;
|
||
11 years ago
|
Random rnd_;
|
||
|
|
||
|
struct Req;
|
||
|
std::deque<Req*> queue_[Env::IO_TOTAL];
|
||
3 years ago
|
bool wait_until_refill_pending_;
|
||
7 years ago
|
|
||
2 years ago
|
bool auto_tuned_;
|
||
7 years ago
|
int64_t num_drains_;
|
||
2 years ago
|
const int64_t max_bytes_per_sec_;
|
||
7 years ago
|
std::chrono::microseconds tuned_time_;
|
||
11 years ago
|
};
|
||
|
|
||
5 years ago
|
} // namespace ROCKSDB_NAMESPACE
|