Files
spark/src/lib/md/MDProducer.cpp
T
2017-12-21 16:33:28 -05:00

158 lines
5.2 KiB
C++

#ifndef NO_CPP0X
#include "MDProducer.h"
#include "lib/utils/SharedList.h"
#include "lib/utils/WaitFreeQueue.h"
#include "Shm/MDStructure.h"
#include "MemorySegmentUtils.h"
using namespace std;
namespace bts {
namespace md {
// Owns shared memory for a single symbol+type
class MDProducerImpl {
public:
MDProducerImpl(const string& symbol, SecurityType type, Venue exchange, Platform platform, bool shared) :
_symbol(symbol),
_type(type),
_exchange(exchange),
_hot(hot_segment_name(symbol, type, exchange, platform), 1024, 1024, shared),
_cold(cold_segment_name(symbol, type, exchange, platform), 6144, 2048, shared),
_trades(_cold.header().trade_buffer, BTS_ARRAYSIZE(_cold.header().trade_buffer), true),
_rfqs(_cold.header().rfq_buffer, BTS_ARRAYSIZE(_cold.header().rfq_buffer), true)
{
// NOTE: Block sizes have been tuned based on sim.
// We should monitor and adjust based on production use for optimal performance
_cold.header().has_depth = enable_depth(symbol, type);
_cold.header().platform = platform;
static_assert(sizeof(HotEntry) <= 32,"HotEntry should be less than 32 bytes");
}
MDProducerHandle add_security(const FixedSecurity& security) {
void* h_entry = nullptr;
if (has_depth()) {
h_entry = new (_hot.add(sizeof(HotDepthEntry))) HotDepthEntry();
}
else {
h_entry = new (_hot.add(sizeof(HotEntry))) HotEntry();
}
// Calculating the size for the variable sized entries is tricky
ColdEntry* c_entry = new (_cold.add(ColdEntry::fixed_size() + security.size)) ColdEntry(security.size);
memcpy(&c_entry->security, &security, security.size);
MDProducerHandle handle(_cold.header(), _hot.header(), *c_entry, h_entry, MDProducerChannel(*this));
if (security.key != invalid_security_key) {
// The only safe way to set the security key is through the set_key() function
// If the security had a key it was copied to cold by the memcpy but not the hot
// So we clear it from the cold and let set_key() set it in hot and cold and make the security public.
c_entry->security.key = invalid_security_key;
handle.set_key(security.key);
}
return handle;
}
void heartbeat() {
_cold.header().heartbeat = utils::TimeStamp::now();
}
void add_trade(const co2::TradeMessage& trade) {
bts_assert(trade.key() != invalid_security_key);
_trades.push(trade);
_cold.header().trade_count++;
}
void add_rfq(const co2::QuoteRequestMessage& rfq) {
ColdHeader::RFQBuffer buffer = {{0}};
if (rfq.SerializePartialToArray(&buffer[0], buffer.size())) {
_rfqs.push(buffer);
_cold.header().rfq_count++;
}
else {
LOG(MarketData,Warn, "Unable to store RFQ due to size limitation {}",
rfq.DebugString());
}
}
const string& symbol() const { return _symbol; }
SecurityType type() const { return _type; }
Venue exchange() const { return _exchange; }
bool has_depth() { return _cold.header().has_depth; }
private:
BTS_NO_COPY(MDProducerImpl);
string _symbol;
SecurityType _type;
Venue _exchange;
utils::SharedList<HotHeader, HotEntry> _hot;
utils::SharedList<ColdHeader, ColdEntry> _cold;
utils::WaitFreeQueue<co2::TradeMessage> _trades;
utils::WaitFreeQueue<ColdHeader::RFQBuffer> _rfqs;
};
//
// MDProducer
//
MDProducer::MDProducer(Platform platform, bool shared) :
_mutex(new Mutex()),
_producers(),
_platform(platform),
_shared(shared)
{
}
MDProducer::MDProducer(MDProducer&& other) :
_mutex(std::move(other._mutex)),
_producers(std::move(other._producers)),
_platform(other._platform),
_shared(other._shared)
{}
MDProducer::~MDProducer() {}
MDProducerImpl& MDProducer::get_producer(const char* symbol, SecurityType type, Venue exchange) {
for (auto& impl : _producers) {
if (impl->symbol() == symbol && impl->type() == type && impl->exchange() == exchange) {
return *impl;
}
}
// Not found add a new one
unique_ptr<MDProducerImpl> newp(new MDProducerImpl(symbol, type, exchange, _platform, _shared));
_producers.push_back(move(newp));
return *_producers.back();
}
MDProducerHandle MDProducer::add_security(const FixedSecurity& security) {
bts_assert_d(strlen(security.symbol()) > 0);
Mutex::scoped_lock lock(*_mutex);
auto& impl = get_producer(security.symbol(), security.type, security.exchange);
auto result = impl.add_security(security);
if (security.key != invalid_security_key) { result.set_key(security.key); }
return result;
}
MDProducerChannel MDProducer::get_channel(const char* symbol, SecurityType type, Venue exchange) {
Mutex::scoped_lock lock(*_mutex);
return MDProducerChannel(get_producer(symbol, type, exchange));
}
//
// MDProducerChannel
//
MDProducerChannel::MDProducerChannel(MDProducerImpl& impl) : _me(&impl) {}
void MDProducerChannel::heartbeat() {_me->heartbeat();}
void MDProducerChannel::add_trade(const co2::TradeMessage& trade) {_me->add_trade(trade);}
void MDProducerChannel::add_rfq(const co2::QuoteRequestMessage& rfq) {_me->add_rfq(rfq);}
}}//namespace
#endif