-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
146 lines (124 loc) · 5.22 KB
/
main.cpp
File metadata and controls
146 lines (124 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <atomic>
#include <chrono>
#include <thread>
#include <unordered_map>
#include <vector>
#include "boost/lockfree/spsc_queue.hpp"
#include "core/OrderBook.h"
#include "core/OrderBuilder.h"
#include "ingress/McastSocket.h"
#include "ingress/OrderDispatcher.h"
#include "ingress/WireOrder.h"
#include "snapshot/SnapshotPublisher.h"
#include "utils/Affinity.h"
#include "utils/Config.h"
#include "utils/Logger.h"
#include "utils/LogMacros.h"
namespace {
#ifdef PROJECT_ROOT
constexpr const char* kConfigPath = PROJECT_ROOT "/config/app.ini";
#else
constexpr const char* kConfigPath = "config/app.ini";
#endif
using WireOrder = ingress::WireOrder;
using Queue = boost::lockfree::spsc_queue<WireOrder>;
constexpr std::size_t kQueueCapacity = 10240;
} // namespace
int main() {
try {
const AppConfig config = loadConfig(kConfigPath);
logging::LoggerOptions logger_opts;
logger_opts.queue_size = config.logging.queue_size;
logger_opts.worker_threads = config.logging.worker_threads;
logger_opts.affinity = config.affinity.logging_cores;
logging::configureLogger(logger_opts);
const std::vector<InstrumentToken> instruments = {26000, 35000};
std::unordered_map<InstrumentToken, std::unique_ptr<Queue>> queue_storage;
OrderDispatcher::QueueMap dispatcher_queues;
std::unordered_map<InstrumentToken, std::unique_ptr<OrderBook>> books;
SnapshotConfig snapshot_cfg;
snapshot_cfg.shm_prefix = config.snapshot.shm_prefix;
snapshot_cfg.interval = std::chrono::milliseconds(config.snapshot.interval_ms);
snapshot_cfg.max_levels = config.snapshot.levels;
SnapshotPublisher publisher(snapshot_cfg, instruments);
const auto& engineCores = config.affinity.engine_cores;
size_t nextEngineCore = 0;
for (const auto token : instruments) {
auto queue = std::make_unique<Queue>(kQueueCapacity);
dispatcher_queues[token] = queue.get();
queue_storage[token] = std::move(queue);
books[token] = std::make_unique<OrderBook>(config.use_std_map);
books[token]->setInstrumentToken(token);
if (!engineCores.empty()) {
const int tradeCore = engineCores[nextEngineCore % engineCores.size()];
++nextEngineCore;
books[token]->bindTradeThreadToCores(std::vector<int>{tradeCore});
}
}
std::vector<std::thread> workers;
workers.reserve(instruments.size());
for (std::size_t idx = 0; idx < instruments.size(); ++idx) {
const InstrumentToken token = instruments[idx];
Queue* queue = dispatcher_queues[token];
OrderBook* book = books[token].get();
int workerCore = -1;
if (!engineCores.empty()) {
workerCore = engineCores[nextEngineCore % engineCores.size()];
++nextEngineCore;
}
workers.emplace_back([queue, book, token, &publisher, workerCore] {
if (workerCore >= 0) {
cpu::setCurrentThreadAffinity(std::vector<int>{workerCore});
}
while (true) {
WireOrder inbound;
std::size_t spins = 0;
while (!queue->pop(inbound)) {
if (++spins % 1000 == 0) {
std::this_thread::yield();
}
}
OrderBuilder builder;
builder.setOrderId(inbound.order_id)
.setInstrumentToken(inbound.instrument)
.setSide(inbound.side)
.setPrice(inbound.price)
.setQuantity(inbound.quantity)
.setOrderType(inbound.type)
.setTimestamp(std::chrono::high_resolution_clock::now());
if (inbound.display > 0) {
builder.setDisplayQuantity(inbound.display);
}
auto order = builder.build();
book->addOrder(std::move(order));
publisher.maybePublish(token, *book);
}
});
if (workerCore >= 0) {
cpu::setThreadAffinity(workers.back(), workerCore);
}
}
SocketUtils::McastSocket socket;
socket.init(config.mcast_ip, config.mcast_iface, config.mcast_port, true);
socket.join(config.mcast_ip);
OrderDispatcher dispatcher(socket, dispatcher_queues);
std::thread dispatcher_thread([&dispatcher] {
dispatcher.run();
});
LOG_INFO("Engine ready on {}:{} via iface {} (orderbook backend: {})",
config.mcast_ip,
config.mcast_port,
config.mcast_iface,
config.use_std_map ? "std::pmr::map" : "RingBuffer");
std::atomic<bool> running{true};
dispatcher_thread.join();
running.store(false, std::memory_order_relaxed);
for (auto& worker : workers) {
worker.join();
}
} catch (const std::exception& ex) {
LOG_ERROR("Engine crashed: {}", ex.what());
return 1;
}
return 0;
}