2012-06-24 02:56:48 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
#include "base/threadpool.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/logger_fwd.h"
|
|
|
|
#include "base/convert.h"
|
2013-08-28 08:18:58 +02:00
|
|
|
#include "base/debug.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/utility.h"
|
2013-09-03 14:05:03 +02:00
|
|
|
#include "base/scriptvariable.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
2013-03-25 16:12:25 +01:00
|
|
|
#include <iostream>
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/bind.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/exception/diagnostic_information.hpp>
|
2013-03-23 00:26:56 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2012-06-24 02:56:48 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-08-30 10:19:32 +02:00
|
|
|
int ThreadPool::m_NextID = 1;
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
ThreadPool::ThreadPool(void)
|
2013-08-30 10:19:32 +02:00
|
|
|
: m_ID(m_NextID++), m_WaitTime(0), m_ServiceTime(0),
|
2013-08-20 11:06:04 +02:00
|
|
|
m_TaskCount(0), m_Stopped(false)
|
2013-02-18 14:40:24 +01:00
|
|
|
{
|
2013-03-23 15:57:12 +01:00
|
|
|
for (int i = 0; i < 2; i++)
|
2013-03-23 12:23:13 +01:00
|
|
|
SpawnWorker();
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-11-03 10:03:11 +01:00
|
|
|
m_Threads.create_thread(boost::bind(&ThreadPool::ManagerThreadProc, this));
|
|
|
|
m_Threads.create_thread(boost::bind(&ThreadPool::StatsThreadProc, this));
|
2013-02-18 14:40:24 +01:00
|
|
|
}
|
2012-07-13 23:33:30 +02:00
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
ThreadPool::~ThreadPool(void)
|
2013-02-15 06:47:26 +01:00
|
|
|
{
|
2013-02-17 19:14:34 +01:00
|
|
|
Stop();
|
2013-02-18 14:40:24 +01:00
|
|
|
Join();
|
2013-02-15 06:47:26 +01:00
|
|
|
}
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
void ThreadPool::Stop(void)
|
2013-02-15 06:47:26 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
m_Stopped = true;
|
2013-04-19 12:58:16 +02:00
|
|
|
m_WorkCV.notify_all();
|
|
|
|
m_MgmtCV.notify_all();
|
2013-02-15 06:47:26 +01:00
|
|
|
}
|
|
|
|
|
2012-09-14 14:41:17 +02:00
|
|
|
/**
|
2013-02-18 14:40:24 +01:00
|
|
|
* Waits for all worker threads to finish.
|
2012-09-14 14:41:17 +02:00
|
|
|
*/
|
2013-03-25 18:36:15 +01:00
|
|
|
void ThreadPool::Join(void)
|
2012-06-24 02:56:48 +02:00
|
|
|
{
|
2013-11-03 10:03:11 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-04-19 12:58:16 +02:00
|
|
|
|
2013-11-03 10:03:11 +01:00
|
|
|
while (!m_Stopped || !m_WorkItems.empty()) {
|
|
|
|
lock.unlock();
|
|
|
|
Utility::Sleep(0.5);
|
|
|
|
lock.lock();
|
|
|
|
}
|
2013-11-02 10:38:54 +01:00
|
|
|
}
|
2013-04-19 12:58:16 +02:00
|
|
|
|
2013-11-03 10:03:11 +01:00
|
|
|
m_Threads.join_all();
|
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-03-25 18:36:15 +01:00
|
|
|
void ThreadPool::QueueThreadProc(int tid)
|
2013-02-17 19:14:34 +01:00
|
|
|
{
|
2013-03-25 18:36:15 +01:00
|
|
|
std::ostringstream idbuf;
|
2013-09-18 09:32:16 +02:00
|
|
|
idbuf << "TP #" << m_ID << " W #" << tid;
|
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
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
2013-03-28 13:14:39 +01:00
|
|
|
UpdateThreadUtilization(tid, ThreadIdle);
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2013-11-03 10:03:11 +01:00
|
|
|
while (m_WorkItems.empty() && !m_Stopped && !m_ThreadStats[tid].Zombie)
|
2013-04-19 12:58:16 +02:00
|
|
|
m_WorkCV.wait(lock);
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-11-03 10:03:11 +01:00
|
|
|
if (m_ThreadStats[tid].Zombie)
|
2013-03-23 12:23:13 +01:00
|
|
|
break;
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
if (m_WorkItems.empty() && m_Stopped)
|
2013-02-19 23:02:08 +01:00
|
|
|
break;
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
wi = m_WorkItems.front();
|
|
|
|
m_WorkItems.pop_front();
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2013-03-28 13:14:39 +01:00
|
|
|
UpdateThreadUtilization(tid, ThreadBusy);
|
2013-02-17 19:14:34 +01:00
|
|
|
}
|
2013-02-16 07:27:45 +01:00
|
|
|
|
2013-03-28 13:14:39 +01:00
|
|
|
double st = Utility::GetTime();;
|
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
#ifdef _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 */
|
2013-03-06 11:03:50 +01:00
|
|
|
#endif /* _DEBUG */
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2013-03-15 09:10:06 +01:00
|
|
|
try {
|
2013-03-25 18:36:15 +01:00
|
|
|
wi.Callback();
|
2013-03-15 09:10:06 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream msgbuf;
|
2013-03-15 09:10:06 +01:00
|
|
|
msgbuf << "Exception thrown in event handler: " << std::endl
|
2013-03-16 21:18:53 +01:00
|
|
|
<< boost::diagnostic_information(ex);
|
2013-03-08 14:43:07 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogCritical, "base", msgbuf.str());
|
2013-03-15 09:10:06 +01:00
|
|
|
} catch (...) {
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogCritical, "base", "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
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
m_WaitTime += latency;
|
|
|
|
m_ServiceTime += et - st;
|
|
|
|
m_TaskCount++;
|
|
|
|
|
|
|
|
if (latency > m_MaxLatency)
|
|
|
|
m_MaxLatency = latency;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _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) +
|
|
|
|
(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) +
|
|
|
|
(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) {
|
2013-03-18 22:40:40 +01:00
|
|
|
std::ostringstream msgbuf;
|
2013-03-07 15:00:26 +01:00
|
|
|
# ifdef RUSAGE_THREAD
|
2013-03-15 09:10:06 +01:00
|
|
|
msgbuf << "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
|
2013-03-15 09:10:06 +01:00
|
|
|
msgbuf << "Event call took " << (et - st) << "s";
|
2013-03-07 15:00:26 +01:00
|
|
|
# endif /* RUSAGE_THREAD */
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogWarning, "base", msgbuf.str());
|
2012-08-03 13:19:55 +02:00
|
|
|
}
|
2013-03-15 09:10:06 +01:00
|
|
|
#endif /* _DEBUG */
|
2012-07-16 22:00:50 +02:00
|
|
|
}
|
2013-03-23 12:23:13 +01:00
|
|
|
|
2013-09-11 10:42:36 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-03-28 13:14:39 +01:00
|
|
|
UpdateThreadUtilization(tid, ThreadDead);
|
2013-11-03 10:03:11 +01:00
|
|
|
m_ThreadStats[tid].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.
|
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
|
|
|
*/
|
2013-05-13 09:58:24 +02:00
|
|
|
bool ThreadPool::Post(const ThreadPool::WorkFunction& callback)
|
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-09-18 11:22:52 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
if (m_Stopped)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_WorkItems.push_back(wi);
|
|
|
|
m_WorkCV.notify_one();
|
|
|
|
}
|
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
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
void ThreadPool::ManagerThreadProc(void)
|
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());
|
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
for (;;) {
|
2013-08-20 11:06:04 +02:00
|
|
|
size_t pending, alive;
|
2013-03-23 20:38:41 +01:00
|
|
|
double avg_latency, max_latency;
|
2013-03-25 18:36:15 +01:00
|
|
|
double utilization = 0;
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-04-19 12:58:16 +02:00
|
|
|
|
2013-10-31 19:09:51 +01:00
|
|
|
if (!m_Stopped)
|
|
|
|
m_MgmtCV.timed_wait(lock, boost::posix_time::seconds(5));
|
2013-04-19 12:58:16 +02:00
|
|
|
|
|
|
|
if (m_Stopped)
|
|
|
|
break;
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
pending = m_WorkItems.size();
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2013-03-23 12:23:13 +01:00
|
|
|
alive = 0;
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2013-11-03 10:03:11 +01:00
|
|
|
for (size_t i = 0; i < sizeof(m_ThreadStats) / sizeof(m_ThreadStats[0]); i++) {
|
|
|
|
if (m_ThreadStats[i].State != ThreadDead && !m_ThreadStats[i].Zombie) {
|
2013-03-23 12:23:13 +01:00
|
|
|
alive++;
|
2013-11-03 10:03:11 +01:00
|
|
|
utilization += m_ThreadStats[i].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
|
|
|
|
|
|
|
if (m_TaskCount > 0)
|
|
|
|
avg_latency = m_WaitTime / (m_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) {
|
2013-08-27 15:57:00 +02:00
|
|
|
double wthreads = ceil((utilization * alive) / 80.0);
|
|
|
|
|
|
|
|
int tthreads = wthreads - alive;
|
2013-03-23 20:38:41 +01:00
|
|
|
|
2013-08-29 15:48:47 +02:00
|
|
|
/* Don't ever kill the last 8 threads. */
|
|
|
|
if (alive + tthreads < 8)
|
|
|
|
tthreads = 8 - alive;
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
/* Spawn more workers if there are outstanding work items. */
|
|
|
|
if (tthreads > 0 && pending > 0)
|
|
|
|
tthreads = 8;
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2013-08-27 15:57:00 +02:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "Thread pool; current: " << alive << "; adjustment: " << tthreads;
|
|
|
|
Log(LogDebug, "base", msgbuf.str());
|
|
|
|
|
2013-03-25 16:12:25 +01:00
|
|
|
for (int i = 0; i < -tthreads; i++)
|
|
|
|
KillWorker();
|
|
|
|
|
|
|
|
for (int i = 0; i < tthreads; i++)
|
2013-03-23 12:23:13 +01:00
|
|
|
SpawnWorker();
|
|
|
|
}
|
2013-03-25 16:12:25 +01:00
|
|
|
|
|
|
|
m_WaitTime = 0;
|
|
|
|
m_ServiceTime = 0;
|
|
|
|
m_TaskCount = 0;
|
|
|
|
|
|
|
|
max_latency = m_MaxLatency;
|
|
|
|
m_MaxLatency = 0;
|
2013-03-23 12:23:13 +01:00
|
|
|
}
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2013-03-23 12:23:13 +01:00
|
|
|
std::ostringstream msgbuf;
|
2013-03-25 18:36:15 +01:00
|
|
|
msgbuf << "Pending tasks: " << pending << "; Average latency: "
|
|
|
|
<< (long)(avg_latency * 1000) << "ms"
|
|
|
|
<< "; Max latency: " << (long)(max_latency * 1000) << "ms"
|
|
|
|
<< "; Threads: " << alive
|
|
|
|
<< "; Pool utilization: " << utilization << "%";
|
2013-03-23 12:23:13 +01:00
|
|
|
Log(LogInformation, "base", msgbuf.str());
|
|
|
|
}
|
|
|
|
}
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2013-03-23 12:23:13 +01:00
|
|
|
/**
|
|
|
|
* Note: Caller must hold m_Mutex
|
|
|
|
*/
|
2013-03-25 18:36:15 +01:00
|
|
|
void ThreadPool::SpawnWorker(void)
|
2013-03-23 12:23:13 +01:00
|
|
|
{
|
2013-11-03 10:03:11 +01:00
|
|
|
for (size_t i = 0; i < sizeof(m_ThreadStats) / sizeof(m_ThreadStats[0]); i++) {
|
|
|
|
if (m_ThreadStats[i].State == ThreadDead) {
|
2013-03-23 20:38:41 +01:00
|
|
|
Log(LogDebug, "debug", "Spawning worker thread.");
|
2013-03-23 00:26:56 +01:00
|
|
|
|
2013-11-03 10:03:11 +01:00
|
|
|
m_ThreadStats[i] = ThreadStats(ThreadIdle);
|
|
|
|
m_Threads.create_thread(boost::bind(&ThreadPool::QueueThreadProc, this, i));
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Note: Caller must hold m_Mutex.
|
|
|
|
*/
|
2013-03-25 18:36:15 +01:00
|
|
|
void ThreadPool::KillWorker(void)
|
2013-03-23 12:23:13 +01:00
|
|
|
{
|
2013-11-03 10:03:11 +01:00
|
|
|
for (size_t i = 0; i < sizeof(m_ThreadStats) / sizeof(m_ThreadStats[0]); i++) {
|
|
|
|
if (m_ThreadStats[i].State == ThreadIdle && !m_ThreadStats[i].Zombie) {
|
2013-08-27 15:57:00 +02:00
|
|
|
Log(LogDebug, "base", "Killing worker thread.");
|
2013-03-23 12:23:13 +01:00
|
|
|
|
2013-11-03 10:03:11 +01:00
|
|
|
m_ThreadStats[i].Zombie = true;
|
2013-08-27 15:57:00 +02:00
|
|
|
m_WorkCV.notify_all();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-03-23 12:23:13 +01:00
|
|
|
}
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2013-03-28 13:14:39 +01:00
|
|
|
void ThreadPool::StatsThreadProc(void)
|
|
|
|
{
|
|
|
|
std::ostringstream idbuf;
|
2013-08-30 10:19:32 +02:00
|
|
|
idbuf << "TP #" << m_ID << " Stats";
|
2013-03-28 13:14:39 +01:00
|
|
|
Utility::SetThreadName(idbuf.str());
|
|
|
|
|
|
|
|
for (;;) {
|
2013-04-19 12:58:16 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-03-28 13:14:39 +01:00
|
|
|
|
2013-10-31 19:09:51 +01:00
|
|
|
if (!m_Stopped)
|
|
|
|
m_MgmtCV.timed_wait(lock, boost::posix_time::milliseconds(250));
|
2013-03-28 13:14:39 +01:00
|
|
|
|
2013-04-19 12:58:16 +02:00
|
|
|
if (m_Stopped)
|
|
|
|
break;
|
|
|
|
|
2013-11-03 10:03:11 +01:00
|
|
|
for (size_t i = 0; i < sizeof(m_ThreadStats) / sizeof(m_ThreadStats[0]); i++)
|
2013-04-19 12:58:16 +02:00
|
|
|
UpdateThreadUtilization(i);
|
2013-03-28 13:14:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-25 16:12:25 +01:00
|
|
|
/**
|
|
|
|
* Note: Caller must hold m_Mutex.
|
|
|
|
*/
|
2013-03-28 13:14:39 +01:00
|
|
|
void ThreadPool::UpdateThreadUtilization(int tid, ThreadState state)
|
2013-03-25 16:12:25 +01:00
|
|
|
{
|
2013-03-28 13:14:39 +01:00
|
|
|
double utilization;
|
|
|
|
|
2013-11-03 10:03:11 +01:00
|
|
|
switch (m_ThreadStats[tid].State) {
|
2013-03-28 13:14:39 +01:00
|
|
|
case ThreadDead:
|
|
|
|
return;
|
|
|
|
case ThreadIdle:
|
|
|
|
utilization = 0;
|
|
|
|
break;
|
|
|
|
case ThreadBusy:
|
|
|
|
utilization = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
double now = Utility::GetTime();
|
2013-11-03 10:03:11 +01:00
|
|
|
double time = now - m_ThreadStats[tid].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-11-03 10:03:11 +01:00
|
|
|
m_ThreadStats[tid].Utilization = (m_ThreadStats[tid].Utilization * (avg_time - time) + utilization * time) / avg_time;
|
|
|
|
m_ThreadStats[tid].LastUpdate = now;
|
2013-03-28 13:14:39 +01:00
|
|
|
|
|
|
|
if (state != ThreadUnspecified)
|
2013-11-03 10:03:11 +01:00
|
|
|
m_ThreadStats[tid].State = state;
|
2013-03-25 16:12:25 +01:00
|
|
|
}
|