]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#892] refactored
authorRazvan Becheriu <razvan@isc.org>
Tue, 18 Feb 2020 07:30:17 +0000 (09:30 +0200)
committerRazvan Becheriu <razvan@isc.org>
Fri, 21 Feb 2020 15:41:31 +0000 (17:41 +0200)
src/bin/dhcp4/ctrl_dhcp4_srv.cc
src/bin/dhcp4/dhcp4_srv.cc
src/bin/dhcp6/ctrl_dhcp6_srv.cc
src/bin/dhcp6/dhcp6_srv.cc
src/lib/dhcpsrv/multi_threading_utils.cc
src/lib/dhcpsrv/multi_threading_utils.h
src/lib/util/multi_threading_mgr.cc
src/lib/util/multi_threading_mgr.h

index f8f8440d9534f412b6fdd0a21fd0a6c78927f47c..1ebf153779070c60723a69c9a648d052e2283be6 100644 (file)
@@ -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).
index 6a2895adc6ab82b856454700cbe1a2b38a011492..d5b2c34846b6448b357cb887e942527fb5b9ef6f 100644 (file)
@@ -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;
index 578eb27c600abd12a75f6eda142bfe673647ac83..768453cc7ab8c5368d0c2302be5dfe839ff15041 100644 (file)
@@ -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).
index 7ba4c8d951faca1ae01053ddcc26acb281450e54..1f28dd4a12a6e40a1ce54239da5a8c467c503f1e 100644 (file)
@@ -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;
index fe9e28345a4fd6e980d4afdd55dc523d499cc84c..37ccc71cd543a2717c7b8fbe4b4de5e844d6455c 100644 (file)
@@ -6,7 +6,6 @@
 
 #include <config.h>
 
-#include <dhcpsrv/cfgmgr.h>
 #include <dhcpsrv/multi_threading_utils.h>
 #include <exceptions/exceptions.h>
 #include <util/multi_threading_mgr.h>
@@ -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
index ba944c4a51bb2db3b45e0a5d72df32f359926f06..3136f2a75a441561cb3c5f99c5a1a8919de2530b 100644 (file)
@@ -9,8 +9,6 @@
 
 #include <boost/noncopyable.hpp>
 
-#include <stdint.h>
-
 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
index 8695905a63233f32fb190f44f844213c370fe4d9..a68ffcf77db6b79f87a5c671ccfd814fe607d6ca 100644 (file)
@@ -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
index b9b9b32f2ab92295624319c8890a3b1122b8657f..1d5728d90f4d3dad7895766dfdc8d93f3d276706 100644 (file)
@@ -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.