FastMM documentation
FastMM C++ API 0.14.0
Public headers, tier 1 and tier 2
Loading...
Searching...
No Matches
session_state.hpp
Go to the documentation of this file.
1#pragma once
2// State that survives a process restart: the session epoch (client order ids) and the latched risk
3// state (kill switch and cumulative PnL). Both are text files next to the journal, written through
4// a temporary file + fsync + rename, so a crash leaves the old file or the new one, never a
5// truncated one. Both stores fail closed: a session that cannot read or bump them must not trade.
9
10#include <cstdint>
11#include <string>
12#include <string_view>
13
14namespace fastmm {
15
16// Writes `contents` to `path` atomically: <path>.tmp, fsync, rename, fsync of the directory.
17[[nodiscard]] Result<void, std::string> write_file_atomic(const std::string& path,
18 std::string_view contents);
20// The strategy state file ([strategy] state_file, written by write_file_atomic): its bytes, true
21// when it exists, false when there is no such file (a first start); an error when it cannot be
22// read. The bytes are the strategy's own: whether they are its is for restore() to say.
23[[nodiscard]] Result<bool, std::string> load_strategy_state(const std::string& path,
24 std::string& out);
26// The [strategy] state_file as the engine's state() bytes go into it: a 16-byte head ("FMSTATE1",
27// the strategy's state_version as a little-endian u32, 4 zero bytes), then the bytes. A file
28// without the head, from before it was written, is read whole as version 0.
29[[nodiscard]] Result<void, std::string> write_strategy_state(const std::string& path,
30 std::string_view bytes,
31 std::uint32_t version);
32[[nodiscard]] Result<bool, std::string> read_strategy_state(const std::string& path,
33 std::string& out,
34 std::uint32_t& version);
35
36// The 16-bit session epoch is the high half of every ClientOrderId, so two sessions that share one
37// would let a venue's late message from the earlier session match an order of the later one.
39 public:
40 // Reads the durable counter, increments it, writes it back and returns the epoch to use
41 // (1..65535, never 0). Fails when the file cannot be read or written; the caller must not trade.
42 // Beyond 65535 sessions the epoch cycles and `wrapped` is set: ids of sessions that long ago
43 // can repeat.
44 [[nodiscard]] static Result<std::uint16_t, std::string> next_epoch(const std::string& path,
45 bool* wrapped = nullptr);
46};
47
48// Cumulative risk state across sessions. `realized` and `fees` are the sum over every session since
49// the file was last cleared, so [risk] max_loss is a budget for the deployment rather than per
50// process. Unrealized PnL is not carried; it is remeasured from the venue's position after the
51// restart.
52struct KillState {
53 bool latched = false; // a max-loss trip is latched: refuse to start until an operator clears it
57 std::uint64_t sessions = 0;
58 std::int64_t updated_ns = 0; // wall clock of the last write
59 // [risk] max_loss_period = "utc_day": the 00:00 UTC (wall ns) `realized` and `fees` count from;
60 // 0 for "total".
61 std::int64_t period_start_ns = 0;
62
63 // The carry [risk] max_loss is measured against, on top of the running session's PnL.
64 [[nodiscard]] Notional carry() const noexcept { return realized - fees; }
65};
67// 00:00 UTC of the day `wall_ns` falls in.
68[[nodiscard]] constexpr std::int64_t utc_day_start(std::int64_t wall_ns) noexcept {
69 constexpr std::int64_t kDay = 86'400'000'000'000;
70 return wall_ns - ((wall_ns % kDay) + kDay) % kDay;
71}
72
73// Starts the loss budget's period that holds `wall_ns` when the state counts an earlier one:
74// `realized` and `fees` go back to zero, the latch stays. False: already that period.
75inline bool start_loss_period(KillState& s, std::int64_t period_start_ns) noexcept {
76 if (s.period_start_ns == period_start_ns) return false;
77 s.realized = Notional{};
78 s.fees = Notional{};
79 s.period_start_ns = period_start_ns;
80 return true;
81}
82
83class KillStateStore {
84 public:
85 // "<journal_dir>/<engine name>.kill".
86 [[nodiscard]] static std::string default_path(std::string_view journal_dir,
87 std::string_view engine_name);
88 // A missing file is a cleared state; a file that cannot be read or parsed is an error.
89 [[nodiscard]] static Result<KillState, std::string> load(const std::string& path);
90 [[nodiscard]] static Result<void, std::string> store(const std::string& path, const KillState& s);
91 // Removes the file (an operator clearing a latched trip and the loss budget with it).
92 [[nodiscard]] static Result<void, std::string> clear(const std::string& path);
93};
95} // namespace fastmm
static std::string default_path(std::string_view journal_dir, std::string_view engine_name)
"<journal_dir>/<engine name>.kill".
static Result< KillState, std::string > load(const std::string &path)
A missing file is a cleared state; a file that cannot be read or parsed is an error.
static Result< void, std::string > clear(const std::string &path)
Removes the file (an operator clearing a latched trip and the loss budget with it).
static Result< void, std::string > store(const std::string &path, const KillState &s)
Definition result.hpp:30
The 16-bit session epoch is the high half of every ClientOrderId, so two sessions that share one woul...
Definition session_state.hpp:40
static Result< std::uint16_t, std::string > next_epoch(const std::string &path, bool *wrapped=nullptr)
Reads the durable counter, increments it, writes it back and returns the epoch to use (1....
Minimal expected<T, E>: the hot path never throws, it returns Result.
All engine enumerations.
Fixed<Tag>: int64 fixed point with a global 1e-8 scale (ADR-0001).
Definition strategy.hpp:23
Result< bool, std::string > load_strategy_state(const std::string &path, std::string &out)
The strategy state file ([strategy] state_file, written by write_file_atomic): its bytes,...
constexpr std::int64_t utc_day_start(std::int64_t wall_ns) noexcept
00:00 UTC of the day wall_ns falls in.
Definition session_state.hpp:70
bool start_loss_period(KillState &s, std::int64_t period_start_ns) noexcept
Starts the loss budget's period that holds wall_ns when the state counts an earlier one: realized and...
Definition session_state.hpp:77
KillReason
Why a kill switch (global or one venue's) was tripped.
Definition enums.hpp:603
Fixed< struct NotionalTag > Notional
Definition fixed_point.hpp:334
Result< void, std::string > write_strategy_state(const std::string &path, std::string_view bytes, std::uint32_t version)
The [strategy] state_file as the engine's state() bytes go into it: a 16-byte head ("FMSTATE1",...
Result< void, std::string > write_file_atomic(const std::string &path, std::string_view contents)
Writes contents to path atomically: <path>.tmp, fsync, rename, fsync of the directory.
Result< bool, std::string > read_strategy_state(const std::string &path, std::string &out, std::uint32_t &version)
Cumulative risk state across sessions.
Definition session_state.hpp:54
Notional carry() const noexcept
The carry [risk] max_loss is measured against, on top of the running session's PnL.
Definition session_state.hpp:66
Notional realized
Definition session_state.hpp:57
std::int64_t updated_ns
wall clock of the last write
Definition session_state.hpp:60
KillReason reason
Definition session_state.hpp:56
std::int64_t period_start_ns
[risk] max_loss_period = "utc_day": the 00:00 UTC (wall ns) realized and fees count from; 0 for "tota...
Definition session_state.hpp:63
bool latched
a max-loss trip is latched: refuse to start until an operator clears it
Definition session_state.hpp:55
std::uint64_t sessions
Definition session_state.hpp:59
Notional fees
Definition session_state.hpp:58