Slick Trader 0.11.0
Multi-broker algorithmic trading engine with plugin strategies and a live web dashboard
Loading...
Searching...
No Matches
fill_journal.cpp
Go to the documentation of this file.
2#include <slick/logger.hpp>
3#include <algorithm>
4#include <atomic>
5#include <cstring>
6#include <stdexcept>
7
8namespace slick::trader {
9
10// Every field at its natural alignment with no padding between them, which the
11// static_asserts pin: the layout is the file format.
13 char magic[8];
14 uint32_t version;
15 uint32_t entry_size;
16 uint64_t capacity;
17 uint64_t first_seq;
18 // Past every number handed out. Written before an entry is overwritten: one cleared
19 // by a process that died mid-write may have been the only entry left.
20 uint64_t next_seq;
21 uint8_t reserved[24];
22};
23
25 // Written last, and cleared first when the entry is overwritten: an entry is never
26 // read half written.
27 uint64_t journal_seq;
30};
31
32static_assert(sizeof(FillJournal::Source) == 24);
33
34FillJournal::FillJournal(const std::string& path, const Format& format, size_t capacity, uint64_t first_seq)
35 : path_(path)
36 , file_(path, sizeof(FileHeader) + std::max<size_t>(capacity, 1) * sizeof(Entry))
37{
38 static_assert(sizeof(FileHeader) == 64 && sizeof(FileHeader) % alignof(Entry) == 0);
39 static_assert(sizeof(Entry) % alignof(Entry) == 0);
40
41 auto* header = reinterpret_cast<FileHeader*>(file_.data());
42 const bool blank = std::all_of(file_.data(), file_.data() + sizeof(FileHeader), [](uint8_t b) { return b == 0; });
43 if (blank) {
44 FileHeader fresh{};
45 fresh.version = format.version;
46 fresh.entry_size = static_cast<uint32_t>(sizeof(Entry));
47 fresh.capacity = std::max<size_t>(capacity, 1);
48 fresh.first_seq = std::max<uint64_t>(first_seq, 1);
49 std::memcpy(fresh.magic, format.magic, sizeof(fresh.magic));
50 std::memcpy(header, &fresh, sizeof(FileHeader));
51 } else if (std::memcmp(header->magic, format.magic, sizeof(header->magic)) != 0) {
52 throw std::runtime_error(std::string(format.name) + ": " + path + " is not a fill journal");
53 } else if (header->version != format.version || header->entry_size != sizeof(Entry) ||
54 sizeof(FileHeader) + header->capacity * sizeof(Entry) > file_.size()) {
55 throw std::runtime_error(std::string(format.name) + ": " + path +
56 " was written with a different layout (version " +
57 std::to_string(header->version) + ")");
58 }
59 // A ring keeps the capacity it was made with: its entries' places depend on it.
60 capacity_ = static_cast<size_t>(header->capacity);
61 if (capacity_ != capacity) {
62 LOG_WARN("{}: {} keeps the capacity it was created with, {} fills, not {}", format.name, path_, capacity_,
63 capacity);
64 }
65
66 uint64_t last_seq = 0;
67 std::unordered_map<uint64_t, uint64_t> newest; // session -> journal number of its last fill
68 for (size_t i = 0; i < capacity_; ++i) {
69 const Entry& e = entry(i);
70 if (e.journal_seq == 0) {
71 continue;
72 }
73 ++size_;
74 last_seq = std::max(last_seq, e.journal_seq);
75 auto [it, added] = newest.try_emplace(e.source.session, e.journal_seq);
76 if (added || e.journal_seq > it->second) {
77 it->second = e.journal_seq;
78 last_[e.source.session] = e.source;
79 }
80 }
81 next_seq_ = std::max({last_seq + 1, header->first_seq, header->next_seq});
82 LOG_INFO("{}: {} holds {} fill(s), room for {}", format.name, path_, size_, capacity_);
83}
84
85FillJournal::FileHeader& FillJournal::header() noexcept {
86 return *reinterpret_cast<FileHeader*>(file_.data());
87}
88
89FillJournal::Entry& FillJournal::entry(size_t index) noexcept {
90 return *reinterpret_cast<Entry*>(file_.data() + sizeof(FileHeader) + index * sizeof(Entry));
91}
92
93const FillJournal::Entry& FillJournal::entry(size_t index) const noexcept {
94 return *reinterpret_cast<const Entry*>(file_.data() + sizeof(FileHeader) + index * sizeof(Entry));
95}
96
97bool FillJournal::record(OrderFillEvent& fill, const Source& source) {
98 auto [last, first] = last_.try_emplace(source.session, source);
99 if (!first) {
100 if (!source.after(last->second)) {
101 return false; // its report was handed over again: this fill is journaled
102 }
103 last->second = source;
104 }
105 fill.journal_seq = next_seq_++;
106 Entry& e = entry(fill.journal_seq % capacity_);
107 if (e.journal_seq == 0) {
108 ++size_;
109 }
110 // The number marked used, then the entry cleared, filled and numbered; the fences keep
111 // the compiler from reordering the stores, which is all a process dying between two
112 // of them could see.
113 header().next_seq = next_seq_;
114 std::atomic_signal_fence(std::memory_order_seq_cst);
115 e.journal_seq = 0;
116 std::atomic_signal_fence(std::memory_order_seq_cst);
117 e.source = source;
118 std::memcpy(static_cast<void*>(&e.fill), &fill, sizeof(OrderFillEvent));
119 std::atomic_signal_fence(std::memory_order_seq_cst);
120 e.journal_seq = fill.journal_seq;
121 return true;
122}
123
124std::vector<OrderFillEvent> FillJournal::fills() const {
125 std::vector<const Entry*> held;
126 held.reserve(size_);
127 for (size_t i = 0; i < capacity_; ++i) {
128 if (entry(i).journal_seq != 0) {
129 held.push_back(&entry(i));
130 }
131 }
132 std::sort(held.begin(), held.end(), [](const Entry* a, const Entry* b) { return a->journal_seq < b->journal_seq; });
133 std::vector<OrderFillEvent> out;
134 out.reserve(held.size());
135 for (const Entry* e : held) {
136 out.push_back(e->fill);
137 }
138 return out;
139}
140
141} // namespace slick::trader
std::vector< OrderFillEvent > fills() const
size_t capacity() const noexcept
FillJournal(const std::string &path, const Format &format, size_t capacity, uint64_t first_seq)
bool record(OrderFillEvent &fill, const Source &source)
const std::string & path() const noexcept
std::uint8_t * data() noexcept
bool after(const Source &other) const noexcept