2013-09-18 09:09:16 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 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"
|
2014-11-16 13:14:42 +01:00
|
|
|
#include "base/application.hpp"
|
2014-12-18 15:11:57 +01:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include <boost/thread/tss.hpp>
|
2013-09-18 09:09:16 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2017-12-14 08:47:04 +01:00
|
|
|
std::atomic<int> WorkQueue::m_NextID(1);
|
2014-12-18 15:11:57 +01:00
|
|
|
boost::thread_specific_ptr<WorkQueue *> l_ThreadWorkQueue;
|
2013-10-17 10:19:17 +02:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
WorkQueue::WorkQueue(size_t maxItems, int threadCount)
|
2018-01-04 09:43:49 +01:00
|
|
|
: m_ID(m_NextID++), m_ThreadCount(threadCount), m_MaxItems(maxItems),
|
|
|
|
m_TaskStats(15 * 60)
|
2013-10-17 10:19:17 +02:00
|
|
|
{
|
2017-05-16 12:54:37 +02:00
|
|
|
/* Initialize logger. */
|
|
|
|
m_StatusTimerTimeout = Utility::GetTime();
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
m_StatusTimer = new Timer();
|
2013-11-29 17:07:04 +01:00
|
|
|
m_StatusTimer->SetInterval(10);
|
2017-11-21 11:52:55 +01:00
|
|
|
m_StatusTimer->OnTimerExpired.connect(std::bind(&WorkQueue::StatusTimerHandler, this));
|
2013-11-29 17:07:04 +01:00
|
|
|
m_StatusTimer->Start();
|
2013-10-17 10:19:17 +02:00
|
|
|
}
|
2013-09-21 17:53:14 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
WorkQueue::~WorkQueue()
|
2013-09-18 09:09:16 +02:00
|
|
|
{
|
2015-03-01 22:25:14 +01:00
|
|
|
m_StatusTimer->Stop(true);
|
2015-02-28 08:43:49 +01:00
|
|
|
|
2013-12-10 21:44:38 +01:00
|
|
|
Join(true);
|
2013-09-18 09:09:16 +02:00
|
|
|
}
|
|
|
|
|
2016-06-14 08:19:13 +02:00
|
|
|
void WorkQueue::SetName(const String& name)
|
|
|
|
{
|
|
|
|
m_Name = name;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String WorkQueue::GetName() const
|
2016-06-14 08:19:13 +02:00
|
|
|
{
|
|
|
|
return m_Name;
|
|
|
|
}
|
|
|
|
|
2013-09-18 09:09:16 +02:00
|
|
|
/**
|
2014-12-18 15:11:57 +01: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
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
void WorkQueue::Enqueue(std::function<void ()>&& function, WorkQueuePriority priority,
|
2017-12-19 15:50:05 +01:00
|
|
|
bool allowInterleaved)
|
2013-09-18 09:09:16 +02:00
|
|
|
{
|
2014-12-18 15:11:57 +01:00
|
|
|
bool wq_thread = IsWorkerThread();
|
2013-12-12 06:30:11 +01:00
|
|
|
|
|
|
|
if (wq_thread && allowInterleaved) {
|
2015-12-10 16:54:43 +01:00
|
|
|
function();
|
2013-12-12 06:30:11 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-18 09:09:16 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-09-21 17:53:14 +02:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
if (!m_Spawned) {
|
2016-08-09 12:39:07 +02:00
|
|
|
Log(LogNotice, "WorkQueue")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Spawning WorkQueue threads for '" << m_Name << "'";
|
2016-08-09 12:39:07 +02:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
for (int i = 0; i < m_ThreadCount; i++) {
|
2017-11-21 11:52:55 +01:00
|
|
|
m_Threads.create_thread(std::bind(&WorkQueue::WorkerThreadProc, this));
|
2014-12-18 15:11:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_Spawned = true;
|
|
|
|
}
|
2014-04-29 13:17:08 +02:00
|
|
|
|
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)
|
2013-11-29 17:07:04 +01:00
|
|
|
m_CVFull.wait(lock);
|
2013-11-21 13:54:01 +01:00
|
|
|
}
|
2013-10-17 10:19:17 +02:00
|
|
|
|
2016-08-29 08:16:33 +02:00
|
|
|
m_Tasks.emplace(std::move(function), priority, ++m_NextTaskID);
|
2013-11-22 10:32:53 +01:00
|
|
|
|
2015-11-16 08:13:55 +01:00
|
|
|
m_CVEmpty.notify_one();
|
2013-09-18 09:09:16 +02:00
|
|
|
}
|
|
|
|
|
2014-12-18 15:11:57 +01: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
|
|
|
|
*/
|
2013-12-10 21:44:38 +01:00
|
|
|
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
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
while (m_Processing || !m_Tasks.empty())
|
2013-12-10 21:44:38 +01:00
|
|
|
m_CVStarved.wait(lock);
|
|
|
|
|
|
|
|
if (stop) {
|
|
|
|
m_Stopped = true;
|
|
|
|
m_CVEmpty.notify_all();
|
|
|
|
lock.unlock();
|
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
m_Threads.join_all();
|
|
|
|
m_Spawned = false;
|
2016-08-09 12:39:07 +02:00
|
|
|
|
|
|
|
Log(LogNotice, "WorkQueue")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Stopped WorkQueue threads for '" << m_Name << "'";
|
2013-12-10 21:44:38 +01:00
|
|
|
}
|
2013-09-18 09:09:16 +02:00
|
|
|
}
|
|
|
|
|
2014-12-18 15:11:57 +01: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
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
bool WorkQueue::IsWorkerThread() const
|
2013-10-30 13:13:09 +01:00
|
|
|
{
|
2014-12-18 15:11:57 +01:00
|
|
|
WorkQueue **pwq = l_ThreadWorkQueue.get();
|
|
|
|
|
|
|
|
if (!pwq)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return *pwq == this;
|
2013-10-30 13:13:09 +01:00
|
|
|
}
|
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
void WorkQueue::SetExceptionCallback(const ExceptionCallback& callback)
|
|
|
|
{
|
|
|
|
m_ExceptionCallback = callback;
|
|
|
|
}
|
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
bool WorkQueue::HasExceptions() const
|
2014-02-18 10:53:44 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2017-12-19 15:50:05 +01:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
return !m_Exceptions.empty();
|
|
|
|
}
|
2014-02-18 10:53:44 +01:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
std::vector<boost::exception_ptr> WorkQueue::GetExceptions() const
|
2014-12-18 15:11:57 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2017-12-19 15:50:05 +01:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
return m_Exceptions;
|
2014-02-18 10:53:44 +01:00
|
|
|
}
|
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
void WorkQueue::ReportExceptions(const String& facility) const
|
2013-11-05 12:40:25 +01:00
|
|
|
{
|
2016-08-31 13:43:14 +02:00
|
|
|
std::vector<boost::exception_ptr> exceptions = GetExceptions();
|
2014-12-18 15:11:57 +01:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const auto& eptr : exceptions) {
|
2014-12-18 15:11:57 +01:00
|
|
|
Log(LogCritical, facility)
|
2017-12-19 15:50:05 +01:00
|
|
|
<< DiagnosticInformation(eptr);
|
2014-12-18 15:11:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Log(LogCritical, facility)
|
2017-12-19 15:50:05 +01:00
|
|
|
<< exceptions.size() << " error" << (exceptions.size() != 1 ? "s" : "");
|
2014-12-18 15:11:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
size_t WorkQueue::GetLength() const
|
2014-12-18 15:11:57 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
return m_Tasks.size();
|
2013-11-05 12:40:25 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void WorkQueue::StatusTimerHandler()
|
2013-11-29 17:07:04 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
2017-05-16 12:54:37 +02:00
|
|
|
ASSERT(!m_Name.IsEmpty());
|
|
|
|
|
2017-12-14 08:47:04 +01:00
|
|
|
size_t pending = m_Tasks.size();
|
2016-06-14 08:19:13 +02:00
|
|
|
|
2017-05-16 12:54:37 +02:00
|
|
|
double now = Utility::GetTime();
|
|
|
|
double gradient = (pending - m_PendingTasks) / (now - m_PendingTasksTimestamp);
|
|
|
|
double timeToZero = pending / gradient;
|
2016-06-14 08:19:13 +02:00
|
|
|
|
2017-05-16 12:54:37 +02:00
|
|
|
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);
|
|
|
|
}
|
2016-06-14 08:19:13 +02:00
|
|
|
|
2017-05-16 12:54:37 +02:00
|
|
|
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")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "#" << 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;
|
2017-05-16 12:54:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reschedule next log entry in 5 minutes. */
|
|
|
|
if (m_StatusTimerTimeout < now) {
|
|
|
|
m_StatusTimerTimeout = now + 60 * 5;
|
|
|
|
}
|
2013-11-29 17:07:04 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void WorkQueue::WorkerThreadProc()
|
2013-09-18 09:09:16 +02:00
|
|
|
{
|
2013-12-12 06:30:11 +01:00
|
|
|
std::ostringstream idbuf;
|
|
|
|
idbuf << "WQ #" << m_ID;
|
|
|
|
Utility::SetThreadName(idbuf.str());
|
2013-09-18 09:09:16 +02:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
l_ThreadWorkQueue.reset(new WorkQueue *(this));
|
|
|
|
|
2013-12-12 06:30:11 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-11-28 12:12:24 +01:00
|
|
|
|
2013-12-12 06:30:11 +01:00
|
|
|
for (;;) {
|
2014-12-18 15:11:57 +01:00
|
|
|
while (m_Tasks.empty() && !m_Stopped)
|
2013-12-12 06:30:11 +01:00
|
|
|
m_CVEmpty.wait(lock);
|
2013-11-05 12:40:25 +01:00
|
|
|
|
2013-12-12 06:30:11 +01:00
|
|
|
if (m_Stopped)
|
|
|
|
break;
|
2013-11-05 12:40:25 +01:00
|
|
|
|
2015-09-02 09:16:20 +02:00
|
|
|
if (m_Tasks.size() >= m_MaxItems && m_MaxItems != 0)
|
2013-12-12 06:30:11 +01:00
|
|
|
m_CVFull.notify_all();
|
2013-10-17 10:19:17 +02:00
|
|
|
|
2015-12-10 16:54:43 +01:00
|
|
|
Task task = m_Tasks.top();
|
|
|
|
m_Tasks.pop();
|
2014-12-18 15:11:57 +01:00
|
|
|
|
|
|
|
m_Processing++;
|
2013-12-10 21:44:38 +01:00
|
|
|
|
2013-12-12 06:30:11 +01:00
|
|
|
lock.unlock();
|
2013-12-10 21:44:38 +01:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
try {
|
2015-12-10 16:54:43 +01:00
|
|
|
task.Function();
|
2014-12-18 15:11:57 +01:00
|
|
|
} catch (const std::exception&) {
|
|
|
|
lock.lock();
|
2013-12-10 21:44:38 +01:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
if (!m_ExceptionCallback)
|
2016-08-31 13:43:14 +02:00
|
|
|
m_Exceptions.push_back(boost::current_exception());
|
2013-11-22 10:32:53 +01:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
lock.unlock();
|
2013-11-22 10:32:53 +01:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
if (m_ExceptionCallback)
|
2016-08-31 13:43:14 +02:00
|
|
|
m_ExceptionCallback(boost::current_exception());
|
2013-12-12 06:30:11 +01:00
|
|
|
}
|
2013-11-22 10:32:53 +01:00
|
|
|
|
2017-12-19 15:50:05 +01:00
|
|
|
/* clear the task so whatever other resources it holds are released _before_ we re-acquire the mutex */
|
2015-02-27 18:29:21 +01:00
|
|
|
task = Task();
|
|
|
|
|
2017-05-16 12:54:37 +02:00
|
|
|
IncreaseTaskCount();
|
|
|
|
|
2013-12-12 06:30:11 +01:00
|
|
|
lock.lock();
|
2013-11-22 10:32:53 +01:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
m_Processing--;
|
2013-11-22 10:32:53 +01:00
|
|
|
|
2014-12-18 15:11:57 +01:00
|
|
|
if (m_Tasks.empty())
|
|
|
|
m_CVStarved.notify_all();
|
2013-11-22 10:32:53 +01:00
|
|
|
}
|
2013-12-10 21:44:38 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void WorkQueue::IncreaseTaskCount()
|
2017-05-16 12:54:37 +02:00
|
|
|
{
|
|
|
|
double now = Utility::GetTime();
|
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_StatsMutex);
|
|
|
|
m_TaskStats.InsertValue(now, 1);
|
|
|
|
}
|
|
|
|
|
2017-12-14 08:47:04 +01:00
|
|
|
size_t WorkQueue::GetTaskCount(RingBuffer::SizeType span)
|
2017-05-16 12:54:37 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_StatsMutex);
|
2017-11-13 16:17:59 +01:00
|
|
|
return m_TaskStats.UpdateAndGetValues(Utility::GetTime(), span);
|
2017-05-16 12:54:37 +02:00
|
|
|
}
|
2018-01-04 10:36:35 +01:00
|
|
|
|
|
|
|
bool icinga::operator<(const Task& a, const Task& b)
|
|
|
|
{
|
|
|
|
if (a.Priority < b.Priority)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (a.Priority == b.Priority) {
|
|
|
|
if (a.ID > b.ID)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|