refactor: Move thread_manager to lib_system
This commit is contained in:
1 parent
19450f4029
commit
a8a474dd7f
3 files changed
+153
-34
No files matched your search
@@ -1,24 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include "spark/config/config.h"
|
||||
#include "system/protobufs/threads.pb.h"
|
||||
#include "system/threading/thread_manager.h"
|
||||
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
namespace bts::system::protobufs {
|
||||
class ThreadGroupCfg;
|
||||
}
|
||||
|
||||
namespace bts {
|
||||
namespace threading {
|
||||
|
||||
std::thread make_thread_from_function(
|
||||
namespace internal {
|
||||
|
||||
inline std::mutex instance_mx_;
|
||||
inline std::unique_ptr<system::threading::ThreadManager> instance_{nullptr};
|
||||
|
||||
inline system::threading::ThreadManager &instance() {
|
||||
if (instance_ == nullptr) {
|
||||
std::lock_guard<std::mutex> lock{instance_mx_};
|
||||
if (instance_ == nullptr) {
|
||||
instance_ = std::make_unique<system::threading::ThreadManager>(
|
||||
config::get<system::protobufs::Threads>());
|
||||
}
|
||||
}
|
||||
return *instance_;
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
inline std::thread make_thread_from_function(
|
||||
std::string const &name,
|
||||
std::function<void()> const &c,
|
||||
system::protobufs::ThreadGroupCfg const &cfg);
|
||||
std::thread make_thread_from_function(
|
||||
std::string const &name, std::function<void()> const &c);
|
||||
system::protobufs::ThreadGroupCfg const &cfg) {
|
||||
return internal::instance().make_thread_from_function(name, c, cfg);
|
||||
}
|
||||
|
||||
inline std::thread make_thread_from_function(
|
||||
std::string const &name, std::function<void()> const &c) {
|
||||
return internal::instance().make_thread_from_function(name, c);
|
||||
}
|
||||
|
||||
template <class C>
|
||||
std::thread make_thread(std::string const &name, C callable) {
|
||||
@@ -33,13 +55,22 @@ std::thread make_thread(
|
||||
return make_thread_from_function(name, std::function<void()>(callable), cfg);
|
||||
}
|
||||
|
||||
void configure_current_thread(std::string const &name);
|
||||
void configure_current_thread(
|
||||
std::string const &name, system::protobufs::ThreadGroupCfg const &);
|
||||
inline void configure_current_thread(std::string const &name) {
|
||||
return internal::instance().configure_current_thread(name);
|
||||
}
|
||||
inline void configure_current_thread(
|
||||
std::string const &name, system::protobufs::ThreadGroupCfg const &cfg) {
|
||||
return internal::instance().configure_current_thread(name, cfg);
|
||||
}
|
||||
|
||||
system::protobufs::ThreadGroupCfg get_thread_cfg(std::string const &);
|
||||
inline system::protobufs::ThreadGroupCfg get_thread_cfg(
|
||||
std::string const &name) {
|
||||
return internal::instance().get_thread_cfg(name);
|
||||
}
|
||||
|
||||
std::vector<uint32_t> get_affinity(std::string const &name);
|
||||
inline std::vector<uint32_t> get_affinity(std::string const &name) {
|
||||
return internal::instance().get_affinity(name);
|
||||
}
|
||||
|
||||
} // namespace threading
|
||||
} // namespace bts
|
||||
+31
-21
@@ -1,7 +1,6 @@
|
||||
#include "thread_manager.h"
|
||||
#include "spark/config/config.h"
|
||||
#include "spark/logging/logging.h"
|
||||
#include "spark/utils/bts_assert.h"
|
||||
#include "system/facades/bts_assert.h"
|
||||
#include "system/os.h"
|
||||
#include "system/protobufs/threads.pb.h"
|
||||
|
||||
#include <boost/regex.hpp>
|
||||
@@ -11,21 +10,32 @@
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
|
||||
namespace bts {
|
||||
namespace threading {
|
||||
namespace bts::system::threading {
|
||||
|
||||
system::protobufs::ThreadGroupCfg get_thread_cfg(std::string const &name) {
|
||||
for (auto const &cfg :
|
||||
config::get<system::protobufs::Threads>().thread_group()) {
|
||||
struct ThreadManager::PImpl {
|
||||
protobufs::Threads config;
|
||||
};
|
||||
|
||||
ThreadManager::ThreadManager(protobufs::Threads config)
|
||||
: me_(std::make_unique<PImpl>()) {
|
||||
me_->config = std::move(config);
|
||||
}
|
||||
|
||||
ThreadManager::~ThreadManager() = default;
|
||||
|
||||
system::protobufs::ThreadGroupCfg ThreadManager::get_thread_cfg(
|
||||
std::string const &name) {
|
||||
for (auto const &cfg : me_->config.thread_group()) {
|
||||
boost::regex rx(cfg.name_rx());
|
||||
if (boost::regex_match(name, rx))
|
||||
if (boost::regex_match(name, rx)) {
|
||||
return cfg;
|
||||
}
|
||||
}
|
||||
bts_assert_msg(
|
||||
false, fmt::format("No configuration found for thread {}", name));
|
||||
}
|
||||
|
||||
std::vector<uint32_t> get_affinity(std::string const &name) {
|
||||
std::vector<uint32_t> ThreadManager::get_affinity(std::string const &name) {
|
||||
std::vector<uint32_t> result;
|
||||
#ifndef _WIN32
|
||||
auto cfg = get_thread_cfg(name);
|
||||
@@ -44,7 +54,7 @@ std::vector<uint32_t> get_affinity(std::string const &name) {
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
void configure_thread(
|
||||
void ThreadManager::configure_thread(
|
||||
std::string const &name,
|
||||
const system::protobufs::ThreadGroupCfg &cfg,
|
||||
pthread_t t) {
|
||||
@@ -65,23 +75,24 @@ void configure_thread(
|
||||
|
||||
int rc = pthread_setaffinity_np(t, sizeof(cpu_set_t), &cpuset);
|
||||
|
||||
if (rc != 0)
|
||||
LOG(Gen, Error,
|
||||
"Could not set core affinity for {} on thread {}. Error "
|
||||
"code={}",
|
||||
if (rc != 0) {
|
||||
const std::string err_msg = fmt::format(
|
||||
"Could not set core affinity for {} on thread {}. Error code={}",
|
||||
name, cfg.name_rx(), rc);
|
||||
std::cerr << err_msg << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void configure_current_thread(std::string const &name) {
|
||||
void ThreadManager::configure_current_thread(std::string const &name) {
|
||||
#ifndef _WIN32
|
||||
configure_thread(name, get_thread_cfg(name), pthread_self());
|
||||
#endif
|
||||
os::set_thread_name(name);
|
||||
}
|
||||
|
||||
void configure_current_thread(
|
||||
void ThreadManager::configure_current_thread(
|
||||
std::string const &name, const system::protobufs::ThreadGroupCfg &cfg) {
|
||||
#ifndef _WIN32
|
||||
configure_thread(name, cfg, pthread_self());
|
||||
@@ -89,7 +100,7 @@ void configure_current_thread(
|
||||
os::set_thread_name(name);
|
||||
}
|
||||
|
||||
std::thread make_thread_from_function(
|
||||
std::thread ThreadManager::make_thread_from_function(
|
||||
std::string const &name, std::function<void()> const &c) {
|
||||
auto result = std::thread([c, name]() {
|
||||
os::set_thread_name(name);
|
||||
@@ -102,7 +113,7 @@ std::thread make_thread_from_function(
|
||||
return result;
|
||||
}
|
||||
|
||||
std::thread make_thread_from_function(
|
||||
std::thread ThreadManager::make_thread_from_function(
|
||||
std::string const &name,
|
||||
std::function<void()> const &c,
|
||||
const system::protobufs::ThreadGroupCfg &cfg) {
|
||||
@@ -116,5 +127,4 @@ std::thread make_thread_from_function(
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace threading
|
||||
} // namespace bts
|
||||
} // namespace bts::system::threading
|
||||
@@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include "system/protobufs/threads.pb.h"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
namespace bts::system::threading {
|
||||
|
||||
class ThreadManager {
|
||||
public:
|
||||
explicit ThreadManager(protobufs::Threads config);
|
||||
~ThreadManager();
|
||||
|
||||
static system::protobufs::Threads default_thread_config();
|
||||
|
||||
static std::thread make_thread_from_function(
|
||||
std::string const &name,
|
||||
std::function<void()> const &c,
|
||||
system::protobufs::ThreadGroupCfg const &cfg);
|
||||
std::thread make_thread_from_function(
|
||||
std::string const &name, std::function<void()> const &c);
|
||||
|
||||
template <class C>
|
||||
std::thread make_thread(std::string const &name, C callable);
|
||||
|
||||
template <class C>
|
||||
static std::thread make_thread(
|
||||
std::string const &name,
|
||||
C callable,
|
||||
system::protobufs::ThreadGroupCfg const &cfg);
|
||||
|
||||
void configure_current_thread(std::string const &name);
|
||||
void configure_current_thread(
|
||||
std::string const &name, system::protobufs::ThreadGroupCfg const &);
|
||||
|
||||
system::protobufs::ThreadGroupCfg get_thread_cfg(std::string const &);
|
||||
|
||||
std::vector<uint32_t> get_affinity(std::string const &name);
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me_;
|
||||
|
||||
#ifndef _WIN32
|
||||
static void configure_thread(
|
||||
std::string const &name,
|
||||
const system::protobufs::ThreadGroupCfg &cfg,
|
||||
pthread_t t);
|
||||
#endif
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Definition
|
||||
//////////////////////////////////////////////////
|
||||
|
||||
inline system::protobufs::Threads ThreadManager::default_thread_config() {
|
||||
system::protobufs::Threads rv;
|
||||
*rv.mutable_thread_group()->Add()->mutable_name_rx() = ".*";
|
||||
return rv;
|
||||
}
|
||||
|
||||
template <class C>
|
||||
std::thread ThreadManager::make_thread(std::string const &name, C callable) {
|
||||
return make_thread_from_function(name, std::function<void()>(callable));
|
||||
}
|
||||
|
||||
template <class C>
|
||||
std::thread ThreadManager::make_thread(
|
||||
std::string const &name,
|
||||
C callable,
|
||||
system::protobufs::ThreadGroupCfg const &cfg) {
|
||||
return make_thread_from_function(name, std::function<void()>(callable), cfg);
|
||||
}
|
||||
|
||||
} // namespace bts::system::threading
|
||||
Reference in new issue
Block a user