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;
78 const Slot& f = from.slots_[v];
79 s.bucket_end = f.bucket_end;
80 s.cur_min = f.cur_min;
81 s.done_min = f.done_min;
84 s.md_samples = f.md_samples;
89 return v.value <
kVenues && now < slots_[v.value].gate_until;
94 const Slot& s = slots_[v.value];
95 if (s.md_samples != 0) {
96 const std::int64_t base = s.cur_min < s.done_min ? s.cur_min : s.done_min;
99 out.feed_lag_excess =
Duration{s.lag - base};
102 out.ack_rtt_smoothed = Duration{s.srtt};
103 out.md_updated = s.md_ts;
104 out.ack_updated = s.ack_ts;
105 out.md_samples = s.md_samples;
106 out.ack_samples = s.ack_samples;
107 out.gate_engagements = s.gate_engagements;
108 out.gated = now < s.gate_until;
113 static constexpr std::int64_t kNone = std::numeric_limits<std::int64_t>::max();
115 Timestamp bucket_end{};
116 std::int64_t cur_min = kNone;
117 std::int64_t done_min = kNone;
118 std::int64_t lag = 0;
120 Timestamp gate_until{};
121 std::uint64_t md_samples = 0;
122 std::uint64_t gate_engagements = 0;
123 std::array<std::int64_t,
kBuckets - 1> done{};
124 std::uint32_t next = 0;
125 std::int64_t rtt = 0;
126 std::int64_t srtt = 0;
128 std::uint64_t ack_samples = 0;
131 static bool over_limit(Slot& s, std::int64_t lag, Timestamp now, Duration limit)
noexcept {
132 const std::int64_t base = s.cur_min < s.done_min ? s.cur_min : s.done_min;
133 if (lag - base <= limit.ns)
return false;
134 const bool engaged = now >= s.gate_until;
136 if (engaged) ++s.gate_engagements;
140 FASTMM_NOINLINE
static void roll(Slot& s, Timestamp now)
noexcept {
141 if (!s.bucket_end.valid() || now - s.bucket_end >=
kWindow) {
147 while (now >= s.bucket_end) {
148 s.done[s.next] = s.cur_min;
149 s.next = (s.next + 1) %
static_cast<std::uint32_t
>(s.done.size());
151 s.bucket_end = s.bucket_end +
kBucket;
155 for (
const std::int64_t m : s.done)
156 if (m < s.done_min) s.done_min = m;
159 std::array<Slot, kVenues> slots_{};
Definition venue_health.hpp:43
static constexpr Duration kWindow
Definition venue_health.hpp:48
void copy_feed(const VenueHealth &from, std::size_t v) noexcept
Venue v's feed-lag half of from (not the gate, nor the acks): it followed the same messages.
Definition venue_health.hpp:77
VenueHealthView view(VenueId v, Timestamp now) const noexcept
Definition venue_health.hpp:93
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:90
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:23
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