2012-06-24 02:56:48 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-10-18 09:27:04 +02:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
|
2012-06-24 02:56:48 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/threadpool.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/debug.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/exception.hpp"
|
2014-11-16 13:14:42 +01:00
|
|
|
#include "base/application.hpp"
|
2014-05-23 12:35:18 +02:00
|
|
|
#include <iostream>
|
2012-06-24 02:56:48 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-08-30 10:19:32 +02:00
|
|
|
int ThreadPool::m_NextID = 1;
|
|
|
|
|
2014-05-11 08:27:42 +02:00
|
|
|
ThreadPool::ThreadPool(size_t max_threads)
|
2018-01-04 09:43:49 +01:00
|
|
|
: m_ID(m_NextID++), m_MaxThreads(max_threads)
|
2013-02-18 14:40:24 +01:00
|
|
|
{
|
2014-05-11 08:27:42 +02:00
|
|
|
if (m_MaxThreads != UINT_MAX && m_MaxThreads < sizeof(m_Queues) / sizeof(m_Queues[0]))
|
2013-12-06 21:46:50 +01:00
|
|
|
m_MaxThreads = sizeof(m_Queues) / sizeof(m_Queues[0]);
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
Start();
|
2013-02-18 14:40:24 +01:00
|
|
|
}
|
2012-07-13 23:33:30 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
ThreadPool::~ThreadPool()
|
2013-02-15 06:47:26 +01:00
|
|
|
{
|
2013-02-17 19:14:34 +01:00
|
|
|
Stop();
|
2013-12-06 21:46:50 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ThreadPool::Start()
|
2013-12-06 21:46:50 +01:00
|
|
|
{
|
2015-10-21 07:02:49 +02:00
|
|
|
if (!m_Stopped)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_Stopped = false;
|
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
for (auto& queue : m_Queues)
|
|
|
|
queue.SpawnWorker(m_ThreadGroup);
|
2013-12-06 21:46:50 +01:00
|
|
|
|
2017-11-21 12:12:58 +01:00
|
|
|
m_MgmtThread = std::thread(std::bind(&ThreadPool::ManagerThreadProc, this));
|
2013-02-15 06:47:26 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ThreadPool::Stop()
|
2013-02-15 06:47:26 +01:00
|
|
|
{
|
2015-10-21 07:02:49 +02:00
|
|
|
if (m_Stopped)
|
|
|
|
return;
|
|
|
|
|
2014-11-13 10:00:49 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_MgmtMutex);
|
|
|
|
m_Stopped = true;
|
|
|
|
m_MgmtCV.notify_all();
|
2013-12-06 21:46:50 +01:00
|
|
|
}
|
|
|
|
|
2014-12-11 09:33:07 +01:00
|
|
|
if (m_MgmtThread.joinable())
|
|
|
|
m_MgmtThread.join();
|
2013-04-19 12:58:16 +02:00
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
for (auto& queue : m_Queues) {
|
|
|
|
boost::mutex::scoped_lock lock(queue.Mutex);
|
|
|
|
queue.Stopped = true;
|
|
|
|
queue.CV.notify_all();
|
2013-12-06 21:46:50 +01:00
|
|
|
}
|
2014-11-13 10:00:49 +01:00
|
|
|
|
|
|
|
m_ThreadGroup.join_all();
|
2014-12-11 09:44:58 +01:00
|
|
|
m_ThreadGroup.~thread_group();
|
|
|
|
new (&m_ThreadGroup) boost::thread_group();
|
2014-11-27 08:04:07 +01:00
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
for (auto& queue : m_Queues)
|
|
|
|
queue.Stopped = false;
|
2014-11-28 11:32:37 +01:00
|
|
|
|
2015-10-21 07:02:49 +02:00
|
|
|
m_Stopped = true;
|
2013-02-17 19:14:34 +01:00
|
|
|
}
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
/**
|
2013-03-25 18:36:15 +01:00
|
|
|
* Waits for work items and processes them.
|
2013-02-17 19:14:34 +01:00
|
|
|
*/
|
2013-12-06 21:46:50 +01:00
|
|
|
void ThreadPool::WorkerThread::ThreadProc(Queue& queue)
|
2013-02-17 19:14:34 +01:00
|
|
|
{
|
2013-03-25 18:36:15 +01:00
|
|
|
std::ostringstream idbuf;
|
2013-12-06 21:46:50 +01:00
|
|
|
idbuf << "Q #" << &queue << " W #" << this;
|
2013-03-25 18:36:15 +01:00
|
|
|
Utility::SetThreadName(idbuf.str());
|
|
|
|
|
2013-02-19 23:02:08 +01:00
|
|
|
for (;;) {
|
2013-03-25 18:36:15 +01:00
|
|
|
WorkItem wi;
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
{
|
2013-12-06 21:46:50 +01:00
|
|
|
boost::mutex::scoped_lock lock(queue.Mutex);
|
|
|
|
|
|
|
|
UpdateUtilization(ThreadIdle);
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
while (queue.Items.empty() && !queue.Stopped && !Zombie) {
|
|
|
|
if (queue.Items.empty())
|
|
|
|
queue.CVStarved.notify_all();
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
queue.CV.wait(lock);
|
|
|
|
}
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
if (Zombie)
|
2013-03-23 12:23:13 +01:00
|
|
|
break;
|
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
if (queue.Items.empty() && queue.Stopped)
|
2013-02-19 23:02:08 +01:00
|
|
|
break;
|
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
wi = queue.Items.front();
|
|
|
|
queue.Items.pop_front();
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
UpdateUtilization(ThreadBusy);
|
2013-02-17 19:14:34 +01:00
|
|
|
}
|
2013-02-16 07:27:45 +01:00
|
|
|
|
2018-01-24 12:07:52 +01:00
|
|
|
double st = Utility::GetTime();
|
2013-03-28 13:14:39 +01:00
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2013-03-07 15:00:26 +01:00
|
|
|
# ifdef RUSAGE_THREAD
|
2013-03-15 09:10:06 +01:00
|
|
|
struct rusage usage_start, usage_end;
|
2013-03-06 11:03:50 +01:00
|
|
|
|
2013-03-15 09:10:06 +01:00
|
|
|
(void) getrusage(RUSAGE_THREAD, &usage_start);
|
2013-03-07 15:00:26 +01:00
|
|
|
# endif /* RUSAGE_THREAD */
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2013-03-15 09:10:06 +01:00
|
|
|
try {
|
2014-08-06 17:49:29 +02:00
|
|
|
if (wi.Callback)
|
|
|
|
wi.Callback();
|
2013-03-15 09:10:06 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "ThreadPool")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Exception thrown in event handler:\n"
|
|
|
|
<< DiagnosticInformation(ex);
|
2013-03-15 09:10:06 +01:00
|
|
|
} catch (...) {
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogCritical, "ThreadPool", "Exception of unknown type thrown in event handler.");
|
2013-03-15 09:10:06 +01:00
|
|
|
}
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2013-03-15 09:10:06 +01:00
|
|
|
double et = Utility::GetTime();
|
2013-03-25 18:36:15 +01:00
|
|
|
double latency = st - wi.Timestamp;
|
2013-03-25 16:12:25 +01:00
|
|
|
|
|
|
|
{
|
2013-12-06 21:46:50 +01:00
|
|
|
boost::mutex::scoped_lock lock(queue.Mutex);
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
queue.WaitTime += latency;
|
|
|
|
queue.ServiceTime += et - st;
|
|
|
|
queue.TaskCount++;
|
2013-03-25 16:12:25 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 12:19:28 +01:00
|
|
|
#ifdef I2_DEBUG
|
2013-03-07 15:00:26 +01:00
|
|
|
# ifdef RUSAGE_THREAD
|
2013-03-15 09:10:06 +01:00
|
|
|
(void) getrusage(RUSAGE_THREAD, &usage_end);
|
2013-03-06 11:03:50 +01:00
|
|
|
|
2013-03-15 09:10:06 +01:00
|
|
|
double duser = (usage_end.ru_utime.tv_sec - usage_start.ru_utime.tv_sec) +
|
2017-12-19 15:50:05 +01:00
|
|
|
(usage_end.ru_utime.tv_usec - usage_start.ru_utime.tv_usec) / 1000000.0;
|
2013-03-06 11:03:50 +01:00
|
|
|
|
2013-03-15 09:10:06 +01:00
|
|
|
double dsys = (usage_end.ru_stime.tv_sec - usage_start.ru_stime.tv_sec) +
|
2017-12-19 15:50:05 +01:00
|
|
|
(usage_end.ru_stime.tv_usec - usage_start.ru_stime.tv_usec) / 1000000.0;
|
2013-03-06 11:03:50 +01:00
|
|
|
|
2013-03-15 09:10:06 +01:00
|
|
|
double dwait = (et - st) - (duser + dsys);
|
2013-03-06 11:03:50 +01:00
|
|
|
|
2013-03-15 09:10:06 +01:00
|
|
|
int dminfaults = usage_end.ru_minflt - usage_start.ru_minflt;
|
|
|
|
int dmajfaults = usage_end.ru_majflt - usage_start.ru_majflt;
|
2013-03-06 11:03:50 +01:00
|
|
|
|
2013-03-15 09:10:06 +01:00
|
|
|
int dvctx = usage_end.ru_nvcsw - usage_start.ru_nvcsw;
|
|
|
|
int divctx = usage_end.ru_nivcsw - usage_start.ru_nivcsw;
|
2013-03-07 15:00:26 +01:00
|
|
|
# endif /* RUSAGE_THREAD */
|
2013-03-15 09:10:06 +01:00
|
|
|
if (et - st > 0.5) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogWarning, "ThreadPool")
|
2013-03-07 15:00:26 +01:00
|
|
|
# ifdef RUSAGE_THREAD
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Event call took user:" << duser << "s, system:" << dsys << "s, wait:" << dwait << "s, minor_faults:" << dminfaults << ", major_faults:" << dmajfaults << ", voluntary_csw:" << dvctx << ", involuntary_csw:" << divctx;
|
2013-03-07 15:00:26 +01:00
|
|
|
# else
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Event call took " << (et - st) << "s";
|
2013-03-07 15:00:26 +01:00
|
|
|
# endif /* RUSAGE_THREAD */
|
2012-08-03 13:19:55 +02:00
|
|
|
}
|
2014-12-19 12:19:28 +01:00
|
|
|
#endif /* I2_DEBUG */
|
2012-07-16 22:00:50 +02:00
|
|
|
}
|
2013-03-23 12:23:13 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
boost::mutex::scoped_lock lock(queue.Mutex);
|
|
|
|
UpdateUtilization(ThreadDead);
|
|
|
|
Zombie = false;
|
2012-06-24 02:56:48 +02:00
|
|
|
}
|
|
|
|
|
2012-09-14 14:41:17 +02:00
|
|
|
/**
|
2013-03-25 18:36:15 +01:00
|
|
|
* Appends a work item to the work queue. Work items will be processed in FIFO order.
|
2012-09-14 14:41:17 +02:00
|
|
|
*
|
2013-03-25 18:36:15 +01:00
|
|
|
* @param callback The callback function for the work item.
|
2014-09-11 11:45:21 +02:00
|
|
|
* @param policy The scheduling policy
|
2013-05-13 09:58:24 +02:00
|
|
|
* @returns true if the item was queued, false otherwise.
|
2012-09-14 14:41:17 +02:00
|
|
|
*/
|
2014-09-11 11:45:21 +02:00
|
|
|
bool ThreadPool::Post(const ThreadPool::WorkFunction& callback, SchedulerPolicy policy)
|
2012-06-24 02:56:48 +02:00
|
|
|
{
|
2013-03-25 18:36:15 +01:00
|
|
|
WorkItem wi;
|
|
|
|
wi.Callback = callback;
|
|
|
|
wi.Timestamp = Utility::GetTime();
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
Queue& queue = m_Queues[Utility::Random() % (sizeof(m_Queues) / sizeof(m_Queues[0]))];
|
|
|
|
|
2013-09-18 11:22:52 +02:00
|
|
|
{
|
2013-12-06 21:46:50 +01:00
|
|
|
boost::mutex::scoped_lock lock(queue.Mutex);
|
2013-09-18 11:22:52 +02:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
if (queue.Stopped)
|
2013-09-18 11:22:52 +02:00
|
|
|
return false;
|
|
|
|
|
2014-09-11 11:45:21 +02:00
|
|
|
if (policy == LowLatencyScheduler)
|
|
|
|
queue.SpawnWorker(m_ThreadGroup);
|
|
|
|
|
2017-11-30 08:19:58 +01:00
|
|
|
queue.Items.emplace_back(std::move(wi));
|
2013-12-06 21:46:50 +01:00
|
|
|
queue.CV.notify_one();
|
2013-09-18 11:22:52 +02:00
|
|
|
}
|
2013-05-13 09:58:24 +02:00
|
|
|
|
|
|
|
return true;
|
2013-02-26 10:13:54 +01:00
|
|
|
}
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ThreadPool::ManagerThreadProc()
|
2013-02-26 10:13:54 +01:00
|
|
|
{
|
2013-03-25 18:36:15 +01:00
|
|
|
std::ostringstream idbuf;
|
2013-08-30 10:19:32 +02:00
|
|
|
idbuf << "TP #" << m_ID << " Manager";
|
2013-03-25 18:36:15 +01:00
|
|
|
Utility::SetThreadName(idbuf.str());
|
|
|
|
|
2014-04-22 19:47:19 +02:00
|
|
|
double lastStats = 0;
|
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
for (;;) {
|
2013-12-06 21:46:50 +01:00
|
|
|
size_t total_pending = 0, total_alive = 0;
|
|
|
|
double total_avg_latency = 0;
|
|
|
|
double total_utilization = 0;
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
{
|
2013-12-06 21:46:50 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_MgmtMutex);
|
2013-04-19 12:58:16 +02:00
|
|
|
|
2013-10-31 19:09:51 +01:00
|
|
|
if (!m_Stopped)
|
2014-09-11 11:45:21 +02:00
|
|
|
m_MgmtCV.timed_wait(lock, boost::posix_time::milliseconds(500));
|
2013-04-19 12:58:16 +02:00
|
|
|
|
|
|
|
if (m_Stopped)
|
|
|
|
break;
|
2013-12-06 21:46:50 +01:00
|
|
|
}
|
2013-04-19 12:58:16 +02:00
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
for (auto& queue : m_Queues) {
|
2013-12-06 21:46:50 +01:00
|
|
|
size_t pending, alive = 0;
|
|
|
|
double avg_latency;
|
|
|
|
double utilization = 0;
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
boost::mutex::scoped_lock lock(queue.Mutex);
|
2013-12-06 21:46:50 +01:00
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
for (auto& thread : queue.Threads)
|
|
|
|
thread.UpdateUtilization();
|
2014-04-22 19:47:19 +02:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
pending = queue.Items.size();
|
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
for (auto& thread : queue.Threads) {
|
|
|
|
if (thread.State != ThreadDead && !thread.Zombie) {
|
2013-03-23 12:23:13 +01:00
|
|
|
alive++;
|
2018-01-04 08:15:20 +01:00
|
|
|
utilization += thread.Utilization * 100;
|
2013-03-25 16:12:25 +01:00
|
|
|
}
|
2013-03-23 00:26:56 +01:00
|
|
|
}
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
utilization /= alive;
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
if (queue.TaskCount > 0)
|
|
|
|
avg_latency = queue.WaitTime / (queue.TaskCount * 1.0);
|
2013-03-23 12:23:13 +01:00
|
|
|
else
|
|
|
|
avg_latency = 0;
|
|
|
|
|
2013-08-29 15:48:47 +02:00
|
|
|
if (utilization < 60 || utilization > 80 || alive < 8) {
|
2014-09-02 13:02:22 +02:00
|
|
|
double wthreads = std::ceil((utilization * alive) / 80.0);
|
2013-08-27 15:57:00 +02:00
|
|
|
|
|
|
|
int tthreads = wthreads - alive;
|
2013-03-23 20:38:41 +01:00
|
|
|
|
2014-12-06 19:21:30 +01:00
|
|
|
/* Make sure there is at least one thread per queue */
|
|
|
|
if (alive + tthreads < 1)
|
|
|
|
tthreads = 1 - alive;
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2014-12-06 19:21:30 +01:00
|
|
|
/* Don't kill more than 2 threads at once. */
|
|
|
|
if (tthreads < -2)
|
|
|
|
tthreads = -2;
|
2013-11-15 14:04:03 +01:00
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
/* Spawn more workers if there are outstanding work items. */
|
|
|
|
if (tthreads > 0 && pending > 0)
|
2014-12-06 19:21:30 +01:00
|
|
|
tthreads = 2;
|
2013-12-06 21:46:50 +01:00
|
|
|
|
2014-05-11 08:27:42 +02:00
|
|
|
if (m_MaxThreads != UINT_MAX && (alive + tthreads) * (sizeof(m_Queues) / sizeof(m_Queues[0])) > m_MaxThreads)
|
2013-12-06 21:46:50 +01:00
|
|
|
tthreads = m_MaxThreads / (sizeof(m_Queues) / sizeof(m_Queues[0])) - alive;
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2014-04-22 07:47:10 +02:00
|
|
|
if (tthreads != 0) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "ThreadPool")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Thread pool; current: " << alive << "; adjustment: " << tthreads;
|
2014-04-22 07:47:10 +02:00
|
|
|
}
|
2013-08-27 15:57:00 +02:00
|
|
|
|
2013-03-25 16:12:25 +01:00
|
|
|
for (int i = 0; i < -tthreads; i++)
|
2013-12-06 21:46:50 +01:00
|
|
|
queue.KillWorker(m_ThreadGroup);
|
2013-03-25 16:12:25 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < tthreads; i++)
|
2013-12-06 21:46:50 +01:00
|
|
|
queue.SpawnWorker(m_ThreadGroup);
|
2013-03-23 12:23:13 +01:00
|
|
|
}
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
queue.WaitTime = 0;
|
|
|
|
queue.ServiceTime = 0;
|
|
|
|
queue.TaskCount = 0;
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
total_pending += pending;
|
|
|
|
total_alive += alive;
|
|
|
|
total_avg_latency += avg_latency;
|
|
|
|
total_utilization += utilization;
|
2013-03-23 12:23:13 +01:00
|
|
|
}
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2014-04-22 19:47:19 +02:00
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
|
|
|
if (lastStats < now - 15) {
|
|
|
|
lastStats = now;
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogNotice, "ThreadPool")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Pool #" << m_ID << ": Pending tasks: " << total_pending << "; Average latency: "
|
|
|
|
<< (long)(total_avg_latency * 1000 / (sizeof(m_Queues) / sizeof(m_Queues[0]))) << "ms"
|
|
|
|
<< "; Threads: " << total_alive
|
|
|
|
<< "; Pool utilization: " << (total_utilization / (sizeof(m_Queues) / sizeof(m_Queues[0]))) << "%";
|
2014-04-22 19:47:19 +02:00
|
|
|
}
|
2013-03-23 12:23:13 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2013-03-23 12:23:13 +01:00
|
|
|
/**
|
|
|
|
* Note: Caller must hold m_Mutex
|
|
|
|
*/
|
2013-12-06 21:46:50 +01:00
|
|
|
void ThreadPool::Queue::SpawnWorker(boost::thread_group& group)
|
2013-03-23 12:23:13 +01:00
|
|
|
{
|
2018-01-04 08:15:20 +01:00
|
|
|
for (auto& thread : Threads) {
|
|
|
|
if (thread.State == ThreadDead) {
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogDebug, "ThreadPool", "Spawning worker thread.");
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
thread = WorkerThread(ThreadIdle);
|
|
|
|
thread.Thread = group.create_thread(std::bind(&ThreadPool::WorkerThread::ThreadProc, std::ref(thread), std::ref(*this)));
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2013-03-23 12:23:13 +01:00
|
|
|
break;
|
2013-03-06 11:03:50 +01:00
|
|
|
}
|
|
|
|
}
|
2012-06-24 02:56:48 +02:00
|
|
|
}
|
2013-03-23 12:23:13 +01:00
|
|
|
|
|
|
|
/**
|
2013-12-06 21:46:50 +01:00
|
|
|
* Note: Caller must hold Mutex.
|
2013-03-23 12:23:13 +01:00
|
|
|
*/
|
2013-12-06 21:46:50 +01:00
|
|
|
void ThreadPool::Queue::KillWorker(boost::thread_group& group)
|
2013-03-23 12:23:13 +01:00
|
|
|
{
|
2018-01-04 08:15:20 +01:00
|
|
|
for (auto& thread : Threads) {
|
|
|
|
if (thread.State == ThreadIdle && !thread.Zombie) {
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogDebug, "ThreadPool", "Killing worker thread.");
|
2013-03-23 12:23:13 +01:00
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
group.remove_thread(thread.Thread);
|
|
|
|
thread.Thread->detach();
|
|
|
|
delete thread.Thread;
|
2013-11-21 07:46:12 +01:00
|
|
|
|
2018-01-04 08:15:20 +01:00
|
|
|
thread.Zombie = true;
|
2013-12-06 21:46:50 +01:00
|
|
|
CV.notify_all();
|
2013-08-27 15:57:00 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-03-23 12:23:13 +01:00
|
|
|
}
|
2013-03-25 16:12:25 +01:00
|
|
|
|
|
|
|
/**
|
2013-12-06 21:46:50 +01:00
|
|
|
* Note: Caller must hold queue Mutex.
|
2013-03-25 16:12:25 +01:00
|
|
|
*/
|
2013-12-06 21:46:50 +01:00
|
|
|
void ThreadPool::WorkerThread::UpdateUtilization(ThreadState state)
|
2013-03-25 16:12:25 +01:00
|
|
|
{
|
2013-03-28 13:14:39 +01:00
|
|
|
double utilization;
|
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
switch (State) {
|
2013-03-28 13:14:39 +01:00
|
|
|
case ThreadDead:
|
|
|
|
return;
|
|
|
|
case ThreadIdle:
|
|
|
|
utilization = 0;
|
|
|
|
break;
|
|
|
|
case ThreadBusy:
|
|
|
|
utilization = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2014-05-11 08:27:42 +02:00
|
|
|
VERIFY(0);
|
2013-03-28 13:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
double now = Utility::GetTime();
|
2013-12-06 21:46:50 +01:00
|
|
|
double time = now - LastUpdate;
|
2013-03-28 13:14:39 +01:00
|
|
|
|
2013-03-25 16:12:25 +01:00
|
|
|
const double avg_time = 5.0;
|
|
|
|
|
|
|
|
if (time > avg_time)
|
|
|
|
time = avg_time;
|
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
Utilization = (Utilization * (avg_time - time) + utilization * time) / avg_time;
|
|
|
|
LastUpdate = now;
|
2013-03-28 13:14:39 +01:00
|
|
|
|
|
|
|
if (state != ThreadUnspecified)
|
2013-12-06 21:46:50 +01:00
|
|
|
State = state;
|
2013-03-25 16:12:25 +01:00
|
|
|
}
|