Files
spark/src/lib/md/MDProducer.cpp
T

191 lines
6.4 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"
#include "SecurityIDRecorder.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), 8192, 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(md,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; }
void gap() { _cold.header().gap_count++; }
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 std::mutex()),
_producers(),
_id_recorder(),
_platform(platform),
_shared(shared)
{ }
#ifndef WIN32
MDProducer::MDProducer(Platform platform, SecurityIDRecorder recorder, bool shared) :
_mutex(new std::mutex()),
_producers(),
_id_recorder(std::make_unique<SecurityIDRecorder>(recorder)),
_platform(platform),
_shared(shared)
{ }
#endif
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) {
// First search existing producers for one that matches
for (auto& impl : _producers) {
if ((impl->symbol() == symbol && impl->type() == type && impl->exchange() == exchange) ||
//single channel for all synthetics
(impl->type() == api::SECURITYTYPE_SYNTHETIC && type == api::SECURITYTYPE_SYNTHETIC)) {
return *impl;
}
}
// Not found add a new one
unique_ptr<MDProducerImpl> newp;
if (type == api::SECURITYTYPE_SYNTHETIC) {
// synthetics are special. We don't want to allocate a separate section for each one so we put them all in one
newp.reset(new MDProducerImpl(SYNTHETIC_SYMBOL, api::SECURITYTYPE_SYNTHETIC, Venue::VENUE_BTS, _platform, _shared));
}
else {
newp.reset(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);
bts_assert(security.size >= sizeof(FixedSecurity));
std::lock_guard<std::mutex> 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); }
else if(_id_recorder) {
_id_recorder->ensure_key(result);
}
return result;
}
MDProducerChannel MDProducer::get_channel(const char* symbol, SecurityType type, Venue exchange) {
std::lock_guard<std::mutex> lock(*_mutex);
return MDProducerChannel(get_producer(symbol, type, exchange));
}
void MDProducer::heartbeat() {
std::lock_guard<std::mutex> lock(*_mutex);
for (auto& impl : _producers) {
impl->heartbeat();
}
}
//
// 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);}
void MDProducerChannel::gap() { _me->gap(); }
}}//namespace
#endif