SPARK-592 - Ensure timestamp write is not re-ordered by compiler for multi-cache line depth update

This commit is contained in:
Eric Harding committed 2018-05-15 17:30:59 -04:00
1 parent a5c3d5cf09
commit eaa12d0788
6 files changed
+46 -40

No files matched your search

+3 -5
View File
@@ -22,11 +22,9 @@ ShmDumper::ShmDumper(const string& name) : _name(name)
ShmDumper::~ShmDumper() {}
double dump(TimeStamp time) {
// stringstream temp;
// temp << (TimeStamp::now() - time).seconds();
// return temp.str();
return (TimeStamp::now() - time).seconds();
}
string dump(PriceSize ps) {
stringstream res;
res << "{";
@@ -156,7 +154,7 @@ string dump_entry(const ColdEntry& entry) {
string dump_entry(const HotDepthEntry& entry) {
stringstream res;
res << "{ ";
res << "'timestamp': " << dump(entry.timestamp) << ", ";
res << "'timestamp': " << dump(entry.time()) << ", ";
res << "'key': " << entry.key << ", ";
res << "'bids': [";
@@ -180,7 +178,7 @@ string dump_entry(const HotDepthEntry& entry) {
string dump_entry(const HotEntry& entry) {
stringstream res;
res << "{ ";
res << "'timestamp': " << dump(entry.timestamp) << ", ";
res << "'timestamp': " << dump(entry.time()) << ", ";
res << "'key': " << entry.key << ", ";
res << "'bid': " << dump(entry.bid) << ", ";
res << "'ask': " << dump(entry.ask) << ", ";
+13 -13
View File
@@ -35,7 +35,7 @@ void MDConsumerHandle::init(const ColdHeader& cheader, const ColdEntry& centry,
set_hot(pvhot);
}
TimeStamp copy_bbo(const TimeStamp& time, const SingleWriterPriceSizeStore& in_bid, const SingleWriterPriceSizeStore& in_ask, PriceSize& out_bid, PriceSize& out_ask) {
TimeStamp copy_bbo(const HotEntry& hot, PriceSize& out_bid, PriceSize& out_ask) {
// We expect the time, in_bid and in_ask to be changing (rapidly)
//
TimeStamp t;
@@ -45,11 +45,11 @@ TimeStamp copy_bbo(const TimeStamp& time, const SingleWriterPriceSizeStore& in_b
// hopefully check the heartbeat.
const int max_attempts = 1619200;
do {
t = time;
out_bid = in_bid.load();
out_ask = in_ask.load();
t = hot.time();
out_bid = hot.bid.load();
out_ask = hot.ask.load();
++attempts;
} while((t != time || t == TimeStamp::invalid()) && attempts < max_attempts);
} while((t != hot.time() || t == TimeStamp::invalid()) && attempts < max_attempts);
if (attempts >= max_attempts) {
return TimeStamp::invalid();
}
@@ -57,8 +57,7 @@ TimeStamp copy_bbo(const TimeStamp& time, const SingleWriterPriceSizeStore& in_b
}
TimeStamp copy_depth(
const TimeStamp& time,
const SingleWriterPriceSizeStore* source,
const HotDepthEntry& hot,
size_t count,
PriceSize* bid, PriceSize* ask)
{
@@ -66,12 +65,13 @@ TimeStamp copy_depth(
int attempts = 0;
const int max_attempts = 1619200;
do {
t = time;
t = hot.time();
for (size_t x = 0; x < count; ++x) {
bid[x] = source[2*x].load();
ask[x] = source[2*x+1].load();
bid[x] = hot.bid(x).load();
ask[x] = hot.ask(x).load();
}
} while((t != time || t == TimeStamp::invalid()) && attempts < max_attempts);
++attempts;
} while((t != hot.time() || t == TimeStamp::invalid()) && attempts < max_attempts);
if (attempts >= max_attempts) {
return TimeStamp::invalid();
}
@@ -206,9 +206,9 @@ TimeStamp MDConsumerHandle::last_market_update() const {
case HANDLETYPE_VOID:
return TimeStamp::invalid();
case HANDLETYPE_DEPTH:
return book.hot->timestamp;
return book.hot->time();
case HANDLETYPE_BBO:
return bbo.hot->timestamp;
return bbo.hot->time();
case HANDLETYPE_SYNTHETIC:
return sane::max<TimeStamp>(synthetic.def->legs, [](const MDConsumerHandle& h) { return h.last_market_update(); });
}
+8 -8
View File
@@ -18,8 +18,8 @@ struct SyntheticStrategy {
std::vector<MDConsumerHandle> legs;
};
TimeStamp copy_bbo(const TimeStamp&, const SingleWriterPriceSizeStore&, const SingleWriterPriceSizeStore&, PriceSize&, PriceSize& );
TimeStamp copy_depth(const TimeStamp& time, const SingleWriterPriceSizeStore* source, size_t count, PriceSize* bid, PriceSize* ask);
TimeStamp copy_bbo(const HotEntry& src, PriceSize& out_bid, PriceSize& out_ask);
TimeStamp copy_depth(const HotDepthEntry&, size_t count, PriceSize* bid, PriceSize* ask);
TimeStamp copy_depth(const SyntheticStrategy&, size_t count, PriceSize* bid, PriceSize* ask);
enum class HandleReadResult {
@@ -83,16 +83,16 @@ public:
switch(_type) {
case HANDLETYPE_DEPTH:
if (book.hot->timestamp > time || book.hot->timestamp == TimeStamp::invalid()) {
TimeStamp temp = copy_bbo(book.hot->timestamp, book.hot->bid(0), book.hot->ask(0), bid, ask);
if (book.hot->time() > time || book.hot->time() == TimeStamp::invalid()) {
TimeStamp temp = copy_depth(*book.hot, 1, &bid, &ask);
if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; }
time = temp;
return HandleReadResult::SUCCESS;
}
return HandleReadResult::NO_CHANGE;
case HANDLETYPE_BBO:
if (bbo.hot->timestamp > time || bbo.hot->timestamp == TimeStamp::invalid()) {
TimeStamp temp = copy_bbo(bbo.hot->timestamp, bbo.hot->bid, bbo.hot->ask, bid, ask);
if (bbo.hot->time() > time || bbo.hot->time() == TimeStamp::invalid()) {
TimeStamp temp = copy_bbo(*bbo.hot, bid, ask);
if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; }
time = temp;
return HandleReadResult::SUCCESS;
@@ -114,8 +114,8 @@ public:
switch(_type) {
case HANDLETYPE_VOID: return HandleReadResult::NO_CHANGE;
case HANDLETYPE_DEPTH:
if (book.hot->timestamp > time || book.hot->timestamp == TimeStamp::invalid()) {
TimeStamp temp = copy_depth(book.hot->timestamp, book.hot->book, std::min(copydepth, COCONUT_MAX_DEPTH), bids, asks);
if (book.hot->time() > time || book.hot->time() == TimeStamp::invalid()) {
TimeStamp temp = copy_depth(*book.hot, std::min(copydepth, COCONUT_MAX_DEPTH), bids, asks);
if (temp == TimeStamp::invalid()) { return HandleReadResult::BUSY; }
time = temp;
return HandleReadResult::SUCCESS;
+8 -8
View File
@@ -75,33 +75,33 @@ void MDProducerHandle::set_legs(const std::vector<security_key>& legs) {
void MDProducerHandle::update_bbo(TimeStamp time, const PriceSize bid, const PriceSize ask, uint16_t seq) {
if (!has_depth()) {
auto oldtime = hdata().timestamp;
hdata().timestamp = TimeStamp::invalid();
auto oldtime = hdata().time();
hdata().set_time(TimeStamp::invalid());
bool set_bid = hdata().bid.store(bid);
bool set_ask = hdata().ask.store(ask);
hdata().status = COCONUTSECURITYSTATUS_OK;
if (set_bid || set_ask) {
hdata().timestamp = time;
hdata().set_time(time);
hdata().sequence = seq;
hheader().bbo_count++;
}
else {
hdata().timestamp = oldtime;
hdata().set_time(oldtime);
}
}
else {
auto oldtime = hdepth().timestamp;
hdepth().timestamp = TimeStamp::invalid();
auto oldtime = hdepth().time();
hdepth().set_time(TimeStamp::invalid());
bool set_bid = hdepth().bid(0).store(bid);
bool set_ask = hdepth().ask(0).store(ask);
hdepth().status = COCONUTSECURITYSTATUS_OK;
if (set_bid || set_ask) {
hdepth().timestamp = time;
hdepth().set_time(time);
hdepth().sequence = seq;
hheader().bbo_count++;
}
else {
hdepth().timestamp = oldtime;
hdepth().set_time(oldtime);
}
}
}
+2 -2
View File
@@ -41,14 +41,14 @@ public:
}
else {
auto& book = hdepth();
book.timestamp = TimeStamp::invalid();
book.set_time(TimeStamp::invalid());
for (size_t x = 0; x < COCONUT_MAX_DEPTH; ++x) {
book.bid(x).store(bids[x]);
book.ask(x).store(asks[x]);
}
book.status = co2::COCONUTSECURITYSTATUS_OK;
book.sequence = seq;
book.timestamp = time;
book.set_time(time);
hheader().depth_count++;
}
}
+12 -4
View File
@@ -36,23 +36,29 @@ struct ALIGNED_(CACHE_LINE) HotHeader {
// These should not be cache line aligned because they are small enough for multiple to fit on one line easily
struct ALIGNED_(32) HotEntry {
HotEntry() : timestamp(), bid(), ask(), key(invalid_security_key), sequence(0), status(co2::COCONUTSECURITYSTATUS_OK) {}
HotEntry() : timestamp(TimeStamp(0)), bid(), ask(), key(invalid_security_key), sequence(0), status(co2::COCONUTSECURITYSTATUS_OK) {}
uint32_t size() const { return sizeof(HotEntry); }
TimeStamp timestamp; // 8
private:
std::atomic<TimeStamp> timestamp; // 8
public:
SingleWriterPriceSizeStore bid; // 8
SingleWriterPriceSizeStore ask; // 8
security_key key; // 4
uint16_t sequence; // 2
uint8_t status; // 1
void set_time(const TimeStamp& t) { timestamp.store(t, std::memory_order_release); }
TimeStamp time() const { return timestamp.load(std::memory_order_acquire); }
};
const uint32_t COCONUT_MAX_DEPTH = 10;
struct ALIGNED_(CACHE_LINE) HotDepthEntry {
HotDepthEntry() :
timestamp(), key(invalid_security_key), status(co2::COCONUTSECURITYSTATUS_OK), book(), sequence(0)
timestamp(TimeStamp(0)), key(invalid_security_key), status(co2::COCONUTSECURITYSTATUS_OK), book(), sequence(0)
{ }
uint32_t size() const { return sizeof(HotDepthEntry); }
TimeStamp timestamp; // 8
private:
std::atomic<TimeStamp> timestamp; // 8
public:
security_key key; // NOTE: key is at a different offset than it is in HotEntry
uint8_t status; // 1
// Interleave bid/ask values so the top of book is always on the first line
@@ -63,6 +69,8 @@ struct ALIGNED_(CACHE_LINE) HotDepthEntry {
const SingleWriterPriceSizeStore& bid(uint32_t depth) const { return book[depth*2]; }
SingleWriterPriceSizeStore& ask(uint32_t depth) { return book[depth*2+1]; }
const SingleWriterPriceSizeStore& ask(uint32_t depth) const { return book[depth*2+1]; }
void set_time(const TimeStamp& t) { timestamp.store(t, std::memory_order_release); }
TimeStamp time() const { return timestamp.load(std::memory_order_acquire); }
};
//