15#include "fastmm/core/config_macros.hpp"
27struct VenueHealthView {
44 static constexpr std::size_t
kBuckets = 8;
55 if (FASTMM_UNLIKELY(v.value >=
kVenues))
return false;
56 Slot& s = slots_[v.value];
57 if (FASTMM_UNLIKELY(now >= s.bucket_end)) roll(s, now);
58 if (lag < s.cur_min) s.cur_min = lag;
62 if (FASTMM_LIKELY(limit.ns <= 0))
return false;
63 return over_limit(s, lag, now, limit);
66 if (v.value >=
kVenues || rtt.ns < 0)
return;
67 Slot& s = slots_[v.value];
69 s.srtt = s.ack_samples == 0 ? rtt.ns : s.srtt + (rtt.ns - s.srtt) / 8;
74 return v.value <
kVenues && now < slots_[v.value].gate_until;
79 const Slot& s = slots_[v.value];
80 if (s.md_samples != 0) {
81 const std::int64_t base = s.cur_min < s.done_min ? s.cur_min : s.done_min;
84 out.feed_lag_excess =
Duration{s.lag - base};
87 out.ack_rtt_smoothed = Duration{s.srtt};
88 out.md_updated = s.md_ts;
89 out.ack_updated = s.ack_ts;
90 out.md_samples = s.md_samples;
91 out.ack_samples = s.ack_samples;
92 out.gate_engagements = s.gate_engagements;
93 out.gated = now < s.gate_until;
98 static constexpr std::int64_t kNone = std::numeric_limits<std::int64_t>::max();
100 Timestamp bucket_end{};
101 std::int64_t cur_min = kNone;
102 std::int64_t done_min = kNone;
103 std::int64_t lag = 0;
105 Timestamp gate_until{};
106 std::uint64_t md_samples = 0;
107 std::uint64_t gate_engagements = 0;
108 std::array<std::int64_t,
kBuckets - 1> done{};
109 std::uint32_t next = 0;
110 std::int64_t rtt = 0;
111 std::int64_t srtt = 0;
113 std::uint64_t ack_samples = 0;
116 static bool over_limit(Slot& s, std::int64_t lag, Timestamp now, Duration limit)
noexcept {
117 const std::int64_t base = s.cur_min < s.done_min ? s.cur_min : s.done_min;
118 if (lag - base <= limit.ns)
return false;
119 const bool engaged = now >= s.gate_until;
121 if (engaged) ++s.gate_engagements;
125 FASTMM_NOINLINE
static void roll(Slot& s, Timestamp now)
noexcept {
126 if (!s.bucket_end.valid() || now - s.bucket_end >=
kWindow) {
132 while (now >= s.bucket_end) {
133 s.done[s.next] = s.cur_min;
134 s.next = (s.next + 1) %
static_cast<std::uint32_t
>(s.done.size());
136 s.bucket_end = s.bucket_end +
kBucket;
140 for (
const std::int64_t m : s.done)
141 if (m < s.done_min) s.done_min = m;
144 std::array<Slot, kVenues> slots_{};
Definition venue_health.hpp:43
static constexpr Duration kWindow
Definition venue_health.hpp:48
VenueHealthView view(VenueId v, Timestamp now) const noexcept
Definition venue_health.hpp:78
static constexpr std::size_t kBuckets
Definition venue_health.hpp:46
static constexpr Duration kGateHold
Definition venue_health.hpp:49
FASTMM_FORCE_INLINE bool gated(VenueId v, Timestamp now) const noexcept
Definition venue_health.hpp:75
static constexpr Duration kBucket
Definition venue_health.hpp:47
FASTMM_FORCE_INLINE bool on_md(VenueId v, std::int64_t lag, Timestamp now, Duration limit) noexcept
A market-data message of venue v with lag = recv_ts - exch_ts, consumed at now.
Definition venue_health.hpp:53
void on_ack(VenueId v, Duration rtt, Timestamp now) noexcept
Definition venue_health.hpp:67
static constexpr std::size_t kVenues
kMaxVenues
Definition venue_health.hpp:45
Definition strategy.hpp:16
constexpr Duration milliseconds(std::int64_t v) noexcept
Definition time.hpp:38
constexpr Duration seconds(std::int64_t v) noexcept
Definition time.hpp:41
Strongly typed identifiers.
std::int64_t ns
Definition time.hpp:23
What StrategyContext::venue_health returns. Durations are zero until the first sample.
Definition venue_health.hpp:29
std::uint64_t ack_samples
Definition venue_health.hpp:38
std::uint64_t gate_engagements
times the feed-lag gate engaged
Definition venue_health.hpp:39
bool gated
the feed-lag gate holds this venue now
Definition venue_health.hpp:40
std::uint64_t md_samples
Definition venue_health.hpp:37
Duration feed_lag
recv_ts - exch_ts of the latest market-data message
Definition venue_health.hpp:30
Duration ack_rtt_smoothed
Definition venue_health.hpp:34
Timestamp ack_updated
... and of the latest ack sample
Definition venue_health.hpp:36
Duration ack_rtt
latest send -> first ack
Definition venue_health.hpp:33
Duration feed_lag_base
its minimum over the last kWindow
Definition venue_health.hpp:31
Duration feed_lag_excess
feed_lag - feed_lag_base
Definition venue_health.hpp:32
Timestamp md_updated
engine time of the latest feed-lag sample
Definition venue_health.hpp:35