MBO perf work

This commit is contained in:
Eric Harding committed 2019-04-18 14:39:15 -04:00
1 parent 73c9be3bec
commit 5392d05ec8
8 files changed
+328 -29

No files matched your search

+78 -13
View File
@@ -1,4 +1,4 @@
#include "MBOProcessor.h"
#include "MBOProcessor.h"
#include "lib/utils/Dispatcher.h"
#include "lib/utils/ConcurrentPool.h"
#include "lib/config/config.h"
@@ -12,8 +12,8 @@
#include <tbb/concurrent_hash_map.h>
namespace bts {
namespace md {
namespace bts {
namespace md {
struct SecurityKeyHashCompare {
static size_t hash(const SecurityKey& k) {
@@ -51,8 +51,8 @@ void print_pool_stats(const utils::ConcurrentPool<MBOSnapshot>& pool) {
}
bid_percentiles << bid_sz[count-1] << " ";
ask_percentiles << ask_sz[count-1] << " ";
LOG(md,Info,
"MBO statistics: count: {} - %[50,75,90,95,99,100] bid: [{}] - ask: [{}]",
LOG(md,Info,
"MBO statistics: count: {} - %[50,75,90,95,99,100] bid: [{}] - ask: [{}]",
count, bid_percentiles.str(), ask_percentiles.str());
}
@@ -60,11 +60,13 @@ typedef tbb::concurrent_hash_map<SecurityKey, MBOSnapshot*, SecurityKeyHashCompa
struct MBOProcessorImpl {
utils::ConcurrentPool<MBOSnapshot> pool;
MapType snapshots;
utils::Dispatcher& dispatcher;
utils::DispatcherTimer timer;
MBOProcessorImpl() :
pool(2048,512),
snapshots(),
timer(utils::Dispatcher::background(), 600*1000, [&]() { print_pool_stats(pool); })
MBOProcessorImpl(utils::Dispatcher& d) :
pool(2048,512),
snapshots(),
dispatcher(d),
timer(d, 600*1000, [&]() { print_pool_stats(pool); })
{
if (config::get<config::toggles::MBOStats>().enabled()) {
timer.start();
@@ -75,8 +77,8 @@ struct MBOProcessorImpl {
//
// Processor
MBOProcessor::MBOProcessor() :
_me(std::make_shared<MBOProcessorImpl>()),
MBOProcessor::MBOProcessor(utils::Dispatcher* d) :
_me(std::make_shared<MBOProcessorImpl>(d == nullptr ? utils::Dispatcher::background() : *d)),
_producer(_me),
_consumer(_me)
{}
@@ -86,9 +88,10 @@ MBOReader& MBOProcessor::reader() { return _consumer; }
//
// Writer
MBOWriter::MBOWriter(std::shared_ptr<MBOProcessorImpl>& me): _me(me) { }
MBOWriter::~MBOWriter() { }
MBOSnapshot* MBOWriter::allocate_snapshot() {
MBOSnapshot* MBOWriter::allocate_snapshot(size_t, size_t) {
return _me->pool.allocate();
}
void MBOWriter::publish_snapshot(SecurityKey key, MBOSnapshot* snapshot) {
@@ -102,6 +105,68 @@ void MBOWriter::publish_snapshot(SecurityKey key, MBOSnapshot* snapshot) {
}
}
bool MBOWriter::validate_snapshot(SecurityKey key, MBOSnapshot* control) {
#define REQUIRE(x) if (!(x)) { LOG(md,Error, "{}", std::string(#x)); return false; }
// retrieve current snapshot and check they are equal
MBOReader reader(_me);
auto snap = reader.acquire_snapshot(key);
REQUIRE (snap->bids.size() == control->bids.size())
REQUIRE (snap->asks.size() == control->asks.size())
for (size_t x = 0; x < snap->bids.size(); ++x) {
REQUIRE (snap->bids[x].id == control->bids[x].id);
REQUIRE (snap->bids[x].price == control->bids[x].price);
REQUIRE (snap->bids[x].size == control->bids[x].size);
}
for (size_t x = 0; x < snap->asks.size(); ++x) {
REQUIRE (snap->asks[x].id == control->asks[x].id);
REQUIRE (snap->asks[x].price == control->asks[x].price);
REQUIRE (snap->asks[x].size == control->asks[x].size);
}
#undef REQUIRE
return true;
}
void MBOWriter::enqueue_incremental(MBOWorkingState& state, const inc::Update& update) {
_me->dispatcher.post([this,&state,update]() {
this->apply_incremental(state,update);
});
}
void MBOWriter::apply_incremental(MBOWorkingState& state, const inc::Update& update) {
// fast part
apply_incremental(state, update);
// determine if we need to publish
// slow part
auto snap = allocate_snapshot(state.bid.size(), state.ask.size());
copy_to_snapshot(state, snap);
publish_snapshot(state.security, snap);
}
void copy_to_snapshot(MBOWorkingState& state, MBOSnapshot* dest) {
if (dest == nullptr) return;
// copy working state to snapshot
dest->updated = state.updated;
auto type_map = [](auto pair) {
return SnapshotOrder { pair.second.id, std::get<IntPrice>(pair.first), pair.second.size };
};
std::transform(state.bid.rbegin(), state.bid.rend(), std::back_inserter(dest->bids), type_map);
std::transform(state.ask.begin(), state.ask.end(), std::back_inserter(dest->asks), type_map);
}
void apply_incremental(MBOWorkingState& state, const inc::Update& update) {
// apply the update to the working state
MBOWorkingState::Side& collection = (update.type == inc::UpdateType::Bid) ? state.bid : state.ask;
auto key = std::make_tuple(update.price, update.priority);
switch(update.action) {
case inc::UpdateAction::New:
case inc::UpdateAction::Change:
collection[key] = { update.order_id, update.size };
break;
case inc::UpdateAction::Delete:
collection.erase(key);
break;
}
}
//
// Reader
@@ -123,4 +188,4 @@ void MBOReader::release_snapshot(MBOSnapshot**s) const {
_me->pool.release(s);
}
}}
}}