Improved performance.

This commit is contained in:
Pedro Pinto committed 2018-10-02 16:24:00 +00:00
1 parent 2769de6f7e
commit c2864d1715
14 files changed
+245 -169

No files matched your search

+51 -37
View File
@@ -17,8 +17,8 @@
#include "lib/orderman/actors/ServiceBridge.h"
#include "lib/orderman/actors/order_manager/OrderManager.h"
#include "lib/utils/bts_chrono.h"
#include "lib/utils/type_name.h"
#include "lib/utils/thread_manager.h"
#include "lib/utils/type_name.h"
#include <boost/algorithm/string/predicate.hpp>
@@ -26,7 +26,8 @@ namespace bts {
namespace orderman {
struct ActorThread {
ActorThread(const config::OrdermanCfg::Executor &cfg,metrics::MetricsContainer &stats)
ActorThread(const config::OrdermanCfg::Executor &cfg,
metrics::MetricsContainer &stats)
: cfg(cfg), thread(), executor(), loop_metric(), done(false) {
std::array<double, 30> buckets;
auto i = 0;
@@ -34,27 +35,33 @@ struct ActorThread {
b = (i += 1000);
loop_metric = std::make_shared<metrics::HistogramMetric<30>>(
fmt::format("spark_metrics_{}_thread_loop_ns", cfg.thread_name()), buckets);
fmt::format("spark_metrics_{}_thread_loop_ns", cfg.thread_name()),
buckets);
stats.publish(loop_metric);
}
void start() {
thread = threading::make_thread(cfg.thread_name(),[this]() {
const auto inner_iter = 1000;
while (!done) {
auto start = Timer::now();
for (auto i = 0; i < inner_iter; i++)
if (cfg.poll())
thread = threading::make_thread(cfg.thread_name(), [this]() {
for (;;)
executor.do_work();
auto stop = Timer::now();
loop_metric->observe(ns_diff(stop, start) / inner_iter);
if (!cfg.poll())
});
else
thread = threading::make_thread(cfg.thread_name(), [this]() {
const auto inner_iter = 1000;
while (!done) {
auto start = Timer::now();
for (auto i = 0; i < inner_iter; i++)
executor.do_work();
auto stop = Timer::now();
loop_metric->observe(ns_diff(stop, start) / inner_iter);
std::this_thread::sleep_for(10ms);
}
});
}
});
}
~ActorThread() {
done= true;
done = true;
thread.join();
}
@@ -71,7 +78,7 @@ ActorThread *find_thread_for_actor(
for (auto const &ex : all_executors)
for (auto const &hosted : ex->cfg.hosted_actor())
if (hosted == "*" || boost::algorithm::to_lower_copy(hosted) ==
boost::algorithm::to_lower_copy(actor_name))
boost::algorithm::to_lower_copy(actor_name))
return ex.get();
return nullptr;
}
@@ -85,7 +92,7 @@ struct OrderServiceServer::PImpl {
admin_service(ctx.locate<api::admin::AdminService>()), md_consumer(ss),
stats(stats) {}
ServiceBridge* service_bridge;
ServiceBridge *service_bridge;
std::vector<std::unique_ptr<ActorThread>> actor_threads;
@@ -109,32 +116,35 @@ OrderServiceServer::OrderServiceServer(
me->publish_orders = make_orders_publisher();
me->publish_fills = make_fills_publisher();
std::vector<mercury::Executor*> all_executors;
std::vector<mercury::Executor *> all_executors;
for (auto const &ex_cfg : config::get<config::OrdermanCfg>().executor()) {
auto at = std::make_unique<ActorThread>(ex_cfg,stats);
auto at = std::make_unique<ActorThread>(ex_cfg, stats);
all_executors.push_back(&at->executor);
me->actor_threads.push_back(std::move(at));
}
for (auto &t : me->actor_threads)
t->executor.init_neighbours(all_executors);
// MDSource
auto thread = find_thread_for_actor("MDSource", me->actor_threads);
bts_assert_msg(thread, "Could not find a configured thread for MDSource. See the "
"OrdermanCfg key");
bts_assert_msg(thread,
"Could not find a configured thread for MDSource. See the "
"OrdermanCfg key");
thread->executor.spawn<MDSource>(me->md_consumer, me->stats);
// OrderManager
thread = find_thread_for_actor("OrderManager", me->actor_threads);
bts_assert_msg(thread, "Could not find a configured thread for OrderManager. See "
"the OrdermanCfg key");
bts_assert_msg(thread,
"Could not find a configured thread for OrderManager. See "
"the OrdermanCfg key");
thread->executor.spawn<OrderManager>();
// ExecutionGateway's
thread = find_thread_for_actor("ExecutionGateway", me->actor_threads);
bts_assert_msg(thread, "Could not find a configured thread for ExecutionGateway "
"instances. See the OrdermanCfg key");
bts_assert_msg(thread,
"Could not find a configured thread for ExecutionGateway "
"instances. See the OrdermanCfg key");
auto &all_connections = config::get<config::JetFIX>().connection();
for (auto const &c : all_connections)
thread->executor.spawn<ExecutionGateway>(c, stats);
@@ -148,32 +158,37 @@ OrderServiceServer::OrderServiceServer(
// AsyncFactory
thread = find_thread_for_actor("AsyncFactory", me->actor_threads);
bts_assert_msg(thread, "Could not find a configured thread for AsyncFactory. See "
"the OrdermanCfg key");
bts_assert_msg(thread,
"Could not find a configured thread for AsyncFactory. See "
"the OrdermanCfg key");
thread->executor.spawn<AsyncFactory>();
// OrderStore
thread = find_thread_for_actor("OrderStore", me->actor_threads);
bts_assert_msg(thread, "Could not find a configured thread for OrderStore. See "
"the OrdermanCfg key");
bts_assert_msg(thread,
"Could not find a configured thread for OrderStore. See "
"the OrdermanCfg key");
thread->executor.spawn<OrderStore>();
// FillStore
thread = find_thread_for_actor("FillStore", me->actor_threads);
bts_assert_msg(thread, "Could not find a configured thread for FilllStore. See "
"the OrdermanCfg key");
bts_assert_msg(thread,
"Could not find a configured thread for FilllStore. See "
"the OrdermanCfg key");
thread->executor.spawn<FillStore>();
// SecuritySrc
thread = find_thread_for_actor("SecuritySrc", me->actor_threads);
bts_assert_msg(thread, "Could not find a configured thread for SecuritySrc. See "
"the OrdermanCfg key");
bts_assert_msg(thread,
"Could not find a configured thread for SecuritySrc. See "
"the OrdermanCfg key");
thread->executor.spawn<SecuritySrc>(me->md_consumer);
// ServiceBridge
thread = find_thread_for_actor("ServiceBridge", me->actor_threads);
bts_assert_msg(thread, "Could not find a configured thread for ServiceBridge. See "
"the OrdermanCfg key");
bts_assert_msg(thread,
"Could not find a configured thread for ServiceBridge. See "
"the OrdermanCfg key");
me->service_bridge = new ServiceBridge(
[this]() { me->service_ready = true; },
@@ -184,8 +199,7 @@ OrderServiceServer::OrderServiceServer(
[this, &ctx](auto const &fill) {
ctx.post([this, fill] { this->on_fills(fill); });
},
thread->executor.make_proxy()
);
thread->executor.make_proxy());
thread->executor.activate(me->service_bridge);