diff --git a/src/lib/mercury/actors/ContentSubscription.h b/src/lib/mercury/actors/ContentSubscription.h index 92a6cccf5..9b7d3257c 100644 --- a/src/lib/mercury/actors/ContentSubscription.h +++ b/src/lib/mercury/actors/ContentSubscription.h @@ -5,6 +5,7 @@ #include #include #include +#include "lib/tracing/PerfLog.h" #include "lib/utils/bts_assert.h" @@ -40,33 +41,37 @@ template class ContentSubscription { } }; - ContentSubscription(Actor &actor) + void init() { + _name = utils::type_name(); + } + + ContentSubscription(Actor &actor, trace::PerfLog& log) : _actor(&actor), _sub(_actor->subscribe( [this](auto const &u) { this->on_update(u); })), _my_subs(), _dispatching(false), _current_sub_id(1), - _pre_sub([](auto) {}) {} + _pre_sub([](auto) {}), _log(log) {init();} - ContentSubscription(Actor &actor, std::function const &pscb) + ContentSubscription(Actor &actor, std::function const &pscb, trace::PerfLog& log) : _actor(&actor), _sub(_actor->subscribe( [this](auto const &u) { this->on_update(u); })), - _my_subs(), _dispatching(false), _current_sub_id(0), _pre_sub(pscb) {} + _my_subs(), _dispatching(false), _current_sub_id(0), _pre_sub(pscb), _log(log) {init();} template - ContentSubscription(Actor &actor, Tag const &tag) + ContentSubscription(Actor &actor, Tag const &tag, trace::PerfLog& log) : _actor(&actor), _sub(_actor->subscribe( tag, [this](auto const &u) { this->on_update(u); })), _my_subs(), _dispatching(false), _current_sub_id(1), - _pre_sub([](auto) {}) {} + _pre_sub([](auto) {}), _log(log) {init();} template ContentSubscription(Actor &actor, Tag const &tag, - std::function const &pscb) + std::function const &pscb, trace::PerfLog& log) : _actor(&actor), _sub(_actor->subscribe( tag, [this](auto const &u) { this->on_update(u); })), _my_subs(), _dispatching(false), _current_sub_id(1), - _pre_sub(pscb) {} + _pre_sub(pscb), _log(log) {init();} template SubID sub(Key k, CBT const &cb) { _pre_sub(k); @@ -111,8 +116,11 @@ template class ContentSubscription { bool _dispatching; uint32_t _current_sub_id; std::function _pre_sub; + trace::PerfLog& _log; + std::string _name; void on_update(Update const &u) { + auto scoped_do_work = trace::log_scope(_log, {}, "ContentSub::on_update", 0, _name.c_str()); KE ke; _dispatching = true; if (u.events) diff --git a/src/lib/mercury/actors/IPollableActor.h b/src/lib/mercury/actors/IPollableActor.h index ab106a7ea..7612f6d09 100644 --- a/src/lib/mercury/actors/IPollableActor.h +++ b/src/lib/mercury/actors/IPollableActor.h @@ -3,6 +3,9 @@ #include namespace bts { +namespace trace { + class PerfLog; +} namespace mercury { // interface to mark an actor that requires on_poll() calls @@ -10,7 +13,7 @@ class IPollableActor { public: ~IPollableActor(); - virtual void on_poll() = 0; + virtual void on_poll(trace::PerfLog&) = 0; }; diff --git a/src/lib/mercury/actors/Timer.h b/src/lib/mercury/actors/Timer.h index 0d4c6c441..793bb49bd 100644 --- a/src/lib/mercury/actors/Timer.h +++ b/src/lib/mercury/actors/Timer.h @@ -21,7 +21,7 @@ class Timer : public Actor, public mercury::IPollableActor { } private: - void on_poll() override { + void on_poll(trace::PerfLog&) override { auto now = bts::Timer::now(); if (ms_diff(now, _last_ts) > 10) { _last_ts = now; diff --git a/src/lib/mercury/broker/BrokerProxy.h b/src/lib/mercury/broker/BrokerProxy.h index dcbb762b5..34abb5d05 100644 --- a/src/lib/mercury/broker/BrokerProxy.h +++ b/src/lib/mercury/broker/BrokerProxy.h @@ -4,6 +4,7 @@ #include "lib/mercury/common/mercury_common.h" #include "lib/mercury/topics/Topic.h" #include "lib/utils/GrowableSPSCQueue.h" +#include "lib/tracing/PerfLog.h" #include #include @@ -79,9 +80,9 @@ class BrokerProxy : public ILocalBroker { send(m); } - void do_work() { + void do_work(trace::PerfLog& log) { _cycles++; - _outgoing.consume_all([this](BrokerMsg *m) { + _outgoing.consume_all([this,&log](BrokerMsg *m) { switch (m->msg_type) { case BrokerMsgType::ADD_NEIGHBOUR: _real_broker.add_neighbour(*m->neighbour); @@ -101,11 +102,13 @@ class BrokerProxy : public ILocalBroker { case BrokerMsgType::UNSUBSCRIBE: _real_broker.unsubscribe(m->broker_id, m->sub_id, m->stream_id); break; - case BrokerMsgType::NOTIFY: + case BrokerMsgType::NOTIFY: { LOG(Latency, Diag, "Notify broker {}. Cycles: {}", _id.value(), _cycles); + auto scoped_do_work = trace::log_scope(log, {}, "BrokerProxy::notify"); _real_broker.notify(m->broker_id, m->stream_id, m->sub_id, m->notification); + } break; case BrokerMsgType::LOCAL_SUBSCRIBE: _real_broker.local_subscribe(m->local_sub_id, m->filter, diff --git a/src/lib/mercury/executor/Executor.h b/src/lib/mercury/executor/Executor.h index db2bb1a5b..cf2f4404b 100644 --- a/src/lib/mercury/executor/Executor.h +++ b/src/lib/mercury/executor/Executor.h @@ -4,11 +4,13 @@ #include "lib/mercury/broker/Broker.h" #include "lib/mercury/broker/BrokerProxy.h" #include "lib/mercury/actors/ActorBroker.h" +#include "lib/tracing/PerfLog.h" #include #include namespace bts { + namespace mercury { class Actor; class BrokerProxy; @@ -22,13 +24,16 @@ class Executor { activate(new A(std::forward(args)...)); } - void do_work() { - for (auto p : _pollable_actors) - p->on_poll(); - for (auto &bp : _incoming_proxies) - bp->do_work(); - for (auto &bp : _outgoing_proxies) + void do_work(trace::PerfLog& log) { + for (auto p : _pollable_actors) { + p->on_poll(log); + } + for (auto &bp : _incoming_proxies) { + bp->do_work(log); + } + for (auto &bp : _outgoing_proxies) { bp->drain(); + } _my_proxy.drain(); } diff --git a/src/lib/orderman/actors/ExecutionGateway.cpp b/src/lib/orderman/actors/ExecutionGateway.cpp index bc659b1dd..9b3b83687 100644 --- a/src/lib/orderman/actors/ExecutionGateway.cpp +++ b/src/lib/orderman/actors/ExecutionGateway.cpp @@ -111,6 +111,8 @@ void ExecutionGateway::on_exe_report( SecurityKey sk; decode_clorid(msg.get(), &ok, &sk); + auto scoped_log = trace::log_scope(me->perf_log, msg.trace(), "ExecutionGateway::on_exe_report", ok.value()); + LOG(Latency, Info, "Started handling of execution report for order {}", ok.value()); auto sec = me->securities[sk.value()]; diff --git a/src/lib/orderman/actors/MDSource.h b/src/lib/orderman/actors/MDSource.h index a92b795fb..4d3882f80 100644 --- a/src/lib/orderman/actors/MDSource.h +++ b/src/lib/orderman/actors/MDSource.h @@ -95,7 +95,9 @@ class MDSource : public mercury::Actor, public mercury::IPollableActor { } private: - void on_poll() override { + void on_poll(trace::PerfLog& log) override { + + auto scoped_do_work = trace::log_scope(log, {}, "MDSource::on_poll"); // Book Updates Book book; diff --git a/src/lib/orderman/actors/MDSupplementarySource.cpp b/src/lib/orderman/actors/MDSupplementarySource.cpp index ae41824b1..bbe3f4737 100644 --- a/src/lib/orderman/actors/MDSupplementarySource.cpp +++ b/src/lib/orderman/actors/MDSupplementarySource.cpp @@ -51,7 +51,7 @@ void MDSupplementarySource::init() { }); } -void MDSupplementarySource::on_poll() { +void MDSupplementarySource::on_poll(trace::PerfLog&) { for (auto &kvp : me->handle_map) { SecurityKey key(kvp.first); HandleRecord &hr = kvp.second; diff --git a/src/lib/orderman/actors/MDSupplementarySource.h b/src/lib/orderman/actors/MDSupplementarySource.h index 4ea5fa261..f0f04725c 100644 --- a/src/lib/orderman/actors/MDSupplementarySource.h +++ b/src/lib/orderman/actors/MDSupplementarySource.h @@ -22,7 +22,7 @@ public: ~MDSupplementarySource(); void init() override; - void on_poll() override; + void on_poll(trace::PerfLog&) override; private: struct PImpl; std::unique_ptr me; diff --git a/src/lib/orderman/actors/order_manager/OrderManager.cpp b/src/lib/orderman/actors/order_manager/OrderManager.cpp index 4a8ed4482..efe2e20e1 100644 --- a/src/lib/orderman/actors/order_manager/OrderManager.cpp +++ b/src/lib/orderman/actors/order_manager/OrderManager.cpp @@ -301,7 +301,7 @@ OrderManager::free_buffers(std::string const &session_name) { } void OrderManager::init_order_services() { - + auto& log = me->services_data.log; for (auto &s : me->services_data.sessions) { auto gw_name = GatewayName(s->session); @@ -345,12 +345,14 @@ void OrderManager::init_order_services() { // ContentSubscription helper class. s->exe_report_subs = std::make_unique>( - *this, gw_name); + *this, gw_name,log); + s->cancel_reject_subs = std::make_unique>(*this, - gw_name); + gw_name, + log); s->gateway_status_subs = std::make_unique< - ContentSubscription>(*this, gw_name); + ContentSubscription>(*this, gw_name,log); // publishers for execution reports, gateway status and cancel // reject evets @@ -415,16 +417,17 @@ void OrderManager::init_order_services() { publish(me->services_data.settlement_request_pub, k); }; me->services_data.md_subs = - std::make_unique>(*this, on_md_sub); + std::make_unique>(*this, on_md_sub,log); me->services_data.ticker_subs = - std::make_unique>(*this, on_ticker_sub); + std::make_unique>(*this, on_ticker_sub,log); me->services_data.settlement_subs = std::make_unique>( - *this, on_settlement_sub); + *this, on_settlement_sub,log); + me->services_data.time_subs = - std::make_unique>(*this); + std::make_unique>(*this,log); me->services_data.order_creation_subs = - std::make_unique>(*this); + std::make_unique>(*this,log); } void OrderManager::after_event() { @@ -432,6 +435,7 @@ void OrderManager::after_event() { for (auto &osd : me->services_data.sessions) { auto &fix_stream = osd->current_buffer->first_stream(); if (fix_stream.n_msgs()) { + auto scoped_do_work = trace::log_scope(me->services_data.log, {}, "OrderManager::after_event"); fix_stream.close(); osd->gw.send_fix(osd->current_buffer, osd->current_trace); reload_fix_buffer(*osd); @@ -453,7 +457,8 @@ void OrderManager::reload_fix_buffer(SessionServicesData &osd) { } } -void OrderManager::on_poll() { +void OrderManager::on_poll(trace::PerfLog& log) { + auto scoped_do_work = trace::log_scope(log, {}, "OrderManager::dispatch_incoming_fix_messages"); for (auto &osd : me->services_data.sessions) osd->gw.dispatch_incoming_fix_messages(); } diff --git a/src/lib/orderman/actors/order_manager/OrderManager.h b/src/lib/orderman/actors/order_manager/OrderManager.h index 4bee86449..8b2d6f20e 100644 --- a/src/lib/orderman/actors/order_manager/OrderManager.h +++ b/src/lib/orderman/actors/order_manager/OrderManager.h @@ -81,7 +81,7 @@ class OrderManager : public mercury::Actor, public mercury::IPollableActor { void on_gateway_state(const mercury::Update &); void on_timer(const mercury::Update &); void on_admin_data(const mercury::Update &); - void on_poll() override; + void on_poll(trace::PerfLog& log) override; // end external events void init_order_services(); diff --git a/src/lib/orderman/server/OrderServiceServer.cpp b/src/lib/orderman/server/OrderServiceServer.cpp index 7c157ca4f..b31bc2416 100644 --- a/src/lib/orderman/server/OrderServiceServer.cpp +++ b/src/lib/orderman/server/OrderServiceServer.cpp @@ -44,24 +44,25 @@ struct ActorThread { stats.publish(loop_metric); } - void start() { + void start(trace::PerfLog& log) { if (cfg.poll()) - thread = threading::make_thread(cfg.thread_name(), [this]() { + thread = threading::make_thread(cfg.thread_name(), [this,&log]() { for (;;) - executor.do_work(); + executor.do_work(log); }); - else - thread = threading::make_thread(cfg.thread_name(), [this]() { + else { + thread = threading::make_thread(cfg.thread_name(), [this,&log]() { const auto inner_iter = 1000; while (!done) { auto start = Timer::now(); for (auto i = 0; i < inner_iter; i++) - executor.do_work(); + executor.do_work(log); auto stop = Timer::now(); loop_metric->observe(ns_diff(stop, start) / inner_iter); std::this_thread::sleep_for(10ms); } }); + } } ~ActorThread() { @@ -212,7 +213,7 @@ OrderServiceServer::OrderServiceServer( // spin up threads for (auto &t : me->actor_threads) - t->start(); + t->start(log); // wait for the service bridge to be ready me->service_bridge->block_until_ready(); diff --git a/src/lib/tracing/PerfLog.h b/src/lib/tracing/PerfLog.h index 4f2906b34..e3503a1c0 100644 --- a/src/lib/tracing/PerfLog.h +++ b/src/lib/tracing/PerfLog.h @@ -57,6 +57,7 @@ class PerfLog { pthread_self(), ts2, cid.sn, uint32_1}); } + void flush() { _pending.push({utils::TimeStamp(), nullptr, nullptr, nullptr, 0, pthread_self(), utils::TimeStamp(), 0, 0}); @@ -94,7 +95,11 @@ class ScopedPerfLogEntry { pthread_self(), utils::TimeStamp(), cid.sn, - uint32_1} {} + uint32_1} { + + _log->log(_begin); + + } ScopedPerfLogEntry(ScopedPerfLogEntry &&src) : _log(src._log), _begin(src._begin) { @@ -103,7 +108,7 @@ class ScopedPerfLogEntry { void finish() { if (_begin.entry_kind) { - _log->log(_begin); + // _log->log(_begin); _begin.cor_event = _begin.ts; _begin.ts = utils::TimeStamp::now(); _log->log(_begin); @@ -118,7 +123,16 @@ class ScopedPerfLogEntry { private: PerfLog *_log; PerfLogEntry _begin; -}; // namespace trace +}; // ScopedPerfLogEntry + +inline ScopedPerfLogEntry log_scope(PerfLog& log, + trace::CorID cid, const char *entry_kind, + uint64_t uint64_1 = 0, + const char *txt_1 = "", + uint32_t uint32_1 = 0) { + return ScopedPerfLogEntry(log, cid, entry_kind, uint64_1, + txt_1, uint32_1); +} } // namespace trace } // namespace bts