118 lines
3.1 KiB
C++
118 lines
3.1 KiB
C++
#include "thread_manager.h"
|
|
#include "bts-system/bts_assert.h"
|
|
#include "bts-system/logging.h"
|
|
#include "bts-system/threading/thread_config.h"
|
|
|
|
#include <fmt/format.h>
|
|
#include <functional>
|
|
#include <pthread.h>
|
|
#include <regex>
|
|
#include <string>
|
|
#include <sys/prctl.h>
|
|
|
|
namespace bts::system::threading {
|
|
|
|
using std::function;
|
|
using std::string;
|
|
using std::uint32_t;
|
|
using std::vector;
|
|
|
|
ThreadConfig ThreadManager::get_thread_cfg(const string &name) {
|
|
for (const auto &cfg : this->threads) {
|
|
std::smatch match;
|
|
std::regex rgx{cfg.name_rx};
|
|
if (std::regex_search(name, match, rgx)) {
|
|
return cfg;
|
|
}
|
|
}
|
|
bts_assert_msg(
|
|
false, fmt::format("No configuration found for thread {}", name));
|
|
}
|
|
|
|
vector<uint32_t> ThreadManager::get_affinity(const string &name) {
|
|
vector<uint32_t> result;
|
|
auto cfg = get_thread_cfg(name);
|
|
if (cfg.exclude) {
|
|
auto total_cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
auto &cores = cfg.cores;
|
|
for (int i = 0; i < total_cpus; i++) {
|
|
if (std::find(cores.begin(), cores.end(), i) == cores.end()) {
|
|
result.push_back(i);
|
|
}
|
|
}
|
|
} else {
|
|
for (const auto &core : cfg.cores) {
|
|
result.push_back(core);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void configure_thread(
|
|
const string &name, const ThreadConfig &cfg, pthread_t t) {
|
|
if (!cfg.cores.empty()) {
|
|
cpu_set_t cpuset;
|
|
CPU_ZERO(&cpuset);
|
|
|
|
if (cfg.exclude) {
|
|
auto total_cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
for (int i = 0; i < total_cpus; i++) {
|
|
CPU_SET(i, &cpuset);
|
|
}
|
|
for (const auto &core : cfg.cores) {
|
|
CPU_CLR(core, &cpuset);
|
|
}
|
|
} else {
|
|
for (const auto &core : cfg.cores) {
|
|
CPU_SET(core, &cpuset);
|
|
}
|
|
}
|
|
|
|
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={}",
|
|
name, cfg.name_rx, rc);
|
|
}
|
|
}
|
|
}
|
|
|
|
void ThreadManager::set_thread_name(const string &name) {
|
|
prctl(PR_SET_NAME, name.c_str(), 0, 0, 0);
|
|
}
|
|
|
|
void ThreadManager::configure_current_thread(const string &name) {
|
|
configure_thread(name, get_thread_cfg(name), pthread_self());
|
|
set_thread_name(name);
|
|
}
|
|
|
|
void ThreadManager::configure_current_thread(
|
|
const string &name, ThreadConfig const &cfg) {
|
|
configure_thread(name, cfg, pthread_self());
|
|
set_thread_name(name);
|
|
}
|
|
|
|
std::thread ThreadManager::make_thread_from_function(
|
|
const string &name, function<void()> const &c) {
|
|
auto result = std::thread([c, name]() {
|
|
set_thread_name(name);
|
|
c();
|
|
});
|
|
|
|
configure_thread(name, get_thread_cfg(name), result.native_handle());
|
|
return result;
|
|
}
|
|
|
|
std::thread ThreadManager::make_thread_from_function(
|
|
const string &name, function<void()> const &c, ThreadConfig const &cfg) {
|
|
auto result = std::thread([c, name]() {
|
|
set_thread_name(name);
|
|
c();
|
|
});
|
|
configure_thread(name, cfg, result.native_handle());
|
|
return result;
|
|
}
|
|
|
|
} // namespace bts::system::threading
|