From: Radoslaw Zarzynski Date: Thu, 18 Jun 2020 13:24:14 +0000 (+0200) Subject: crimson: move Throttle to src/crimson/common. X-Git-Tag: v17.0.0~2045^2 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=0158c84c8361ffcfe5031be17afbd1c9842c950e;p=ceph.git crimson: move Throttle to src/crimson/common. Signed-off-by: Radoslaw Zarzynski --- diff --git a/src/crimson/CMakeLists.txt b/src/crimson/CMakeLists.txt index f11718fdeb2b7..e5b4da69888ea 100644 --- a/src/crimson/CMakeLists.txt +++ b/src/crimson/CMakeLists.txt @@ -19,7 +19,8 @@ set(crimson_common_srcs common/config_proxy.cc common/formatter.cc common/perf_counters_collection.cc - common/log.cc) + common/log.cc + common/throttle.cc) # the specialized version of ceph-common, where # - the logging is sent to Seastar backend @@ -166,14 +167,11 @@ set(crimson_net_srcs net/ProtocolV1.cc net/ProtocolV2.cc net/chained_dispatchers.cc) -set(crimson_thread_srcs - thread/Throttle.cc) add_library(crimson STATIC ${crimson_auth_srcs} ${crimson_mgr_srcs} ${crimson_mon_srcs} ${crimson_net_srcs} - ${crimson_thread_srcs} ${CMAKE_SOURCE_DIR}/src/common/buffer_seastar.cc) target_compile_options(crimson PUBLIC "-ftemplate-backtrace-limit=0") diff --git a/src/crimson/common/throttle.cc b/src/crimson/common/throttle.cc new file mode 100644 index 0000000000000..4ddf77dee227a --- /dev/null +++ b/src/crimson/common/throttle.cc @@ -0,0 +1,59 @@ +#include "throttle.h" + +namespace crimson::common { + +int64_t Throttle::take(int64_t c) +{ + if (!max) { + return 0; + } + count += c; + return count; +} + +int64_t Throttle::put(int64_t c) +{ + if (!max) { + return 0; + } + if (!c) { + return count; + } + on_free_slots.signal(); + count -= c; + return count; +} + +seastar::future<> Throttle::get(size_t c) +{ + if (!max) { + return seastar::now(); + } + return on_free_slots.wait([this, c] { + return !_should_wait(c); + }).then([this, c] { + count += c; + return seastar::now(); + }); +} + +void Throttle::reset_max(size_t m) { + if (max == m) { + return; + } + + if (m > max) { + on_free_slots.signal(); + } + max = m; +} + +bool Throttle::_should_wait(size_t c) const { + if (!max) { + return false; + } + return ((c <= max && count + c > max) || // normally stay under max + (c >= max && count > max)); // except for large c +} + +} // namespace crimson::common diff --git a/src/crimson/common/throttle.h b/src/crimson/common/throttle.h new file mode 100644 index 0000000000000..fea471c8d61fd --- /dev/null +++ b/src/crimson/common/throttle.h @@ -0,0 +1,39 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*- +// vim: ts=8 sw=2 smarttab + +#pragma once + +#include +// pull seastar::timer<...>::timer definitions. FIX SEASTAR or reactor.hh +// is obligatory and should be included everywhere? +#include + +#include "common/ThrottleInterface.h" + +namespace crimson::common { + +class Throttle final : public ThrottleInterface { + size_t max = 0; + size_t count = 0; + // we cannot change the "count" of seastar::semaphore after it is created, + // so use condition_variable instead. + seastar::condition_variable on_free_slots; +public: + explicit Throttle(size_t m) + : max(m) + {} + int64_t take(int64_t c = 1) override; + int64_t put(int64_t c = 1) override; + seastar::future<> get(size_t c); + size_t get_current() const { + return count; + } + size_t get_max() const { + return max; + } + void reset_max(size_t m); +private: + bool _should_wait(size_t c) const; +}; + +} // namespace crimson::common diff --git a/src/crimson/net/Messenger.h b/src/crimson/net/Messenger.h index 2b136aa4fe09b..7065d2ad2afdf 100644 --- a/src/crimson/net/Messenger.h +++ b/src/crimson/net/Messenger.h @@ -17,8 +17,8 @@ #include #include "Fwd.h" +#include "crimson/common/throttle.h" #include "crimson/net/chained_dispatchers.h" -#include "crimson/thread/Throttle.h" #include "msg/Message.h" #include "msg/Policy.h" @@ -35,7 +35,7 @@ namespace crimson::net { class Interceptor; #endif -using Throttle = crimson::thread::Throttle; +using Throttle = crimson::common::Throttle; using SocketPolicy = ceph::net::Policy; class Messenger { diff --git a/src/crimson/net/SocketConnection.h b/src/crimson/net/SocketConnection.h index 67657a6e19051..0af08e0e4f28c 100644 --- a/src/crimson/net/SocketConnection.h +++ b/src/crimson/net/SocketConnection.h @@ -17,10 +17,10 @@ #include #include "msg/Policy.h" +#include "crimson/common/throttle.h" #include "crimson/net/chained_dispatchers.h" #include "crimson/net/Connection.h" #include "crimson/net/Socket.h" -#include "crimson/thread/Throttle.h" namespace crimson::net { @@ -34,7 +34,7 @@ class SocketConnection : public Connection { SocketMessenger& messenger; std::unique_ptr protocol; - ceph::net::Policy policy; + ceph::net::Policy policy; /// the seq num of the last transmitted message seq_num_t out_seq = 0; diff --git a/src/crimson/thread/Throttle.cc b/src/crimson/thread/Throttle.cc deleted file mode 100644 index 99be02fa65865..0000000000000 --- a/src/crimson/thread/Throttle.cc +++ /dev/null @@ -1,59 +0,0 @@ -#include "Throttle.h" - -namespace crimson::thread { - -int64_t Throttle::take(int64_t c) -{ - if (!max) { - return 0; - } - count += c; - return count; -} - -int64_t Throttle::put(int64_t c) -{ - if (!max) { - return 0; - } - if (!c) { - return count; - } - on_free_slots.signal(); - count -= c; - return count; -} - -seastar::future<> Throttle::get(size_t c) -{ - if (!max) { - return seastar::now(); - } - return on_free_slots.wait([this, c] { - return !_should_wait(c); - }).then([this, c] { - count += c; - return seastar::now(); - }); -} - -void Throttle::reset_max(size_t m) { - if (max == m) { - return; - } - - if (m > max) { - on_free_slots.signal(); - } - max = m; -} - -bool Throttle::_should_wait(size_t c) const { - if (!max) { - return false; - } - return ((c <= max && count + c > max) || // normally stay under max - (c >= max && count > max)); // except for large c -} - -} // namespace crimson::thread diff --git a/src/crimson/thread/Throttle.h b/src/crimson/thread/Throttle.h deleted file mode 100644 index 117924b2e9471..0000000000000 --- a/src/crimson/thread/Throttle.h +++ /dev/null @@ -1,39 +0,0 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*- -// vim: ts=8 sw=2 smarttab - -#pragma once - -#include -// pull seastar::timer<...>::timer definitions. FIX SEASTAR or reactor.hh -// is obligatory and should be included everywhere? -#include - -#include "common/ThrottleInterface.h" - -namespace crimson::thread { - -class Throttle final : public ThrottleInterface { - size_t max = 0; - size_t count = 0; - // we cannot change the "count" of seastar::semaphore after it is created, - // so use condition_variable instead. - seastar::condition_variable on_free_slots; -public: - explicit Throttle(size_t m) - : max(m) - {} - int64_t take(int64_t c = 1) override; - int64_t put(int64_t c = 1) override; - seastar::future<> get(size_t c); - size_t get_current() const { - return count; - } - size_t get_max() const { - return max; - } - void reset_max(size_t m); -private: - bool _should_wait(size_t c) const; -}; - -} // namespace crimson::thread diff --git a/src/test/crimson/test_alien_echo.cc b/src/test/crimson/test_alien_echo.cc index 0a0f22eb9aeba..b5710adcbea07 100644 --- a/src/test/crimson/test_alien_echo.cc +++ b/src/test/crimson/test_alien_echo.cc @@ -4,10 +4,10 @@ #include "messages/MPing.h" #include "common/ceph_argparse.h" #include "crimson/auth/DummyAuth.h" +#include "crimson/common/throttle.h" #include "crimson/net/Connection.h" #include "crimson/net/Dispatcher.h" #include "crimson/net/Messenger.h" -#include "crimson/thread/Throttle.h" #include #include @@ -38,7 +38,7 @@ struct DummyAuthAuthorizer : public AuthAuthorizer { }; struct Server { - crimson::thread::Throttle byte_throttler; + crimson::common::Throttle byte_throttler; crimson::net::MessengerRef msgr; crimson::auth::DummyAuthClientServer dummy_auth; struct ServerDispatcher : crimson::net::Dispatcher { @@ -64,7 +64,7 @@ struct Server { }; struct Client { - crimson::thread::Throttle byte_throttler; + crimson::common::Throttle byte_throttler; crimson::net::MessengerRef msgr; crimson::auth::DummyAuthClientServer dummy_auth; struct ClientDispatcher : crimson::net::Dispatcher {