diff --git a/src/lib/md/MDConsumerHandle.cpp b/src/lib/md/MDConsumerHandle.cpp index 275f032f9..a76defd28 100644 --- a/src/lib/md/MDConsumerHandle.cpp +++ b/src/lib/md/MDConsumerHandle.cpp @@ -1,29 +1,40 @@ #ifndef NO_CPP0X #include "MDConsumerHandle.h" +#include "lib/utils/sane_algorithm.h" + +#define never bts_assert(false) namespace bts { namespace md { -MDConsumerHandle::MDConsumerHandle() : - _hot(nullptr), - _cdata(nullptr), - _cheader(nullptr) +MDConsumerHandle::MDConsumerHandle() { - static_assert(sizeof(MDConsumerHandle) == sizeof(uintptr_t) * 3, ""); + memset(this, 0, sizeof(MDConsumerHandle)); + _type = HANDLETYPE_VOID; + static_assert(sizeof(MDConsumerHandle) <= 32, ""); } MDConsumerHandle::MDConsumerHandle(const MDConsumerHandle& other) : - _hot(other._hot), - _cdata(other._cdata), - _cheader(other._cheader) -{} + _type(other._type) +{ + *this = other; +} MDConsumerHandle::~MDConsumerHandle() {} void MDConsumerHandle::init(const ColdHeader& cheader, const ColdEntry& centry, const void* pvhot) { - _hot = pvhot; - _cdata = ¢ry; - _cheader = &cheader; + if (cheader.has_depth) { + _type = HANDLETYPE_DEPTH; + book.hot = (HotDepthEntry*)pvhot; + book.cdata = ¢ry; + book.cheader = &cheader; + } + else { + _type = HANDLETYPE_BBO; + bbo.hot = (HotEntry*)pvhot; + bbo.cdata = ¢ry; + bbo.cheader = &cheader; + } } TimeStamp copy_bbo(const TimeStamp& time, const SingleWriterPriceSizeStore& in_bid, const SingleWriterPriceSizeStore& in_ask, PriceSize& out_bid, PriceSize& out_ask) { @@ -69,59 +80,143 @@ TimeStamp copy_depth( return t; } + +TimeStamp copy_depth(const std::vector& legs, size_t count, PriceSize* bid, PriceSize* ask) { + (void)legs; + (void)count; + (void)bid; (void)ask; + never;/*todo*/ +} + TimeStamp MDConsumerHandle::last_market_update() const { - if (has_depth()) { - return hotd()->timestamp; + switch(_type) { + case HANDLETYPE_VOID: TimeStamp::invalid(); + case HANDLETYPE_DEPTH: + return book.hot->timestamp; + case HANDLETYPE_BBO: + return bbo.hot->timestamp; + case HANDLETYPE_SYNTHETIC: + return sane::max(*synthetic.legs, [](const MDConsumerHandle& h) { return h.last_market_update(); }); } - return hot()->timestamp; + never; } PriceSize MDConsumerHandle::get_bid(size_t depth) const { - bts_assert_d(has_depth()); bts_assert_d(depth < COCONUT_MAX_DEPTH); - return hotd()->bid(depth).load(); + switch(_type) { + case HANDLETYPE_VOID: return PriceSize::NO_MARKET; + case HANDLETYPE_DEPTH: return book.hot->bid(depth).load(); + case HANDLETYPE_BBO: + if (depth == 1) return bbo.hot->bid.load(); + return PriceSize::NO_MARKET; + case HANDLETYPE_SYNTHETIC: never; + } + never; } PriceSize MDConsumerHandle::get_ask(size_t depth) const { - bts_assert_d(has_depth()); bts_assert_d(depth < COCONUT_MAX_DEPTH); - return hotd()->ask(depth).load(); + switch(_type) { + case HANDLETYPE_VOID: return PriceSize::NO_MARKET; + case HANDLETYPE_DEPTH: return book.hot->ask(depth).load(); + case HANDLETYPE_BBO: + if (depth == 1) return bbo.hot->ask.load(); + return PriceSize::NO_MARKET; + case HANDLETYPE_SYNTHETIC: never; + } + never; } bool MDConsumerHandle::get_customer_bbo(TimeStamp& time, PriceSize& out_bid, PriceSize& out_ask) const { - if (!is_initialized()) return false; - - bool result = false; - while(_cdata->customer_bbo_updated > time || time == TimeStamp::invalid()) { - time = _cdata->customer_bbo_updated; - out_bid = _cdata->customer_bid.load(); - out_ask = _cdata->customer_ask.load(); - result = true; + switch(_type) { + case HANDLETYPE_SYNTHETIC: + case HANDLETYPE_VOID: + return false; + case HANDLETYPE_DEPTH: + case HANDLETYPE_BBO: { + const ColdEntry* pcold = (_type == HANDLETYPE_BBO) ? bbo.cdata : book.cdata; + // This can loop forever if publisher crashes with an invalid time in shm + // good thing nobody uses customer_bbo + bool result = false; + while(pcold->customer_bbo_updated > time || time == TimeStamp::invalid()) { + time = pcold->customer_bbo_updated; + out_bid = pcold->customer_bid.load(); + out_ask = pcold->customer_ask.load(); + result = true; + } + return result; + } } - return result; + never; } bool MDConsumerHandle::get_last_trade(TimeStamp& time, PriceSize& trade, datetime::LLDateTime& trade_timestamp, size_t* total_volume) const { - if (!is_initialized()) return false; - bool result = false; - while (_cdata->last_trade_updated > time) { - time = _cdata->last_trade_updated; - trade = _cdata->last_trade.load(); - trade_timestamp = _cdata->last_trade_time; - if (total_volume) { *total_volume = _cdata->total_volume; } - result = true; + switch(_type) { + case HANDLETYPE_VOID: return false; + case HANDLETYPE_DEPTH: + case HANDLETYPE_BBO: { + const ColdEntry* pcold = (_type == HANDLETYPE_BBO) ? bbo.cdata : book.cdata; + bool result = false; + while (pcold->last_trade_updated > time) { + time = pcold->last_trade_updated; + trade = pcold->last_trade.load(); + trade_timestamp = pcold->last_trade_time; + if (total_volume) { *total_volume = pcold->total_volume; } + result = true; + } + return result; + } + // Does last trade make sense for synthetic? + case HANDLETYPE_SYNTHETIC: return false; } - return result; + never; } bool is_heart_beat_ok(const ColdHeader &); bool MDConsumerHandle::is_heart_beating() const { - return _cheader && is_heart_beat_ok(*_cheader); + switch(_type) { + case HANDLETYPE_VOID: return true; + case HANDLETYPE_DEPTH: return is_heart_beat_ok(*book.cheader); + case HANDLETYPE_BBO: return is_heart_beat_ok(*bbo.cheader); + case HANDLETYPE_SYNTHETIC: return sane::true_for_all(*synthetic.legs, [](const MDConsumerHandle& h) { return h.is_heart_beating(); }); + } + never; } TimeStamp MDConsumerHandle::heartbeat() const { - return _cheader->heartbeat; + switch(_type) { + case HANDLETYPE_VOID: return TimeStamp::now(); + case HANDLETYPE_DEPTH: return book.cheader->heartbeat; + case HANDLETYPE_BBO: return bbo.cheader->heartbeat; + case HANDLETYPE_SYNTHETIC: return sane::min(*synthetic.legs, [](const MDConsumerHandle& h) { return h.heartbeat(); }); + } + never; +} +CoconutSecurityStatus MDConsumerHandle::status() const { + switch(_type) { + case HANDLETYPE_VOID: return CoconutSecurityStatus::COCONUTSECURITYSTATUS_OK; + case HANDLETYPE_DEPTH: return (CoconutSecurityStatus)book.hot->status; + case HANDLETYPE_BBO: return (CoconutSecurityStatus)bbo.hot->status; + case HANDLETYPE_SYNTHETIC: { + for (const auto& handle : *synthetic.legs) { + auto status = handle.status(); + if (status != CoconutSecurityStatus::COCONUTSECURITYSTATUS_OK) { + return status; + } + } + } + } + never; +} +uint16_t MDConsumerHandle::sequence() const { + switch(_type) { + case HANDLETYPE_VOID: return 0; + case HANDLETYPE_DEPTH: return book.hot->sequence; + case HANDLETYPE_BBO: return bbo.hot->sequence; + case HANDLETYPE_SYNTHETIC: + bts_assert(!synthetic.legs->empty()); + return sane::min_by(*synthetic.legs, [](const MDConsumerHandle& h) { return h.last_market_update(); })->sequence(); + } + never; } -CoconutSecurityStatus MDConsumerHandle::status() const { return (CoconutSecurityStatus)(has_depth() ? hotd()->status : hot()->status); } -uint16_t MDConsumerHandle::sequence() const { return has_depth() ? hotd()->sequence : hot()->sequence; } }}//namespace diff --git a/src/lib/md/MDConsumerHandle.h b/src/lib/md/MDConsumerHandle.h index 0ae80f8d7..9b5f9f1a8 100644 --- a/src/lib/md/MDConsumerHandle.h +++ b/src/lib/md/MDConsumerHandle.h @@ -11,6 +11,8 @@ namespace md { TimeStamp copy_bbo(const TimeStamp&, const SingleWriterPriceSizeStore&, const SingleWriterPriceSizeStore&, PriceSize&, PriceSize& ); TimeStamp copy_depth(const TimeStamp& time, const SingleWriterPriceSizeStore* source, size_t count, PriceSize* bid, PriceSize* ask); +class MDConsumerHandle; +TimeStamp copy_depth(const std::vector& legs, size_t count, PriceSize* bid, PriceSize* ask); enum class HandleReadResult { NO_CHANGE, @@ -28,15 +30,31 @@ public: void init(const ColdHeader&, const ColdEntry&, const void* hot); - bool is_initialized() const { return _hot != nullptr && _cdata != nullptr && _cheader != nullptr; } + bool is_initialized() const { return _type != HANDLETYPE_VOID; } + bool is_synthetic() const { return _type == HANDLETYPE_SYNTHETIC; } + // bool has_depth() const { return _type == HANDLETYPE_DEPTH || _type == HANDLETYPE_SYNTHETIC; }; bool is_heart_beating() const; TimeStamp heartbeat() const; CoconutSecurityStatus status() const; uint16_t sequence() const; - bool has_depth() const { return _cheader && _cheader->has_depth; } - security_key key() const { return has_depth() ? hotd()->key : hot()->key; } - Security get_normalized_security() const { bts_assert(is_initialized()); return convert_security(_cdata->security); } - + security_key key() const { + switch(_type) { + case HANDLETYPE_VOID: return invalid_security_key; + case HANDLETYPE_DEPTH: return book.hot->key; + case HANDLETYPE_BBO: return bbo.hot->key; + case HANDLETYPE_SYNTHETIC: return synthetic.security->id(); + } + bts_assert(false); + } + Security get_normalized_security() const { + switch(_type) { + case HANDLETYPE_VOID: bts_assert_msg(false, "Handle not initialized"); + case HANDLETYPE_DEPTH: return convert_security(book.cdata->security); + case HANDLETYPE_BBO: return convert_security(bbo.cdata->security); + case HANDLETYPE_SYNTHETIC: return *(synthetic.security); + } + bts_assert(false); + } TimeStamp last_market_update() const; @@ -44,77 +62,117 @@ public: { if (!is_initialized()) return HandleReadResult::NO_CHANGE; - if (_cheader->has_depth) { - auto* pdepth = hotd(); - if (pdepth->timestamp > time || pdepth->timestamp == TimeStamp::invalid()) { - TimeStamp temp = copy_bbo(pdepth->timestamp, pdepth->bid(0), pdepth->ask(0), bid, ask); - if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } - time = temp; - return HandleReadResult::SUCCESS; - } + switch(_type) { + case HANDLETYPE_DEPTH: + if (book.hot->timestamp > time || book.hot->timestamp == TimeStamp::invalid()) { + TimeStamp temp = copy_bbo(book.hot->timestamp, book.hot->bid(0), book.hot->ask(0), bid, ask); + if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } + time = temp; + return HandleReadResult::SUCCESS; + } + break; + case HANDLETYPE_BBO: + if (bbo.hot->timestamp > time || bbo.hot->timestamp == TimeStamp::invalid()) { + TimeStamp temp = copy_bbo(bbo.hot->timestamp, bbo.hot->bid, bbo.hot->ask, bid, ask); + if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } + time = temp; + return HandleReadResult::SUCCESS; + } + break; + case HANDLETYPE_SYNTHETIC: + return get_depth(time, 1, &bid, &ask); + case HANDLETYPE_VOID: + return HandleReadResult::NO_CHANGE; } - else { - auto* pdepth = hot(); - if (pdepth->timestamp > time || pdepth->timestamp == TimeStamp::invalid()) { - TimeStamp temp = copy_bbo(pdepth->timestamp, pdepth->bid, pdepth->ask, bid, ask); - if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } - time = temp; - return HandleReadResult::SUCCESS; - } - } - return HandleReadResult::NO_CHANGE; + bts_assert(false); } - HandleReadResult get_depth(TimeStamp& time, size_t depth, PriceSize* bids, PriceSize* asks) const { - bts_assert_d(depth > 0 && depth <= COCONUT_MAX_DEPTH); + HandleReadResult get_depth(TimeStamp& time, size_t copydepth, PriceSize* bids, PriceSize* asks) const { + bts_assert_d(copydepth > 0 && copydepth <= COCONUT_MAX_DEPTH); if (!is_initialized()) return HandleReadResult::NO_CHANGE; - if (_cheader->has_depth) { - auto* pdepth = hotd(); - if (pdepth->timestamp > time || pdepth->timestamp == TimeStamp::invalid()) { - TimeStamp temp = copy_depth(pdepth->timestamp, pdepth->book, depth, bids, asks); - if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } - time = temp; - return HandleReadResult::SUCCESS; - } + + switch(_type) { + case HANDLETYPE_VOID: return HandleReadResult::NO_CHANGE; + case HANDLETYPE_DEPTH: + if (book.hot->timestamp > time || book.hot->timestamp == TimeStamp::invalid()) { + TimeStamp temp = copy_depth(book.hot->timestamp, book.hot->book, copydepth, bids, asks); + if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } + time = temp; + return HandleReadResult::SUCCESS; + } + return HandleReadResult::NO_CHANGE; + case HANDLETYPE_BBO: + return get_bbo(time, *bids, *asks); + case HANDLETYPE_SYNTHETIC: + auto t = last_market_update(); + if (t > time || t == TimeStamp::invalid()) { + TimeStamp temp = copy_depth(*synthetic.legs, copydepth, bids, asks); + if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; } + time = temp; + return HandleReadResult::SUCCESS; + } } - else { - return get_bbo(time, *bids, *asks); - } - return HandleReadResult::NO_CHANGE; + bts_assert(false); } // Last bbo or depth update - PriceSize get_bid(size_t depth) const; - PriceSize get_ask(size_t depth) const; + PriceSize get_bid(size_t depth) const __attribute__ ((deprecated)); + PriceSize get_ask(size_t depth) const __attribute__ ((deprecated)); bool get_customer_bbo(TimeStamp& time, PriceSize& out_bid, PriceSize& out_ask) const; bool get_last_trade(TimeStamp& time, PriceSize& out_trade, datetime::LLDateTime& out_trade_timestamp, size_t* total_volume=nullptr) const; - TimeStamp misc_timestamp() const { return _cdata->misc.misc_timestamp; } - MiscData misc() const { return _cdata->misc; } + TimeStamp misc_timestamp() const; + MiscData misc() const; MDConsumerHandle& operator=(const MDConsumerHandle& other) { - _hot = other._hot; - _cdata = other._cdata; - // Ensure that the data pointers are stored before the header. - // This is primarily a compiler barrier - I don't want to change all the members to std::atomics for the cheaper compiler barrier - // since this is not called often + _type = HANDLETYPE_VOID; + switch(_type) { + case HANDLETYPE_VOID: break; + case HANDLETYPE_DEPTH: + book = other.book; + break; + case HANDLETYPE_BBO: + bbo = other.bbo; + break; + case HANDLETYPE_SYNTHETIC: + synthetic.security = other.synthetic.security; + synthetic.legs = std::make_unique>(*other.synthetic.legs); + break; + } std::atomic_thread_fence(std::memory_order_release); - _cheader = other._cheader; + _type = other._type; return *this; } private: friend class MDConsumerInst; - void set_hot(const void* hot) { _hot = hot; } - void set_cold(const ColdHeader* header, const ColdEntry* cold) { _cheader = header; _cdata = cold; } - - const HotDepthEntry* hotd() const { bts_assert_d(has_depth()); return static_cast(_hot); } - const HotEntry* hot() const { return static_cast(_hot); } - - // const void* _hot; - // const ColdEntry* _cdata; - // const ColdHeader* _cheader; + void set_hot(const void* hot) { + bts_assert(!is_synthetic()); + switch(_type) { + case HANDLETYPE_DEPTH: + book.hot = (HotDepthEntry*)hot; + break; + case HANDLETYPE_BBO: + bbo.hot = (HotEntry*)hot; + break; + default: + bts_assert(false); + } + } + void set_cold(const ColdHeader* header, const ColdEntry* cold) { + switch(_type) { + case HANDLETYPE_DEPTH: + book.cheader = header; + book.cdata = cold; + break; + case HANDLETYPE_BBO: + bbo.cheader = header; + bbo.cdata = cold; + default: + bts_assert(false); + } + } enum HandleType : uint8_t { HANDLETYPE_VOID, @@ -133,14 +191,12 @@ private: const ColdHeader* cheader; }; struct SyntheticHandle { - uint16_t num_legs; - std::unique_ptr leg1; - std::unique_ptr leg2; - std::unique_ptr> legs_n; + std::shared_ptr security; + std::unique_ptr> legs; }; HandleType _type; union { - DepthHandle depth; + DepthHandle book; BBOHandle bbo; SyntheticHandle synthetic; }; diff --git a/src/lib/md/PriceSize.h b/src/lib/md/PriceSize.h index 596e498b0..850b78804 100644 --- a/src/lib/md/PriceSize.h +++ b/src/lib/md/PriceSize.h @@ -92,10 +92,10 @@ public: return *this; } - bool operator==(const PriceSize& other) { + bool operator==(const PriceSize& other) const { return _val64 == other._val64; } - bool operator!=(const PriceSize& other) { + bool operator!=(const PriceSize& other) const { return _val64 != other._val64; } diff --git a/src/lib/md/relay/QueryServer.cpp b/src/lib/md/relay/QueryServer.cpp index 704a408bb..7b70648a6 100644 --- a/src/lib/md/relay/QueryServer.cpp +++ b/src/lib/md/relay/QueryServer.cpp @@ -233,23 +233,20 @@ struct QueryServerImpl : public IQueryServer { snapshot = true; } if (HASFLAG(flags, MDRequest::BBO) || HASFLAG(flags, MDRequest::DEPTH)) { - PriceSize bid, ask; + std::array bids, asks; TimeStamp time(snapshot ? 0 : chan.time()); - auto result = handle.get_bbo(time, bid, ask); + size_t copy_depth = HASFLAG(flags, MDRequest::DEPTH) ? COCONUT_MAX_DEPTH : 1; + auto result = handle.get_depth(time, copy_depth, bids.data(), asks.data()); if (result == HandleReadResult::SUCCESS) { auto* p = pupd->mutable_book(); - convert(bid, p->add_bids()); - convert(ask, p->add_asks()); - if (HASFLAG(flags, MDRequest::DEPTH) && handle.has_depth()) { - for (size_t x = 1; x < COCONUT_MAX_DEPTH; ++x) { - PriceSize bid = handle.get_bid(x); - if (bid != PriceSize::NO_MARKET) { - convert(bid, p->add_bids()); - } - PriceSize ask = handle.get_ask(x); - if (ask != PriceSize::NO_MARKET) { - convert(ask, p->add_asks()); - } + for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) { + PriceSize b = bids[x]; + if (b != PriceSize::NO_MARKET) { + convert(b, p->add_bids()); + } + PriceSize a = asks[x]; + if (a != PriceSize::NO_MARKET) { + convert(a, p->add_asks()); } } } diff --git a/src/lib/utils/TimeStamp.h b/src/lib/utils/TimeStamp.h index af56a5523..e6e7db470 100644 --- a/src/lib/utils/TimeStamp.h +++ b/src/lib/utils/TimeStamp.h @@ -20,10 +20,10 @@ class TimeStamp { public: TimeStamp() : _time(0) {} - explicit TimeStamp (uint64_t time) : _time (time) {} + explicit constexpr TimeStamp (uint64_t time) : _time (time) {} static TimeStamp now(); - static TimeStamp invalid() { return TimeStamp(std::numeric_limits().max()); } + constexpr static TimeStamp invalid() { return TimeStamp(std::numeric_limits().max()); } class Interval { public: @@ -168,3 +168,11 @@ inline bool operator <= ( const TimeStamp& t1, const TimeStamp& t2) { } }}//namespace + +namespace std { + template <> + struct numeric_limits { + static bts::utils::TimeStamp min() { return bts::utils::TimeStamp(); } + static bts::utils::TimeStamp max() { return bts::utils::TimeStamp(std::numeric_limits().max()); } + }; +} diff --git a/src/lib/utils/sane_algorithm.h b/src/lib/utils/sane_algorithm.h new file mode 100644 index 000000000..dcf671c0a --- /dev/null +++ b/src/lib/utils/sane_algorithm.h @@ -0,0 +1,49 @@ +#pragma once + +namespace bts { +namespace sane { + +template +T max(const TCollection& collection, TGet get) { + auto max = std::numeric_limits::min(); + for (const auto& item : collection) { + if (max < get(item)) { + max = get(item); + } + } + return max; +} + +template +T min(const TCollection& collection, TGet get) { + auto min = std::numeric_limits::max(); + for (const auto& item : collection) { + if (min > get(item)) { + min = get(item); + } + } + return min; +} + +template +const T* min_by(const TCollection& collection, TGet get) { + auto min = std::numeric_limits::max(); + const T* pitem = nullptr; + for (const auto& item : collection) { + if (min > get(item)) { + min = get(item); + pitem = &item; + } + } + return pitem; +} + +template +bool true_for_all(const TCollection& collection, TWhat what) { + for (const auto& item : collection) { + if (!what(item)) return false; + } + return true; +} + +}}//namespace \ No newline at end of file diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp index c38942a52..669eaf86f 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.cpp @@ -81,14 +81,14 @@ MDP3Listener::MDP3Listener( CMESecurityIndex& index, MDProducer& producer, SecurityIDRecorder& recorder, - co2::SettlementRecorder& settles, + co2::SettlementRecorder&, std::vector channels) : _index(index), _producer(producer), _packet_arrived(), _latest_sequence(0), _security_recorder(recorder), - _settlement_recorder(settles), + // _settlement_recorder(settles), _symbol_filter_storage(), _symbol_filter(), _channels(channels), diff --git a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h index 979091a6b..68f2149fa 100644 --- a/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h +++ b/src/server/sparkd/md/platforms/mdp3_onixs/MDP3Listener.h @@ -119,7 +119,7 @@ private: utils::TimeStamp _packet_arrived; uint32_t _latest_sequence; SecurityIDRecorder& _security_recorder; - co2::SettlementRecorder& _settlement_recorder; + // co2::SettlementRecorder& _settlement_recorder; std::vector _symbol_filter_storage; std::set _symbol_filter; std::vector _channels;