From 48372f2189f04c7b64fa86cec65d339d83cf626c Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Wed, 8 Jan 2020 16:02:58 -0500 Subject: [PATCH] Refactored Gateway/IVenue protocol to always generate orders --- src/lib/orderman/doc/orderman_notes.org | 4 + .../orderman_v2/actors/gateway/Gateway.cpp | 148 ++++------ src/lib/orderman_v2/actors/gateway/Gateway.h | 2 +- src/lib/orderman_v2/actors/gateway/IVenue.h | 41 ++- .../actors/gateway/fix/FIXVenue.cpp | 2 +- .../orderman_v2/actors/gateway/fix/FIXVenue.h | 21 +- src/lib/orderman_v2/common.h | 6 + src/lib/orderman_v2/doc/orderman_v2_notes.org | 5 + src/lib/orderman_v2/events/events.h | 4 + src/lib/orderman_v2/fix/Fields.h | 41 ++- .../sparkd/cmds/benchmarks/fix_gen_bm.cpp | 269 +++++++++++------- 11 files changed, 313 insertions(+), 230 deletions(-) diff --git a/src/lib/orderman/doc/orderman_notes.org b/src/lib/orderman/doc/orderman_notes.org index 3fcb36ceb..40f5ff6b9 100644 --- a/src/lib/orderman/doc/orderman_notes.org +++ b/src/lib/orderman/doc/orderman_notes.org @@ -42,18 +42,22 @@ BeginString = FIX.4.2 8=FIX.4.2 BodyLength = 271 9=271 MsgType = NewOrderSingle 35=D MsgSeqNum = 8026 34=8026 + SenderCompID = DT9238N 49=DT9238N SenderSubID = ROR 50=ROR SendingTime = 20161102-20:36:47.620 52=20161102-20:36:47.620 TargetCompID = CME 56=CME + TargetSubID = 64 57=64 SenderLocationID = US,IL 142=US,IL LastMsgSeqNumProcessed = 7374 369=7374 Account = X8CM1 1=X8CM1 + ClOrdID = 2KjMkw-1C22h9-001v8Q 11=2KjMkw-1C22h9-001v8Q HandlInst = 1 21=1 OrderQty = 1 38=1 OrdType = 2 40=2 + Price = 209075 44=209075 SecurityID = 2928 48=2928 Side = 1 54=1 diff --git a/src/lib/orderman_v2/actors/gateway/Gateway.cpp b/src/lib/orderman_v2/actors/gateway/Gateway.cpp index 9fc79192d..7e67824c6 100644 --- a/src/lib/orderman_v2/actors/gateway/Gateway.cpp +++ b/src/lib/orderman_v2/actors/gateway/Gateway.cpp @@ -26,26 +26,15 @@ template GW_Op_Ack make_ack(Req const &r) { struct Gateway::OrderRec { OrderRec() = default; OrderRec(OM_ID om_id, GW_BoundOrder const &order) - : order(order), cancel_replace(nullptr), cancel(nullptr), - originator(om_id) {} + : order(order), originator(om_id), last_ack(), routing_record(nullptr) {} GW_BoundOrder order; - ICancelReplaceMsg *cancel_replace; - ICancelMsg *cancel; OM_ID originator; GW_Op_Ack last_ack; - - void clear_msgs() { - cancel_replace = nullptr; - cancel = nullptr; - } + api::orderman::RoutingRecord const *routing_record; - bool is_routable() const { return cancel_replace != nullptr; } - - void set_msgs(ICancelReplaceMsg const &cr, ICancelMsg &can) { - cancel_replace = &cr.clone(); - cancel = &can.clone(); - } + bool is_routable() const { return routing_record != nullptr; } + void mark_non_routable() { routing_record = nullptr; } }; class Gateway::OrderMap { @@ -56,6 +45,7 @@ class Gateway::OrderMap { return _store.make_and_index(bo.key.value, om_id, bo); } void remove(OrderKey k) { _store.remove(k.value); } + std::vector get_all() { return _store.get_all(); } private: @@ -71,27 +61,29 @@ enum class Gateway::State { }; struct Gateway::Hot { - using MsgTemplatesMap = - std::unordered_map; + using RoutingRecordsMap = + std::unordered_map; Hot(IVenue &v, ILimiter &l) : state(State::W_BOOTSTRAP), venue(v), limiter(l), working_orders(), - pending_msgs(), msg_templates() {} + routing_records() {} State state; IVenue &venue; ILimiter &limiter; - // TODO: Separate ourders that cannot be routed from working orders to avoid - // the if is_routable() on order events + + // TODO: Separate orders that cannot be routed from working orders + // to avoid the if is_routable() on order events OrderMap working_orders; - boost::container::small_vector pending_msgs; - MsgTemplatesMap msg_templates; + RoutingRecordsMap routing_records; }; struct Gateway::Cold { Cold(IRouter &r, IClock &c, GatewayCfg const &cfg) : router(r), clock(c), admin_data(), cfg(cfg), start_of_pause(), - current_exponential_backoff(0), bootstrap_orders(), interned_strings () {} + current_exponential_backoff(0), bootstrap_orders(), + interned_strings() {} IRouter &router; IClock &clock; @@ -166,7 +158,7 @@ void Gateway::on_om_bootstrap_orders(OM_BootstrapOrders const &orders) { void Gateway::on_gw_ops(const GW_Op_Batch &op_batch) { op_batch.visit(*this); - send_pending_msgs(); + me->hot.venue.send_queued_msgs(); } void Gateway::on_account_pnl(const AccountPnLUpdate &pnl) { @@ -184,13 +176,14 @@ void Gateway::on_excess_liquidity(const ExcessLiquidityUpdate &l) { void Gateway::on_admin_data(AdminDataUpdate const &admin) { me->hot.limiter.update(*me->cold.admin_data); me->cold.router.update(*me->cold.admin_data); + me->hot.routing_records.clear(); switch (me->hot.state) { case State::W_BOOTSTRAP: me->cold.admin_data = admin.admin_data; break; default: - rebuild_msg_templates(); + rebuild_routing_records(); } } @@ -202,7 +195,7 @@ void Gateway::on_venue_login_success(std::vector const &recovered) { bts_assert(me->hot.state == State::W_LOGON); me->cold.current_exponential_backoff = 0; transition(State::READY); - for (auto const &er : recovered) + for (auto const &er : recovered) on_venue_exe_report(er); } @@ -210,10 +203,9 @@ void Gateway::on_venue_login_failure() { bts_assert(me->hot.state == State::W_LOGON); if (in_active_schedule()) { - me->cold.current_exponential_backoff ++; + me->cold.current_exponential_backoff++; transition(State::PAUSED); - } - else { + } else { me->cold.current_exponential_backoff = 0; transition(State::W_SCHEDULE); } @@ -248,8 +240,8 @@ void Gateway::on_venue_exe_report(const ExecReport &report) { on_order_report(make_order_report(r)); } else if (report.status == OrderStatus::WORKING) { auto sec = me->hot.venue.resolve_security(report); - if (sec) - on_unknown_native_order_update ( {sec,me->cold.cfg.my_id,report}); + if (sec) + on_unknown_native_order_update({sec, me->cold.cfg.my_id, report}); } } @@ -289,20 +281,12 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCreate &create) { case State::READY: { // Validate route - auto &msg_templates = me->hot.msg_templates; - INewOrderSingleMsg *new_order_single; - auto it = msg_templates.find(order.uav); - if (it == msg_templates.end()) { + auto it = me->hot.routing_records.find(order.uav); + if (it == me->hot.routing_records.end()) { ack.result = GW_OP_Result::ERROR_NO_ROUTE; break; - } - - // TODO: Replace the chained ifs with the accumulate error flags - // and check them once. - - // Set msgs in order record - new_order_single = &it->second.new_order->clone(); - gw_rec->set_msgs(*it->second.cancel_replace, *it->second.cancel); + } else + gw_rec->routing_record = &it->second; // Validate risk limit auto lqt = me->hot.limiter.can_send_new_order( @@ -319,10 +303,12 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderCreate &create) { break; } - // All is good. Copy the order params to the order message and - // queue the message for sending. - new_order_single->set_order_params(order); - me->hot.pending_msgs.push_back(new_order_single); + // TODO: Replace the chained ifs with accumulated error flags + // and check them once, to avoid pressure on the branch + // predictor. + + // All is good. Queue for send. + me->hot.venue.queue_send(gw_rec->order, *gw_rec->routing_record); // For speed we do not send acks for the operation until we hear // from the exchange. @@ -359,12 +345,12 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderBind &bind) { auto &ack = gw_rec->last_ack; ack = make_ack(bind); - // Find the message templates for this UAV and clone them for this - // order - auto &msg_templates = me->hot.msg_templates; - auto it = msg_templates.find(order.uav); - if (it == msg_templates.end()) + // Find the routing record for this order + auto it = me->hot.routing_records.find(order.uav); + if (it == me->hot.routing_records.end()) ack.result = GW_OP_Result::ERROR_NO_ROUTE; + else + gw_rec->routing_record = &it->second; // In this case we have to send a reply to the bind operation // because there is no guarantee that we will be able to piggy back @@ -415,8 +401,7 @@ void Gateway::on_gw_op(OM_ID, const GW_OrderQuickUpdate &quick_update) { } order.price = quick_update.price; order.quantity = quick_update.quantity; - gw_rec->cancel_replace->update(order); - me->hot.pending_msgs.push_back(gw_rec->cancel_replace); + me->hot.venue.queue_update(gw_rec->order, *gw_rec->routing_record); // We do not send out a order report for performance // reasons. Instead we delay the report until the exchange @@ -437,9 +422,9 @@ void Gateway::on_gw_op(OM_ID, const GW_OrderQuickUpdate &quick_update) { } // The difference between a full update and a quick update is that in -// a full update we allow for the account / user to change. In this -// case we have to re-create the messages for cancel and update since -// these messages embed in them account and user specific information. +// a full update we allow for any change including the account/user +// associated with the order. This could change the routing of the +// order so we need to retrieve a (possibly new) routing record. void Gateway::on_gw_op(OM_ID om_id, const GW_OrderFullUpdate &full_update) { auto gw_rec = me->hot.working_orders.lookup(full_update.order_key); auto &ack = gw_rec->last_ack; @@ -461,13 +446,13 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderFullUpdate &full_update) { } auto &order = gw_rec->order; - auto &msg_templates = me->hot.msg_templates; - auto it = msg_templates.find(order.uav); - if (it == msg_templates.end()) { + auto it = me->hot.routing_records.find(order.uav); + if (it == me->hot.routing_records.end()) { + gw_rec->mark_non_routable(); ack.result = GW_OP_Result::ERROR_NO_ROUTE; - gw_rec->clear_msgs(); break; - } + } else + gw_rec->routing_record = &it->second; // Update existing record with the non quick parts of the full // order update. There is a bit of inconsistency here. We are @@ -475,8 +460,8 @@ void Gateway::on_gw_op(OM_ID om_id, const GW_OrderFullUpdate &full_update) { // fields below, even though the quick update might later fail // to set the quantity and price. This could leave the order in // a half-updated half not-updated state. This should be rare - // and benign enough so we live with it. - gw_rec->set_msgs(*it->second.cancel_replace, *it->second.cancel); + // and benign enough so we live with it to avoid duplicating the + // update logic from quick update here. gw_rec->order.uav = full_update.uav; gw_rec->order.moi = full_update.moi; gw_rec->order.tif = full_update.tif; @@ -522,10 +507,7 @@ void Gateway::on_gw_op(OM_ID, const GW_OrderCancel &cancel) { break; } - auto &order = gw_rec->order; - gw_rec->cancel->update(order); - me->hot.pending_msgs.push_back(gw_rec->cancel); - + me->hot.venue.queue_cancel(gw_rec->order, *gw_rec->routing_record); // We do not send out a order report yet for performance // reasons. Instead we delay the report until the exchange // reponds to our cancel @@ -544,18 +526,10 @@ void Gateway::on_gw_op(OM_ID, const GW_OrderCancel &cancel) { on_order_report(make_order_report(gw_rec)); } -const char* Gateway::intern_string(std::string const&str) { +const char *Gateway::intern_string(std::string const &str) { return me->cold.interned_strings.insert(str).first->data(); } -void Gateway::send_pending_msgs() { - auto &pending = me->hot.pending_msgs; - // TODO: Need to make sure that there is no danger of reentrancy - // here, in the case that venue calls us back from the send call - me->hot.venue.send(pending.data(), (pending.data() + pending.size())); - pending.clear(); -} - bool Gateway::has_bootstrap_data() const { return me->cold.bootstrap_orders.size() == me->cold.cfg.om_count && me->cold.admin_data != nullptr; @@ -573,29 +547,27 @@ void Gateway::transition(State state) { me->hot.state = state; } -void Gateway::rebuild_msg_templates() { +void Gateway::rebuild_routing_records() { // First just rebuild our template cache - me->hot.msg_templates.clear(); + me->hot.routing_records.clear(); + auto all_uav = me->hot.limiter.get_all_user_account_venue(); for (auto const &uav : all_uav) { auto rq = me->cold.router.get_route(uav); - if (rq.has_route) { - me->hot.msg_templates[uav] = - me->hot.venue.get_msg_templates(rq.routing_record); - } + if (rq.has_route) + me->hot.routing_records[uav] = rq.routing_record; } // Now go through every order and update its messages auto working = me->hot.working_orders.get_all(); for (auto &orec : working) { - orec->clear_msgs(); - if (auto it = me->hot.msg_templates.find(orec->order.uav); - it != me->hot.msg_templates.end()) { - orec->set_msgs(*it->second.cancel_replace, *it->second.cancel); + if (auto it = me->hot.routing_records.find(orec->order.uav); + it != me->hot.routing_records.end()) { + orec->routing_record = &it->second; } else { LOG(OrderMan, Error, "Order {} can no longer be routed. {}", orec->order.key, orec->order.uav.to_str()); - orec->clear_msgs(); + orec->mark_non_routable(); } } } diff --git a/src/lib/orderman_v2/actors/gateway/Gateway.h b/src/lib/orderman_v2/actors/gateway/Gateway.h index 55f40fb05..cb106079f 100644 --- a/src/lib/orderman_v2/actors/gateway/Gateway.h +++ b/src/lib/orderman_v2/actors/gateway/Gateway.h @@ -68,7 +68,7 @@ class Gateway : public Actor, ISlowWorker, IGW_Op_Visitor, IVenueObserver { bool has_bootstrap_data() const; bool in_active_schedule() const; void transition(State); - void rebuild_msg_templates(); + void rebuild_routing_records(); void setup_bootstrap_orders(); GW_OrderReport make_order_report(OrderRec *); static log::CharPtrWrapper state_name(State); diff --git a/src/lib/orderman_v2/actors/gateway/IVenue.h b/src/lib/orderman_v2/actors/gateway/IVenue.h index da80156bf..c97b630de 100644 --- a/src/lib/orderman_v2/actors/gateway/IVenue.h +++ b/src/lib/orderman_v2/actors/gateway/IVenue.h @@ -5,8 +5,10 @@ namespace bts { namespace api { class Security; -namespace orderman { class RoutingRecord;} +namespace orderman { +class RoutingRecord; } +} // namespace api namespace orderman_v2 { @@ -17,19 +19,13 @@ class IVenueMsg; class IVenueObserver; class ExecReport; class VenueCheckpoint; - -struct MsgTemplates { - INewOrderSingleMsg* new_order; - ICancelReplaceMsg* cancel_replace; - ICancelMsg* cancel; -}; +struct GW_BoundOrder; struct SessionSpan { datetime::PTime start; datetime::PTime stop; }; - class IVenue { public: // call this before any of the below @@ -40,35 +36,34 @@ class IVenue { virtual SessionSpan get_current_session_span() = 0; // Logon to the venue. This can only be called if the caller knows that the - // venue is in the idle state. + // venue is in the idle state. virtual void login() = 0; - // Build new order, cancel replace and cancel messages appropriate - // for this venue and using the information in the passed routing - // record - virtual MsgTemplates - get_msg_templates(api::orderman::RoutingRecord const &) = 0; - // Check to to see if the venue is currently able to receive new // messages without breaching messaging limits virtual bool under_messaging_limit() = 0; - // Send the passed messages to the venue. It is only legal to call - // this if the venue is idle or ready. - virtual void send(IVenueMsg**begin, IVenueMsg**end) = 0; + virtual void queue_send(GW_BoundOrder const &, + api::orderman::RoutingRecord const &) = 0; + virtual void queue_update(GW_BoundOrder const &, + api::orderman::RoutingRecord const &) = 0; + virtual void queue_cancel(GW_BoundOrder const &, + api::orderman::RoutingRecord const &) = 0; + virtual void send_queued_msgs() = 0; // Resolved the security in the execution report. This is only // needed for disaster recovery. Null should be returned in case of // failure. - virtual api::Security const * resolve_security (ExecReport const&) = 0; + virtual api::Security const *resolve_security(ExecReport const &) = 0; - // Tell the venue what was the last message which had its effects - // commited to disk - virtual void set_checkpoint(VenueCheckpoint const&) = 0; + // Tell the venue the last message that had its effects commited to + // disk + virtual void set_checkpoint(VenueCheckpoint const &) = 0; // call this periodically to allow the venue to process its // internal timers virtual void do_work() = 0; }; -}} +} // namespace orderman_v2 +} // namespace bts diff --git a/src/lib/orderman_v2/actors/gateway/fix/FIXVenue.cpp b/src/lib/orderman_v2/actors/gateway/fix/FIXVenue.cpp index 9cb4a3865..f3388bc1c 100644 --- a/src/lib/orderman_v2/actors/gateway/fix/FIXVenue.cpp +++ b/src/lib/orderman_v2/actors/gateway/fix/FIXVenue.cpp @@ -1,4 +1,4 @@ -// #include "FIXVenue.h" +#include "FIXVenue.h" // #include "VenueCheckpoint.h" // #include "lib/jetfix/store/session_name.h" // #include "lib/orderman_v2/common.h" diff --git a/src/lib/orderman_v2/actors/gateway/fix/FIXVenue.h b/src/lib/orderman_v2/actors/gateway/fix/FIXVenue.h index 097bfaa65..7a0d015e9 100644 --- a/src/lib/orderman_v2/actors/gateway/fix/FIXVenue.h +++ b/src/lib/orderman_v2/actors/gateway/fix/FIXVenue.h @@ -22,19 +22,23 @@ class FIXVenue : public IVenue, public IConnectionHandler { FIXVenue(config::JetFIX::Connection const &cfg, VenueCheckpoint const &initial_checkpoint, IClock &, IFIXStore &); ~FIXVenue(); - + // IVenue void set_observer(IVenueObserver &) override; SessionSpan get_current_session_span() override; void login() override; - MsgTemplates - get_msg_templates(api::orderman::RoutingRecord const &) override; bool under_messaging_limit() override; - void send(IVenueMsg **begin, IVenueMsg **end) override; + void queue_send(GW_BoundOrder const &, + api::orderman::RoutingRecord const &) override; + void queue_update(GW_BoundOrder const &, + api::orderman::RoutingRecord const &) override; + void queue_cancel(GW_BoundOrder const &, + api::orderman::RoutingRecord const &) override; + void send_queued_msgs() override; api::Security const *resolve_security(ExecReport const &) override; void set_checkpoint(VenueCheckpoint const &) override; - void do_work () override; - + void do_work() override; + // IConnectionHandler void init_handler(IConnectionController &) override; void on_connect_success() override; @@ -42,8 +46,7 @@ class FIXVenue : public IVenue, public IConnectionHandler { void on_disconnect() override; void on_data(std::string_view const &data) override; -protected: - + protected: private: struct PImpl; struct Hot; @@ -53,7 +56,7 @@ protected: void internal_send(); void transition(State); - void start_new_session(std::string const&session_name); + void start_new_session(std::string const &session_name); }; } // namespace orderman_v2 diff --git a/src/lib/orderman_v2/common.h b/src/lib/orderman_v2/common.h index fedab2cf9..9d6c715f8 100644 --- a/src/lib/orderman_v2/common.h +++ b/src/lib/orderman_v2/common.h @@ -45,6 +45,11 @@ std::ostream &operator<<(std::ostream &s, Value const &v) { return s; } +struct ExchangeCorID { + std::array channel; + uint32_t sn; +}; + using BookSide = std::array; using OrderKey = Value; using Price = Value; @@ -57,6 +62,7 @@ using UserID = Value; using AccountID = Value; using SecurityKey = Value; using Side = Value; +using OrderType = Value; using ManualOrderIndicator = Value; using TimeInForce = Value; diff --git a/src/lib/orderman_v2/doc/orderman_v2_notes.org b/src/lib/orderman_v2/doc/orderman_v2_notes.org index dae7ef2bf..ce8cc061e 100644 --- a/src/lib/orderman_v2/doc/orderman_v2_notes.org +++ b/src/lib/orderman_v2/doc/orderman_v2_notes.org @@ -77,22 +77,27 @@ BeginString = FIX.4.2 8=FIX.4.2 BodyLength = 000257 9=000257 MsgType = NewOrderSingle 35=D MsgSeqNum = 000000113 34=000000113 + LastMsgSeqNumProcessed = 000000110 369=000000110 SendingTime = 20190804-22:11:06.602 52=20190804-22:11:06.602 SenderCompID = DE7534N 49=DE7534N TargetCompID = CME 56=CME + TargetSubID = 80 57=80 SenderSubID = DO3BTSJGR 50=DO3BTSJGR SenderLocationID = US,IL 142=US,IL Account = CLX60635 1=CLX60635 + Side = 2 54=2 TimeInForce = 0 59=0 1028 = N 1028=N CustomerOrFirm = 0 204=0 + 9702 = 1 9702=1 SecurityDesc = HOU9-HOX9 107=HOU9-HOX9 ClOrdID = 00000101730000432660 11=00000101730000432660 7928 = 6935102 7928=6935102 + TransactTime = 20190804-22:11:06.592 60=20190804-22:11:06.592 OrderQty = 000001 38=000001 OrdType = 2 40=2 diff --git a/src/lib/orderman_v2/events/events.h b/src/lib/orderman_v2/events/events.h index fab0642e8..9a4ca49a6 100644 --- a/src/lib/orderman_v2/events/events.h +++ b/src/lib/orderman_v2/events/events.h @@ -145,6 +145,10 @@ struct GW_BoundOrder { ManualOrderIndicator moi; TimeInForce tif; Side side; + OrderType order_type; + ExchangeCorID corid; + Price stop_price; + Quantity display_quantity; }; struct GW_OrderCreate { diff --git a/src/lib/orderman_v2/fix/Fields.h b/src/lib/orderman_v2/fix/Fields.h index 533e96c6c..f9c4ce887 100644 --- a/src/lib/orderman_v2/fix/Fields.h +++ b/src/lib/orderman_v2/fix/Fields.h @@ -1,6 +1,7 @@ #pragma once #include "FieldsHelpers.h" +#include "lib/jetfix/codec/i32_toa.h" #include "lib/utils/bts_assert.h" #include #include @@ -27,6 +28,8 @@ using CtiCode = BaseFIXField<9702, 1>; using SelfMatchPreventionID = BaseFIXField<7928, 12>; using OrdType = BaseFIXField<40, 1>; +using HandlInst = BaseFIXField<21, 1>; +using CustOrderHandlingInst = BaseFIXField<1031, 1>; using TransactTime = BaseFIXField<60, 21>; using ClOrdID = BaseFIXField<11, 20>; using Side = BaseFIXField<54, 1>; @@ -53,7 +56,7 @@ struct Encoding { } }; -template class FIXFragment { +template class FIXFragment { public: FIXFragment() : sealed_crc(0), hole_crc(0), current_offset(0), @@ -76,6 +79,23 @@ template class FIXFragment { sealed_crc += F::tag_info.crc + 1; } + template void add_value(int32_t v, int pad = 0) { + static_assert(find_type() >= 0); + bts_assert_d(current_offset + F::max_sz < buffer.size()); + bts_assert_d(!is_sealed); + + memcpy(buffer.data() + current_offset, F::tag_info.str, F::tag_info.sz); + current_offset += F::tag_info.sz; + auto start_of_text = current_offset; + auto len_of_text = + i32_toa(v, (char *)(buffer.data() + current_offset), pad); + current_offset += len_of_text; + buffer[current_offset] = 1; + current_offset++; + sealed_crc += + sum_bytes((char *)(buffer.data() + start_of_text), len_of_text); + sealed_crc += F::tag_info.crc + 1; + } struct Hole { size_t offset; size_t size; @@ -118,6 +138,12 @@ template class FIXFragment { memcpy(buffer.data() + h.offset, dt.data(), dt.size()); } + void fill_hole(Hole const &h, int32_t v) { + bts_assert_d(is_sealed); + i32_toa(v, (char *)(buffer.data() + h.offset), h.size); + hole_crc += sum_bytes((char*)(buffer.data() + h.offset), h.size); + } + void fill_hole(Hole const &h, int crc, std::string_view const &dt) { bts_assert_d(is_sealed); bts_assert_d(dt.size() == h.size); @@ -137,7 +163,18 @@ template class FIXFragment { sealed_crc + hole_crc}; } - FIXFragment& operator=(const FIXFragment&src) = default; + FIXFragment &operator=(const FIXFragment &src) = default; + + void reset() { + current_offset = 0; + sealed_crc = 0; + hole_crc = 0; + is_sealed = false; + } + + uint8_t crc() { return (sealed_crc + hole_crc) % 256; } + + uint32_t size() { return current_offset; } private: uint64_t sealed_crc; diff --git a/src/server/sparkd/cmds/benchmarks/fix_gen_bm.cpp b/src/server/sparkd/cmds/benchmarks/fix_gen_bm.cpp index e3402432f..659167b42 100644 --- a/src/server/sparkd/cmds/benchmarks/fix_gen_bm.cpp +++ b/src/server/sparkd/cmds/benchmarks/fix_gen_bm.cpp @@ -2,6 +2,7 @@ #include "lib/api/services/OrderService.pb.h" #include "lib/domain_model/Security.pb.h" +#include "lib/jetfix/codec/FIXEncodingStream.h" #include "lib/jetfix/codec/FIXFragment.h" #include "lib/jetfix/codec/FieldDefinitions.h" #include "lib/jetfix/codec/f_toa.h" @@ -41,19 +42,65 @@ inline int my_u32_toa(uint32_t v, unsigned char *buffer, int pad) { } } -using NewOrderSingle = FIXFragment< - fix::BeginString, fix::BodyLength, fix::MessageType, fix::MsgSeqNum, - fix::TargetCompID, fix::TargetSubID, fix::SenderCompID, - fix::SenderLocationID, fix::SenderSubID, fix::SendingTime, fix::Account, - fix::CtiCode, fix::ManualOrderIndicator, fix::CustomerOrFirm, - fix::SelfMatchPreventionID, fix::LastMsgSeqNumProcessed, fix::Checksum, - fix::TransactTime, fix::SecurityDesc, fix::Side, fix::TimeInForce, - fix::ClOrdID, fix::OrderQty, fix::OrdType, fix::Price>; +using NewOrderSingle = + FIXFragment; static void new_order_single_hybrid(benchmark::State &state) { NewOrderSingle no; using namespace fix; + for (auto _ : state) { + no.reset(); + + no.add_value("FIX.4.2"); + auto body_length = no.add_hole(); + no.add_value("D"); + no.add_value(12345); + + no.add_value("1"); + no.add_value("20190804-22:11:06.602"); + no.add_value("DE7534N"); + no.add_value("CME"); + + no.add_value("80"); + no.add_value("DO3BTSJGR"); + no.add_value("US,IL"); + no.add_value("CLX60635"); + + no.add_value("1"); + no.add_value("1"); + no.add_value("1"); + no.add_value("1"); + + no.add_value("1"); + no.add_value("CH01"); + no.add_value(88412); + no.add_value("6935102"); + + no.add_value("20190804-22:11:06.602"); + no.add_value(12); + no.add_value("2"); + no.add_value("17"); + + no.add_value(123); + no.fill_hole(body_length,232); + } + std::cout << "fix: " << no.encode().human_str() << " crc: " << (int) no.crc() + << " sz: " << no.size() << "\n"; +} +BENCHMARK(new_order_single_hybrid); + +static void new_order_single_no_jetfixt(benchmark::State &state) { + NewOrderSingle no; + using namespace fix; + no.add_value("FIX.4.2"); auto body_length = no.add_hole(); no.add_value("D"); @@ -109,77 +156,88 @@ static void new_order_single_hybrid(benchmark::State &state) { } std::cout << no.encode().human_str() << "\n"; } -BENCHMARK(new_order_single_hybrid); +BENCHMARK(new_order_single_no_jetfixt); -static void fragment_copy(benchmark::State &state) { - NewOrderSingle no; - using namespace fix; +// static void fragment_copy(benchmark::State &state) { +// NewOrderSingle no; +// using namespace fix; - no.add_value("FIX.4.2"); - no.add_hole(); - no.add_value("D"); - no.add_hole(); - no.add_hole(); - no.add_hole(); +// no.add_value("FIX.4.2"); +// no.add_hole(); +// no.add_value("D"); +// no.add_hole(); +// no.add_hole(); +// no.add_hole(); - no.add_value("DE7534N"); - no.add_value("CME"); - no.add_value("80"); - no.add_value("US,IL"); - no.add_value("6935102"); - no.add_value("DO3BTSJGR"); - no.add_value("CLX60635"); - no.add_value("1"); - no.add_value("1"); - no.add_value("2"); +// no.add_value("DE7534N"); +// no.add_value("CME"); +// no.add_value("80"); +// no.add_value("US,IL"); +// no.add_value("6935102"); +// no.add_value("DO3BTSJGR"); +// no.add_value("CLX60635"); +// no.add_value("1"); +// no.add_value("1"); +// no.add_value("2"); - no.add_hole(); - no.add_hole(9); - no.add_hole(); - no.add_hole(); - no.add_hole(); - no.add_hole(); - no.add_hole(17); - no.add_hole(9); - no.add_hole(); +// no.add_hole(); +// no.add_hole(9); +// no.add_hole(); +// no.add_hole(); +// no.add_hole(); +// no.add_hole(); +// no.add_hole(17); +// no.add_hole(9); +// no.add_hole(); - no.seal(); +// no.seal(); - NewOrderSingle no2; - for (auto _ : state) { - benchmark::DoNotOptimize(no2 = no); - } -} -BENCHMARK(fragment_copy); +// NewOrderSingle no2; +// for (auto _ : state) { +// benchmark::DoNotOptimize(no2 = no); +// } +// } +// BENCHMARK(fragment_copy); static void jetfix_gen_new_order(benchmark::State &state) { - jetfix::FIXFragment frag; + config::JetFIX::Connection cfg; + cfg.set_socket_connect_host_ip("69.50.112.199"); + cfg.set_socket_connect_port(61922); + cfg.set_sender_compid("MC5004N"); + cfg.set_target_compid("CME"); + cfg.set_target_subid("99"); + cfg.set_cme_market_segment_id(99); + cfg.set_start_time("00:30:00"); + cfg.set_end_time("23:30:00"); + cfg.set_start_day("sunday"); + cfg.set_end_day("friday"); + cfg.set_sender_compid("BTS"); + cfg.set_raw_data("S4PMA"); + cfg.set_auto_fix_seq_num(true); + jetfix::FIXEncodingStream stream(cfg); + auto ts = std::string("20121204-14:44:02.000"); for (auto _ : state) { - frag.reset(); - frag.add("pedro"); - frag.add("NC,US"); - frag.add("pp_account"); - frag.add(api::SIDE_BUY); - //frag.add(api::TIME_IN_FORCE_DAY); + // stream.new_msg(jetfix::MessageTypes::NewOrderSingle); + // frag.add("pedro"); + // frag.add("NC,US"); + // frag.add("pp_account"); + // frag.add(api::SIDE_BUY); + // frag.add(api::orderman::TIME_IN_FORCE_DAY); + // frag.add(api::orderman::MANUAL_ORDER_INDICATOR_NO); + // frag.add(api::orderman::CUSTOMER_OR_FIRM_CUSTOMER); + // frag.add(api::orderman::CTI_CODE_UNDEFINED); + // frag.add("CH07"); + // frag.add("1234567890"); + // frag.add("1234"); + // frag.add("20121204-14:44:02.000"); + // frag.add(123); + // frag.add(12.0,7,17); + // frag.add(api::orderman::NATIVE_ORDER_TYPE_LIMIT); + // stream.close(); + // stream.stamp(1,1,ts); + stream.reset(); } - // frag.add(def.time_in_force()); - // frag.add(def.manual_order_indicator()); - // frag.add(rr.customer_or_firm()); - // frag.add(rr.cti_code()); - // frag.add(sec.glbx().security_desc()); - // frag.add(clordid); - // frag.add(rr.smp_id()); - // frag.add( - // rr.smp_instruction()); - // } - - // me->create_transact_time_bm = frag.add( - // datetime::FIX_timestamp(datetime::now())); - // me->create_quantity_bm = - // frag.add(0, quantity_sz()); - // me->create_corid_bm = - // frag.add(std::string(corid_sz(), ' ')); } BENCHMARK(jetfix_gen_new_order); @@ -220,54 +278,53 @@ BENCHMARK(jetfix_gen_new_order); // } // BENCHMARK(gw_op_serialize); -// static void new_order_single_raw_gen(benchmark::State &state) { +static void new_order_single_raw_gen(benchmark::State &state) { -// std::string msg = -// "8=FIX.4.2\\019=000257\\0135=D\\0134=000000113\\01369=000000110\\0152=" -// "20190804-22:11:06.602\\0149=DE7534N\\0156=CME\\0157=80\\0150=" -// "DO3BTSJGR\\01142=US,IL\\011=CLX60635\\0154=2\\0159=0\\011028=N\\01204=" -// "0\\019702=1\\01107=HOU9-HOX9\\0111=00000101730000432660\\017928=" -// "6935102\\0160=20190804-22:11:06.592\\0138=000001\\0140=2\\0144=-" -// "0000093." -// "00000000\\0110=018\\01"; + std::string msg = + "8=FIX.4.2\\019=000257\\0135=D\\0134=000000113\\01369=000000110\\0152=" + "20190804-22:11:06.602\\0149=DE7534N\\0156=CME\\0157=80\\0150=" + "DO3BTSJGR\\01142=US,IL\\011=CLX60635\\0154=2\\0159=0\\011028=N\\01204=" + "0\\019702=1\\01107=HOU9-HOX9\\0111=00000101730000432660\\017928=" + "6935102\\0160=20190804-22:11:06.592\\0138=000001\\0140=2\\0144=-" + "0000093." + "00000000\\0110=018\\01"; -// auto sn_index = msg.find("000000113"); -// auto lsn_index = msg.find("000000110"); -// auto st_index = msg.find("20190804-22:11:06.602"); -// auto tt_index = msg.find("20190804-22:11:06.592"); -// auto qty_index = msg.find("000001"); -// auto p_index = msg.find("-0000093.00000000"); + auto sn_index = msg.find("000000113"); + auto lsn_index = msg.find("000000110"); + auto st_index = msg.find("20190804-22:11:06.602"); + auto tt_index = msg.find("20190804-22:11:06.592"); + auto qty_index = msg.find("000001"); + auto p_index = msg.find("-0000093.00000000"); -// char buffer[1024]; -// memcpy(buffer, msg.data(), msg.size()); -// uint64_t cb = 0; + char buffer[1024]; + memcpy(buffer, msg.data(), msg.size()); + uint64_t cb = 0; -// memcpy(buffer, msg.data(), msg.size()); -// for (auto _ : state) { + memcpy(buffer, msg.data(), msg.size()); + for (auto _ : state) { -// memcpy(buffer + sn_index, "000000113", sizeof("000000113") - 1); -// cb += sum_bytes(buffer + sn_index, sizeof("000000113") - 1); + memcpy(buffer + sn_index, "000000113", sizeof("000000113") - 1); + cb += sum_bytes(buffer + sn_index, sizeof("000000113") - 1); -// memcpy(buffer + lsn_index, "000000113", sizeof("000000113") - 1); -// cb += sum_bytes(buffer + lsn_index, sizeof("000000113") - 1); + memcpy(buffer + lsn_index, "000000113", sizeof("000000113") - 1); + cb += sum_bytes(buffer + lsn_index, sizeof("000000113") - 1); -// memcpy(buffer + st_index, "20190804-22:11:06.592", -// sizeof("20190804-22:11:06.592") - 1); + memcpy(buffer + st_index, "20190804-22:11:06.592", + sizeof("20190804-22:11:06.592") - 1); -// memcpy(buffer + tt_index, "20190804-22:11:06.592", -// sizeof("20190804-22:11:06.592") - 1); + memcpy(buffer + tt_index, "20190804-22:11:06.592", + sizeof("20190804-22:11:06.592") - 1); -// memcpy(buffer + qty_index, "000001", sizeof("000001") - 1); -// cb += sum_bytes(buffer + qty_index, sizeof("000001") - 1); + memcpy(buffer + qty_index, "000001", sizeof("000001") - 1); + cb += sum_bytes(buffer + qty_index, sizeof("000001") - 1); -// memcpy(buffer + p_index, "-0000093.00000000", -// sizeof("-0000093.00000000") - 1); -// benchmark::DoNotOptimize( -// cb += sum_bytes(buffer + p_index, sizeof("-0000093.00000000") - -// 1)); -// } -// } -// BENCHMARK(new_order_single_raw_gen); + memcpy(buffer + p_index, "-0000093.00000000", + sizeof("-0000093.00000000") - 1); + benchmark::DoNotOptimize( + cb += sum_bytes(buffer + p_index, sizeof("-0000093.00000000") - 1)); + } +} +BENCHMARK(new_order_single_raw_gen); } // namespace cmd } // namespace bts