diff --git a/CMakeLists.txt b/CMakeLists.txt index ea7a014f7..ceaf74ffe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,9 +40,8 @@ include_directories(BEFORE SYSTEM ${EXTERNALS_INCLUDE} ${EXTERNALS_ROOT}/include link_directories(${EXTERNALS_LIB}) add_compile_definitions(SHARED_MEMORY_PREFIX="${SHARED_MEMORY_PREFIX}") -add_compile_definitions(APP_DATA_DIR="${APP_DATA_DIR}") -if(DEFINED APP_DATA_DIR_OVERRIDE) - add_compile_definitions(APP_DATA_DIR_OVERRIDE="${APP_DATA_DIR_OVERRIDE}") +if(DEFINED APP_DATA_DIR) + add_compile_definitions(APP_DATA_DIR="${APP_DATA_DIR}") endif() add_compile_definitions(EXPORT_MD_CONSUMER="${EXPORT_MD_CONSUMER}") if(WIN32) diff --git a/CMakePresets.json b/CMakePresets.json index bbb15e819..3b06c43d9 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -20,8 +20,6 @@ "SPARK_CMAKE_CXX_FLAGS": "/MP /WX /wd4250 /wd4251 /wd4244 /wd4267 /wd4355 /wd4275 /wd4345 /bigobj /EHsc /DCURL_STATICLIB /D_WINSOCK_DEPRECATED_NO_WARNINGS /D__TBB_NO_IMPLICIT_LINKAGE /DBOOST_ALL_NO_LIB /std:c++20 /D_SCL_SECURE_NO_WARNINGS /D_CRT_SECURE_NO_WARNINGS /D_WIN32_WINNT=0x0600" }, "cacheVariables": { - "APP_DATA_DIR": "${dollar}{APPDATA}/bts", - "APP_DATA_DIR_OVERRIDE": "${dollar}{SPARK_DATA_DIR}", "CMAKE_CXX_FLAGS": "$env{SPARK_CMAKE_CXX_FLAGS}", "CMAKE_CXX_FLAGS_DEBUG": "$env{SPARK_CMAKE_CXX_FLAGS} /MTd", "CMAKE_CXX_FLAGS_RELEASE": "$env{SPARK_CMAKE_CXX_FLAGS} /MT", @@ -36,8 +34,7 @@ "EXPORT_MD_CONSUMER": { "type": "BOOL", "value": "ON" - }, - "SHARED_MEMORY_PREFIX": "spark" + } } }, @@ -53,8 +50,6 @@ "binaryDir": "${sourceDir}/build/${presetName}", "cacheVariables": { "platform": "", - "APP_DATA_DIR": "${dollar}{HOME}/sparkdata", - "APP_DATA_DIR_OVERRIDE": "${dollar}{SPARKD_DATA_DIR}", "CMAKE_BUILD_WITH_INSTALL_RPATH": { "type": "BOOL", "value": "OFF" @@ -77,8 +72,7 @@ "BUILD_ADMIN_WEB_APP": { "type": "BOOL", "value": "ON" - }, - "SHARED_MEMORY_PREFIX": "sparkd" + } } }, @@ -159,11 +153,7 @@ { "name": "spark-api-server-windows", "inherits": "spark", - "displayName": "Spark Api Server (Windows)", - "cacheVariables": { - "APP_DATA_DIR_OVERRIDE": "${dollar}{SPARK_API_SERVER_DATA_DIR}", - "SHARED_MEMORY_PREFIX": "spark_api_server" - } + "displayName": "Spark Api Server (Windows)" }, { @@ -172,7 +162,6 @@ "displayName": "Spark Api Server (Linux)", "generator": "Ninja Multi-Config", "cacheVariables": { - "APP_DATA_DIR_OVERRIDE": "${dollar}{SPARK_API_SERVER_DATA_DIR}", "BUILD_ADMIN_WEB_APP": { "type": "BOOL", "value": "OFF" @@ -191,7 +180,7 @@ "value": "ON" }, "CONFIG_DEFAULTS_PATH": "spark/config/profiles/default/*.conf", - "DOTNET_BINARY_DIR": "${sourceDir}/build/$/bin/net8.0", + "DOTNET_BINARY_DIR": "${sourceDir}/build/Spark.Api.Server/$/bin/net8.0", "EXPORT_MD_CONSUMER": { "type": "BOOL", "value": "ON" @@ -229,10 +218,8 @@ "description": "Data Stream utils ported over from Edge", "binaryDir": "${sourceDir}/build/${presetName}", "cacheVariables": { - "APP_DATA_DIR": "${dollar}{APPDATA}/bts/DataStream", - "APP_DATA_DIR_OVERRIDE": "${dollar}{DATA_STREAM_DATA_DIR}", "CONFIG_DEFAULTS_PATH": "spark/config/profiles/default/*.conf;spark/config/profiles/data_stream/*.conf", - "SHARED_MEMORY_PREFIX": "data_stream" + "DOTNET_BINARY_DIR": "${sourceDir}/build/$/bin/net8.0;${sourceDir}/build/$/bin/net8.0-windows" } }, @@ -248,8 +235,6 @@ "inherits": "sparkd", "generator": "Ninja Multi-Config", "cacheVariables": { - "APP_DATA_DIR": "${dollar}{HOME}/.local/share/DataStream", - "APP_DATA_DIR_OVERRIDE": "${dollar}{DATA_STREAM_DATA_DIR}", "BUILD_ADMIN_WEB_APP": { "type": "BOOL", "value": "OFF" @@ -271,8 +256,7 @@ "EXPORT_MD_CONSUMER": { "type": "BOOL", "value": "ON" - }, - "SHARED_MEMORY_PREFIX": "data_stream" + } } } ], diff --git a/src/lib_spark/spark/md/ShmSegmentName.cpp b/src/lib_spark/spark/md/ShmSegmentName.cpp new file mode 100644 index 000000000..e88f09dd1 --- /dev/null +++ b/src/lib_spark/spark/md/ShmSegmentName.cpp @@ -0,0 +1,29 @@ +#include "ShmSegmentName.h" +#include "spark/os/os.h" +#include "spark/utils/bts_assert.h" + +namespace bts::md { + +bool ShmSegmentName::prefix_initialized_ = false; +std::string ShmSegmentName::prefix_ = {}; +std::mutex ShmSegmentName::prefix_mx_ = {}; + +std::string ShmSegmentName::prefix() { + // Optimistically check if prefix has been initialized without using a lock + if (!prefix_initialized_) { + // It hasn't been initialized. Use a lock to initialize it + std::lock_guard lock{prefix_mx_}; + // Verify it hasn't been initialized again (since the initial check was done without a lock). + if (!prefix_initialized_) { + if (!os::try_expand_env(SHARED_MEMORY_PREFIX, prefix_)) { + prefix_ = os::process_path().filename().string(); + } + } + bts_assert(!prefix_.empty()); + prefix_initialized_ = true; + } + return prefix_ + "_" + + config::get().shared_memory_namespace(); +} + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/ShmSegmentName.h b/src/lib_spark/spark/md/ShmSegmentName.h index d15e57475..65a4af451 100644 --- a/src/lib_spark/spark/md/ShmSegmentName.h +++ b/src/lib_spark/spark/md/ShmSegmentName.h @@ -3,8 +3,12 @@ #include "spark/config/config.h" #include "spark/config/protos/market_data.pb.h" #include "spark/domain_model/Security.pb.h" +#include "spark/os/os.h" +#include "spark/md/protos/co2.pb.h" +#include "spark/md/shared_memory_prefix.h" #include +#include #include namespace bts::md { @@ -28,17 +32,16 @@ class ShmSegmentName { api::SecurityType type, api::Venue exchange, const std::string &suffix); + + static bool prefix_initialized_; + static std::string prefix_; + static std::mutex prefix_mx_; }; ////////////////////////////////////////////////// // Definition ////////////////////////////////////////////////// -inline std::string ShmSegmentName::prefix() { - return std::string{SHARED_MEMORY_PREFIX} + "_" + - config::get().shared_memory_namespace(); -} - inline std::string ShmSegmentName::prefix( const std::string &symbol, api::SecurityType type, api::Venue exchange) { return prefix() + "_" + symbol + "_" + api::SecurityType_Name(type) + "_" + diff --git a/src/lib_spark/spark/md/shared_memory_prefix.h b/src/lib_spark/spark/md/shared_memory_prefix.h new file mode 100644 index 000000000..5c8ffeb8d --- /dev/null +++ b/src/lib_spark/spark/md/shared_memory_prefix.h @@ -0,0 +1,5 @@ +#pragma once + +#ifndef SHARED_MEMORY_PREFIX +#define SHARED_MEMORY_PREFIX "${BTS_SHARED_MEMORY_PREFIX}" +#endif diff --git a/src/lib_spark/spark/os/app_data_dir.h b/src/lib_spark/spark/os/app_data_dir.h new file mode 100644 index 000000000..43d8dd047 --- /dev/null +++ b/src/lib_spark/spark/os/app_data_dir.h @@ -0,0 +1,9 @@ +#pragma once + +#ifndef APP_DATA_DIR +#ifdef _WIN32 +#define APP_DATA_DIR "${APPDATA}" +#else +#define APP_DATA_DIR "${HOME}/.local/share" +#endif +#endif diff --git a/src/lib_spark/spark/os/os.cpp b/src/lib_spark/spark/os/os.cpp index 130cdb14f..fda41b0da 100644 --- a/src/lib_spark/spark/os/os.cpp +++ b/src/lib_spark/spark/os/os.cpp @@ -1,6 +1,7 @@ #include "os.h" #include "spark/config/config.h" #include "spark/config/protos/system_config.pb.h" +#include "spark/os/app_data_dir.h" #include "spark/utils/bts_array_size.h" #include "spark/utils/bts_assert.h" #include "spark/utils/bts_debugger.h" @@ -65,6 +66,8 @@ namespace bts { namespace os { +using Path = boost::filesystem::path; + unsigned int process_id() { #ifdef _WIN32 return GetCurrentProcessId(); @@ -73,6 +76,24 @@ unsigned int process_id() { #endif } +Path process_path() { +#ifdef _WIN32 + char process_name[MAX_PATH]; + if (GetModuleFileName(NULL, process_name, MAX_PATH)) { + return Path(process_name); + } +#else + char process_name[PATH_MAX]; + ssize_t count = readlink("/proc/self/exe", process_name, PATH_MAX); + if (count != -1) { + // Null terminate the string + process_name[count] = '\0'; + return Path(process_name); + } +#endif + bts_assert_msg(false, "Failed to get process name"); +} + bool is_running(unsigned int pid) { #ifdef _WIN32 HANDLE handle = OpenProcess(SYNCHRONIZE, false, pid); @@ -677,23 +698,29 @@ bool expand_env_in_place(std::string &name, bool assert_vars_exist) { } std::optional try_expand_env(const std::string &name) { + std::string result; + if (try_expand_env(name, result)) { + return result; + } + return std::nullopt; +} + +bool try_expand_env(const std::string &name, std::string &result) { + result = name; static const std::regex env_re{R"--(\$\{([^}]+)\})--"}; std::smatch match; - std::string result{name}; while (std::regex_search(result, match, env_re)) { const auto from = match[0]; const auto var_name = match[1].str(); const auto var = std::getenv(var_name.c_str()); if (!var) { - return std::nullopt; + return false; } result.replace(from.first, from.second, var); } - return result; + return true; } -using Path = boost::filesystem::path; - Path app_data_path() { // Since the app data path is expanded from environment variables, we only // compute it once and save the result in a static singleton. This way, even @@ -709,27 +736,9 @@ Path app_data_path() { if (app_data_path_) { return *app_data_path_; } - // APP_DATA_DIR_OVERRIDE is the name of an environment variable which can be used - // to override the default app data dir (%APPDATA% on windows, $HOME on linux). - // The name itself is a variable because this library is built by multiple - // executables, which may run simultaneously on the same machine. For example, - // to run 'sparkd' and 'spark api server' on the same box with different data - // dirs, one might define the environment variables: - // SPARKD_DATA_DIR=~/sparkdata - // SPARK_API_SERVER_DATA_DIR=~/spark_api_data - // In this example, sparkd and the api server were built with (respectively): - // APP_DATA_DIR_OVERRIDE="${SPARKD_DATA_DIR}", - // APP_DATA_DIR_OVERRIDE="${SPARK_API_SERVER_DATA_DIR}", -#ifdef APP_DATA_DIR_OVERRIDE - const auto data_dir = try_expand_env(APP_DATA_DIR_OVERRIDE); - if (data_dir) { - app_data_path_ = Path{*data_dir}; - } -#endif - if (!app_data_path_) { - app_data_path_ = Path{expand_env(APP_DATA_DIR)}; - } - bts_assert(app_data_path_); + std::string base_path; + bts_assert(try_expand_env(APP_DATA_DIR, base_path)); + app_data_path_ = Path{base_path} / "bts" / process_path().stem(); app_data_path_ = app_data_path_->make_preferred(); return *app_data_path_; } diff --git a/src/lib_spark/spark/os/os.h b/src/lib_spark/spark/os/os.h index 4cfcab46f..bb6bbdbfb 100644 --- a/src/lib_spark/spark/os/os.h +++ b/src/lib_spark/spark/os/os.h @@ -33,6 +33,7 @@ inline bool is_windows() { } BTS_EXPORT char const *platform_name(); BTS_EXPORT unsigned int process_id(); +boost::filesystem::path process_path(); BTS_EXPORT bool is_running(unsigned int pid); struct ProcInfo { ProcInfo() : pid(0), exe(), args() {} @@ -92,6 +93,7 @@ boost::filesystem::path app_data_path( std::string expand_env(std::string name, bool assert_vars_exist = true); bool expand_env_in_place(std::string &name, bool assert_vars_exist = true); +bool try_expand_env(const std::string &name, std::string &result); std::string username(); std::string find_newest_file(