-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL62.cpp
More file actions
433 lines (366 loc) · 15.4 KB
/
Copy pathL62.cpp
File metadata and controls
433 lines (366 loc) · 15.4 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// ============================================================
// L62: Async Logger (Lock-Free)
// ============================================================
// WHAT: A trading system logger that logs without blocking the
// hot path. A lock-free SPSC queue decouples the trading
// thread (producer) from the disk writer (consumer). The
// trading thread enqueues a log record in ~20ns; the
// background thread flushes to disk when convenient.
// WHY (TRADING): std::cout or fprintf in the hot path can take
// 1-10µs (system call, formatting, flushing). That destroys
// latency. Solution: the trading thread writes a compact record
// to a lock-free queue (no syscall), and a background thread
// writes to disk. Every order, fill, risk event, and state
// change is logged without stalling the trading loop.
// PHASE: Trading Systems Implementation
// ============================================================
/*
CONCEPT OVERVIEW:
WHY LOGGING IS SLOW:
- std::cout: mutex lock + format + write syscall + optional flush
- fprintf: format + write syscall
- spdlog: lock-free queue (fast path ~100ns), background flush
- Our logger: memcpy to SPSC queue (~20ns hot path), async write
LOG RECORD DESIGN:
Fixed-size records (no heap allocation, no std::string):
struct LogRecord {
uint64_t ts_ns; // rdtsc timestamp
uint8_t level; // DEBUG/INFO/WARN/ERROR
uint8_t category; // FILL, ORDER, RISK, MARKET, SYS
char msg[246]; // message, null-terminated
}; // 256 bytes = 4 cache lines
SPSC QUEUE:
Producer (trading thread): pushes records
Consumer (logger thread): pops records, formats, writes to file
No locks, no condition variables — spin-polling consumer.
LOG LEVELS:
TRACE: every tick (disable in production, floods disk)
DEBUG: order lifecycle events, fill details
INFO: strategy signals, risk checks
WARN: approaching position limits, unusual conditions
ERROR: system errors, rejected orders
FATAL: trigger kill switch, then log
DISK WRITE STRATEGY:
- Open file in O_APPEND | O_WRONLY mode
- Write batches of records (reduce syscall frequency)
- fsync() only on FATAL or shutdown (not per-record)
COMMON MISTAKES:
- Logging inside the hot path with a mutex → 1µs+ overhead
- Formatting (sprintf) inside the hot path → 500ns overhead
- SPSC queue overflow → silently dropped log records
(add a dropped_count_ counter; alert if non-zero)
- Calling fflush() after every write → serializes I/O
- Storing std::string in the log record (heap allocation)
*/
#include <iostream>
#include <cstdint>
#include <cstring>
#include <atomic>
#include <thread>
#include <fstream>
#include <cstdio>
#include <ctime>
#include <chrono>
#include <cassert>
#include <string>
#include <array>
// ============================================================
// LOG LEVELS AND CATEGORIES
// ============================================================
enum class LogLevel : uint8_t {
TRACE = 0,
DEBUG = 1,
INFO = 2,
WARN = 3,
ERROR = 4,
FATAL = 5
};
enum class LogCategory : uint8_t {
SYS = 0,
ORDER = 1,
FILL = 2,
RISK = 3,
MARKET = 4,
STRAT = 5
};
const char* level_str(LogLevel l) {
switch (l) {
case LogLevel::TRACE: return "TRACE";
case LogLevel::DEBUG: return "DEBUG";
case LogLevel::INFO: return "INFO ";
case LogLevel::WARN: return "WARN ";
case LogLevel::ERROR: return "ERROR";
case LogLevel::FATAL: return "FATAL";
}
return "?????";
}
// ============================================================
// LOG RECORD — fixed size, trivially copyable (no malloc)
// ============================================================
constexpr int LOG_MSG_SIZE = 246;
struct alignas(64) LogRecord {
uint64_t ts_ns; // nanoseconds since epoch (from steady_clock)
LogLevel level; // log level
LogCategory category; // subsystem
uint16_t strategy_id; // 0 = system
char msg[LOG_MSG_SIZE]; // null-terminated message
// Total: 8 + 1 + 1 + 2 + 246 = 258 bytes → pad to 4 cache lines (256 bytes)
// The alignas(64) means the struct starts on a cache-line boundary
};
// Practical size — keeping the design clear matters more than exact 256B here
// ============================================================
// LOCK-FREE SPSC QUEUE (from L38)
// ============================================================
template<typename T, int N>
class SPSCQueue {
static_assert((N & (N-1)) == 0, "N must be power of 2");
alignas(64) std::atomic<uint64_t> head_{0};
alignas(64) std::atomic<uint64_t> tail_{0};
T buf_[N]{};
public:
bool push(const T& item) noexcept {
uint64_t h = head_.load(std::memory_order_relaxed);
if (h - tail_.load(std::memory_order_acquire) >= N) return false;
buf_[h & (N-1)] = item;
head_.store(h + 1, std::memory_order_release);
return true;
}
bool pop(T& item) noexcept {
uint64_t t = tail_.load(std::memory_order_relaxed);
if (head_.load(std::memory_order_acquire) == t) return false;
item = buf_[t & (N-1)];
tail_.store(t + 1, std::memory_order_release);
return true;
}
};
// ============================================================
// ASYNC LOGGER
// ============================================================
class AsyncLogger {
public:
static constexpr int QUEUE_SIZE = 4096; // must be power of 2
explicit AsyncLogger(const std::string& filename, LogLevel min_level = LogLevel::DEBUG)
: min_level_(min_level)
, running_(false)
, dropped_(0)
{
file_ = fopen(filename.c_str(), "a"); // append mode
if (!file_) {
std::cerr << "[Logger] Failed to open " << filename << "\n";
}
}
~AsyncLogger() {
stop();
if (file_) fclose(file_);
}
// Start the background consumer thread
void start() {
running_.store(true, std::memory_order_release);
thread_ = std::thread([this]() { consume_loop(); });
}
// Stop: drain the queue, then join the thread
void stop() {
if (!running_.load(std::memory_order_acquire)) return;
running_.store(false, std::memory_order_release);
if (thread_.joinable()) thread_.join();
flush_to_disk(); // final drain
}
// ── HOT PATH: called from the trading thread ─────────────
// Log with printf-style formatting.
// This runs on the trading thread — must be fast.
// Worst case: one snprintf + one memcpy to the queue.
template<typename... Args>
void log(LogLevel level, LogCategory cat, uint16_t strat_id,
const char* fmt, Args... args) noexcept {
if (level < min_level_) return; // filter before formatting
LogRecord rec{};
rec.ts_ns = now_ns();
rec.level = level;
rec.category = cat;
rec.strategy_id = strat_id;
// Format message into the record's buffer — no heap allocation
snprintf(rec.msg, LOG_MSG_SIZE, fmt, args...);
if (!queue_.push(rec)) {
++dropped_; // queue full — log record dropped
}
}
// Shortcut macros for common cases
void info (const char* msg, uint16_t strat = 0) {
log(LogLevel::INFO, LogCategory::SYS, strat, "%s", msg);
}
void warn (const char* msg, uint16_t strat = 0) {
log(LogLevel::WARN, LogCategory::SYS, strat, "%s", msg);
}
void error(const char* msg, uint16_t strat = 0) {
log(LogLevel::ERROR, LogCategory::SYS, strat, "%s", msg);
}
uint64_t dropped() const { return dropped_.load(std::memory_order_relaxed); }
private:
FILE* file_;
LogLevel min_level_;
std::atomic<bool> running_;
std::atomic<uint64_t> dropped_;
std::thread thread_;
SPSCQueue<LogRecord, QUEUE_SIZE> queue_;
// Flush buffer — accumulate records here before writing to disk
static constexpr int FLUSH_BUF = 16384;
char flush_buf_[FLUSH_BUF];
int flush_pos_ = 0;
uint64_t now_ns() const {
return static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count());
}
// Consumer loop — runs on the background thread
void consume_loop() {
LogRecord rec;
while (running_.load(std::memory_order_relaxed)) {
if (queue_.pop(rec)) {
format_and_buffer(rec);
} else {
// Queue empty — flush pending writes to disk
flush_to_disk();
// Yield briefly to avoid burning 100% CPU in logger thread
// In production: use condition_variable for slow-path wakeup
std::this_thread::yield();
}
}
}
// Format one record into the flush buffer
void format_and_buffer(const LogRecord& rec) {
// Convert nanoseconds to HH:MM:SS.nnnnnnnnn
time_t secs = static_cast<time_t>(rec.ts_ns / 1000000000ULL);
uint64_t nanos = rec.ts_ns % 1000000000ULL;
struct tm t{};
#if defined(_MSC_VER) || defined(_WIN32)
gmtime_s(&t, &secs);
#else
gmtime_r(&secs, &t);
#endif
char line[512];
int len = snprintf(line, sizeof(line),
"%02d:%02d:%02d.%09llu [%s] [%s] strat=%u | %s\n",
t.tm_hour, t.tm_min, t.tm_sec, (unsigned long long)nanos,
level_str(rec.level),
cat_str(rec.category),
rec.strategy_id,
rec.msg
);
if (flush_pos_ + len < FLUSH_BUF) {
memcpy(flush_buf_ + flush_pos_, line, len);
flush_pos_ += len;
} else {
flush_to_disk();
memcpy(flush_buf_, line, len);
flush_pos_ = len;
}
}
void flush_to_disk() {
if (flush_pos_ > 0 && file_) {
fwrite(flush_buf_, 1, flush_pos_, file_);
flush_pos_ = 0;
// Note: NOT calling fflush() here — batches writes for throughput
// Call fflush(file_) only on FATAL or shutdown
}
}
const char* cat_str(LogCategory c) const {
switch (c) {
case LogCategory::SYS: return "SYS ";
case LogCategory::ORDER: return "ORDER ";
case LogCategory::FILL: return "FILL ";
case LogCategory::RISK: return "RISK ";
case LogCategory::MARKET: return "MARKET";
case LogCategory::STRAT: return "STRAT ";
}
return "? ";
}
};
// ============================================================
// GLOBAL LOGGER (singleton pattern for easy access across modules)
// ============================================================
static AsyncLogger* g_logger = nullptr;
// Convenience macros — zero overhead when level is filtered
#define LOG_INFO(cat, strat, fmt, ...) \
if (g_logger) g_logger->log(LogLevel::INFO, LogCategory::cat, strat, fmt, ##__VA_ARGS__)
#define LOG_WARN(cat, strat, fmt, ...) \
if (g_logger) g_logger->log(LogLevel::WARN, LogCategory::cat, strat, fmt, ##__VA_ARGS__)
#define LOG_ERROR(cat, strat, fmt, ...) \
if (g_logger) g_logger->log(LogLevel::ERROR, LogCategory::cat, strat, fmt, ##__VA_ARGS__)
#define LOG_FILL(strat, price, qty, sym) \
if (g_logger) g_logger->log(LogLevel::INFO, LogCategory::FILL, strat, \
"FILL %s qty=%d px=%.4f", sym, qty, price)
// ============================================================
// MAIN
// ============================================================
int main() {
std::cout << "=== Async Logger ===\n";
std::cout << " LogRecord size: " << sizeof(LogRecord) << " bytes\n";
// -------------------------------------------------------
// START LOGGER
// -------------------------------------------------------
// Write to stdout as well for demo (in production: use a real log file)
AsyncLogger logger("trading_log.txt", LogLevel::DEBUG);
g_logger = &logger;
logger.start();
// -------------------------------------------------------
// LOG VARIOUS EVENTS (from the "trading thread")
// -------------------------------------------------------
std::cout << "\n--- Logging events ---\n";
LOG_INFO(SYS, 0, "Trading system starting up");
LOG_INFO(ORDER, 1, "New order: BUY 100 SPY @ 182.50");
LOG_FILL(1, 182.50, 100, "SPY");
LOG_WARN(RISK, 0, "Position approaching limit: SPY %d / 500", 450);
LOG_ERROR(SYS, 0, "Connection to exchange dropped");
LOG_INFO(STRAT, 2, "Signal: momentum triggered, price=%.2f sma=%.2f", 183.0, 182.5);
// -------------------------------------------------------
// HOT PATH LATENCY TEST
// -------------------------------------------------------
std::cout << "\n=== Hot-path log latency ===\n";
constexpr int REPS = 1000000;
auto t0 = std::chrono::steady_clock::now();
for (int i = 0; i < REPS; ++i) {
// This is what the trading thread calls on the hot path
logger.log(LogLevel::DEBUG, LogCategory::MARKET, 1,
"tick #%d bid=%.4f ask=%.4f", i, 182.50 + i*0.0001, 182.51 + i*0.0001);
}
auto t1 = std::chrono::steady_clock::now();
uint64_t ns = static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count());
std::cout << " " << REPS << " log calls in " << ns / 1000 << "µs\n";
std::cout << " Per log call: " << ns / REPS << "ns (on trading thread)\n";
std::cout << " Dropped (queue full): " << logger.dropped() << "\n";
std::cout << " (queue full = QUEUE_SIZE=" << AsyncLogger::QUEUE_SIZE
<< " records. Increase or drain faster if drops occur.)\n";
// -------------------------------------------------------
// STOP
// -------------------------------------------------------
std::cout << "\n--- Stopping logger ---\n";
logger.stop();
g_logger = nullptr;
std::cout << " Logger stopped. Log written to trading_log.txt\n";
std::cout << " Total dropped: " << logger.dropped() << "\n";
// Show first few lines of the log
std::cout << "\n--- First lines of trading_log.txt ---\n";
std::ifstream logfile("trading_log.txt");
std::string line;
int lines = 0;
while (std::getline(logfile, line) && lines < 8) {
std::cout << " " << line << "\n";
++lines;
}
return 0;
/*
TRADING CONTEXT EXAMPLE:
Every significant event in the trading lifecycle is logged:
on_market_data → LOG(TRACE, MARKET, strat, "bid=%.4f ask=%.4f", ...)
on_order_submit → LOG(INFO, ORDER, strat, "NEW %s %d@%.4f id=%lu", ...)
on_ack → LOG(INFO, ORDER, strat, "ACK id=%lu exchg=%lu", ...)
on_fill → LOG(INFO, FILL, strat, "FILL %d@%.4f cum=%d", ...)
on_reject → LOG(WARN, ORDER, strat, "REJECT id=%lu reason=%s", ...)
on_risk_breach → LOG(ERROR, RISK, strat, "LIMIT %s pos=%d max=%d", ...)
End of day: concatenate log files, grep for FILL to get trade blotter,
grep for ERROR/WARN for post-trade review.
Log file rotation: at midnight, rename trading_log.txt to trading_log_YYYYMMDD.txt.
Keep 30 days of logs. Compress old logs with zstd for storage efficiency.
*/
}