From 9386a58bf2f9537fcaa527308727b8cb4cc59a4c Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sun, 16 Nov 2014 13:14:42 +0100 Subject: [PATCH] Implement an experimental variable to limit the number of threads --- icinga-app/icinga.cpp | 1 + lib/base/application.cpp | 22 ++++++++++++++++++++++ lib/base/application.hpp | 3 +++ lib/base/threadpool.cpp | 3 ++- lib/base/workqueue.cpp | 3 ++- 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index 14925ec08..38525fc65 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -156,6 +156,7 @@ int Main(void) Application::DeclareApplicationType("icinga/IcingaApplication"); Application::DeclareRunAsUser(ICINGA_USER); Application::DeclareRunAsGroup(ICINGA_GROUP); + Application::DeclareConcurrency(boost::thread::hardware_concurrency()); LogSeverity logLevel = Logger::GetConsoleLogSeverity(); Logger::SetConsoleLogSeverity(LogWarning); diff --git a/lib/base/application.cpp b/lib/base/application.cpp index d5085bfd7..6e489d3ef 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -1093,6 +1093,7 @@ void Application::DeclareRunAsUser(const String& user) { ScriptVariable::Set("RunAsUser", user, false); } + /** * Retrieves the name of the group. * @@ -1103,6 +1104,27 @@ String Application::GetRunAsGroup(void) return ScriptVariable::Get("RunAsGroup"); } +/** + * Sets the concurrency level. + * + * @param path The new concurrency level. + */ +void Application::DeclareConcurrency(int ncpus) +{ + ScriptVariable::Set("Concurrency", ncpus, false); +} + +/** + * Retrieves the concurrency level. + * + * @returns The concurrency level. + */ +int Application::GetConcurrency(void) +{ + Value defaultConcurrency = boost::thread::hardware_concurrency(); + return ScriptVariable::Get("Concurrency", &defaultConcurrency); +} + /** * Sets the name of the group. * diff --git a/lib/base/application.hpp b/lib/base/application.hpp index 42db85696..be662297e 100644 --- a/lib/base/application.hpp +++ b/lib/base/application.hpp @@ -122,6 +122,9 @@ public: static String GetRunAsGroup(void); static void DeclareRunAsGroup(const String& group); + static int GetConcurrency(void); + static void DeclareConcurrency(int ncpus); + static void MakeVariablesConstant(void); static ThreadPool& GetTP(void); diff --git a/lib/base/threadpool.cpp b/lib/base/threadpool.cpp index b9771c406..2b2682860 100644 --- a/lib/base/threadpool.cpp +++ b/lib/base/threadpool.cpp @@ -22,6 +22,7 @@ #include "base/debug.hpp" #include "base/utility.hpp" #include "base/exception.hpp" +#include "base/application.hpp" #include #include @@ -260,7 +261,7 @@ void ThreadPool::ManagerThreadProc(void) int tthreads = wthreads - alive; /* Make sure there is at least one thread per CPU */ - int ncput = std::max(boost::thread::hardware_concurrency() / QUEUECOUNT, 4U); + int ncput = std::max(static_cast(Application::GetConcurrency()) / QUEUECOUNT, 4U); if (alive + tthreads < ncput) tthreads = ncput - alive; diff --git a/lib/base/workqueue.cpp b/lib/base/workqueue.cpp index 1d0256329..0450493b5 100644 --- a/lib/base/workqueue.cpp +++ b/lib/base/workqueue.cpp @@ -21,6 +21,7 @@ #include "base/utility.hpp" #include "base/logger.hpp" #include "base/convert.hpp" +#include "base/application.hpp" #include #include @@ -177,7 +178,7 @@ void WorkQueue::WorkerThreadProc(void) } ParallelWorkQueue::ParallelWorkQueue(void) - : m_QueueCount(boost::thread::hardware_concurrency()), + : m_QueueCount(Application::GetConcurrency()), m_Queues(new WorkQueue[m_QueueCount]), m_Index(0) { }