From dd90bf183a8605dd616b06107986ced32d58bfc9 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 26 Sep 2024 19:13:03 +0000 Subject: [PATCH] refactor: Break shared mem md code up into smaller files For easier reading/navigation --- .../exchange_sim/ExchangeSimApplication.cpp | 3 +- .../sparkd/exchange_sim/IMDPlayerObserver.h | 2 +- .../md/platforms/emumba/EmMDListener.cpp | 2 +- .../sparkd/md/recording/MDRecorder.cpp | 7 +- src/client/spark_test/Co2/Co2Tests.cs | 5 +- src/data_files/documents/order_flow.txt | 8 +- src/lib_spark/spark/md/Diag/ShmDumper.cpp | 9 +- src/lib_spark/spark/md/Diag/ShmDumper.h | 4 +- .../spark/md/GeneralMarketDataListener.cpp | 4 +- .../spark/md/GeneralMarketDataListener.h | 2 +- src/lib_spark/spark/md/MDConsumerHandle.h | 9 +- src/lib_spark/spark/md/MDProducer.cpp | 1 - src/lib_spark/spark/md/MDProducer.h | 3 +- src/lib_spark/spark/md/MDProducerHandle.h | 9 +- src/lib_spark/spark/md/PriceSize.h | 73 +--- src/lib_spark/spark/md/SecurityDB.cpp | 2 +- src/lib_spark/spark/md/SecurityIndex.h | 1 - src/lib_spark/spark/md/SecurityMapper.h | 4 +- src/lib_spark/spark/md/Shm/MDStructure.h | 381 ------------------ src/lib_spark/spark/md/cme/MDP3Utils.h | 2 +- src/lib_spark/spark/md/coconut2.h | 2 +- src/lib_spark/spark/md/implied_price_level.h | 26 ++ src/lib_spark/spark/md/md_consumer_inst.h | 9 +- src/lib_spark/spark/md/price_level.h | 50 +++ src/lib_spark/spark/md/readme.md | 6 +- src/lib_spark/spark/md/shm/cold_entry.cpp | 32 ++ src/lib_spark/spark/md/shm/cold_entry.h | 43 ++ src/lib_spark/spark/md/shm/cold_header.cpp | 33 ++ src/lib_spark/spark/md/shm/cold_header.h | 77 ++++ .../fixed_security.cpp} | 3 +- .../FixedSecurity.h => shm/fixed_security.h} | 0 .../spark/md/shm/hot_depth_entry.cpp | 42 ++ src/lib_spark/spark/md/shm/hot_depth_entry.h | 102 +++++ src/lib_spark/spark/md/shm/hot_entry.h | 61 +++ src/lib_spark/spark/md/shm/hot_header.h | 40 ++ src/lib_spark/spark/md/shm/misc_data.h | 52 +++ .../spark/md/{Shm => shm}/readme.txt | 0 src/lib_spark/spark/md/shm/shm_version.h | 9 + .../md/test/synthetic_book_merge_tests.cpp | 1 + .../gateways/sim/SimExecutionGateway.cpp | 1 - .../gateways/sim/SimExecutionGateway.h | 3 +- .../spark/orderman/orderman_utils/Book.h | 1 + 42 files changed, 630 insertions(+), 494 deletions(-) delete mode 100644 src/lib_spark/spark/md/Shm/MDStructure.h create mode 100644 src/lib_spark/spark/md/implied_price_level.h create mode 100644 src/lib_spark/spark/md/price_level.h create mode 100644 src/lib_spark/spark/md/shm/cold_entry.cpp create mode 100644 src/lib_spark/spark/md/shm/cold_entry.h create mode 100644 src/lib_spark/spark/md/shm/cold_header.cpp create mode 100644 src/lib_spark/spark/md/shm/cold_header.h rename src/lib_spark/spark/md/{Shm/FixedSecurity.cpp => shm/fixed_security.cpp} (99%) rename src/lib_spark/spark/md/{Shm/FixedSecurity.h => shm/fixed_security.h} (100%) create mode 100644 src/lib_spark/spark/md/shm/hot_depth_entry.cpp create mode 100644 src/lib_spark/spark/md/shm/hot_depth_entry.h create mode 100644 src/lib_spark/spark/md/shm/hot_entry.h create mode 100644 src/lib_spark/spark/md/shm/hot_header.h create mode 100644 src/lib_spark/spark/md/shm/misc_data.h rename src/lib_spark/spark/md/{Shm => shm}/readme.txt (100%) create mode 100644 src/lib_spark/spark/md/shm/shm_version.h diff --git a/src/app_sparkd/sparkd/exchange_sim/ExchangeSimApplication.cpp b/src/app_sparkd/sparkd/exchange_sim/ExchangeSimApplication.cpp index 7f90ee549..3b3ad0bed 100644 --- a/src/app_sparkd/sparkd/exchange_sim/ExchangeSimApplication.cpp +++ b/src/app_sparkd/sparkd/exchange_sim/ExchangeSimApplication.cpp @@ -7,8 +7,7 @@ #include "spark/domain_model/Security.h" #include "spark/md/MBOProcessor.h" #include "spark/md/MDProducer.h" -#include "spark/md/Shm/FixedSecurity.h" -#include "spark/md/Shm/MDStructure.h" +#include "spark/md/shm/fixed_security.h" #include "spark/orderman/orderman_utils/fix_order_codec.h" #include "spark/persistence/SqliteStore.h" #include "spark/utils/Dispatcher.h" diff --git a/src/app_sparkd/sparkd/exchange_sim/IMDPlayerObserver.h b/src/app_sparkd/sparkd/exchange_sim/IMDPlayerObserver.h index c30db1421..5e4bc6923 100644 --- a/src/app_sparkd/sparkd/exchange_sim/IMDPlayerObserver.h +++ b/src/app_sparkd/sparkd/exchange_sim/IMDPlayerObserver.h @@ -1,7 +1,7 @@ #pragma once #include "spark/md/PriceSize.h" -#include "spark/md/Shm/MDStructure.h" +#include "spark/md/shm/hot_depth_entry.h" namespace bts { namespace api { diff --git a/src/app_sparkd/sparkd/md/platforms/emumba/EmMDListener.cpp b/src/app_sparkd/sparkd/md/platforms/emumba/EmMDListener.cpp index 1bf3c5436..2295eb2cf 100644 --- a/src/app_sparkd/sparkd/md/platforms/emumba/EmMDListener.cpp +++ b/src/app_sparkd/sparkd/md/platforms/emumba/EmMDListener.cpp @@ -6,9 +6,9 @@ #include "spark/md/IntPrice.h" #include "spark/md/MDProducer.h" #include "spark/md/PriceSize.h" -#include "spark/md/Shm/FixedSecurity.h" #include "spark/md/common.h" #include "spark/md/protos/co2.pb.h" +#include "spark/md/shm/fixed_security.h" #include "spark/orderman/gateways/sim/SimExecutionGateway.h" #include "spark/orderman/gateways/sim/SimExecutionGatewayFactory.h" #include "spark/persistence/IDGenerator.h" diff --git a/src/app_sparkd/sparkd/md/recording/MDRecorder.cpp b/src/app_sparkd/sparkd/md/recording/MDRecorder.cpp index 1291aa0c4..8e174781a 100644 --- a/src/app_sparkd/sparkd/md/recording/MDRecorder.cpp +++ b/src/app_sparkd/sparkd/md/recording/MDRecorder.cpp @@ -1,26 +1,21 @@ #include "MDRecorder.h" #include "spark/api/md/MarketDataConfig.pb.h" #include "spark/api/md/MarketDataConfigUtils.h" -#include "spark/api/services/MarketDataRelay.client.pb.h" -#include "spark/api/services/MarketDataRelay.pb.h" #include "spark/config/config.h" #include "spark/domain_model/Security.pb.h" #include "spark/md/MDClient.h" #include "spark/md/MDConsumer.h" #include "spark/md/MDConsumerHandle.h" #include "spark/md/PriceSize.h" -#include "spark/md/Shm/MDStructure.h" -#include "spark/md/common.h" #include "spark/md/protos/co2.pb.h" +#include "spark/md/shm/misc_data.h" #include "spark/utils/PBUtils.h" -#include "spark/utils/bts_assert.h" #include "spark/utils/datetime.h" #include "spark/utils/thread_manager.h" #include "system/logging/logging.h" #include "system/protobufs/config/system_config.pb.h" #include "system/time_stamp.h" -#include #include #include #include diff --git a/src/client/spark_test/Co2/Co2Tests.cs b/src/client/spark_test/Co2/Co2Tests.cs index bcb598da4..30e44b43c 100644 --- a/src/client/spark_test/Co2/Co2Tests.cs +++ b/src/client/spark_test/Co2/Co2Tests.cs @@ -117,7 +117,10 @@ namespace mbts.co2.tests var entry = new MDConsumerHandle.ColdEntry(); ulong entry_size; Co2NativeTestMethods.co2_get_sample_cold(out entry_size, ref entry); - Assert.AreEqual(entry_size, (ulong)Marshal.SizeOf(entry), "If this test fails it's probably due to an addition to the FixedSecurity struct in FixedSecurity.h"); + Assert.AreEqual( + entry_size, + (ulong)Marshal.SizeOf(entry), + "If this test fails it's probably due to an addition to the FixedSecurity struct in fixed_security.h"); // Last trade (var time, var last) = entry.last_trade.load(); diff --git a/src/data_files/documents/order_flow.txt b/src/data_files/documents/order_flow.txt index fe5ec4ba2..9a65ca599 100644 --- a/src/data_files/documents/order_flow.txt +++ b/src/data_files/documents/order_flow.txt @@ -127,8 +127,8 @@ adding security type modified: src/lib/domain_model/Security.proto modified: src/lib/md/MDConsumer.cpp modified: src/lib/md/SecurityMapper.cpp - modified: src/lib/md/Shm/FixedSecurity.cpp - modified: src/lib/md/Shm/FixedSecurity.h + modified: src/lib/md/shm/fixed_security.cpp + modified: src/lib/md/shm/fixed_security.h modified: src/lib/md/ccApi/CCMain.cpp modified: src/lib/md/ccApi/CCMarketDataListener.cpp modified: src/lib/md/relay/RelayClient.cpp @@ -189,8 +189,8 @@ adding security type modified: src/lib/domain_model/Security.proto modified: src/lib/md/MDConsumer.cpp modified: src/lib/md/SecurityMapper.cpp - modified: src/lib/md/Shm/FixedSecurity.cpp - modified: src/lib/md/Shm/FixedSecurity.h + modified: src/lib/md/shm/fixed_security.cpp + modified: src/lib/md/shm/fixed_security.h modified: src/lib/md/ccApi/CCMain.cpp modified: src/lib/md/ccApi/CCMarketDataListener.cpp modified: src/lib/md/relay/RelayClient.cpp diff --git a/src/lib_spark/spark/md/Diag/ShmDumper.cpp b/src/lib_spark/spark/md/Diag/ShmDumper.cpp index cc0dd4349..0d7f23d37 100644 --- a/src/lib_spark/spark/md/Diag/ShmDumper.cpp +++ b/src/lib_spark/spark/md/Diag/ShmDumper.cpp @@ -1,13 +1,20 @@ #ifndef NO_CPP0X #include "ShmDumper.h" -#include "spark/md/Shm/MDStructure.h" +#include "spark/md/PriceSize.h" +#include "spark/md/shm/cold_entry.h" +#include "spark/md/shm/cold_header.h" +#include "spark/md/shm/hot_depth_entry.h" +#include "spark/md/shm/hot_entry.h" +#include "spark/md/shm/hot_header.h" #include "spark/utils/SharedList.h" +#include "spark/utils/TimeStamp.h" #include "system/json/json_dumper.h" #include "system/logging/logging.h" #include #include +#include namespace bts { namespace md { diff --git a/src/lib_spark/spark/md/Diag/ShmDumper.h b/src/lib_spark/spark/md/Diag/ShmDumper.h index d22dfe266..76c6c1a98 100644 --- a/src/lib_spark/spark/md/Diag/ShmDumper.h +++ b/src/lib_spark/spark/md/Diag/ShmDumper.h @@ -1,7 +1,9 @@ #pragma once -#include "spark/md/Shm/FixedSecurity.h" + +#include "spark/md/shm/fixed_security.h" #include "spark/utils/bts_export.h" #include "spark/utils/bts_no_copy.h" + #include namespace bts { diff --git a/src/lib_spark/spark/md/GeneralMarketDataListener.cpp b/src/lib_spark/spark/md/GeneralMarketDataListener.cpp index 6fdb6390f..9e6f65874 100644 --- a/src/lib_spark/spark/md/GeneralMarketDataListener.cpp +++ b/src/lib_spark/spark/md/GeneralMarketDataListener.cpp @@ -25,9 +25,9 @@ #include "spark/md/PriceSize.h" #include "spark/md/SecurityIDRecorder.h" #include "spark/md/SecurityIndex.h" -#include "spark/md/Shm/FixedSecurity.h" -#include "spark/md/Shm/MDStructure.h" +#include "spark/md/shm/fixed_security.h" #include "spark/md/protos/co2.pb.h" +#include "spark/md/shm/hot_depth_entry.h" #include "spark/orderman/actors/SODPosition.h" #include "spark/orderman/gateways/sim/SimExecutionGateway.h" #include "spark/orderman/gateways/sim/SimExecutionGatewayFactory.h" diff --git a/src/lib_spark/spark/md/GeneralMarketDataListener.h b/src/lib_spark/spark/md/GeneralMarketDataListener.h index cfcc9bcd0..d5e6dcb3c 100644 --- a/src/lib_spark/spark/md/GeneralMarketDataListener.h +++ b/src/lib_spark/spark/md/GeneralMarketDataListener.h @@ -4,7 +4,7 @@ #include "spark/domain_model/Security.pb.h" #include "spark/md/GeneralSecurityIndex.h" #include "spark/md/IMarketDataListener.h" -#include "spark/md/Shm/FixedSecurity.h" +#include "spark/md/shm/fixed_security.h" #include "spark/persistence/IDGenerator.h" namespace bts::md { diff --git a/src/lib_spark/spark/md/MDConsumerHandle.h b/src/lib_spark/spark/md/MDConsumerHandle.h index 301422a64..cf05e160c 100644 --- a/src/lib_spark/spark/md/MDConsumerHandle.h +++ b/src/lib_spark/spark/md/MDConsumerHandle.h @@ -2,11 +2,14 @@ #include "UserSymbol.h" #include "domain/protobufs/trading_status.pb.h" -#include "spark/md/Shm/MDStructure.h" +#include "spark/md/shm/cold_entry.h" +#include "spark/md/shm/cold_header.h" +#include "spark/md/shm/hot_depth_entry.h" +#include "spark/md/shm/hot_entry.h" #include "spark/sparkproto/SparkProtos.pb.h" -#include "spark/utils/bts_export.h" -#include "spark/utils/bts_no_copy.h" +#include "spark/utils/TimeStamp.h" #include "spark/utils/sane_algorithm.h" + #include #include diff --git a/src/lib_spark/spark/md/MDProducer.cpp b/src/lib_spark/spark/md/MDProducer.cpp index ee55f4a7e..05bcfaec7 100644 --- a/src/lib_spark/spark/md/MDProducer.cpp +++ b/src/lib_spark/spark/md/MDProducer.cpp @@ -2,7 +2,6 @@ #include "MDProducer.h" #include "MemorySegmentUtils.h" #include "SecurityIDRecorder.h" -#include "Shm/MDStructure.h" #include "spark/domain_model/Security.pb.h" #include "spark/md/Diag/ShmDumper.h" #include "spark/md/ShmSegmentName.h" diff --git a/src/lib_spark/spark/md/MDProducer.h b/src/lib_spark/spark/md/MDProducer.h index 721586587..e7e2b5bd6 100644 --- a/src/lib_spark/spark/md/MDProducer.h +++ b/src/lib_spark/spark/md/MDProducer.h @@ -6,8 +6,9 @@ #include "MDProducerChannel.h" #include "MDProducerHandle.h" #include "SecurityMapper.h" -#include "Shm/FixedSecurity.h" #include "common.h" +#include "spark/md/shm/fixed_security.h" + #include #include #include diff --git a/src/lib_spark/spark/md/MDProducerHandle.h b/src/lib_spark/spark/md/MDProducerHandle.h index 0ea9b0bb9..5710bc92b 100644 --- a/src/lib_spark/spark/md/MDProducerHandle.h +++ b/src/lib_spark/spark/md/MDProducerHandle.h @@ -1,14 +1,15 @@ #pragma once #include "MDProducerChannel.h" -#include "Shm/MDStructure.h" #include "domain/protobufs/aggressor_side.pb.h" -#include "spark/utils/bts_export.h" -#include "spark/utils/bts_no_copy.h" +#include "spark/md/shm/cold_entry.h" +#include "spark/md/shm/cold_header.h" +#include "spark/md/shm/hot_depth_entry.h" +#include "spark/md/shm/hot_entry.h" +#include "spark/md/shm/hot_header.h" #include "spark/utils/datetime.h" #include -#include namespace bts { namespace co2 { diff --git a/src/lib_spark/spark/md/PriceSize.h b/src/lib_spark/spark/md/PriceSize.h index 56447990c..b16063ea5 100644 --- a/src/lib_spark/spark/md/PriceSize.h +++ b/src/lib_spark/spark/md/PriceSize.h @@ -1,18 +1,11 @@ #pragma once -#include "IntPrice.h" -#include "SizeTypes.h" -#include "common.h" - +#include "spark/md/IntPrice.h" +#include "spark/md/SizeTypes.h" +#include "spark/md/protos/co2.pb.h" #include "spark/utils/Optimistic.h" -#include "spark/utils/bts_assert.h" -#include "spark/utils/bts_no_copy.h" -#include "spark/utils/warn_ctrl.h" #include "system/os.h" -#include -#include -#include #include namespace bts { @@ -95,66 +88,6 @@ typedef utils::Optimistic AtomicPriceSize; void to_json(nlohmann::json &j, const PriceSize &ps); -// -// PriceSize has grown to 12 bytes but because of 8 byte alignment it will pad -// out to 16 bytes This structure can store the same data in 24 bytes so it's -// more efficient for shared memory -struct PriceLevel { - PriceLevel() : _bid(), _ask(), _bid_size(0), _ask_size(0) {} - PriceLevel(const PriceSize &bid, const PriceSize &ask) - : _bid(bid.int_price()), _ask(ask.int_price()), _bid_size(bid.size()), - _ask_size(ask.size()) {} - - IntPrice _bid; - IntPrice _ask; - Size _bid_size; - Size _ask_size; - - const IntPrice &bid_price() const { return _bid; } - const IntPrice &ask_price() const { return _ask; } - Size bid_size() const { return _bid_size; } - Size ask_size() const { return _ask_size; } - - bool operator==(const PriceLevel &other) const { - return _ask_size == other._ask_size && _bid_size == other._bid_size && - _bid == other._bid && _ask == other._ask; - } - bool operator!=(const PriceLevel &other) const { - return _ask_size != other._ask_size || _bid_size != other._bid_size || - _bid != other._bid || _ask != other._ask; - } - - bool update(const PriceLevel &temp) { - if (*this != temp) { - *this = temp; - return true; - } - return false; - } - - PriceSize bid() const { return PriceSize(_bid, _bid_size); } - PriceSize ask() const { return PriceSize(_ask, _ask_size); } -}; -static_assert(sizeof(PriceLevel) == 24, "PriceLevel should be small"); - -struct ImpliedPriceLevel : private PriceLevel { - ImpliedPriceLevel() : PriceLevel() {} - ImpliedPriceLevel(const PriceSize &bid, const PriceSize &ask) - : PriceLevel(bid, ask) {} - using PriceLevel::ask_price; - using PriceLevel::ask_size; - using PriceLevel::bid_price; - using PriceLevel::bid_size; - using PriceLevel::operator==; - using PriceLevel::operator!=; - bool operator=(const PriceLevel &other) { return update(other); } - PriceSize bid() const { return PriceSize(_bid, 0, ImpliedSize(_bid_size)); } - PriceSize ask() const { return PriceSize(_ask, 0, ImpliedSize(_ask_size)); } -}; -static_assert( - sizeof(ImpliedPriceLevel) == sizeof(PriceLevel), - "ImpliedPriceLevel should not be bigger than PriceLevel"); - ////////////////////////////////////////////////// // Definition ////////////////////////////////////////////////// diff --git a/src/lib_spark/spark/md/SecurityDB.cpp b/src/lib_spark/spark/md/SecurityDB.cpp index b0f71154a..476ca4f82 100644 --- a/src/lib_spark/spark/md/SecurityDB.cpp +++ b/src/lib_spark/spark/md/SecurityDB.cpp @@ -8,7 +8,7 @@ #include "spark/config/config.h" #include "spark/config/protos/market_data.pb.h" #include "spark/domain_model/Security.h" -#include "spark/md/Shm/FixedSecurity.h" +#include "spark/md/shm/fixed_security.h" #include "system/protobufs/config/system_config.pb.h" #include diff --git a/src/lib_spark/spark/md/SecurityIndex.h b/src/lib_spark/spark/md/SecurityIndex.h index bbdb4d6be..5fcf1aac0 100644 --- a/src/lib_spark/spark/md/SecurityIndex.h +++ b/src/lib_spark/spark/md/SecurityIndex.h @@ -2,7 +2,6 @@ #include "MDProducerHandle.h" #include "common.h" -#include "spark/md/Shm/MDStructure.h" #include "spark/utils/bts_no_copy.h" #include "system/logging/logging.h" diff --git a/src/lib_spark/spark/md/SecurityMapper.h b/src/lib_spark/spark/md/SecurityMapper.h index b15351b7f..b9fcc7a82 100644 --- a/src/lib_spark/spark/md/SecurityMapper.h +++ b/src/lib_spark/spark/md/SecurityMapper.h @@ -1,5 +1,7 @@ #pragma once -#include "Shm/FixedSecurity.h" + +#include "shm/fixed_security.h" + #include #include diff --git a/src/lib_spark/spark/md/Shm/MDStructure.h b/src/lib_spark/spark/md/Shm/MDStructure.h deleted file mode 100644 index 31bfdd1e5..000000000 --- a/src/lib_spark/spark/md/Shm/MDStructure.h +++ /dev/null @@ -1,381 +0,0 @@ -#pragma once - -#include "FixedSecurity.h" -#include "domain/protobufs/trading_status.pb.h" -#include "spark/config/config.h" -#include "spark/config/protos/market_data.pb.h" -#include "spark/md/PriceSize.h" -#include "spark/md/common.h" -#include "spark/md/protos/co2.pb.h" -#include "spark/utils/TimeStamp.h" -#include "system/bool_name.h" -#include "system/time_stamp.h" - -#include -#include -#include -#include - -/* - * Coconut 2.0 separates data into high (hot) and low (cold) priority based on - * how fast the clients need to access the data Any data critical for the - * quoter/eyes goes in the Hot structure and is polled very fast. This data - * should be kept as small as possible and should be cache-line aligned Any - * other data goes in the Cold list and will be accessed more slowly - */ - -namespace bts { -namespace md { - -// -// HOT Data -/////////////////////// - -const uint32_t shm_version = 107; - -struct ALIGNED_(CACHE_LINE) HotHeader { - size_t count; - size_t segment_count; - // Just the counters. Publisher process can compute messages/second by - // polling these counters - size_t direct_count; - size_t implied_count; -}; - -// These should not be cache line aligned because they are small enough for -// multiple to fit on one line easily -class HotEntry { - std::atomic timestamp; // 8 - public: - HotEntry() - : timestamp(utils::TimeStamp(0)), book(), key(invalid_security_key), - sequence(0), status(co2::COCONUTSECURITYSTATUS_OK) {} - uint32_t size() const { return sizeof(HotEntry); } - PriceLevel book; // 24 - security_key key; // 4 - uint32_t sequence; // 4 - IntPrice settlement; // 8 - uint8_t status; // 1 - void set_time(const utils::TimeStamp &t) { - timestamp.store(t, std::memory_order_release); - } - utils::TimeStamp time() const { - return timestamp.load(std::memory_order_acquire); - } -}; -static_assert(sizeof(HotEntry) <= CACHE_LINE, "HotEntry should be small"); - -void to_json(nlohmann::json &j, const HotHeader *h); -void to_json(nlohmann::json &j, const HotHeader &h); -void to_json(nlohmann::json &j, const HotEntry *h); -void to_json(nlohmann::json &j, const HotEntry &h); - -const uint32_t COCONUT_MAX_DEPTH = 10; -const uint32_t COCONUT_MAX_IMPLIED_DEPTH = 2; -class HotDepthEntry { - std::atomic timestamp; // 8 - public: - HotDepthEntry() - : timestamp(utils::TimeStamp(0)), key(invalid_security_key), sequence(0), - direct(), implied(), status(co2::COCONUTSECURITYSTATUS_OK) {} - uint32_t size() const { return sizeof(HotDepthEntry); } - security_key - key; // 4 NOTE: key is at a different offset than it is in HotEntry - uint32_t sequence; // 4 - IntPrice settlement; // 8 - std::array direct; // 240 - std::array implied; // 48 - uint8_t status; // 1 - - void read_direct(uint32_t depth, PriceSize *bid, PriceSize *ask) const { - *bid = direct[depth].bid(); - *ask = direct[depth].ask(); - } - void read_implied(uint32_t depth, PriceSize *bid, PriceSize *ask) const { - *bid = implied[depth].bid(); - *ask = implied[depth].ask(); - } - bool write_direct( - uint32_t depth, const PriceSize &bid, const PriceSize &ask) { - bts_assert_d(depth < direct.size()); - auto temp = PriceLevel(bid, ask); - if (direct[depth] != temp) { - direct[depth] = temp; - return true; - } - return false; - } - bool write_implied( - uint32_t depth, const PriceSize &bid, const PriceSize &ask) { - bts_assert_d(depth < implied.size()); - auto temp = PriceLevel(bid, ask); - if (implied[depth] != temp) { - implied[depth] = temp; - return true; - } - return false; - } - - void set_time(const utils::TimeStamp &t) { - timestamp.store(t, std::memory_order_release); - } - utils::TimeStamp time() const { - return timestamp.load(std::memory_order_acquire); - } -}; -static_assert(sizeof(HotDepthEntry) <= 320, "HotDepthEntry should be small"); - -void to_json(nlohmann::json &j, const HotDepthEntry *h); -void to_json(nlohmann::json &j, const HotDepthEntry &h); - -// -// COLD Data, brrr -/////////////////////// - -struct ALIGNED_(CACHE_LINE) ColdHeader { - ColdHeader() - : version(shm_version), count(0), segment_count(0), - create_time(utils::TimeStamp::now()), heartbeat(), - disable_heartbeat( - config::get().disable_heartbeat()), - heartbeat_ms(config::get().heartbeat_ms()), - heartbeat_boot_seconds( - config::get().heartbeat_boot_seconds()), - security_count(0), has_depth(false), trade_count(0), - coconut_status_count(0), exchange_status_count(0), rfq_count(0) {} - - uint32_t version; - size_t count; - size_t segment_count; - utils::TimeStamp create_time; - utils::TimeStamp heartbeat; - bool disable_heartbeat; - uint32_t heartbeat_ms; - uint32_t heartbeat_boot_seconds; - - uint32_t security_count; // incremented when a security is made public - bool has_depth; - - std::atomic trade_count; - std::atomic coconut_status_count; - std::atomic exchange_status_count; - std::atomic rfq_count; - std::atomic gap_count; - - // Ring buffer for LastTrade for all securities in this list - uint8_t trade_buffer[1024 * 1024 * 4]; // 4mb - // Ring buffer for RFQ - // static const int rfq_buffer_entry_size = 64; - typedef std::array RFQBuffer; - uint8_t rfq_buffer[1024 * 256]; // 256k -}; - -struct MiscData { - MiscData() - : misc_timestamp(), settlement_date(), - previous_settlement_price(INT_NAN), previous_settlement_date(), - open_price(NO_PRICE), open_date(), close_price(NO_PRICE), close_date(), - session_high(NO_PRICE), session_low(NO_PRICE), - session_high_bid(NO_PRICE), session_low_offer(NO_PRICE), - fixing_price(NO_PRICE) {} - utils::TimeStamp misc_timestamp; - // Settlement - datetime::Date settlement_date; - IntPrice previous_settlement_price; - datetime::Date previous_settlement_date; - - // Opening/Closing - Price open_price; - datetime::Date open_date; - Price close_price; - datetime::Date close_date; - - // Misc stats for current session - Price session_high; - Price session_low; - Price session_high_bid; - Price session_low_offer; - Price fixing_price; - - bool operator==(const MiscData &other) const { - return settlement_date == other.settlement_date && - previous_settlement_price == other.previous_settlement_price && - previous_settlement_date == other.previous_settlement_date && - open_price == other.open_price && open_date == other.open_date && - close_price == other.close_price && - close_date == other.close_date && - session_high == other.session_high && - session_low == other.session_low && - session_high_bid == other.session_high_bid && - session_low_offer == other.session_low_offer && - fixing_price == other.fixing_price; - } - bool operator!=(const MiscData &other) const { return !(*this == other); } -}; - -struct ColdEntry { - ColdEntry(size_t security_size) - : exchange_status(domain::protobufs::TRADINGSTATUS_UNKNOWN), - last_trade(), total_volume(0), last_trade_time(), misc(), - from_spread(0), deleted(0) { - this->security.size = security_size; - } - - static size_t fixed_size() { - return sizeof(ColdEntry) - sizeof(FixedSecurity); - } - size_t size() const { return fixed_size() + this->security.size; } - - domain::protobufs::TradingStatus exchange_status; - - // Last Trade - AtomicPriceSize last_trade; - size_t total_volume; - datetime::LLDateTime last_trade_time; - - MiscData misc; - - int from_spread; - int deleted; - FixedSecurity security; -}; - -void to_json(nlohmann::json &j, const ColdHeader *c); -void to_json(nlohmann::json &j, const ColdHeader &c); - -void to_json(nlohmann::json &j, const ColdEntry *c); -void to_json(nlohmann::json &j, const ColdEntry &c); - -////////////////////////////////////////////////// -// Definition -////////////////////////////////////////////////// - -inline void to_json(nlohmann::json &j, const HotHeader *h) { to_json(j, *h); } - -inline void to_json(nlohmann::json &j, const HotHeader &h) { - j = { - {"count", h.count}, - {"segment_count", h.segment_count}, - {"direct_count", h.direct_count}, - {"implied_count", h.implied_count}, - }; -} - -inline void to_json(nlohmann::json &j, const HotEntry *h) { to_json(j, *h); } - -inline void to_json(nlohmann::json &j, const HotEntry &h) { - const core::TimeStamp timestamp{h.time().to_uint64()}; - j = { - {"timestamp", timestamp.to_iso_string()}, - {"key", h.key}, - {"bid", h.book.bid()}, - {"ask", h.book.ask()}, - {"status", - co2::CoconutSecurityStatus_Name((CoconutSecurityStatus)h.status)}, - {"seq", h.sequence}, - }; -} - -inline void to_json(nlohmann::json &j, const HotDepthEntry *h) { - to_json(j, *h); -} - -inline void to_json(nlohmann::json &j, const HotDepthEntry &h) { - const core::TimeStamp timestamp{h.time().to_uint64()}; - j = { - {"timestamp", timestamp.to_iso_string()}, - {"key", h.key}, - {"settle", h.settlement.price()}, - {"status", - co2::CoconutSecurityStatus_Name((co2::CoconutSecurityStatus)h.status)}, - {"seq", h.sequence}, - }; - std::vector bids; - std::vector asks; - for (const auto &l : h.direct) { - if (!l.bid().is_nan()) { - bids.push_back(l.bid()); - } - if (!l.ask().is_nan()) { - asks.push_back(l.ask()); - } - } - j["bids"] = bids; - j["asks"] = asks; - std::vector ibids; - std::vector iasks; - for (const auto &l : h.implied) { - if (!l.bid().is_nan()) { - ibids.push_back(l.bid()); - } - if (!l.ask().is_nan()) { - iasks.push_back(l.ask()); - } - } - j["ibids"] = ibids; - j["iasks"] = iasks; -} - -inline void to_json(nlohmann::json &j, const ColdHeader *c) { to_json(j, *c); } - -inline void to_json(nlohmann::json &j, const ColdHeader &c) { - j = { - {"version", c.version}, - {"count", c.count}, - {"segment_count", c.segment_count}, - {"create_time", - core::TimeStamp{c.create_time.to_uint64()}.to_iso_string()}, - {"hb", core::TimeStamp{c.heartbeat.to_uint64()}.to_iso_string()}, - {"disable_hb", core::BoolName::yes_no(c.disable_heartbeat)}, - {"hb_ms", c.heartbeat_ms}, - {"hb_boot_secs", c.heartbeat_boot_seconds}, - {"sec_count", c.security_count}, - {"has_depth", core::BoolName::yes_no(c.has_depth)}, - {"trade_count", static_cast(c.trade_count)}, - {"coconut_status_count", - static_cast(c.coconut_status_count)}, - {"exchange_status_count", - static_cast(c.exchange_status_count)}, - {"rfq_count", static_cast(c.rfq_count)}, - {"gap_count", static_cast(c.gap_count)}, - }; -} - -inline void to_json(nlohmann::json &j, const ColdEntry *c) { to_json(j, *c); } - -inline void to_json(nlohmann::json &j, const ColdEntry &c) { - j = {{"security", c.security}}; - const auto last_trade = std::get(c.last_trade.load()); - j["last_trade_price"] = last_trade.size() > 0 ? last_trade.price() : 0; - j["last_trade_qty"] = last_trade.size(); - j["last_trade_time"] = c.last_trade_time; - j["volume"] = c.total_volume; - core::TimeStamp settle_date; - core::TimeStamp::from_ymd( - c.misc.settlement_date.year, c.misc.settlement_date.month, - c.misc.settlement_date.day, &settle_date); - j["settle_date"] = settle_date.to_iso_string(); - j["prev_settle_price"] = c.misc.previous_settlement_price.price(); - core::TimeStamp prev_settle_date; - core::TimeStamp::from_ymd( - c.misc.previous_settlement_date.year, - c.misc.previous_settlement_date.month, - c.misc.previous_settlement_date.day, &prev_settle_date); - j["prev_settle_date"] = prev_settle_date.to_iso_string(); - j["status"] = domain::protobufs::TradingStatus_Name(c.exchange_status); - j["deleted"] = core::BoolName::yes_no(c.deleted); -} - -inline bool is_heart_beat_ok(const ColdHeader &header) { - uint32_t s_heartbeat_ms = header.heartbeat_ms; - bool s_disable_heartbeat = header.disable_heartbeat; - uint32_t s_grace_period_ms = header.heartbeat_boot_seconds * 1000; - TimeStamp hb = header.heartbeat; - TimeStamp now = TimeStamp::now(); - return (header.version == shm_version) && - (s_disable_heartbeat || (now - hb).milliseconds() < s_heartbeat_ms || - (now - header.create_time).milliseconds() < s_grace_period_ms); -} - -} // namespace md -} // namespace bts diff --git a/src/lib_spark/spark/md/cme/MDP3Utils.h b/src/lib_spark/spark/md/cme/MDP3Utils.h index d60817668..c329cc82d 100644 --- a/src/lib_spark/spark/md/cme/MDP3Utils.h +++ b/src/lib_spark/spark/md/cme/MDP3Utils.h @@ -1,7 +1,7 @@ #pragma once -#include "spark/md/Shm/FixedSecurity.h" #include "spark/md/cme/CMEChannel.h" +#include "spark/md/shm/fixed_security.h" #include diff --git a/src/lib_spark/spark/md/coconut2.h b/src/lib_spark/spark/md/coconut2.h index d238069fd..2c6e2f4cb 100644 --- a/src/lib_spark/spark/md/coconut2.h +++ b/src/lib_spark/spark/md/coconut2.h @@ -4,8 +4,8 @@ #include "SecurityIDRecorder.h" #include "SecurityIndex.h" #include "SettlementRecorder.h" -#include "Shm/FixedSecurity.h" #include "SymbolLoader.h" #include "common.h" +#include "spark/md/shm/fixed_security.h" // TODO: separate coconut2.h into consumer and producer sides diff --git a/src/lib_spark/spark/md/implied_price_level.h b/src/lib_spark/spark/md/implied_price_level.h new file mode 100644 index 000000000..ffd9924f3 --- /dev/null +++ b/src/lib_spark/spark/md/implied_price_level.h @@ -0,0 +1,26 @@ +#pragma once + +#include "spark/md/price_level.h" + +namespace bts::md { + +struct ImpliedPriceLevel : private PriceLevel { + ImpliedPriceLevel() : PriceLevel() {} + ImpliedPriceLevel(const PriceSize &bid, const PriceSize &ask) + : PriceLevel(bid, ask) {} + using PriceLevel::ask_price; + using PriceLevel::ask_size; + using PriceLevel::bid_price; + using PriceLevel::bid_size; + using PriceLevel::operator==; + using PriceLevel::operator!=; + bool operator=(const PriceLevel &other) { return update(other); } + PriceSize bid() const { return PriceSize(_bid, 0, ImpliedSize(_bid_size)); } + PriceSize ask() const { return PriceSize(_ask, 0, ImpliedSize(_ask_size)); } +}; + +static_assert( + sizeof(ImpliedPriceLevel) == sizeof(PriceLevel), + "ImpliedPriceLevel should not be bigger than PriceLevel"); + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/md_consumer_inst.h b/src/lib_spark/spark/md/md_consumer_inst.h index 32c999e40..b570c1567 100644 --- a/src/lib_spark/spark/md/md_consumer_inst.h +++ b/src/lib_spark/spark/md/md_consumer_inst.h @@ -2,11 +2,16 @@ #include "spark/domain_model/Security.pb.h" #include "spark/md/MemorySegmentUtils.h" -#include "spark/md/Shm/FixedSecurity.h" -#include "spark/md/Shm/MDStructure.h" #include "spark/md/ShmSegmentName.h" #include "spark/md/queue_struct.h" +#include "spark/md/shm/cold_entry.h" +#include "spark/md/shm/cold_header.h" +#include "spark/md/shm/fixed_security.h" +#include "spark/md/shm/hot_depth_entry.h" +#include "spark/md/shm/hot_entry.h" +#include "spark/md/shm/hot_header.h" #include "spark/utils/SharedList.h" + #include namespace bts::md { diff --git a/src/lib_spark/spark/md/price_level.h b/src/lib_spark/spark/md/price_level.h new file mode 100644 index 000000000..6de3fa08f --- /dev/null +++ b/src/lib_spark/spark/md/price_level.h @@ -0,0 +1,50 @@ +#pragma once + +#include "spark/md/PriceSize.h" + +namespace bts::md { + +// +// PriceSize has grown to 12 bytes but because of 8 byte alignment it will pad +// out to 16 bytes This structure can store the same data in 24 bytes so it's +// more efficient for shared memory +struct PriceLevel { + PriceLevel() : _bid(), _ask(), _bid_size(0), _ask_size(0) {} + PriceLevel(const PriceSize &bid, const PriceSize &ask) + : _bid(bid.int_price()), _ask(ask.int_price()), _bid_size(bid.size()), + _ask_size(ask.size()) {} + + IntPrice _bid; + IntPrice _ask; + Size _bid_size; + Size _ask_size; + + const IntPrice &bid_price() const { return _bid; } + const IntPrice &ask_price() const { return _ask; } + Size bid_size() const { return _bid_size; } + Size ask_size() const { return _ask_size; } + + bool operator==(const PriceLevel &other) const { + return _ask_size == other._ask_size && _bid_size == other._bid_size && + _bid == other._bid && _ask == other._ask; + } + bool operator!=(const PriceLevel &other) const { + return _ask_size != other._ask_size || _bid_size != other._bid_size || + _bid != other._bid || _ask != other._ask; + } + + bool update(const PriceLevel &temp) { + if (*this != temp) { + *this = temp; + return true; + } + return false; + } + + PriceSize bid() const { return PriceSize(_bid, _bid_size); } + PriceSize ask() const { return PriceSize(_ask, _ask_size); } +}; + +static_assert(sizeof(PriceLevel) == 24, "PriceLevel should be small"); + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/readme.md b/src/lib_spark/spark/md/readme.md index cb2789c06..bd529ba4c 100644 --- a/src/lib_spark/spark/md/readme.md +++ b/src/lib_spark/spark/md/readme.md @@ -28,7 +28,7 @@ The MDConsumer is the dual of the MDProducer. It provides an on_security() call * SecurityMapper* * SettlementRecorder * SyntheticSecurityService -* FixedSecurity.h / MDStructure.h +* fixed_security.h / cold_header.h / cold_entry.h / hot_header.h / hot_entry.h / hot_depth_entry.h * SharedList.h ## MBOProcessor* @@ -125,11 +125,9 @@ In Edge SettlementRecorder stored the settlements for a security so we could pre Each synthetic security is a api::Security and a chunk of metadata. This service handles API requests to create/update synthetics and fires callbacks for MDConsumer which does the work of producing synethic books. -## MDStructure.h - ### Hot vs Cold -## FixedSecurity.h +## fixed_security.h * variable sized structure in shared mem * For FixedSpreadSecurity we stack allocate a lot of legs then memcpy() into shared memory only the correct number of legs * When accessing FixedSpreadSecurity it is cast back to a large number of legs. Iterate legs based on leg_count() not the static leg count. diff --git a/src/lib_spark/spark/md/shm/cold_entry.cpp b/src/lib_spark/spark/md/shm/cold_entry.cpp new file mode 100644 index 000000000..4c1efe58a --- /dev/null +++ b/src/lib_spark/spark/md/shm/cold_entry.cpp @@ -0,0 +1,32 @@ +#include "cold_entry.h" + +#include + +namespace bts::md { + +void to_json(nlohmann::json &j, const ColdEntry *c) { to_json(j, *c); } + +void to_json(nlohmann::json &j, const ColdEntry &c) { + j = {{"security", c.security}}; + const auto last_trade = std::get(c.last_trade.load()); + j["last_trade_price"] = last_trade.size() > 0 ? last_trade.price() : 0; + j["last_trade_qty"] = last_trade.size(); + j["last_trade_time"] = c.last_trade_time; + j["volume"] = c.total_volume; + core::TimeStamp settle_date; + core::TimeStamp::from_ymd( + c.misc.settlement_date.year, c.misc.settlement_date.month, + c.misc.settlement_date.day, &settle_date); + j["settle_date"] = settle_date.to_iso_string(); + j["prev_settle_price"] = c.misc.previous_settlement_price.price(); + core::TimeStamp prev_settle_date; + core::TimeStamp::from_ymd( + c.misc.previous_settlement_date.year, + c.misc.previous_settlement_date.month, + c.misc.previous_settlement_date.day, &prev_settle_date); + j["prev_settle_date"] = prev_settle_date.to_iso_string(); + j["status"] = domain::protobufs::TradingStatus_Name(c.exchange_status); + j["deleted"] = c.deleted; +} + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/shm/cold_entry.h b/src/lib_spark/spark/md/shm/cold_entry.h new file mode 100644 index 000000000..f080f5749 --- /dev/null +++ b/src/lib_spark/spark/md/shm/cold_entry.h @@ -0,0 +1,43 @@ +#pragma once + +#include "domain/protobufs/trading_status.pb.h" +#include "spark/md/PriceSize.h" +#include "spark/md/shm/fixed_security.h" +#include "spark/md/shm/misc_data.h" + +#include +#include + +namespace bts::md { + +struct ColdEntry { + ColdEntry(std::size_t security_size) + : exchange_status(domain::protobufs::TRADINGSTATUS_UNKNOWN), + last_trade(), total_volume(0), last_trade_time(), misc(), + from_spread(0), deleted(0) { + this->security.size = security_size; + } + + static size_t fixed_size() { + return sizeof(ColdEntry) - sizeof(FixedSecurity); + } + size_t size() const { return fixed_size() + this->security.size; } + + domain::protobufs::TradingStatus exchange_status; + + // Last Trade + AtomicPriceSize last_trade; + size_t total_volume; + datetime::LLDateTime last_trade_time; + + MiscData misc; + + int from_spread; + int deleted; + FixedSecurity security; +}; + +void to_json(nlohmann::json &j, const ColdEntry *c); +void to_json(nlohmann::json &j, const ColdEntry &c); + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/shm/cold_header.cpp b/src/lib_spark/spark/md/shm/cold_header.cpp new file mode 100644 index 000000000..62ee9b6db --- /dev/null +++ b/src/lib_spark/spark/md/shm/cold_header.cpp @@ -0,0 +1,33 @@ +#include "cold_header.h" +#include "system/time_stamp.h" + +#include + +namespace bts::md { + +using std::size_t; + +void to_json(nlohmann::json &j, const ColdHeader *c) { to_json(j, *c); } + +void to_json(nlohmann::json &j, const ColdHeader &c) { + j = { + {"version", c.version}, + {"count", c.count}, + {"segment_count", c.segment_count}, + {"create_time", + core::TimeStamp{c.create_time.to_uint64()}.to_iso_string()}, + {"hb", core::TimeStamp{c.heartbeat.to_uint64()}.to_iso_string()}, + {"disable_hb", c.disable_heartbeat}, + {"hb_ms", c.heartbeat_ms}, + {"hb_boot_secs", c.heartbeat_boot_seconds}, + {"sec_count", c.security_count}, + {"has_depth", c.has_depth}, + {"trade_count", static_cast(c.trade_count)}, + {"coconut_status_count", static_cast(c.coconut_status_count)}, + {"exchange_status_count", static_cast(c.exchange_status_count)}, + {"rfq_count", static_cast(c.rfq_count)}, + {"gap_count", static_cast(c.gap_count)}, + }; +} + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/shm/cold_header.h b/src/lib_spark/spark/md/shm/cold_header.h new file mode 100644 index 000000000..5cb66b541 --- /dev/null +++ b/src/lib_spark/spark/md/shm/cold_header.h @@ -0,0 +1,77 @@ +#pragma once + +#include "spark/config/config.h" +#include "spark/config/protos/market_data.pb.h" +#include "spark/md/shm/shm_version.h" +#include "spark/utils/TimeStamp.h" +#include "system/os.h" + +#include +#include +#include + +namespace bts::md { + +// +// COLD Data, brrr +/////////////////////// + +struct ALIGNED_(CACHE_LINE) ColdHeader { + ColdHeader() + : version(shm_version), count(0), segment_count(0), + create_time(utils::TimeStamp::now()), heartbeat(), + disable_heartbeat( + config::get().disable_heartbeat()), + heartbeat_ms(config::get().heartbeat_ms()), + heartbeat_boot_seconds( + config::get().heartbeat_boot_seconds()), + security_count(0), has_depth(false), trade_count(0), + coconut_status_count(0), exchange_status_count(0), rfq_count(0) {} + + std::uint32_t version; + std::size_t count; + std::size_t segment_count; + utils::TimeStamp create_time; + utils::TimeStamp heartbeat; + bool disable_heartbeat; + std::uint32_t heartbeat_ms; + std::uint32_t heartbeat_boot_seconds; + + std::uint32_t security_count; // incremented when a security is made public + bool has_depth; + + std::atomic trade_count; + std::atomic coconut_status_count; + std::atomic exchange_status_count; + std::atomic rfq_count; + std::atomic gap_count; + + // Ring buffer for LastTrade for all securities in this list + std::uint8_t trade_buffer[1024 * 1024 * 4]; // 4mb + // Ring buffer for RFQ + // static const int rfq_buffer_entry_size = 64; + typedef std::array RFQBuffer; + std::uint8_t rfq_buffer[1024 * 256]; // 256k +}; + +bool is_heart_beat_ok(const ColdHeader &header); + +void to_json(nlohmann::json &j, const ColdHeader *c); +void to_json(nlohmann::json &j, const ColdHeader &c); + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline bool is_heart_beat_ok(const ColdHeader &header) { + std::uint32_t s_heartbeat_ms = header.heartbeat_ms; + bool s_disable_heartbeat = header.disable_heartbeat; + std::uint32_t s_grace_period_ms = header.heartbeat_boot_seconds * 1000; + utils::TimeStamp hb = header.heartbeat; + utils::TimeStamp now = utils::TimeStamp::now(); + return (header.version == shm_version) && + (s_disable_heartbeat || (now - hb).milliseconds() < s_heartbeat_ms || + (now - header.create_time).milliseconds() < s_grace_period_ms); +} + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/Shm/FixedSecurity.cpp b/src/lib_spark/spark/md/shm/fixed_security.cpp similarity index 99% rename from src/lib_spark/spark/md/Shm/FixedSecurity.cpp rename to src/lib_spark/spark/md/shm/fixed_security.cpp index e64ecc298..ae6db3af0 100644 --- a/src/lib_spark/spark/md/Shm/FixedSecurity.cpp +++ b/src/lib_spark/spark/md/shm/fixed_security.cpp @@ -3,7 +3,8 @@ #ifndef NO_CPP0X #include "../PriceSize.h" -#include "FixedSecurity.h" +#include "spark/md/shm/fixed_security.h" + #include #ifdef _WIN32 diff --git a/src/lib_spark/spark/md/Shm/FixedSecurity.h b/src/lib_spark/spark/md/shm/fixed_security.h similarity index 100% rename from src/lib_spark/spark/md/Shm/FixedSecurity.h rename to src/lib_spark/spark/md/shm/fixed_security.h diff --git a/src/lib_spark/spark/md/shm/hot_depth_entry.cpp b/src/lib_spark/spark/md/shm/hot_depth_entry.cpp new file mode 100644 index 000000000..08e4f885d --- /dev/null +++ b/src/lib_spark/spark/md/shm/hot_depth_entry.cpp @@ -0,0 +1,42 @@ +#include "hot_depth_entry.h" +#include "system/time_stamp.h" + +namespace bts::md { + +void to_json(nlohmann::json &j, const HotDepthEntry &h) { + const core::TimeStamp timestamp{h.time().to_uint64()}; + j = { + {"timestamp", timestamp.to_iso_string()}, + {"key", h.key}, + {"settle", h.settlement.price()}, + {"status", + co2::CoconutSecurityStatus_Name((co2::CoconutSecurityStatus)h.status)}, + {"seq", h.sequence}, + }; + std::vector bids; + std::vector asks; + for (const auto &l : h.direct) { + if (!l.bid().is_nan()) { + bids.push_back(l.bid()); + } + if (!l.ask().is_nan()) { + asks.push_back(l.ask()); + } + } + j["bids"] = bids; + j["asks"] = asks; + std::vector ibids; + std::vector iasks; + for (const auto &l : h.implied) { + if (!l.bid().is_nan()) { + ibids.push_back(l.bid()); + } + if (!l.ask().is_nan()) { + iasks.push_back(l.ask()); + } + } + j["ibids"] = ibids; + j["iasks"] = iasks; +} + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/shm/hot_depth_entry.h b/src/lib_spark/spark/md/shm/hot_depth_entry.h new file mode 100644 index 000000000..360e2182d --- /dev/null +++ b/src/lib_spark/spark/md/shm/hot_depth_entry.h @@ -0,0 +1,102 @@ +#pragma once + +#include "spark/md/PriceSize.h" +#include "spark/md/common.h" +#include "spark/md/implied_price_level.h" +#include "spark/md/price_level.h" +#include "spark/md/protos/co2.pb.h" +#include "spark/utils/TimeStamp.h" + +#include +#include +#include +#include + +namespace bts::md { + +const std::uint32_t COCONUT_MAX_DEPTH = 10; +const std::uint32_t COCONUT_MAX_IMPLIED_DEPTH = 2; +class HotDepthEntry { + std::atomic timestamp; // 8 + public: + HotDepthEntry() + : timestamp(utils::TimeStamp(0)), key(invalid_security_key), sequence(0), + direct(), implied(), status(co2::COCONUTSECURITYSTATUS_OK) {} + std::uint32_t size() const { return sizeof(HotDepthEntry); } + security_key + key; // 4 NOTE: key is at a different offset than it is in HotEntry + std::uint32_t sequence; // 4 + IntPrice settlement; // 8 + std::array direct; // 240 + std::array implied; // 48 + std::uint8_t status; // 1 + + void read_direct(std::uint32_t depth, PriceSize *bid, PriceSize *ask) const; + void read_implied(std::uint32_t depth, PriceSize *bid, PriceSize *ask) const; + + bool write_direct( + std::uint32_t depth, const PriceSize &bid, const PriceSize &ask); + bool write_implied( + std::uint32_t depth, const PriceSize &bid, const PriceSize &ask); + + void set_time(const utils::TimeStamp &t); + + utils::TimeStamp time() const; +}; + +static_assert(sizeof(HotDepthEntry) <= 320, "HotDepthEntry should be small"); + +void to_json(nlohmann::json &j, const HotDepthEntry *h); +void to_json(nlohmann::json &j, const HotDepthEntry &h); + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline void HotDepthEntry::read_direct( + std::uint32_t depth, PriceSize *bid, PriceSize *ask) const { + *bid = direct[depth].bid(); + *ask = direct[depth].ask(); +} + +inline void HotDepthEntry::read_implied( + std::uint32_t depth, PriceSize *bid, PriceSize *ask) const { + *bid = implied[depth].bid(); + *ask = implied[depth].ask(); +} + +inline bool HotDepthEntry::write_direct( + std::uint32_t depth, const PriceSize &bid, const PriceSize &ask) { + bts_assert_d(depth < direct.size()); + auto temp = PriceLevel(bid, ask); + if (direct[depth] != temp) { + direct[depth] = temp; + return true; + } + return false; +} + +inline bool HotDepthEntry::write_implied( + std::uint32_t depth, const PriceSize &bid, const PriceSize &ask) { + bts_assert_d(depth < implied.size()); + auto temp = PriceLevel(bid, ask); + if (implied[depth] != temp) { + implied[depth] = temp; + return true; + } + return false; +} + +inline void HotDepthEntry::set_time(const utils::TimeStamp &t) { + timestamp.store(t, std::memory_order_release); +} + +inline utils::TimeStamp HotDepthEntry::time() const { + return timestamp.load(std::memory_order_acquire); +} + +inline void to_json(nlohmann::json &j, const HotDepthEntry *h) { + to_json(j, *h); +} + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/shm/hot_entry.h b/src/lib_spark/spark/md/shm/hot_entry.h new file mode 100644 index 000000000..92fc94307 --- /dev/null +++ b/src/lib_spark/spark/md/shm/hot_entry.h @@ -0,0 +1,61 @@ +#pragma once + +#include "spark/md/common.h" +#include "spark/md/price_level.h" +#include "spark/md/protos/co2.pb.h" +#include "spark/utils/TimeStamp.h" +#include "system/time_stamp.h" + +#include +#include +#include + +namespace bts::md { + +// These should not be cache line aligned because they are small enough for +// multiple to fit on one line easily +class HotEntry { + std::atomic timestamp{utils::TimeStamp{0}}; // 8 + public: + uint32_t size() const { return sizeof(HotEntry); } + + PriceLevel book{}; // 24 + std::uint32_t key{invalid_security_key}; // 4 + std::uint32_t sequence{0}; // 4 + IntPrice settlement; // 8 + std::uint8_t status{co2::COCONUTSECURITYSTATUS_OK}; // 1 + + void set_time(const utils::TimeStamp &t) { + timestamp.store(t, std::memory_order_release); + } + + utils::TimeStamp time() const { + return timestamp.load(std::memory_order_acquire); + } +}; + +static_assert(sizeof(HotEntry) <= CACHE_LINE, "HotEntry should be small"); + +void to_json(nlohmann::json &j, const HotEntry *h); +void to_json(nlohmann::json &j, const HotEntry &h); + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline void to_json(nlohmann::json &j, const HotEntry *h) { to_json(j, *h); } + +inline void to_json(nlohmann::json &j, const HotEntry &h) { + const core::TimeStamp timestamp{h.time().to_uint64()}; + j = { + {"timestamp", timestamp.to_iso_string()}, + {"key", h.key}, + {"bid", h.book.bid()}, + {"ask", h.book.ask()}, + {"status", + co2::CoconutSecurityStatus_Name((CoconutSecurityStatus)h.status)}, + {"seq", h.sequence}, + }; +} + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/shm/hot_header.h b/src/lib_spark/spark/md/shm/hot_header.h new file mode 100644 index 000000000..11a0addd6 --- /dev/null +++ b/src/lib_spark/spark/md/shm/hot_header.h @@ -0,0 +1,40 @@ +#pragma once + +#include "system/os.h" + +#include + +namespace bts::md { + +// +// HOT Data +/////////////////////// + +struct ALIGNED_(CACHE_LINE) HotHeader { + size_t count; + size_t segment_count; + // Just the counters. Publisher process can compute messages/second by + // polling these counters + size_t direct_count; + size_t implied_count; +}; + +void to_json(nlohmann::json &j, const HotHeader *h); +void to_json(nlohmann::json &j, const HotHeader &h); + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline void to_json(nlohmann::json &j, const HotHeader *h) { to_json(j, *h); } + +inline void to_json(nlohmann::json &j, const HotHeader &h) { + j = { + {"count", h.count}, + {"segment_count", h.segment_count}, + {"direct_count", h.direct_count}, + {"implied_count", h.implied_count}, + }; +} + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/shm/misc_data.h b/src/lib_spark/spark/md/shm/misc_data.h new file mode 100644 index 000000000..994f37a5b --- /dev/null +++ b/src/lib_spark/spark/md/shm/misc_data.h @@ -0,0 +1,52 @@ +#pragma once + +#include "spark/md/IntPrice.h" +#include "spark/utils/TimeStamp.h" +#include "spark/utils/datetime.h" + +namespace bts::md { + +struct MiscData { + MiscData() + : misc_timestamp(), settlement_date(), + previous_settlement_price(INT_NAN), previous_settlement_date(), + open_price(NO_PRICE), open_date(), close_price(NO_PRICE), close_date(), + session_high(NO_PRICE), session_low(NO_PRICE), + session_high_bid(NO_PRICE), session_low_offer(NO_PRICE), + fixing_price(NO_PRICE) {} + utils::TimeStamp misc_timestamp; + // Settlement + datetime::Date settlement_date; + IntPrice previous_settlement_price; + datetime::Date previous_settlement_date; + + // Opening/Closing + double open_price; + datetime::Date open_date; + double close_price; + datetime::Date close_date; + + // Misc stats for current session + double session_high; + double session_low; + double session_high_bid; + double session_low_offer; + double fixing_price; + + bool operator==(const MiscData &other) const { + return settlement_date == other.settlement_date && + previous_settlement_price == other.previous_settlement_price && + previous_settlement_date == other.previous_settlement_date && + open_price == other.open_price && open_date == other.open_date && + close_price == other.close_price && + close_date == other.close_date && + session_high == other.session_high && + session_low == other.session_low && + session_high_bid == other.session_high_bid && + session_low_offer == other.session_low_offer && + fixing_price == other.fixing_price; + } + bool operator!=(const MiscData &other) const { return !(*this == other); } +}; + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/Shm/readme.txt b/src/lib_spark/spark/md/shm/readme.txt similarity index 100% rename from src/lib_spark/spark/md/Shm/readme.txt rename to src/lib_spark/spark/md/shm/readme.txt diff --git a/src/lib_spark/spark/md/shm/shm_version.h b/src/lib_spark/spark/md/shm/shm_version.h new file mode 100644 index 000000000..8e201dc5d --- /dev/null +++ b/src/lib_spark/spark/md/shm/shm_version.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace bts::md { + +const std::uint32_t shm_version = 107; + +} // namespace bts::md diff --git a/src/lib_spark/spark/md/test/synthetic_book_merge_tests.cpp b/src/lib_spark/spark/md/test/synthetic_book_merge_tests.cpp index 8774e6abd..a4c78b8e6 100644 --- a/src/lib_spark/spark/md/test/synthetic_book_merge_tests.cpp +++ b/src/lib_spark/spark/md/test/synthetic_book_merge_tests.cpp @@ -1,6 +1,7 @@ #ifndef _WIN32 #include "../MDConsumerHandle.h" #include "Co2TestInstrument.h" +#include "spark/utils/TimeStamp.h" #include "spark/utils/sane_algorithm.h" #include "md_test_utils.h" #include diff --git a/src/lib_spark/spark/orderman/gateways/sim/SimExecutionGateway.cpp b/src/lib_spark/spark/orderman/gateways/sim/SimExecutionGateway.cpp index 13874bb22..4fe2f0476 100644 --- a/src/lib_spark/spark/orderman/gateways/sim/SimExecutionGateway.cpp +++ b/src/lib_spark/spark/orderman/gateways/sim/SimExecutionGateway.cpp @@ -8,7 +8,6 @@ #include "spark/md/GeneralSecurityIndex.h" #include "spark/md/IntPrice.h" #include "spark/md/PriceSize.h" -#include "spark/md/Shm/MDStructure.h" #include "spark/orderman/OrderMsg.h" #include "spark/orderman/events/NewAdminDataEvent.h" #include "spark/orderman/events/NewSecuritiesEvent.h" diff --git a/src/lib_spark/spark/orderman/gateways/sim/SimExecutionGateway.h b/src/lib_spark/spark/orderman/gateways/sim/SimExecutionGateway.h index 4b3ca44ea..de6ea7393 100644 --- a/src/lib_spark/spark/orderman/gateways/sim/SimExecutionGateway.h +++ b/src/lib_spark/spark/orderman/gateways/sim/SimExecutionGateway.h @@ -6,14 +6,15 @@ #include "spark/md/GeneralSecurityIndex.h" #include "spark/md/MDClient.h" #include "spark/md/MDProducer.h" -#include "spark/md/Shm/FixedSecurity.h" #include "spark/md/protos/co2.pb.h" +#include "spark/md/shm/fixed_security.h" #include "spark/orderman/CancelOrderMsg.h" #include "spark/orderman/CancelReplaceOrderMsg.h" #include "spark/orderman/CreateOrderMsg.h" #include "spark/orderman/OrderMsg.h" #include "spark/orderman/gateways/IExecutionGateway.h" #include "spark/orderman/orderman_utils/orderman_common.h" + #include namespace bts::md { diff --git a/src/lib_spark/spark/orderman/orderman_utils/Book.h b/src/lib_spark/spark/orderman/orderman_utils/Book.h index 649d9cb19..47d19d829 100644 --- a/src/lib_spark/spark/orderman/orderman_utils/Book.h +++ b/src/lib_spark/spark/orderman/orderman_utils/Book.h @@ -1,5 +1,6 @@ #pragma once +#include "spark/domain_model/SecurityKey.h" #include "spark/md/PriceSize.h" #include "system/FMT_IMPL.h"