refactor: Move threads.proto to lib_system

This commit is contained in:
John Colvin committed 2024-09-18 14:02:40 +00:00
1 parent b413f519b5
commit 86d54fdd68
9 files changed
+98 -33

No files matched your search

+4 -1
View File
@@ -5,6 +5,7 @@
#include "spark/utils/bts_assert.h"
#include "spark/utils/bts_chrono.h"
#include "spark/utils/thread_manager.h"
#include "system/protobufs/threads.pb.h"
#include <algorithm>
#include <atomic>
@@ -14,6 +15,8 @@
namespace bts {
namespace log {
using bts::system::protobufs::ThreadGroupCfg;
std::atomic<size_t> thread_idx;
struct LoggerHub::SinkRec {
@@ -38,7 +41,7 @@ struct LoggerHub::PImpl {
size_t logger_initial_queue_size;
size_t logger_max_queue_growth;
std::string logger_name;
config::ThreadGroupCfg thread_cfg;
ThreadGroupCfg thread_cfg;
};
LoggerHub::LoggerHub(std::string const &logger_name)
+3 -2
View File
@@ -2,7 +2,8 @@
#include "Common.h"
#include "Sink.h"
#include "spark/config/protos/threads.pb.h"
#include "system/protobufs/threads.pb.h"
#include <map>
#include <vector>
@@ -21,7 +22,7 @@ struct LoggingConfig {
std::map<Level, std::string> level_names;
std::map<Subsystem, std::string> subsystem_names;
std::string deployment_type;
config::ThreadGroupCfg thread_cfg;
system::protobufs::ThreadGroupCfg thread_cfg;
};
} // namespace log
+6 -7
View File
@@ -8,6 +8,7 @@
#include "spark/utils/Dispatcher.h"
#include "spark/utils/bts_assert.h"
#include "spark/utils/thread_manager.h"
#include "system/protobufs/threads.pb.h"
#ifdef SPARK_ENABLE_CCAPI
#include "spark/logging/ccapi_logger.h"
#endif
@@ -29,6 +30,7 @@ namespace bts {
namespace log {
using bts::config::SystemConfig;
using bts::system::protobufs::ThreadGroupCfg;
using Path = boost::filesystem::path;
static LoggerHub _default_logger_hub("logger");
@@ -86,11 +88,10 @@ void configure_logging(LoggingConfig const &cfg) {
_default_logger_hub.config(cfg);
}
void configure_logging(bts::config::Logging const &cfg) {
configure_logging(cfg, bts::config::ThreadGroupCfg());
configure_logging(cfg, ThreadGroupCfg());
}
void configure_logging(
bts::config::Logging const &cfg,
bts::config::ThreadGroupCfg const &thread_cfg) {
bts::config::Logging const &cfg, const ThreadGroupCfg &thread_cfg) {
LoggingConfig real_cfg;
real_cfg.deployment_type = config::get<config::DeploymentType>().type();
real_cfg.logger_initial_queue_size = cfg.initial_queue_size();
@@ -128,8 +129,7 @@ void configure_logging(
Path log_cache_file_path() { return log_path() / "log_control"; }
LoggingConfigPoller::LoggingConfigPoller(
bts::config::Logging const &cfg,
bts::config::ThreadGroupCfg const &thread_cfg)
bts::config::Logging const &cfg, const ThreadGroupCfg &thread_cfg)
: _poller(
utils::Dispatcher::background(), 1000, [=, this]() { poll_once(); }),
_last_ts(std::time(nullptr)), _log_cache_file(log_cache_file_path()),
@@ -143,8 +143,7 @@ LoggingConfigPoller::LoggingConfigPoller()
}
void LoggingConfigPoller::init(
bts::config::Logging const &cfg,
bts::config::ThreadGroupCfg const &thread_cfg) {
bts::config::Logging const &cfg, const ThreadGroupCfg &thread_cfg) {
auto parent = _log_cache_file.parent_path();
if (!boost::filesystem::is_directory(parent))
bts_assert(boost::filesystem::create_directories(parent));
+8 -4
View File
@@ -10,10 +10,13 @@
#include <boost/filesystem/path.hpp>
#include <thread>
namespace bts::system::protobufs {
class ThreadGroupCfg;
}
namespace bts {
namespace config {
class Logging;
class ThreadGroupCfg;
} // namespace config
// Please keep this list in sync with Log.cs
@@ -77,7 +80,8 @@ inline std::ostream &operator<<(std::ostream &s, CharPtrWrapper const &ptr) {
extern thread_local LoggerHandle _thread_logger;
void configure_logging(LoggingConfig const &cfg);
void configure_logging(
bts::config::Logging const &cfg, bts::config::ThreadGroupCfg const &);
bts::config::Logging const &cfg,
const system::protobufs::ThreadGroupCfg &thread_cfg);
void configure_logging(bts::config::Logging const &cfg);
void start_default_logging_thread();
@@ -86,13 +90,13 @@ class LoggingConfigPoller {
LoggingConfigPoller();
LoggingConfigPoller(
bts::config::Logging const &cfg,
bts::config::ThreadGroupCfg const &thread_cfg);
const system::protobufs::ThreadGroupCfg &thread_cfg);
~LoggingConfigPoller();
private:
void init(
bts::config::Logging const &cfg,
bts::config::ThreadGroupCfg const &thread_cfg);
const system::protobufs::ThreadGroupCfg &thread_cfg);
void poll_once();
utils::DispatcherTimer _poller;
std::time_t _last_ts;
@@ -134,7 +134,7 @@ void QBConnection::recv_msg_header() {
asio::async_read(
me->socket,
asio::buffer(&me->read_buffer_size, sizeof(me->read_buffer_size)),
[this, self](system::error_code error, std::size_t) {
[this, self](boost::system::error_code error, std::size_t) {
if (!error)
recv_msg_body();
else
@@ -148,7 +148,7 @@ void QBConnection::recv_msg_body() {
me->read_buffer.resize(me->read_buffer_size);
asio::async_read(
me->socket, asio::buffer(&me->read_buffer[0], me->read_buffer_size),
[this, self](system::error_code error, std::size_t) {
[this, self](boost::system::error_code error, std::size_t) {
if (!error) {
msgs::QBMsg m;
auto parse_ok = m.ParseFromArray(
@@ -448,7 +448,7 @@ void QBConnection::send(msgs::QBMsg const &msg) {
asio::async_write(
me->socket, to_send,
[this, self](system::error_code error, std::size_t) {
[this, self](boost::system::error_code error, std::size_t) {
if (!error) {
me->write_buffer.clear();
try_send_pending();
+9 -6
View File
@@ -1,8 +1,8 @@
#include "thread_manager.h"
#include "spark/config/config.h"
#include "spark/config/protos/threads.pb.h"
#include "spark/logging/logging.h"
#include "spark/utils/bts_assert.h"
#include "system/protobufs/threads.pb.h"
#include <boost/regex.hpp>
#include <fmt/format.h>
@@ -14,8 +14,9 @@
namespace bts {
namespace threading {
config::ThreadGroupCfg get_thread_cfg(std::string const &name) {
for (auto const &cfg : config::get<config::Threads>().thread_group()) {
system::protobufs::ThreadGroupCfg get_thread_cfg(std::string const &name) {
for (auto const &cfg :
config::get<system::protobufs::Threads>().thread_group()) {
boost::regex rx(cfg.name_rx());
if (boost::regex_match(name, rx))
return cfg;
@@ -44,7 +45,9 @@ std::vector<uint32_t> get_affinity(std::string const &name) {
#ifndef _WIN32
void configure_thread(
std::string const &name, config::ThreadGroupCfg const &cfg, pthread_t t) {
std::string const &name,
const system::protobufs::ThreadGroupCfg &cfg,
pthread_t t) {
if (cfg.core().size()) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
@@ -79,7 +82,7 @@ void configure_current_thread(std::string const &name) {
}
void configure_current_thread(
std::string const &name, config::ThreadGroupCfg const &cfg) {
std::string const &name, const system::protobufs::ThreadGroupCfg &cfg) {
#ifndef _WIN32
configure_thread(name, cfg, pthread_self());
#endif
@@ -102,7 +105,7 @@ std::thread make_thread_from_function(
std::thread make_thread_from_function(
std::string const &name,
std::function<void()> const &c,
config::ThreadGroupCfg const &cfg) {
const system::protobufs::ThreadGroupCfg &cfg) {
auto result = std::thread([c, name]() {
os::set_thread_name(name);
c();
+11 -7
View File
@@ -1,20 +1,22 @@
#pragma once
#include "spark/config/protos/threads.pb.h"
#include "system/protobufs/threads.pb.h"
#include <functional>
#include <string>
#include <thread>
namespace bts {
namespace config {
namespace bts::system::protobufs {
class ThreadGroupCfg;
}
namespace bts {
namespace threading {
std::thread make_thread_from_function(
std::string const &name,
std::function<void()> const &c,
config::ThreadGroupCfg const &cfg);
system::protobufs::ThreadGroupCfg const &cfg);
std::thread make_thread_from_function(
std::string const &name, std::function<void()> const &c);
@@ -25,15 +27,17 @@ std::thread make_thread(std::string const &name, C callable) {
template <class C>
std::thread make_thread(
std::string const &name, C callable, config::ThreadGroupCfg const &cfg) {
std::string const &name,
C callable,
system::protobufs::ThreadGroupCfg const &cfg) {
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, config::ThreadGroupCfg const &);
std::string const &name, system::protobufs::ThreadGroupCfg const &);
config::ThreadGroupCfg get_thread_cfg(std::string const &);
system::protobufs::ThreadGroupCfg get_thread_cfg(std::string const &);
std::vector<uint32_t> get_affinity(std::string const &name);
+53 -2
View File
@@ -1,13 +1,64 @@
cmake_minimum_required(VERSION 3.23)
project(system)
include(${CMAKE_SOURCE_DIR}/cmake/InitLibrary.cmake)
include(GNUInstallDirs)
include(${CMAKE_SOURCE_DIR}/cmake/InstallBasic.cmake)
init_library()
include(${CMAKE_SOURCE_DIR}/cmake/safe_dep_glob.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/ProtobufGenerateCpp.cmake)
# Sources
file(
GLOB_RECURSE ${PROJECT_NAME}_SOURCES
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
*.cpp
)
# Proto sources
file(
GLOB_RECURSE ${PROJECT_NAME}_PROTO_SOURCES
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
*.proto
)
update_deps_file("${${PROJECT_NAME}_PROTO_SOURCES};")
# Generate proto cpp
message(STATUS "Generating proto cpp")
protobuf_generate_cpp(
CXX_SOURCES ${PROJECT_NAME}_CXX_PROTO_SOURCES
CXX_HEADERS ${PROJECT_NAME}_CXX_PROTO_HEADERS
CXX_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}
PROTO_INCLUDE_DIRS
${EXTERNALS_INCLUDE}
${CMAKE_CURRENT_SOURCE_DIR}
PROTO_SOURCES
${${PROJECT_NAME}_PROTO_SOURCES}
)
add_library(${PROJECT_NAME} SHARED ${${PROJECT_NAME}_CXX_PROTO_SOURCES} ${${PROJECT_NAME}_SOURCES})
target_include_directories(
${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_link_directories(
${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_LIBDIR}>
)
target_link_libraries(
${PROJECT_NAME}
PUBLIC
debug fmtd
optimized fmt
boost_filesystem
protobuf
)
foreach(_hdr ${${PROJECT_NAME}_CXX_PROTO_HEADERS})
file(RELATIVE_PATH _relative_path ${CMAKE_CURRENT_BINARY_DIR} ${_hdr})
install(FILES ${_hdr} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${_relative_path})
endforeach()
install_basic()
@@ -1,6 +1,6 @@
syntax = "proto3";
package bts.config;
package bts.system.protobufs;
message ThreadGroupCfg {
string name_rx = 1;