Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions icinga-app/icinga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ static int Main()

Configuration::SetReadOnly(true);

if (!Configuration::ConcurrencyWasModified) {
Configuration::Concurrency = std::thread::hardware_concurrency();
}

Application::GetTP().Restart();

/* Ensure that all defined constants work in the way we expect them. */
HandleLegacyDefines();

Expand Down
4 changes: 3 additions & 1 deletion lib/base/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ String Configuration::ApiBindHost;
String Configuration::ApiBindPort{"5665"};
bool Configuration::AttachDebugger{false};
String Configuration::CacheDir;
int Configuration::Concurrency{static_cast<int>(std::thread::hardware_concurrency())};
int Configuration::Concurrency{1};
bool Configuration::ConcurrencyWasModified{false};
String Configuration::ConfigDir;
String Configuration::DataDir;
String Configuration::EventEngine;
Expand Down Expand Up @@ -101,6 +102,7 @@ int Configuration::GetConcurrency() const
void Configuration::SetConcurrency(int val, bool suppress_events, const Value& cookie)
{
HandleUserWrite("Concurrency", &Configuration::Concurrency, val, m_ReadOnly);
Configuration::ConcurrencyWasModified = true;
}

String Configuration::GetConfigDir() const
Expand Down
1 change: 1 addition & 0 deletions lib/base/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class Configuration : public ObjectImpl<Configuration>
static bool AttachDebugger;
static String CacheDir;
static int Concurrency;
static bool ConcurrencyWasModified;
static String ConfigDir;
static String DataDir;
static String EventEngine;
Expand Down
5 changes: 3 additions & 2 deletions lib/base/io-engine.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "base/configuration.hpp"
#include "base/exception.hpp"
#include "base/io-engine.hpp"
#include "base/lazy-init.hpp"
Expand Down Expand Up @@ -84,10 +85,10 @@ boost::asio::io_context& IoEngine::GetIoContext()
return m_IoContext;
}

IoEngine::IoEngine() : m_IoContext(), m_KeepAlive(boost::asio::make_work_guard(m_IoContext)), m_Threads(decltype(m_Threads)::size_type(std::thread::hardware_concurrency() * 2u)), m_AlreadyExpiredTimer(m_IoContext)
IoEngine::IoEngine() : m_IoContext(), m_KeepAlive(boost::asio::make_work_guard(m_IoContext)), m_Threads(decltype(m_Threads)::size_type(Configuration::Concurrency * 2u)), m_AlreadyExpiredTimer(m_IoContext)
{
m_AlreadyExpiredTimer.expires_at(boost::posix_time::neg_infin);
m_CpuBoundSemaphore.store(std::thread::hardware_concurrency() * 3u / 2u);
m_CpuBoundSemaphore.store(Configuration::Concurrency * 3u / 2u);

for (auto& thread : m_Threads) {
thread = std::thread(&IoEngine::RunEventLoop, this);
Expand Down
21 changes: 18 additions & 3 deletions lib/base/threadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

using namespace icinga;

ThreadPool::ThreadPool(size_t threads)
: m_Threads(threads), m_Pending(0)
ThreadPool::ThreadPool() : m_Pending(0)
{
Start();
}
Expand All @@ -21,10 +20,15 @@ void ThreadPool::Start()
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);

if (!m_Pool) {
m_Pool = decltype(m_Pool)(new boost::asio::thread_pool(m_Threads));
InitializePool();
}
}

void ThreadPool::InitializePool()
{
m_Pool = decltype(m_Pool)(new boost::asio::thread_pool(Configuration::Concurrency * 2u));
}

void ThreadPool::Stop()
{
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
Expand All @@ -34,3 +38,14 @@ void ThreadPool::Stop()
m_Pool = nullptr;
}
}

void ThreadPool::Restart()
{
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);

if (m_Pool) {
m_Pool->join();
}

InitializePool();
}
7 changes: 5 additions & 2 deletions lib/base/threadpool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define THREADPOOL_H

#include "base/atomic.hpp"
#include "base/configuration.hpp"
#include "base/exception.hpp"
#include "base/logger.hpp"
#include <cstddef>
Expand Down Expand Up @@ -36,11 +37,12 @@ class ThreadPool
public:
typedef std::function<void ()> WorkFunction;

ThreadPool(size_t threads = std::thread::hardware_concurrency() * 2u);
ThreadPool();
~ThreadPool();

void Start();
Comment thread
julianbrost marked this conversation as resolved.
void Stop();
void Restart();

/**
* Appends a work item to the work queue. Work items will be processed in FIFO order.
Expand Down Expand Up @@ -89,8 +91,9 @@ class ThreadPool
private:
boost::shared_mutex m_Mutex;
std::unique_ptr<boost::asio::thread_pool> m_Pool;
size_t m_Threads;
Atomic<uint_fast64_t> m_Pending;

void InitializePool();
};

}
Expand Down