FastMM documentation
FastMM C++ API 0.6.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
store_thread.hpp
Go to the documentation of this file.
1#pragma once
2// StoreThread: drains the engine's record ring into a Backend, on its own thread.
3//
4// The mirror of JournalFileWriter: the engine writes into an SPSC MsgRing and never waits, this
5// side batches what it finds into one backend transaction. A pass over the ring commits every
6// max_batch records and whatever is left at its end, and the thread sleeps 2 ms when the ring is
7// empty; so a busy session does not commit per fill and an idle one commits within 2 ms.
8//
9// It never signals the engine. A backend that fails counts the error and keeps going; a ring that
10// fills drops records on the engine side (EngineStats::records_dropped). Both are logged when the
11// session ends and stored in the session's row; neither stops trading (docs/reference/storage.md).
12#include "fastmm/core/msg_ring.hpp"
14
15#include <atomic>
16#include <cstddef>
17#include <cstdint>
18#include <memory>
19#include <thread>
20
21namespace fastmm::store {
22
23struct StoreThreadOptions {
24 std::size_t max_batch = 512; // records per backend transaction
25};
27struct StoreThreadStats {
28 std::uint64_t records = 0;
29 std::uint64_t batches = 0;
30 std::uint64_t unknown = 0; // records of a type this build does not know (a newer writer)
31};
33class StoreThread {
34 public:
35 StoreThread(MsgRing& ring, std::unique_ptr<Backend> backend, const StoreThreadOptions& opts = {});
37 StoreThread(const StoreThread&) = delete;
40 [[nodiscard]] Backend& backend() noexcept { return *backend_; }
41
42 void start(); // spawns the drain thread
43 void stop(); // joins, drains what is left, commits
45 // Single-threaded driving (tests): drains and commits everything the ring holds now.
46 std::size_t drain_once();
47
48 [[nodiscard]] StoreThreadStats stats() const noexcept;
49
50 private:
51 void run();
52 std::size_t drain(bool force_commit);
53
54 MsgRing& ring_;
55 std::unique_ptr<Backend> backend_;
57 std::thread thread_;
58 std::atomic<bool> stop_{false};
59 std::atomic<std::uint64_t> records_{0};
60 std::atomic<std::uint64_t> batches_{0};
61 std::atomic<std::uint64_t> unknown_{0};
62 bool stopped_ = false;
63};
64
65} // namespace fastmm::store
Storage backend interface: what a session's fills, orders, positions, PnL and kill events are handed ...
Definition backend.hpp:100
Definition store_thread.hpp:35
void start()
spawns the drain thread
std::size_t drain_once()
Single-threaded driving (tests): drains and commits everything the ring holds now.
StoreThread(MsgRing &ring, std::unique_ptr< Backend > backend, const StoreThreadOptions &opts={})
StoreThreadStats stats() const noexcept
Backend & backend() noexcept
Definition store_thread.hpp:42
StoreThread & operator=(const StoreThread &)=delete
void stop()
joins, drains what is left, commits
Definition backend.hpp:39
Definition store_thread.hpp:25
std::size_t max_batch
records per backend transaction
Definition store_thread.hpp:26
Definition store_thread.hpp:29
std::uint64_t records
Definition store_thread.hpp:30
std::uint64_t unknown
records of a type this build does not know (a newer writer)
Definition store_thread.hpp:32
std::uint64_t batches
Definition store_thread.hpp:31