icinga2/lib/base/eventqueue.cpp

256 lines
6.9 KiB
C++
Raw Normal View History

2012-06-23 20:56:48 -04:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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-16 16:18:53 -04:00
#include "base/eventqueue.h"
#include "base/logger_fwd.h"
#include "base/convert.h"
#include "base/utility.h"
#include <sstream>
2013-03-15 13:21:29 -04:00
#include <boost/bind.hpp>
2013-03-16 16:18:53 -04:00
#include <boost/exception/diagnostic_information.hpp>
#include <boost/foreach.hpp>
2012-06-23 20:56:48 -04:00
using namespace icinga;
2013-02-15 00:47:26 -05:00
EventQueue::EventQueue(void)
2013-03-23 07:23:13 -04:00
: m_Stopped(false), m_ThreadDeaths(0), m_Latency(0), m_LatencyCount(0)
2013-02-18 08:40:24 -05:00
{
2013-03-23 07:23:13 -04:00
for (int i = 0; i < sizeof(m_ThreadStates) / sizeof(m_ThreadStates[0]); i++)
m_ThreadStates[i] = ThreadDead;
2013-02-18 08:40:24 -05:00
2013-03-23 10:57:12 -04:00
for (int i = 0; i < 2; i++)
2013-03-23 07:23:13 -04:00
SpawnWorker();
2013-02-26 04:13:54 -05:00
2013-03-15 13:21:29 -04:00
boost::thread reportThread(boost::bind(&EventQueue::ReportThreadProc, this));
2013-03-06 05:03:50 -05:00
reportThread.detach();
2013-02-18 08:40:24 -05:00
}
2012-07-13 17:33:30 -04:00
2013-02-17 13:14:34 -05:00
EventQueue::~EventQueue(void)
2013-02-15 00:47:26 -05:00
{
2013-02-17 13:14:34 -05:00
Stop();
2013-02-18 08:40:24 -05:00
Join();
2013-02-15 00:47:26 -05:00
}
void EventQueue::Stop(void)
{
boost::mutex::scoped_lock lock(m_Mutex);
m_Stopped = true;
2013-02-17 13:14:34 -05:00
m_CV.notify_all();
2013-02-15 00:47:26 -05:00
}
2012-09-14 08:41:17 -04:00
/**
2013-02-18 08:40:24 -05:00
* Waits for all worker threads to finish.
2012-09-14 08:41:17 -04:00
*/
2013-02-18 08:40:24 -05:00
void EventQueue::Join(void)
2012-06-23 20:56:48 -04:00
{
2013-03-23 07:23:13 -04:00
boost::mutex::scoped_lock lock(m_Mutex);
while (!m_Stopped || !m_Events.empty()) {
lock.unlock();
Utility::Sleep(0.5);
lock.lock();
}
2013-02-17 13:14:34 -05:00
}
2012-06-23 20:56:48 -04:00
2013-02-17 13:14:34 -05:00
/**
* Waits for events and processes them.
*/
void EventQueue::QueueThreadProc(int tid)
2013-02-17 13:14:34 -05:00
{
2013-02-19 17:02:08 -05:00
for (;;) {
EventQueueWorkItem event;
2012-06-23 20:56:48 -04:00
2013-02-17 13:14:34 -05:00
{
boost::mutex::scoped_lock lock(m_Mutex);
2013-03-23 07:23:13 -04:00
m_ThreadStates[tid] = ThreadIdle;
2013-03-23 07:23:13 -04:00
while (m_Events.empty() && !m_Stopped && m_ThreadDeaths == 0)
2013-02-17 13:14:34 -05:00
m_CV.wait(lock);
2013-03-23 07:23:13 -04:00
if (m_ThreadDeaths > 0) {
m_ThreadDeaths--;
break;
}
2013-02-23 19:10:34 -05:00
if (m_Events.empty() && m_Stopped)
2013-02-19 17:02:08 -05:00
break;
event = m_Events.front();
m_Events.pop_front();
2013-03-23 07:23:13 -04:00
m_ThreadStates[tid] = ThreadBusy;
m_Latency += Utility::GetTime() - event.Timestamp;
m_LatencyCount++;
2013-02-17 13:14:34 -05:00
}
2013-03-06 05:03:50 -05:00
#ifdef _DEBUG
double st = Utility::GetTime();
# ifdef RUSAGE_THREAD
struct rusage usage_start, usage_end;
2013-03-06 05:03:50 -05:00
(void) getrusage(RUSAGE_THREAD, &usage_start);
# endif /* RUSAGE_THREAD */
2013-03-06 05:03:50 -05:00
#endif /* _DEBUG */
2012-08-03 07:19:55 -04:00
try {
event.Callback();
} catch (const std::exception& ex) {
2013-03-16 16:18:53 -04:00
std::ostringstream msgbuf;
msgbuf << "Exception thrown in event handler: " << std::endl
2013-03-16 16:18:53 -04:00
<< boost::diagnostic_information(ex);
2013-03-16 16:18:53 -04:00
Log(LogCritical, "base", msgbuf.str());
} catch (...) {
2013-03-16 16:18:53 -04:00
Log(LogCritical, "base", "Exception of unknown type thrown in event handler.");
}
2012-08-03 07:19:55 -04:00
2013-03-06 05:03:50 -05:00
#ifdef _DEBUG
double et = Utility::GetTime();
# ifdef RUSAGE_THREAD
(void) getrusage(RUSAGE_THREAD, &usage_end);
2013-03-06 05:03:50 -05: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 05:03:50 -05: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 05:03:50 -05:00
double dwait = (et - st) - (duser + dsys);
2013-03-06 05:03:50 -05:00
int dminfaults = usage_end.ru_minflt - usage_start.ru_minflt;
int dmajfaults = usage_end.ru_majflt - usage_start.ru_majflt;
2013-03-06 05:03:50 -05:00
int dvctx = usage_end.ru_nvcsw - usage_start.ru_nvcsw;
int divctx = usage_end.ru_nivcsw - usage_start.ru_nivcsw;
# endif /* RUSAGE_THREAD */
if (et - st > 0.5) {
2013-03-18 17:40:40 -04:00
std::ostringstream msgbuf;
# ifdef RUSAGE_THREAD
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;
# else
msgbuf << "Event call took " << (et - st) << "s";
# endif /* RUSAGE_THREAD */
2013-03-16 16:18:53 -04:00
Log(LogWarning, "base", msgbuf.str());
2012-08-03 07:19:55 -04:00
}
#endif /* _DEBUG */
2012-07-16 16:00:50 -04:00
}
2013-03-23 07:23:13 -04:00
m_ThreadStates[tid] = ThreadDead;
2012-06-23 20:56:48 -04:00
}
2012-09-14 08:41:17 -04:00
/**
2013-02-17 13:14:34 -05:00
* Appends an event to the event queue. Events will be processed in FIFO order.
2012-09-14 08:41:17 -04:00
*
* @param callback The callback function for the event.
*/
void EventQueue::Post(const EventQueueCallback& callback)
2012-06-23 20:56:48 -04:00
{
2013-02-17 13:14:34 -05:00
boost::mutex::scoped_lock lock(m_Mutex);
2013-03-23 07:23:13 -04:00
if (m_Stopped)
BOOST_THROW_EXCEPTION(std::runtime_error("EventQueue has been stopped."));
EventQueueWorkItem event;
event.Callback = callback;
event.Timestamp = Utility::GetTime();
m_Events.push_back(event);
2013-02-19 17:02:08 -05:00
m_CV.notify_one();
2013-02-26 04:13:54 -05:00
}
2013-02-20 13:52:25 -05:00
2013-03-06 05:03:50 -05:00
void EventQueue::ReportThreadProc(void)
2013-02-26 04:13:54 -05:00
{
2013-03-06 05:03:50 -05:00
for (;;) {
Utility::Sleep(5);
2013-02-26 04:13:54 -05:00
double now = Utility::GetTime();
2013-03-23 07:23:13 -04:00
int pending, alive, busy;
double avg_latency;
2013-02-26 04:13:54 -05:00
2013-03-06 05:03:50 -05:00
{
boost::mutex::scoped_lock lock(m_Mutex);
pending = m_Events.size();
2013-03-23 07:23:13 -04:00
alive = 0;
busy = 0;
2013-03-23 07:23:13 -04:00
for (int i = 0; i < sizeof(m_ThreadStates) / sizeof(m_ThreadStates[0]); i++) {
if (m_ThreadStates[i] != ThreadDead)
alive++;
if (m_ThreadStates[i] == ThreadBusy)
busy++;
}
2013-03-23 07:23:13 -04:00
if (m_LatencyCount > 0)
avg_latency = m_Latency / (m_LatencyCount * 1.0);
else
avg_latency = 0;
m_Latency = 0;
m_LatencyCount = 0;
2013-03-23 07:50:07 -04:00
if (pending > alive - busy) {
2013-03-23 07:23:13 -04:00
/* Spawn a few additional workers. */
2013-03-23 07:54:14 -04:00
for (int i = 0; i < 8; i++)
2013-03-23 07:23:13 -04:00
SpawnWorker();
2013-03-23 10:57:12 -04:00
} else if (alive > busy + 2) {
2013-03-23 07:23:13 -04:00
KillWorker();
}
}
2013-03-23 07:23:13 -04:00
std::ostringstream msgbuf;
msgbuf << "Pending tasks: " << pending << "; Busy threads: " << busy << "; Idle threads: " << alive - busy << "; Average latency: " << (long)(avg_latency * 1000) << "ms";
Log(LogInformation, "base", msgbuf.str());
}
}
2013-03-23 07:23:13 -04:00
/**
* Note: Caller must hold m_Mutex
*/
void EventQueue::SpawnWorker(void)
{
for (int i = 0; i < sizeof(m_ThreadStates) / sizeof(m_ThreadStates[0]); i++) {
if (m_ThreadStates[i] == ThreadDead) {
Log(LogInformation, "debug", "Spawning worker thread.");
2013-03-23 07:23:13 -04:00
m_ThreadStates[i] = ThreadIdle;
boost::thread worker(boost::bind(&EventQueue::QueueThreadProc, this, i));
worker.detach();
2013-03-23 07:23:13 -04:00
break;
2013-03-06 05:03:50 -05:00
}
}
2012-06-23 20:56:48 -04:00
}
2013-03-23 07:23:13 -04:00
/**
* Note: Caller must hold m_Mutex.
*/
void EventQueue::KillWorker(void)
{
Log(LogInformation, "base", "Killing worker thread.");
m_ThreadDeaths++;
}