From 7a2d24cf54b63f1941205e5575f6073877bb77fa Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 20 Feb 2018 17:39:16 -0500 Subject: [PATCH] Spark-496, spark-497. Store and retrieve synthetic securities from a single block of shared memory. --- src/lib/md/MDProducer.cpp | 14 +++++++-- src/lib/md/PlatformLoader.cpp | 5 +-- src/lib/md/Shm/FixedSecurity.cpp | 42 +++---------------------- src/lib/md/Shm/FixedSecurity.h | 2 -- src/lib/md/SyntheticSecurityService.cpp | 2 +- src/lib/md/common.cpp | 1 + src/lib/md/common.h | 1 + src/lib/md/relay/QueryServer.cpp | 24 +++++++------- 8 files changed, 36 insertions(+), 55 deletions(-) diff --git a/src/lib/md/MDProducer.cpp b/src/lib/md/MDProducer.cpp index d402179e3..3635dfc58 100644 --- a/src/lib/md/MDProducer.cpp +++ b/src/lib/md/MDProducer.cpp @@ -117,14 +117,24 @@ MDProducer::MDProducer(MDProducer&& other) : MDProducer::~MDProducer() {} MDProducerImpl& MDProducer::get_producer(const char* symbol, SecurityType type, Venue exchange) { + // First search existing producers for one that matches for (auto& impl : _producers) { - if (impl->symbol() == symbol && impl->type() == type && impl->exchange() == exchange) { + if ((impl->symbol() == symbol && impl->type() == type && impl->exchange() == exchange) || + //single channel for all synthetics + (impl->type() == api::SECURITYTYPE_SYNTHETIC && type == api::SECURITYTYPE_SYNTHETIC)) { return *impl; } } // Not found add a new one - unique_ptr newp(new MDProducerImpl(symbol, type, exchange, _platform, _shared)); + unique_ptr newp; + if (type == api::SECURITYTYPE_SYNTHETIC) { + // synthetics are special. We don't want to allocate a separate section for each one so we put them all in one + newp.reset(new MDProducerImpl(SYNTHETIC_SYMBOL, api::SECURITYTYPE_SYNTHETIC, Venue::VENUE_BTS, _platform, _shared)); + } + else { + newp.reset(new MDProducerImpl(symbol, type, exchange, _platform, _shared)); + } _producers.push_back(move(newp)); return *_producers.back(); } diff --git a/src/lib/md/PlatformLoader.cpp b/src/lib/md/PlatformLoader.cpp index ff9012f28..48158a3b3 100644 --- a/src/lib/md/PlatformLoader.cpp +++ b/src/lib/md/PlatformLoader.cpp @@ -83,8 +83,6 @@ void add_symbols_to_result( } } -// size_t get_system_md_symbols(google::protobuf::RepeatedPtrField - size_t get_system_md_symbols(api::Venue exchange, SecurityType type, std::set& result) { bts_assert(config::system_symbols::Symbols::descriptor()->field_count() == 1); auto const& e = config::get().glbx(); @@ -99,6 +97,9 @@ size_t get_system_md_symbols(api::Venue exchange, SecurityType type, std::setset_cfi_code(source.cfi_code()); dest.mutable_glbx()->set_cme_symbol(source.alt_symbol()); break; + case Venue::VENUE_BTS: + break; default: bts_assert(false); } @@ -213,31 +215,6 @@ void convert_security(const FixedSecurity& source, Security* pres) { get_contract_spec(source, *dest.mutable_contract_spec()); } -size_t recursive_leg_count(const Security& security) { - size_t count = 0; - switch(security.Contract_case()) { - case Security::ContractCase::kFuture: - case Security::ContractCase::kOption: - case Security::ContractCase::CONTRACT_NOT_SET: - return 0; - case Security::ContractCase::kSpread: { - count += security.spread().legs_size(); - for (const auto& leg : security.spread().legs()) { - count += recursive_leg_count(leg.security()); - } - return count; - } - case Security::ContractCase::kSynthetic: { - count += security.synthetic().legs_size(); - for (const auto& leg : security.synthetic().legs()) { - count += recursive_leg_count(leg.security()); - } - return count; - } - } - bts_assert(false); -} - SecurityType securitytype_of_contractcase(Security::ContractCase cc) { switch(cc) { case Security::ContractCase::kFuture: return SECURITYTYPE_FUTURE; @@ -261,15 +238,6 @@ size_t num_legs(const Security& sec) { } return 0; } -size_t num_legs(const FixedSecurity& fs) { - if (fs.type == SecurityType::SECURITYTYPE_SYNTHETIC) { - return static_cast(&fs)->leg_count(); - } - else if (fs.type == SecurityType::SECURITYTYPE_SPREAD) { - return static_cast(&fs)->leg_count(); - } - return 0; -} void convert_security(const Security& src, FixedSecurity& dest, bool set_security_key) { dest.key = set_security_key ? src.id() : invalid_security_key; diff --git a/src/lib/md/Shm/FixedSecurity.h b/src/lib/md/Shm/FixedSecurity.h index b2fd22f3f..8d34a4800 100644 --- a/src/lib/md/Shm/FixedSecurity.h +++ b/src/lib/md/Shm/FixedSecurity.h @@ -163,13 +163,11 @@ std::string security_descriptor(const api::Security&); // Convert a fixedsecurity to a normal Security - caveat: spreads will be normalized Security convert_security(const FixedSecurity&); void convert_security(const FixedSecurity&, Security*); -size_t recursive_leg_count(const Security&); void convert_security(const Security& src, FixedSecurity& dest, bool set_security_key=false); SecurityType securitytype_of_contractcase(Security::ContractCase cc); SecurityType securitytype_of(const Security&); size_t num_legs(const Security&); -size_t num_legs(const FixedSecurity&); // Stack allocates a security of the given type template diff --git a/src/lib/md/SyntheticSecurityService.cpp b/src/lib/md/SyntheticSecurityService.cpp index 98e553fee..9c0fe8e8f 100644 --- a/src/lib/md/SyntheticSecurityService.cpp +++ b/src/lib/md/SyntheticSecurityService.cpp @@ -43,7 +43,7 @@ SyntheticSecurityService::SyntheticSecurityService(api::ServiceContext& context) // load securities std::set symbols; - md::get_system_md_symbols(symbols); + md::get_system_md_symbols(api::VENUE_BTS, api::SECURITYTYPE_UNSET, symbols); md::db::load_securities(symbols, [=](const api::Security& security) { if (security.Contract_case() == api::Security::kSynthetic) { auto key = SecurityKey(security.id()); diff --git a/src/lib/md/common.cpp b/src/lib/md/common.cpp index 4071f33ec..c7f073647 100644 --- a/src/lib/md/common.cpp +++ b/src/lib/md/common.cpp @@ -14,6 +14,7 @@ namespace bts { namespace md { const double NO_PRICE = std::numeric_limits().quiet_NaN(); + const char* SYNTHETIC_SYMBOL = "SYNTHETIC"; std::ostream& operator<<(std::ostream& os, const UserSymbol& sym) { os << sym.symbol() << "-" << api::SecurityType_Name(sym.type()); diff --git a/src/lib/md/common.h b/src/lib/md/common.h index cb3ebb646..20c5cf1e8 100644 --- a/src/lib/md/common.h +++ b/src/lib/md/common.h @@ -13,6 +13,7 @@ namespace bts { namespace md { BTS_EXPORT const extern double NO_PRICE; + const extern char* SYNTHETIC_SYMBOL; typedef uint32_t security_key; const security_key invalid_security_key = 0; diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index 846c7d10a..7ba48a4c8 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -59,11 +59,11 @@ struct DatedHandle { }; struct MDConsumerGroup { - MDConsumerGroup(const UserSymbol& symbol, std::shared_ptr& ss) : + // probably just have to send the consumer in from outside + MDConsumerGroup(const UserSymbol& symbol) : symbol(symbol), - synt(ss), platform(platform_for_exchange(symbol.exchange())), - consumer(symbol, ss), + consumer(symbol, nullptr), channel(consumer.get_channel(symbol)), handles(new tbb::concurrent_vector()) { @@ -74,7 +74,7 @@ struct MDConsumerGroup { // if hearbeat is ok just return if (consumer.all_heartbeats_ok()) return; // otherwise construct a new MDConsumer - MDConsumer temp(symbol, synt); + MDConsumer temp(symbol, nullptr); if (temp.all_heartbeats_ok()) { consumer.swap(temp); auto temphandles = std::make_unique>(); @@ -92,7 +92,6 @@ struct MDConsumerGroup { } UserSymbol symbol; - std::shared_ptr synt; Platform platform; MDConsumer consumer; MDConsumerChannel channel; @@ -139,11 +138,11 @@ public: InternalMap::const_iterator find(const UserSymbol& symbol) const { return _map.find(symbol); } }; -Map form_groups(const std::set& symbols, std::shared_ptr& ss) { +Map form_groups(const std::set& symbols) { // todo: Probably want to pass null for ss for everything except the last one boost::container::flat_map> groups; for (const auto& sym : symbols) { - groups.emplace(std::make_pair(sym, std::make_unique(sym, ss))); + groups.emplace(std::make_pair(sym, std::make_unique(sym))); } groups.shrink_to_fit(); return Map(groups); @@ -177,9 +176,14 @@ void copy_misc(MiscData misc, co2::MiscMessage* pmisc) { } // Handles market data queries and responds -struct QueryServerImpl : public IQueryServer { +class QueryServerImpl : public IQueryServer { + MDConsumer consumer; + Map groups; + +public: QueryServerImpl(const std::set& symbols, std::shared_ptr& ss) : - groups(form_groups(symbols, ss)) + consumer(symbols, ss), + groups(form_groups(symbols)) { } // Scan for new securities and get their handles+times @@ -331,8 +335,6 @@ struct QueryServerImpl : public IQueryServer { } } -private: - Map groups; }; struct MDRelayServer : bts::co2::MarketDataRelayBase {