osquery-1/osquery/dispatcher/dispatcher.cpp

132 lines
3.5 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include <boost/date_time/posix_time/posix_time.hpp>
#include <osquery/flags.h>
#include <osquery/logger.h>
#include "osquery/core/conversions.h"
2015-02-19 23:19:00 +00:00
#include "osquery/dispatcher/dispatcher.h"
using namespace apache::thrift::concurrency;
namespace osquery {
/// The worker_threads define the default thread pool size.
2015-02-17 08:36:20 +00:00
FLAG(int32, worker_threads, 4, "Number of work dispatch threads");
void interruptableSleep(size_t milli) {
boost::this_thread::sleep(boost::posix_time::milliseconds(milli));
}
Dispatcher::Dispatcher() {
thread_manager_ = InternalThreadManager::newSimpleThreadManager(
(size_t)FLAGS_worker_threads, 0);
auto threadFactory = ThriftThreadFactory(new PosixThreadFactory());
2014-09-15 18:09:33 +00:00
thread_manager_->threadFactory(threadFactory);
thread_manager_->start();
}
Status Dispatcher::add(ThriftInternalRunnableRef task) {
try {
2015-05-04 03:02:01 +00:00
instance().thread_manager_->add(task, 0, 0);
2014-09-21 21:29:28 +00:00
} catch (std::exception& e) {
return Status(1, e.what());
}
return Status(0, "OK");
}
Status Dispatcher::addService(InternalRunnableRef service) {
2015-02-04 03:55:16 +00:00
if (service->hasRun()) {
return Status(1, "Cannot schedule a service twice");
}
2015-05-04 03:02:01 +00:00
auto& self = instance();
2015-02-04 03:55:16 +00:00
auto thread = std::make_shared<boost::thread>(
boost::bind(&InternalRunnable::run, &*service));
2015-05-04 03:02:01 +00:00
self.service_threads_.push_back(thread);
self.services_.push_back(std::move(service));
2015-02-04 03:55:16 +00:00
return Status(0, "OK");
}
2015-05-04 03:02:01 +00:00
InternalThreadManagerRef Dispatcher::getThreadManager() const {
return instance().thread_manager_;
}
2015-05-04 03:02:01 +00:00
void Dispatcher::join() { instance().thread_manager_->join(); }
2015-02-04 03:55:16 +00:00
void Dispatcher::joinServices() {
2015-05-04 03:02:01 +00:00
for (auto& thread : instance().service_threads_) {
2015-02-04 03:55:16 +00:00
thread->join();
}
}
void Dispatcher::removeServices() {
2015-05-04 03:02:01 +00:00
auto& self = instance();
for (const auto& service : self.services_) {
2015-02-04 03:55:16 +00:00
while (true) {
// Wait for each thread's entry point (enter) meaning the thread context
// was allocated and (run) was called by boost::thread started.
if (service->hasRun()) {
break;
}
2015-05-04 03:02:01 +00:00
// We only need to check if std::terminate is called very quickly after
2015-02-04 03:55:16 +00:00
// the boost::thread is created.
::usleep(200);
}
}
2015-05-04 03:02:01 +00:00
for (auto& thread : self.service_threads_) {
2015-02-04 03:55:16 +00:00
thread->interrupt();
}
// Deallocate services.
2015-05-04 03:02:01 +00:00
self.service_threads_.clear();
self.services_.clear();
2015-02-04 03:55:16 +00:00
}
InternalThreadManager::STATE Dispatcher::state() const {
2015-05-04 03:02:01 +00:00
return instance().thread_manager_->state();
}
2015-05-04 03:02:01 +00:00
void Dispatcher::addWorker(size_t value) {
instance().thread_manager_->addWorker(value);
}
void Dispatcher::removeWorker(size_t value) {
2015-05-04 03:02:01 +00:00
instance().thread_manager_->removeWorker(value);
}
size_t Dispatcher::idleWorkerCount() const {
2015-05-04 03:02:01 +00:00
return instance().thread_manager_->idleWorkerCount();
}
2014-09-15 18:09:33 +00:00
size_t Dispatcher::workerCount() const {
2015-05-04 03:02:01 +00:00
return instance().thread_manager_->workerCount();
2014-09-15 18:09:33 +00:00
}
size_t Dispatcher::pendingTaskCount() const {
2015-05-04 03:02:01 +00:00
return instance().thread_manager_->pendingTaskCount();
}
size_t Dispatcher::totalTaskCount() const {
2015-05-04 03:02:01 +00:00
return instance().thread_manager_->totalTaskCount();
}
size_t Dispatcher::pendingTaskCountMax() const {
2015-05-04 03:02:01 +00:00
return instance().thread_manager_->pendingTaskCountMax();
}
size_t Dispatcher::expiredTaskCount() const {
2015-05-04 03:02:01 +00:00
return instance().thread_manager_->expiredTaskCount();
}
}