rate_limiter.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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).
  5. //
  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.
  9. #pragma once
  10. #include <algorithm>
  11. #include <atomic>
  12. #include <chrono>
  13. #include <deque>
  14. #include "port/port.h"
  15. #include "rocksdb/env.h"
  16. #include "rocksdb/rate_limiter.h"
  17. #include "util/mutexlock.h"
  18. #include "util/random.h"
  19. namespace ROCKSDB_NAMESPACE {
  20. class GenericRateLimiter : public RateLimiter {
  21. public:
  22. GenericRateLimiter(int64_t refill_bytes, int64_t refill_period_us,
  23. int32_t fairness, RateLimiter::Mode mode, Env* env,
  24. bool auto_tuned);
  25. virtual ~GenericRateLimiter();
  26. // This API allows user to dynamically change rate limiter's bytes per second.
  27. virtual void SetBytesPerSecond(int64_t bytes_per_second) override;
  28. // Request for token to write bytes. If this request can not be satisfied,
  29. // the call is blocked. Caller is responsible to make sure
  30. // bytes <= GetSingleBurstBytes()
  31. using RateLimiter::Request;
  32. virtual void Request(const int64_t bytes, const Env::IOPriority pri,
  33. Statistics* stats) override;
  34. virtual int64_t GetSingleBurstBytes() const override {
  35. return refill_bytes_per_period_.load(std::memory_order_relaxed);
  36. }
  37. virtual int64_t GetTotalBytesThrough(
  38. const Env::IOPriority pri = Env::IO_TOTAL) const override {
  39. MutexLock g(&request_mutex_);
  40. if (pri == Env::IO_TOTAL) {
  41. return total_bytes_through_[Env::IO_LOW] +
  42. total_bytes_through_[Env::IO_HIGH];
  43. }
  44. return total_bytes_through_[pri];
  45. }
  46. virtual int64_t GetTotalRequests(
  47. const Env::IOPriority pri = Env::IO_TOTAL) const override {
  48. MutexLock g(&request_mutex_);
  49. if (pri == Env::IO_TOTAL) {
  50. return total_requests_[Env::IO_LOW] + total_requests_[Env::IO_HIGH];
  51. }
  52. return total_requests_[pri];
  53. }
  54. virtual int64_t GetBytesPerSecond() const override {
  55. return rate_bytes_per_sec_;
  56. }
  57. private:
  58. void Refill();
  59. int64_t CalculateRefillBytesPerPeriod(int64_t rate_bytes_per_sec);
  60. Status Tune();
  61. uint64_t NowMicrosMonotonic(Env* env) {
  62. return env->NowNanos() / std::milli::den;
  63. }
  64. // This mutex guard all internal states
  65. mutable port::Mutex request_mutex_;
  66. const int64_t kMinRefillBytesPerPeriod = 100;
  67. const int64_t refill_period_us_;
  68. int64_t rate_bytes_per_sec_;
  69. // This variable can be changed dynamically.
  70. std::atomic<int64_t> refill_bytes_per_period_;
  71. Env* const env_;
  72. bool stop_;
  73. port::CondVar exit_cv_;
  74. int32_t requests_to_wait_;
  75. int64_t total_requests_[Env::IO_TOTAL];
  76. int64_t total_bytes_through_[Env::IO_TOTAL];
  77. int64_t available_bytes_;
  78. int64_t next_refill_us_;
  79. int32_t fairness_;
  80. Random rnd_;
  81. struct Req;
  82. Req* leader_;
  83. std::deque<Req*> queue_[Env::IO_TOTAL];
  84. bool auto_tuned_;
  85. int64_t num_drains_;
  86. int64_t prev_num_drains_;
  87. const int64_t max_bytes_per_sec_;
  88. std::chrono::microseconds tuned_time_;
  89. };
  90. } // namespace ROCKSDB_NAMESPACE