mirror of
https://github.com/Icinga/icinga2.git
synced 2026-05-28 04:12:13 -04:00
213 lines
5.9 KiB
C++
213 lines
5.9 KiB
C++
/******************************************************************************
|
|
* 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. *
|
|
******************************************************************************/
|
|
|
|
#include "base/eventqueue.h"
|
|
#include "base/logger_fwd.h"
|
|
#include "base/convert.h"
|
|
#include "base/utility.h"
|
|
#include <sstream>
|
|
#include <boost/bind.hpp>
|
|
#include <boost/exception/diagnostic_information.hpp>
|
|
#include <boost/foreach.hpp>
|
|
|
|
using namespace icinga;
|
|
|
|
EventQueue::EventQueue(void)
|
|
: m_Stopped(false)
|
|
{
|
|
m_ThreadCount = boost::thread::hardware_concurrency();
|
|
|
|
if (m_ThreadCount == 0)
|
|
m_ThreadCount = 1;
|
|
|
|
m_ThreadCount *= 8;
|
|
|
|
m_ThreadCount = 128;
|
|
|
|
m_States = new ThreadState[m_ThreadCount];
|
|
|
|
for (int i = 0; i < m_ThreadCount; i++) {
|
|
m_States[i] = ThreadIdle;
|
|
m_Threads.create_thread(boost::bind(&EventQueue::QueueThreadProc, this, i));
|
|
}
|
|
|
|
boost::thread reportThread(boost::bind(&EventQueue::ReportThreadProc, this));
|
|
reportThread.detach();
|
|
}
|
|
|
|
EventQueue::~EventQueue(void)
|
|
{
|
|
Stop();
|
|
Join();
|
|
}
|
|
|
|
void EventQueue::Stop(void)
|
|
{
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
m_Stopped = true;
|
|
m_CV.notify_all();
|
|
}
|
|
|
|
/**
|
|
* Waits for all worker threads to finish.
|
|
*/
|
|
void EventQueue::Join(void)
|
|
{
|
|
m_Threads.join_all();
|
|
}
|
|
|
|
/**
|
|
* Waits for events and processes them.
|
|
*/
|
|
void EventQueue::QueueThreadProc(int tid)
|
|
{
|
|
for (;;) {
|
|
EventQueueWorkItem event;
|
|
|
|
{
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
m_States[tid] = ThreadIdle;
|
|
|
|
while (m_Events.empty() && !m_Stopped)
|
|
m_CV.wait(lock);
|
|
|
|
if (m_Events.empty() && m_Stopped)
|
|
break;
|
|
|
|
event = m_Events.front();
|
|
m_Events.pop_front();
|
|
|
|
m_States[tid] = ThreadBusy;
|
|
}
|
|
|
|
#ifdef _DEBUG
|
|
double st = Utility::GetTime();
|
|
|
|
# ifdef RUSAGE_THREAD
|
|
struct rusage usage_start, usage_end;
|
|
|
|
(void) getrusage(RUSAGE_THREAD, &usage_start);
|
|
# endif /* RUSAGE_THREAD */
|
|
#endif /* _DEBUG */
|
|
|
|
try {
|
|
event.Callback();
|
|
} catch (const std::exception& ex) {
|
|
std::ostringstream msgbuf;
|
|
msgbuf << "Exception thrown in event handler: " << std::endl
|
|
<< boost::diagnostic_information(ex);
|
|
|
|
Log(LogCritical, "base", msgbuf.str());
|
|
} catch (...) {
|
|
Log(LogCritical, "base", "Exception of unknown type thrown in event handler.");
|
|
}
|
|
|
|
#ifdef _DEBUG
|
|
double et = Utility::GetTime();
|
|
# ifdef RUSAGE_THREAD
|
|
(void) getrusage(RUSAGE_THREAD, &usage_end);
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
double dwait = (et - st) - (duser + dsys);
|
|
|
|
int dminfaults = usage_end.ru_minflt - usage_start.ru_minflt;
|
|
int dmajfaults = usage_end.ru_majflt - usage_start.ru_majflt;
|
|
|
|
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) {
|
|
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 */
|
|
|
|
Log(LogWarning, "base", msgbuf.str());
|
|
}
|
|
#endif /* _DEBUG */
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Appends an event to the event queue. Events will be processed in FIFO order.
|
|
*
|
|
* @param callback The callback function for the event.
|
|
*/
|
|
void EventQueue::Post(const EventQueueCallback& callback)
|
|
{
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
EventQueueWorkItem event;
|
|
event.Callback = callback;
|
|
event.Timestamp = Utility::GetTime();
|
|
|
|
m_Events.push_back(event);
|
|
m_CV.notify_one();
|
|
}
|
|
|
|
void EventQueue::ReportThreadProc(void)
|
|
{
|
|
for (;;) {
|
|
Utility::Sleep(5);
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
int pending, busy;
|
|
double max_latency, avg_latency;
|
|
|
|
{
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
pending = m_Events.size();
|
|
|
|
busy = 0;
|
|
|
|
for (int i = 0; i < m_ThreadCount; i++) {
|
|
if (m_States[i] == ThreadBusy)
|
|
busy++;
|
|
}
|
|
|
|
max_latency = 0;
|
|
avg_latency = 0;
|
|
|
|
BOOST_FOREACH(const EventQueueWorkItem& event, m_Events) {
|
|
double latency = now - event.Timestamp;
|
|
|
|
avg_latency += latency;
|
|
|
|
if (latency > max_latency)
|
|
max_latency = latency;
|
|
}
|
|
|
|
avg_latency /= pending;
|
|
}
|
|
|
|
Log(LogInformation, "base", "Pending tasks: " + Convert::ToString(pending) + "; Busy threads: " +
|
|
Convert::ToString(busy) + "; Idle threads: " + Convert::ToString(m_ThreadCount - busy) +
|
|
"; Maximum latency: " + Convert::ToString((long)max_latency * 1000) + "ms"
|
|
"; Average latency: " + Convert::ToString((long)avg_latency * 1000) + "ms");
|
|
}
|
|
}
|