From 16dfb865b1750b277137278b1dcb5445df225cd7 Mon Sep 17 00:00:00 2001 From: Razvan Becheriu Date: Tue, 18 Feb 2020 09:30:17 +0200 Subject: [PATCH] [#892] refactored --- src/bin/dhcp4/ctrl_dhcp4_srv.cc | 13 +++------- src/bin/dhcp4/dhcp4_srv.cc | 8 +++--- src/bin/dhcp6/ctrl_dhcp6_srv.cc | 13 +++------- src/bin/dhcp6/dhcp6_srv.cc | 8 +++--- src/lib/dhcpsrv/multi_threading_utils.cc | 20 --------------- src/lib/dhcpsrv/multi_threading_utils.h | 13 +--------- src/lib/util/multi_threading_mgr.cc | 32 ++++++++++++++++++++++++ src/lib/util/multi_threading_mgr.h | 15 +++++++++++ 8 files changed, 60 insertions(+), 62 deletions(-) diff --git a/src/bin/dhcp4/ctrl_dhcp4_srv.cc b/src/bin/dhcp4/ctrl_dhcp4_srv.cc index f8f8440d95..1ebf153779 100644 --- a/src/bin/dhcp4/ctrl_dhcp4_srv.cc +++ b/src/bin/dhcp4/ctrl_dhcp4_srv.cc @@ -169,16 +169,9 @@ ControlledDhcpv4Srv::loadConfigFile(const std::string& file_name) { "processCommand(\"config-set\", json)"); } - if (MultiThreadingUtil::threadCount()) { - auto& thread_pool = MultiThreadingMgr::instance().getPktThreadPool(); - if (thread_pool.size()) { - thread_pool.stop(); - } - MultiThreadingMgr::instance().setMode(true); - thread_pool.start(MultiThreadingUtil::threadCount()); - } else { - MultiThreadingMgr::instance().setMode(false); - } + // @todo enable multi-threading - disabled for now + MultiThreadingMgr::instance().apply(false, + CfgMgr::instance().getCurrentCfg()->getServerThreadCount()); // Now check is the returned result is successful (rcode=0) or not // (see @ref isc::config::parseAnswer). diff --git a/src/bin/dhcp4/dhcp4_srv.cc b/src/bin/dhcp4/dhcp4_srv.cc index 6a2895adc6..d5b2c34846 100644 --- a/src/bin/dhcp4/dhcp4_srv.cc +++ b/src/bin/dhcp4/dhcp4_srv.cc @@ -803,9 +803,7 @@ Dhcpv4Srv::run() { } // destroying the thread pool - if (MultiThreadingMgr::instance().getMode()) { - MultiThreadingMgr::instance().getPktThreadPool().reset(); - } + MultiThreadingMgr::instance().apply(false, 0); return (true); } @@ -821,8 +819,8 @@ Dhcpv4Srv::run_one() { // Do not read more packets from socket if there are enough // packets to be processed in the packet thread pool queue - const int max_queue_size = MultiThreadingUtil::maxThreadQueueSize(); - const int thread_count = MultiThreadingUtil::threadCount(); + const int max_queue_size = CfgMgr::instance().getCurrentCfg()->getServerMaxThreadQueueSize(); + const int thread_count = MultiThreadingMgr::instance().getPktThreadPoolSize(); size_t pkt_queue_size = MultiThreadingMgr::instance().getPktThreadPool().count(); if (thread_count && (pkt_queue_size >= thread_count * max_queue_size)) { read_pkt = false; diff --git a/src/bin/dhcp6/ctrl_dhcp6_srv.cc b/src/bin/dhcp6/ctrl_dhcp6_srv.cc index 578eb27c60..768453cc7a 100644 --- a/src/bin/dhcp6/ctrl_dhcp6_srv.cc +++ b/src/bin/dhcp6/ctrl_dhcp6_srv.cc @@ -141,16 +141,9 @@ ControlledDhcpv6Srv::loadConfigFile(const std::string& file_name) { "processCommand(\"config-set\", json)"); } - if (MultiThreadingUtil::threadCount()) { - auto& thread_pool = MultiThreadingMgr::instance().getPktThreadPool(); - if (thread_pool.size()) { - thread_pool.stop(); - } - MultiThreadingMgr::instance().setMode(true); - thread_pool.start(MultiThreadingUtil::threadCount()); - } else { - MultiThreadingMgr::instance().setMode(false); - } + // @todo enable multi-threading - disabled for now + MultiThreadingMgr::instance().apply(false, + CfgMgr::instance().getCurrentCfg()->getServerThreadCount()); // Now check is the returned result is successful (rcode=0) or not // (see @ref isc::config::parseAnswer). diff --git a/src/bin/dhcp6/dhcp6_srv.cc b/src/bin/dhcp6/dhcp6_srv.cc index 7ba4c8d951..1f28dd4a12 100644 --- a/src/bin/dhcp6/dhcp6_srv.cc +++ b/src/bin/dhcp6/dhcp6_srv.cc @@ -473,9 +473,7 @@ bool Dhcpv6Srv::run() { } // destroying the thread pool - if (MultiThreadingMgr::instance().getMode()) { - MultiThreadingMgr::instance().getPktThreadPool().reset(); - } + MultiThreadingMgr::instance().apply(false, 0); return (true); } @@ -490,8 +488,8 @@ void Dhcpv6Srv::run_one() { // Do not read more packets from socket if there are enough // packets to be processed in the packet thread pool queue - const int max_queue_size = MultiThreadingUtil::maxThreadQueueSize(); - const int thread_count = MultiThreadingUtil::threadCount(); + const int max_queue_size = CfgMgr::instance().getCurrentCfg()->getServerMaxThreadQueueSize(); + const int thread_count = MultiThreadingMgr::instance().getPktThreadPoolSize(); size_t pkt_queue_size = MultiThreadingMgr::instance().getPktThreadPool().count(); if (thread_count && (pkt_queue_size >= thread_count * max_queue_size)) { read_pkt = false; diff --git a/src/lib/dhcpsrv/multi_threading_utils.cc b/src/lib/dhcpsrv/multi_threading_utils.cc index fe9e28345a..37ccc71cd5 100644 --- a/src/lib/dhcpsrv/multi_threading_utils.cc +++ b/src/lib/dhcpsrv/multi_threading_utils.cc @@ -6,7 +6,6 @@ #include -#include #include #include #include @@ -43,24 +42,5 @@ MultiThreadingCriticalSection::~MultiThreadingCriticalSection() { } } -uint32_t -MultiThreadingUtil::threadCount() { - uint32_t sys_threads = CfgMgr::instance().getCurrentCfg()->getServerThreadCount(); - if (sys_threads) { - return sys_threads; - } - sys_threads = std::thread::hardware_concurrency(); - return sys_threads * 0; -} - -uint32_t -MultiThreadingUtil::maxThreadQueueSize() { - uint32_t max_thread_queue_size = CfgMgr::instance().getCurrentCfg()->getServerMaxThreadQueueSize(); - if (max_thread_queue_size) { - return max_thread_queue_size; - } - return 4; -} - } // namespace dhcp } // namespace isc diff --git a/src/lib/dhcpsrv/multi_threading_utils.h b/src/lib/dhcpsrv/multi_threading_utils.h index ba944c4a51..3136f2a75a 100644 --- a/src/lib/dhcpsrv/multi_threading_utils.h +++ b/src/lib/dhcpsrv/multi_threading_utils.h @@ -9,8 +9,6 @@ #include -#include - namespace isc { namespace dhcp { @@ -48,16 +46,7 @@ public: static void startPktProcessing(); }; -class MultiThreadingUtil { -public: - - /// @brief returns Kea DHCPv4 server thread count. - static uint32_t threadCount(); - - /// @brief returns Kea DHCPv4 server max thread queue size. - static uint32_t maxThreadQueueSize(); -}; - } // namespace dhcp } // namespace isc + #endif // MULTI_THREADING_UTIL_H diff --git a/src/lib/util/multi_threading_mgr.cc b/src/lib/util/multi_threading_mgr.cc index 8695905a63..a68ffcf77d 100644 --- a/src/lib/util/multi_threading_mgr.cc +++ b/src/lib/util/multi_threading_mgr.cc @@ -46,5 +46,37 @@ MultiThreadingMgr::setPktThreadPoolSize(uint32_t size) { pkt_thread_pool_size_ = size; } +uint32_t +MultiThreadingMgr::supportedThreadCount(uint32_t thread_count) { + return (std::thread::hardware_concurrency()); +} + +void +MultiThreadingMgr::apply(bool enabled, uint32_t thread_count) { + // check the enabled flag + if (enabled) { + // check for auto scaling (enabled flag true but thread_count 0) + if (!thread_count) { + // might also return 0 + thread_count = MultiThreadingMgr::supportedThreadCount(); + } + } else { + thread_count = 0; + } + // check enabled flag and explicit number of threads or system supports + // hardware concurrency + if (thread_count) { + if (pkt_thread_pool_.size()) { + pkt_thread_pool_.stop(); + } + setPktThreadPoolSize(thread_count); + setMode(true); + pkt_thread_pool_.start(thread_count); + } else { + pkt_thread_pool_.reset(); + setMode(false); + } +} + } // namespace util } // namespace isc diff --git a/src/lib/util/multi_threading_mgr.h b/src/lib/util/multi_threading_mgr.h index b9b9b32f2a..1d5728d90f 100644 --- a/src/lib/util/multi_threading_mgr.h +++ b/src/lib/util/multi_threading_mgr.h @@ -81,6 +81,21 @@ public: /// @param size The packet thread pool size of this binary instance. void setPktThreadPoolSize(uint32_t size); + /// @brief The system current supported hardware concurrency thread count. + /// + /// This function will return 0 if the value can not be determined. + /// + /// @return The thread count. + static uint32_t supportedThreadCount(); + + /// @brief Apply the multi-threading related settings + /// + /// @param enabled The enabled flag: true if multi-threading is enabled, + /// false otherwise. + /// @param thread_count The number of desired threads: non 0 if explicitly + /// configured, 0 if auto scaling is desired + void apply(bool enabled, uint32_t thread_count); + protected: /// @brief Constructor.