Added central control of threading.

This commit is contained in:
Pedro Pinto committed 2018-10-02 16:24:00 +00:00
1 parent ef3b39361a
commit 2769de6f7e
25 files changed
+259 -110

No files matched your search

+101
View File
@@ -0,0 +1,101 @@
#include "thread_manager.h"
#include "lib/config/config.h"
#include "lib/config/protos/threads.pb.h"
#include "lib/logging/logging.h"
#include "lib/utils/bts_assert.h"
#include <boost/regex.hpp>
#include <fmt/format.h>
#ifndef WIN32
#include <pthread.h>
#endif
namespace bts {
namespace threading {
config::ThreadGroupCfg get_thread_cfg(std::string const &name) {
for (auto const &cfg : config::get<config::Threads>().thread_group()) {
boost::regex rx(cfg.name_rx());
if (boost::regex_match(name, rx))
return cfg;
}
bts_assert_msg(false,
fmt::format("No configuration found for thread {}", name));
}
#ifndef WIN32
void configure_thread(std::string const &name,
config::ThreadGroupCfg const &cfg, pthread_t t) {
if (cfg.core().size()) {
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 (auto const &core : cfg.core())
CPU_CLR(core, &cpuset);
} else {
for (auto const &core : cfg.core())
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);
}
}
#endif
void 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(std::string const &name,
config::ThreadGroupCfg const &cfg) {
#ifndef WIN32
configure_thread(name, cfg, pthread_self());
#endif
os::set_thread_name(name);
}
std::thread make_thread_from_function(std::string const &name,
std::function<void()> const &c) {
auto result = std::thread(
[c,name] () {
os::set_thread_name(name);
c();
});
#ifndef WIN32
configure_thread(name, get_thread_cfg(name), result.native_handle());
#endif
return result;
}
std::thread make_thread_from_function(std::string const &name,
std::function<void()> const &c,
config::ThreadGroupCfg const &cfg) {
auto result = std::thread(
[c,name] () {
os::set_thread_name(name);
c();
});
#ifndef WIN32
configure_thread(name, cfg, result.native_handle());
#endif
return result;
}
} // namespace threading
} // namespace bts