diff --git a/cmake/InitLibrary.cmake b/cmake/InitLibrary.cmake index 742ccaee8..2e4a890cc 100644 --- a/cmake/InitLibrary.cmake +++ b/cmake/InitLibrary.cmake @@ -1,6 +1,7 @@ include_guard(DIRECTORY) include(${CMAKE_SOURCE_DIR}/cmake/ExportToParent.cmake) include(${CMAKE_SOURCE_DIR}/cmake/InstallBasic.cmake) +include(${CMAKE_SOURCE_DIR}/cmake/ProtobufGenerateCpp.cmake) include(${CMAKE_SOURCE_DIR}/cmake/SetFirstDefined.cmake) include(${CMAKE_SOURCE_DIR}/cmake/safe_dep_glob.cmake) @@ -33,18 +34,39 @@ function(init_library) list(REMOVE_ITEM ${_name}_SOURCES ${item}) endforeach() + # Proto sources + file( + GLOB_RECURSE ${_name}_PROTO_SOURCES + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + ${_source_dir}/protobufs/*.proto + ) + update_deps_file("${${_name}_PROTO_SOURCES};") + protobuf_generate_cpp( + CXX_SOURCES ${_name}_CXX_PROTO_SOURCES + CXX_HEADERS ${_name}_CXX_PROTO_HEADERS + CXX_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR} + PROTO_INCLUDE_DIRS + ${EXTERNALS_INCLUDE} + ${CMAKE_CURRENT_SOURCE_DIR} + PROTO_SOURCES + ${${_name}_PROTO_SOURCES} + ) + export_to_parent( ${_name}_SOURCES ${_name}_TEST_SOURCES + ${_name}_PROTO_SOURCES + ${_name}_CXX_PROTO_SOURCES + ${_name}_CXX_PROTO_HEADERS ) # Add main lib if (_ARGS_STATIC) - add_library(${_name} STATIC ${${_name}_SOURCES}) + add_library(${_name} STATIC ${${_name}_SOURCES} ${${_name}_CXX_PROTO_SOURCES}) elseif(_ARGS_SHARED) - add_library(${_name} SHARED ${${_name}_SOURCES}) + add_library(${_name} SHARED ${${_name}_SOURCES} ${${_name}_CXX_PROTO_SOURCES}) else() - add_library(${_name} ${${_name}_SOURCES}) + add_library(${_name} ${${_name}_SOURCES} ${${_name}_CXX_PROTO_SOURCES}) endif() target_include_directories( ${_name} @@ -52,6 +74,14 @@ function(init_library) $ $ ) + # Include protos (if any) + if (${_name}_PROTO_SOURCES) + target_include_directories( + ${_name} + PUBLIC + $ + ) + endif() target_link_directories( ${_name} PUBLIC @@ -59,6 +89,10 @@ function(init_library) $ ) # Install main lib + foreach(_hdr ${${_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( NAME ${_name} INCLUDE_DIR ${_include_dir} diff --git a/src/lib_spark/spark/logging/LoggerHub.cpp b/src/lib_spark/spark/logging/LoggerHub.cpp index ba1b96330..9a05c3b80 100644 --- a/src/lib_spark/spark/logging/LoggerHub.cpp +++ b/src/lib_spark/spark/logging/LoggerHub.cpp @@ -4,7 +4,7 @@ #include "spark/utils/bts_assert.h" #include "spark/utils/bts_chrono.h" #include "spark/utils/thread_manager.h" -#include "system/os.h" +#include "system/protobufs/config/threads.pb.h" #include #include @@ -14,6 +14,8 @@ namespace bts { namespace log { +using bts::system::protobufs::ThreadGroupCfg; + std::atomic thread_idx; struct LoggerHub::SinkRec { @@ -38,7 +40,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) diff --git a/src/lib_spark/spark/logging/LoggingConfig.h b/src/lib_spark/spark/logging/LoggingConfig.h index e343427d9..39fcb3d66 100644 --- a/src/lib_spark/spark/logging/LoggingConfig.h +++ b/src/lib_spark/spark/logging/LoggingConfig.h @@ -2,7 +2,8 @@ #include "Common.h" #include "Sink.h" -#include "spark/config/protos/threads.pb.h" +#include "system/protobufs/config/threads.pb.h" + #include #include @@ -21,7 +22,7 @@ struct LoggingConfig { std::map level_names; std::map subsystem_names; std::string deployment_type; - config::ThreadGroupCfg thread_cfg; + system::protobufs::ThreadGroupCfg thread_cfg; }; } // namespace log diff --git a/src/lib_spark/spark/logging/logging.cpp b/src/lib_spark/spark/logging/logging.cpp index 0ec461a1b..fef296a2d 100644 --- a/src/lib_spark/spark/logging/logging.cpp +++ b/src/lib_spark/spark/logging/logging.cpp @@ -8,6 +8,7 @@ #include "spark/utils/bts_assert.h" #include "spark/utils/thread_manager.h" #include "system/os.h" +#include "system/protobufs/config/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().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)); diff --git a/src/lib_spark/spark/logging/logging.h b/src/lib_spark/spark/logging/logging.h index dfe84be4b..1fd7b4f6a 100644 --- a/src/lib_spark/spark/logging/logging.h +++ b/src/lib_spark/spark/logging/logging.h @@ -10,10 +10,13 @@ #include #include +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; diff --git a/src/lib_spark/spark/quickbus/network/QBConnection.cpp b/src/lib_spark/spark/quickbus/network/QBConnection.cpp index 682f0c12a..59fd25cd5 100644 --- a/src/lib_spark/spark/quickbus/network/QBConnection.cpp +++ b/src/lib_spark/spark/quickbus/network/QBConnection.cpp @@ -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(); diff --git a/src/lib_spark/spark/utils/thread_manager.cpp b/src/lib_spark/spark/utils/thread_manager.cpp index 5f96c060b..ff2ab6a2c 100644 --- a/src/lib_spark/spark/utils/thread_manager.cpp +++ b/src/lib_spark/spark/utils/thread_manager.cpp @@ -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/config/threads.pb.h" #include #include @@ -14,8 +14,9 @@ namespace bts { namespace threading { -config::ThreadGroupCfg get_thread_cfg(std::string const &name) { - for (auto const &cfg : config::get().thread_group()) { +system::protobufs::ThreadGroupCfg get_thread_cfg(std::string const &name) { + for (auto const &cfg : + config::get().thread_group()) { boost::regex rx(cfg.name_rx()); if (boost::regex_match(name, rx)) return cfg; @@ -44,7 +45,9 @@ std::vector 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 const &c, - config::ThreadGroupCfg const &cfg) { + const system::protobufs::ThreadGroupCfg &cfg) { auto result = std::thread([c, name]() { os::set_thread_name(name); c(); diff --git a/src/lib_spark/spark/utils/thread_manager.h b/src/lib_spark/spark/utils/thread_manager.h index cc40985c2..a3fa64030 100644 --- a/src/lib_spark/spark/utils/thread_manager.h +++ b/src/lib_spark/spark/utils/thread_manager.h @@ -1,20 +1,22 @@ #pragma once -#include "spark/config/protos/threads.pb.h" +#include "system/protobufs/config/threads.pb.h" + #include #include #include -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 const &c, - config::ThreadGroupCfg const &cfg); + system::protobufs::ThreadGroupCfg const &cfg); std::thread make_thread_from_function( std::string const &name, std::function const &c); @@ -25,15 +27,17 @@ std::thread make_thread(std::string const &name, C callable) { template 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(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 get_affinity(std::string const &name); diff --git a/src/lib_system/CMakeLists.txt b/src/lib_system/CMakeLists.txt index 0a34f00c3..a40894b07 100644 --- a/src/lib_system/CMakeLists.txt +++ b/src/lib_system/CMakeLists.txt @@ -10,4 +10,5 @@ target_link_libraries( debug fmtd optimized fmt boost_filesystem + protobuf ) diff --git a/src/lib_spark/spark/config/protos/threads.proto b/src/lib_system/system/protobufs/config/threads.proto similarity index 85% rename from src/lib_spark/spark/config/protos/threads.proto rename to src/lib_system/system/protobufs/config/threads.proto index 0f6926146..48a418b76 100644 --- a/src/lib_spark/spark/config/protos/threads.proto +++ b/src/lib_system/system/protobufs/config/threads.proto @@ -1,6 +1,6 @@ syntax = "proto3"; -package bts.config; +package bts.system.protobufs; message ThreadGroupCfg { string name_rx = 1;