diff --git a/src/lib/md/Diag/ShmDumper.cpp b/src/lib/md/Diag/ShmDumper.cpp index 156feda4e..976bd7461 100644 --- a/src/lib/md/Diag/ShmDumper.cpp +++ b/src/lib/md/Diag/ShmDumper.cpp @@ -213,7 +213,10 @@ void dump_hot_t(SharedListReader& reader) { if (!reader.is_initialized()) { LOG(md,Info,"Unable to initialize section "); return; } auto& header = reader.header(); cout << "{ " << endl; - cout << "'header': { 'count': " << header.count << ", 'segment_count': " << header.segment_count << ", 'bbo_count': " << header.bbo_count << ", 'depth_count': " << header.depth_count << "}," << endl; + cout << "'header': { 'count': " << header.count + << ", 'segment_count': " << header.segment_count + << ", 'direct_count': " << header.direct_count + << ", 'implied_count': " << header.implied_count << "}," << endl; cout << "'entries': [ " << endl; reader.all_entries([&](const TEntry& entry) { cout << dump_entry(entry) << "," << endl; }); cout << "] }" << endl; diff --git a/src/lib/md/MDConsumer.cpp b/src/lib/md/MDConsumer.cpp index 7e3c827c8..b534058ec 100644 --- a/src/lib/md/MDConsumer.cpp +++ b/src/lib/md/MDConsumer.cpp @@ -932,13 +932,13 @@ TimeStamp MDConsumerChannel::create_time() const { return _pimpl ? _pimpl->cold_ size_t MDConsumerChannel::security_count() const { return _pimpl->cold_header().security_count; } -size_t MDConsumerChannel::bbo_count() const { +size_t MDConsumerChannel::direct_count() const { return is_initialized() ? - _pimpl->hot_header().bbo_count : 0; + _pimpl->hot_header().direct_count : 0; } -size_t MDConsumerChannel::depth_count() const { +size_t MDConsumerChannel::implied_count() const { return is_initialized() ? - _pimpl->hot_header().depth_count : 0; + _pimpl->hot_header().implied_count : 0; } size_t MDConsumerChannel::trade_count() const { return _pimpl->cold_header().trade_count; diff --git a/src/lib/md/MDConsumer.h b/src/lib/md/MDConsumer.h index 99aa40c7e..643ef2635 100644 --- a/src/lib/md/MDConsumer.h +++ b/src/lib/md/MDConsumer.h @@ -87,8 +87,8 @@ public: // // Statistic counters size_t security_count() const; - size_t bbo_count() const; - size_t depth_count() const; + size_t direct_count() const; + size_t implied_count() const; size_t trade_count() const; size_t rfq_count() const; size_t gap_count() const; diff --git a/src/lib/md/MDConsumerHandle.cpp b/src/lib/md/MDConsumerHandle.cpp index a172f1fa3..0b82746bf 100644 --- a/src/lib/md/MDConsumerHandle.cpp +++ b/src/lib/md/MDConsumerHandle.cpp @@ -67,7 +67,7 @@ TimeStamp copy_depth( do { t = hot.time(); for (size_t x = 0; x < count; ++x) { - hot.read_level(x, &bid[x], &ask[x]); + hot.read_direct(x, &bid[x], &ask[x]); } ++attempts; } while((t != hot.time() || t == TimeStamp::invalid()) && attempts < max_attempts); diff --git a/src/lib/md/MDProducerHandle.cpp b/src/lib/md/MDProducerHandle.cpp index 7fb5bd5d8..3af9741da 100644 --- a/src/lib/md/MDProducerHandle.cpp +++ b/src/lib/md/MDProducerHandle.cpp @@ -82,7 +82,7 @@ void MDProducerHandle::update_bbo(TimeStamp time, const PriceSize bid, const Pri if (updated) { hdata().set_time(time); hdata().sequence = seq; - hheader().bbo_count++; + hheader().direct_count++; } else { hdata().set_time(oldtime); @@ -91,12 +91,12 @@ void MDProducerHandle::update_bbo(TimeStamp time, const PriceSize bid, const Pri else { auto oldtime = hdepth().time(); hdepth().set_time(TimeStamp::invalid()); - bool updated = hdepth().write_level(0, bid,ask); + bool updated = hdepth().write_direct(0, bid,ask); hdepth().status = COCONUTSECURITYSTATUS_OK; if (updated) { hdepth().set_time(time); hdepth().sequence = seq; - hheader().bbo_count++; + hheader().direct_count++; } else { hdepth().set_time(oldtime); diff --git a/src/lib/md/MDProducerHandle.h b/src/lib/md/MDProducerHandle.h index b30af47b0..4203c1fe4 100644 --- a/src/lib/md/MDProducerHandle.h +++ b/src/lib/md/MDProducerHandle.h @@ -35,7 +35,7 @@ public: void set_legs(const std::vector& legs); void update_bbo(TimeStamp time, const PriceSize bid_price, const PriceSize ask_price, uint16_t seq=0); // hot - void update_book(TimeStamp time, std::array& bids, std::array& asks, uint16_t seq=0) { + void update_book(TimeStamp time, std::array& bids, std::array& asks, uint32_t seq=0) { if (!has_depth()) { update_bbo(time, bids[0], asks[0], seq); } @@ -43,12 +43,25 @@ public: auto& book = hdepth(); book.set_time(TimeStamp::invalid()); for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { - book.write_level(x, bids[x], asks[x]); + book.write_direct(x, bids[x], asks[x]); } book.status = co2::COCONUTSECURITYSTATUS_OK; book.sequence = seq; book.set_time(time); - hheader().depth_count++; + hheader().direct_count += 1; + } + } + void update_implied(TimeStamp time, std::array& bids, std::array& asks, uint32_t seq=0) { + if (has_depth()) { + auto& book = hdepth(); + book.set_time(TimeStamp::invalid()); + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + book.write_implied(x, bids[x], asks[x]); + } + book.status = co2::COCONUTSECURITYSTATUS_OK; + book.sequence = seq; + book.set_time(time); + hheader().implied_count += 1; } } diff --git a/src/lib/md/Shm/MDStructure.h b/src/lib/md/Shm/MDStructure.h index 09166d8ec..177235ea9 100644 --- a/src/lib/md/Shm/MDStructure.h +++ b/src/lib/md/Shm/MDStructure.h @@ -32,8 +32,8 @@ 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 bbo_count; - size_t depth_count; + 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 @@ -44,7 +44,7 @@ class HotEntry { uint32_t size() const { return sizeof(HotEntry); } PriceLevel book; // 24 security_key key; // 4 - uint16_t sequence; // 4 + uint32_t sequence; // 4 uint8_t status; // 1 void set_time(const TimeStamp& t) { timestamp.store(t, std::memory_order_release); } TimeStamp time() const { return timestamp.load(std::memory_order_acquire); } @@ -52,6 +52,7 @@ class HotEntry { static_assert(sizeof(HotEntry) <= CACHE_LINE, "HotEntry should be small"); const uint32_t COCONUT_MAX_DEPTH = 10; +const uint32_t COCONUT_IMPLIED_DEPTH = 2; class HotDepthEntry { std::atomic timestamp; // 8 public: @@ -59,32 +60,50 @@ class HotDepthEntry { timestamp(TimeStamp(0)), key(invalid_security_key), sequence(0), - status(co2::COCONUTSECURITYSTATUS_OK), - book() + 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 - uint16_t sequence; // 2 + uint32_t sequence; // 4 + std::array implied; + std::array direct; uint8_t status; // 1 - std::array book; const PriceLevel* at(uint32_t depth) const { - if (depth < book.size()) { - return &book[depth]; + if (depth < direct.size()) { + return &direct[depth]; } return nullptr; } - void read_level(uint32_t depth, PriceSize* bid, PriceSize* ask) const { - if (depth < book.size()) { - *bid = book[depth].bid(); - *ask = book[depth].ask(); + void read_direct(uint32_t depth, PriceSize* bid, PriceSize* ask) const { + if (depth < direct.size()) { + *bid = direct[depth].bid(); + *ask = direct[depth].ask(); } } - bool write_level(uint32_t depth, const PriceSize& bid, const PriceSize& ask) { - if (depth < book.size()) { + void read_implied(uint32_t depth, PriceSize* bid, PriceSize* ask) const { + if (depth < implied.size()) { + *bid = implied[depth].bid(); + *ask = implied[depth].ask(); + } + } + bool write_direct(uint32_t depth, const PriceSize& bid, const PriceSize& ask) { + if (depth < direct.size()) { auto temp = PriceLevel(bid,ask); - if (book[depth] != temp) { - book[depth] = PriceLevel(bid,ask); + if (direct[depth] != temp) { + direct[depth] = PriceLevel(bid,ask); + return true; + } + } + return false; + } + bool write_implied(uint32_t depth, const PriceSize& bid, const PriceSize& ask) { + if (depth < implied.size()) { + auto temp = PriceLevel(bid,ask); + if (implied[depth] != temp) { + implied[depth] = PriceLevel(bid,ask); return true; } } @@ -94,7 +113,7 @@ class HotDepthEntry { void set_time(const TimeStamp& t) { timestamp.store(t, std::memory_order_release); } TimeStamp time() const { return timestamp.load(std::memory_order_acquire); } }; -static_assert(sizeof(HotDepthEntry) == 256, "HotDepthEntry should be small"); +static_assert(sizeof(HotDepthEntry) <= 320, "HotDepthEntry should be small"); // // COLD Data, brrr diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index af2e2e0fd..d6252b220 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -80,7 +80,9 @@ struct MDConsumerGroup { labels.push_back({ "type", SecurityType_Name(symbol.type()).substr(13) /*SECURITYTYPE_*/ }); labels.push_back({ "exch", Venue_Name(symbol.exchange()).substr(6) /*VENUE_*/ }); stats.publish( - std::make_shared("spark_md_book_updates", [=]() { return std::max(channel.bbo_count(), channel.depth_count()); }, labels)); + std::make_shared("spark_md_book_updates", [=]() { + return channel.direct_count() + channel.implied_count(); + }, labels)); stats.publish( std::make_shared("spark_md_security_count", [=]() { return channel.security_count(); }, labels)); stats.publish( diff --git a/src/lib/md/test/Co2ManagedTestHelper.cpp b/src/lib/md/test/Co2ManagedTestHelper.cpp index b440e33fd..b0444b350 100644 --- a/src/lib/md/test/Co2ManagedTestHelper.cpp +++ b/src/lib/md/test/Co2ManagedTestHelper.cpp @@ -48,7 +48,7 @@ extern "C" BTS_EXPORT void co2_get_sample_depth(size_t& size, HotDepthEntry& ent size = sizeof(HotDepthEntry); entry.key = 1234; for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { - entry.write_level(x, + entry.write_direct(x, PriceSize::from_double<2>(x + 0.5, x + 1), PriceSize::from_double<2>(x + 0.6, x + 2)); }