icinga2/lib/base/workqueue.cpp

304 lines
8.0 KiB
C++
Raw Normal View History

2013-09-18 09:09:16 +02:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
2013-09-18 09:09:16 +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/workqueue.hpp"
#include "base/utility.hpp"
2014-10-19 14:21:12 +02:00
#include "base/logger.hpp"
2014-05-25 16:23:35 +02:00
#include "base/convert.hpp"
#include "base/application.hpp"
#include "base/exception.hpp"
2013-09-18 09:09:16 +02:00
#include <boost/bind.hpp>
#include <boost/thread/tss.hpp>
2013-09-18 09:09:16 +02:00
using namespace icinga;
2013-10-17 10:19:17 +02:00
int WorkQueue::m_NextID = 1;
boost::thread_specific_ptr<WorkQueue *> l_ThreadWorkQueue;
2013-10-17 10:19:17 +02:00
WorkQueue::WorkQueue(size_t maxItems, int threadCount)
2015-01-12 12:36:49 +01:00
: m_ID(m_NextID++), m_ThreadCount(threadCount), m_Spawned(false), m_MaxItems(maxItems), m_Stopped(false),
m_Processing(0), m_NextTaskID(0), m_TaskStats(15 * 60), m_PendingTasks(0), m_PendingTasksTimestamp(0)
2013-10-17 10:19:17 +02:00
{
/* Initialize logger. */
m_StatusTimerTimeout = Utility::GetTime();
m_StatusTimer = new Timer();
m_StatusTimer->SetInterval(10);
m_StatusTimer->OnTimerExpired.connect(boost::bind(&WorkQueue::StatusTimerHandler, this));
m_StatusTimer->Start();
2013-10-17 10:19:17 +02:00
}
2013-09-21 17:53:14 +02:00
2013-09-18 09:09:16 +02:00
WorkQueue::~WorkQueue(void)
{
m_StatusTimer->Stop(true);
Join(true);
2013-09-18 09:09:16 +02:00
}
void WorkQueue::SetName(const String& name)
{
m_Name = name;
}
String WorkQueue::GetName(void) const
{
return m_Name;
}
2013-09-18 09:09:16 +02:00
/**
* Enqueues a task. Tasks are guaranteed to be executed in the order
* they were enqueued in except if there is more than one worker thread or when
* allowInterleaved is true in which case the new task might be run
* immediately if it's being enqueued from within the WorkQueue thread.
2013-09-18 09:09:16 +02:00
*/
void WorkQueue::Enqueue(boost::function<void (void)>&& function, WorkQueuePriority priority,
bool allowInterleaved)
2013-09-18 09:09:16 +02:00
{
bool wq_thread = IsWorkerThread();
if (wq_thread && allowInterleaved) {
function();
return;
}
2013-09-18 09:09:16 +02:00
boost::mutex::scoped_lock lock(m_Mutex);
2013-09-21 17:53:14 +02:00
if (!m_Spawned) {
Log(LogNotice, "WorkQueue")
<< "Spawning WorkQueue threads for '" << m_Name << "'";
for (int i = 0; i < m_ThreadCount; i++) {
m_Threads.create_thread(boost::bind(&WorkQueue::WorkerThreadProc, this));
}
m_Spawned = true;
}
2013-11-22 10:32:53 +01:00
if (!wq_thread) {
2015-09-02 09:16:20 +02:00
while (m_Tasks.size() >= m_MaxItems && m_MaxItems != 0)
m_CVFull.wait(lock);
}
2013-10-17 10:19:17 +02:00
m_Tasks.emplace(std::move(function), priority, ++m_NextTaskID);
2013-11-22 10:32:53 +01:00
m_CVEmpty.notify_one();
2013-09-18 09:09:16 +02:00
}
/**
* Waits until all currently enqueued tasks have completed. This only works reliably
* when no other thread is enqueuing new tasks when this method is called.
*
* @param stop Whether to stop the worker threads
*/
void WorkQueue::Join(bool stop)
2013-09-18 09:09:16 +02:00
{
boost::mutex::scoped_lock lock(m_Mutex);
2013-10-17 15:49:53 +02:00
while (m_Processing || !m_Tasks.empty())
m_CVStarved.wait(lock);
if (stop) {
m_Stopped = true;
m_CVEmpty.notify_all();
lock.unlock();
m_Threads.join_all();
m_Spawned = false;
Log(LogNotice, "WorkQueue")
<< "Stopped WorkQueue threads for '" << m_Name << "'";
}
2013-09-18 09:09:16 +02:00
}
/**
* Checks whether the calling thread is one of the worker threads
* for this work queue.
*
* @returns true if called from one of the worker threads, false otherwise
*/
bool WorkQueue::IsWorkerThread(void) const
{
WorkQueue **pwq = l_ThreadWorkQueue.get();
if (!pwq)
return false;
return *pwq == this;
}
void WorkQueue::SetExceptionCallback(const ExceptionCallback& callback)
{
m_ExceptionCallback = callback;
}
/**
* Checks whether any exceptions have occurred while executing tasks for this
* work queue. When a custom exception callback is set this method will always
* return false.
*/
bool WorkQueue::HasExceptions(void) const
{
boost::mutex::scoped_lock lock(m_Mutex);
return !m_Exceptions.empty();
}
/**
* Returns all exceptions which have occurred for tasks in this work queue. When a
* custom exception callback is set this method will always return an empty list.
*/
std::vector<boost::exception_ptr> WorkQueue::GetExceptions(void) const
{
boost::mutex::scoped_lock lock(m_Mutex);
return m_Exceptions;
}
void WorkQueue::ReportExceptions(const String& facility) const
{
std::vector<boost::exception_ptr> exceptions = GetExceptions();
for (const auto& eptr : exceptions) {
Log(LogCritical, facility)
<< DiagnosticInformation(eptr);
}
Log(LogCritical, facility)
<< exceptions.size() << " error" << (exceptions.size() != 1 ? "s" : "");
}
size_t WorkQueue::GetLength(void) const
{
boost::mutex::scoped_lock lock(m_Mutex);
return m_Tasks.size();
}
void WorkQueue::StatusTimerHandler(void)
{
boost::mutex::scoped_lock lock(m_Mutex);
ASSERT(!m_Name.IsEmpty());
int pending = m_Tasks.size();
double now = Utility::GetTime();
double gradient = (pending - m_PendingTasks) / (now - m_PendingTasksTimestamp);
double timeToZero = pending / gradient;
String timeInfo;
if (pending > GetTaskCount(5)) {
timeInfo = " empty in ";
if (timeToZero < 0)
timeInfo += "infinite time, your task handler isn't able to keep up";
else
timeInfo += Utility::FormatDuration(timeToZero);
}
m_PendingTasks = pending;
m_PendingTasksTimestamp = now;
/* Log if there are pending items, or 5 minute timeout is reached. */
if (pending > 0 || m_StatusTimerTimeout < now) {
Log(LogInformation, "WorkQueue")
<< "#" << m_ID << " (" << m_Name << ") "
<< "items: " << pending << ", "
<< "rate: " << std::setw(2) << GetTaskCount(60) / 60.0 << "/s "
<< "(" << GetTaskCount(60) << "/min " << GetTaskCount(60 * 5) << "/5min " << GetTaskCount(60 * 15) << "/15min);"
<< timeInfo;
}
/* Reschedule next log entry in 5 minutes. */
if (m_StatusTimerTimeout < now) {
m_StatusTimerTimeout = now + 60 * 5;
}
}
void WorkQueue::WorkerThreadProc(void)
2013-09-18 09:09:16 +02:00
{
std::ostringstream idbuf;
idbuf << "WQ #" << m_ID;
Utility::SetThreadName(idbuf.str());
2013-09-18 09:09:16 +02:00
l_ThreadWorkQueue.reset(new WorkQueue *(this));
boost::mutex::scoped_lock lock(m_Mutex);
for (;;) {
while (m_Tasks.empty() && !m_Stopped)
m_CVEmpty.wait(lock);
if (m_Stopped)
break;
2015-09-02 09:16:20 +02:00
if (m_Tasks.size() >= m_MaxItems && m_MaxItems != 0)
m_CVFull.notify_all();
2013-10-17 10:19:17 +02:00
Task task = m_Tasks.top();
m_Tasks.pop();
m_Processing++;
lock.unlock();
try {
task.Function();
} catch (const std::exception&) {
lock.lock();
if (!m_ExceptionCallback)
m_Exceptions.push_back(boost::current_exception());
2013-11-22 10:32:53 +01:00
lock.unlock();
2013-11-22 10:32:53 +01:00
if (m_ExceptionCallback)
m_ExceptionCallback(boost::current_exception());
}
2013-11-22 10:32:53 +01:00
/* clear the task so whatever other resources it holds are released
_before_ we re-acquire the mutex */
task = Task();
IncreaseTaskCount();
lock.lock();
2013-11-22 10:32:53 +01:00
m_Processing--;
2013-11-22 10:32:53 +01:00
if (m_Tasks.empty())
m_CVStarved.notify_all();
2013-11-22 10:32:53 +01:00
}
}
void WorkQueue::IncreaseTaskCount(void)
{
double now = Utility::GetTime();
boost::mutex::scoped_lock lock(m_StatsMutex);
m_TaskStats.InsertValue(now, 1);
}
int WorkQueue::GetTaskCount(RingBuffer::SizeType span) const
{
boost::mutex::scoped_lock lock(m_StatsMutex);
return m_TaskStats.GetValues(span);
}