diff options
| author | IlyassRS <ilyassroussi2000@gmail.com> | 2026-11-25 17:45:11 +0100 |
|---|---|---|
| committer | IlyassRS <ilyassroussi2000@gmail.com> | 2026-11-25 17:45:11 +0100 |
| commit | ec95c3c0b451ae10e84da2f9f0150aec5a7ee56f (patch) | |
| tree | edab9a26a0e95345143ddd1e9414dd53e5718b5d /runtime/src | |
| parent | 9e7720c9e764211e0021aa23907d2d3ecd58963f (diff) | |
runtime: pause list value wait timeouts while submitted or held work can still satisfy them
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/list_value_wait_budget.h | 214 |
1 files changed, 214 insertions, 0 deletions
diff --git a/runtime/src/list_value_wait_budget.h b/runtime/src/list_value_wait_budget.h new file mode 100644 index 000000000..625e6c1f3 --- /dev/null +++ b/runtime/src/list_value_wait_budget.h | |||
| @@ -0,0 +1,214 @@ | |||
| 1 | #pragma once | ||
| 2 | // Work-aware timeout clock and cycle-safe producer dependency analysis for console command-list memory waits | ||
| 3 | // (runtime/shims/d3d12_xs_list_values.h, docs/d3d12_xs_console_abi.md §5.5). | ||
| 4 | // | ||
| 5 | // A memory wait gate needs a bounded timeout so an unmodelled write (one the runtime never lands) cannot hang a queue | ||
| 6 | // forever. Counting wall-clock time blindly cuts waits short whenever the runtime is busy running held work (e.g. | ||
| 7 | // multi-second CPU shadow/texture copies inside the releaser) or while an in-flight producer write has not finished on | ||
| 8 | // the GPU yet, running dependent queues out of order. | ||
| 9 | // | ||
| 10 | // This header separates the policy from Windows/D3D12 handles so it can be verified portably under sanitizers: | ||
| 11 | // - `pause_reason()` decides whether submitted or held work can still satisfy a gate without creating a deadlock cycle: | ||
| 12 | // 1. `releaser_busy`: held work from an already-opened gate is currently being released/submitted; | ||
| 13 | // 2. `matching_producer`: an in-flight WriteGroup whose final write to a waited address can satisfy an unmet wait, and | ||
| 14 | // every pending gate preceding that WriteGroup is cycle-free with respect to the waiting queue; | ||
| 15 | // 3. `active_work`: unrelated unblocked GPU work (a WriteGroup or action gate with no pending gate ahead of it) is | ||
| 16 | // still executing, paused only up to a finite cap so a continuously streaming queue cannot starve an unmodelled wait. | ||
| 17 | // - `GateTimeoutState` tracks idle wait time (`elapsed_us`) separately from paused time, expiring only once `elapsed_us` | ||
| 18 | // reaches `budget_us`. | ||
| 19 | // | ||
| 20 | // Pure C++20 standard library only. | ||
| 21 | |||
| 22 | #include <algorithm> | ||
| 23 | #include <cstddef> | ||
| 24 | #include <cstdint> | ||
| 25 | #include <span> | ||
| 26 | #include <vector> | ||
| 27 | |||
| 28 | namespace leonida::list_value_wait { | ||
| 29 | |||
| 30 | enum class PauseReason : std::uint8_t { | ||
| 31 | none = 0, // no submitted or held work can still satisfy this gate -> timeout clock ticks | ||
| 32 | releaser_busy = 1, // releaser thread is executing or draining opened gates | ||
| 33 | matching_producer = 2, // an acyclic in-flight WriteGroup can satisfy an unmet wait of this gate | ||
| 34 | active_work = 3, // unrelated unblocked submitted work is still executing (bounded by active_pause_cap_us) | ||
| 35 | }; | ||
| 36 | |||
| 37 | struct WaitPredicate { | ||
| 38 | std::uint64_t address = 0; | ||
| 39 | std::uint64_t reference = 0; | ||
| 40 | std::uint32_t comparison = 0; | ||
| 41 | bool wide = false; | ||
| 42 | }; | ||
| 43 | |||
| 44 | struct PendingWrite { | ||
| 45 | std::uint64_t address = 0; | ||
| 46 | std::uint64_t value = 0; | ||
| 47 | std::uint8_t bytes = 0; | ||
| 48 | bool timestamp = false; // GPU/system timestamp: final value is unknown until landing | ||
| 49 | }; | ||
| 50 | |||
| 51 | struct PendingWriteGroup { | ||
| 52 | const void* queue = nullptr; | ||
| 53 | std::uint64_t after_gate = 0; // highest pending gate value on `queue` ahead of this group when submitted (0: none) | ||
| 54 | std::span<const PendingWrite> writes; | ||
| 55 | }; | ||
| 56 | |||
| 57 | struct PendingGate { | ||
| 58 | const void* queue = nullptr; | ||
| 59 | std::uint64_t value = 0; | ||
| 60 | bool is_action = false; // waits only on earlier GPU work on `queue`, never on memory | ||
| 61 | std::span<const WaitPredicate> unmet_waits; // waits not currently satisfied in memory (empty for action gates) | ||
| 62 | }; | ||
| 63 | |||
| 64 | // Default cap on pausing solely for unrelated unblocked GPU work: 300x the idle timeout, clamped to [1 s, 10 min]. | ||
| 65 | // At the default 200 ms timeout this allows up to 60 s for slow world-phase frames while preventing an unrelated | ||
| 66 | // continuously streaming queue from starving an unmodelled wait indefinitely. | ||
| 67 | inline constexpr std::uint64_t default_active_pause_cap_us(unsigned full_timeout_ms) { | ||
| 68 | const std::uint64_t base_ms = full_timeout_ms ? std::uint64_t{full_timeout_ms} : 1u; | ||
| 69 | const std::uint64_t scaled_ms = base_ms > 2000u ? 600000u : base_ms * 300u; | ||
| 70 | const std::uint64_t clamped_ms = scaled_ms < 1000u ? 1000u : (scaled_ms > 600000u ? 600000u : scaled_ms); | ||
| 71 | return clamped_ms * 1000u; | ||
| 72 | } | ||
| 73 | |||
| 74 | struct GateTimeoutState { | ||
| 75 | std::uint64_t budget_us = 0; | ||
| 76 | std::uint64_t elapsed_us = 0; | ||
| 77 | std::uint64_t active_paused_us = 0; | ||
| 78 | std::uint64_t active_pause_cap_us = 0; | ||
| 79 | bool paused_ever = false; | ||
| 80 | |||
| 81 | void reset(std::uint64_t budget, std::uint64_t active_cap) { | ||
| 82 | budget_us = budget; | ||
| 83 | elapsed_us = 0; | ||
| 84 | active_paused_us = 0; | ||
| 85 | active_pause_cap_us = active_cap; | ||
| 86 | paused_ever = false; | ||
| 87 | } | ||
| 88 | |||
| 89 | // Advances the gate by `dt_us` under `reason`. Returns true once the idle timeout budget is exhausted. | ||
| 90 | bool advance(std::uint64_t dt_us, PauseReason reason) { | ||
| 91 | if (reason == PauseReason::releaser_busy || reason == PauseReason::matching_producer) { | ||
| 92 | paused_ever = true; | ||
| 93 | return false; | ||
| 94 | } | ||
| 95 | if (reason == PauseReason::active_work && active_paused_us < active_pause_cap_us) { | ||
| 96 | paused_ever = true; | ||
| 97 | const std::uint64_t room = active_pause_cap_us - active_paused_us; | ||
| 98 | const std::uint64_t absorb = dt_us < room ? dt_us : room; | ||
| 99 | active_paused_us += absorb; | ||
| 100 | dt_us -= absorb; | ||
| 101 | } | ||
| 102 | if (dt_us > UINT64_MAX - elapsed_us) | ||
| 103 | elapsed_us = UINT64_MAX; | ||
| 104 | else | ||
| 105 | elapsed_us += dt_us; | ||
| 106 | return elapsed_us >= budget_us; | ||
| 107 | } | ||
| 108 | }; | ||
| 109 | |||
| 110 | namespace detail { | ||
| 111 | |||
| 112 | // True when `group`'s final write touching [wait.address, wait.address + need) lands a value that meets `wait` | ||
| 113 | // (or is a timestamp write to the exact address whose runtime value is unknown until landing). | ||
| 114 | template <class MetFn> | ||
| 115 | inline bool write_group_matches_wait(const PendingWriteGroup& group, const WaitPredicate& wait, MetFn&& met) { | ||
| 116 | const std::uint64_t need = wait.wide ? 8u : 4u; | ||
| 117 | if (!wait.address || wait.address > UINT64_MAX - need) return false; | ||
| 118 | const std::uint64_t wait_end = wait.address + need; | ||
| 119 | for (std::size_t i = group.writes.size(); i-- > 0;) { | ||
| 120 | const PendingWrite& w = group.writes[i]; | ||
| 121 | if (!w.bytes || w.address > UINT64_MAX - w.bytes) continue; | ||
| 122 | const std::uint64_t write_end = w.address + w.bytes; | ||
| 123 | if (w.address >= wait_end || wait.address >= write_end) continue; | ||
| 124 | if (w.address == wait.address && w.bytes >= need && | ||
| 125 | (w.timestamp || met(wait.comparison, w.value, wait.reference, wait.wide))) | ||
| 126 | return true; | ||
| 127 | // A later write in the same group overwrote or partially overlapped the waited range with a non-matching value. | ||
| 128 | return false; | ||
| 129 | } | ||
| 130 | return false; | ||
| 131 | } | ||
| 132 | |||
| 133 | // True when no pending gate in `gates` precedes `(queue, after_gate)`. | ||
| 134 | inline bool is_unblocked(std::span<const PendingGate> gates, const void* queue, std::uint64_t after_gate) { | ||
| 135 | if (!after_gate) return true; | ||
| 136 | for (const PendingGate& g : gates) | ||
| 137 | if (g.queue == queue && g.value <= after_gate) return false; | ||
| 138 | return true; | ||
| 139 | } | ||
| 140 | |||
| 141 | // True when `gate_idx` (directly or transitively via matching in-flight writes) depends on a gate on `target_queue`. | ||
| 142 | template <class MetFn> | ||
| 143 | inline bool gate_can_reach_queue(std::size_t gate_idx, const void* target_queue, | ||
| 144 | std::span<const PendingWriteGroup> writes, std::span<const PendingGate> gates, | ||
| 145 | MetFn&& met, std::vector<std::size_t>& visiting) { | ||
| 146 | const PendingGate& g = gates[gate_idx]; | ||
| 147 | if (g.queue == target_queue) return true; | ||
| 148 | if (g.is_action) return false; | ||
| 149 | if (std::find(visiting.begin(), visiting.end(), gate_idx) != visiting.end()) return false; | ||
| 150 | visiting.push_back(gate_idx); | ||
| 151 | for (const WaitPredicate& wait : g.unmet_waits) { | ||
| 152 | for (const PendingWriteGroup& w : writes) { | ||
| 153 | if (!write_group_matches_wait(w, wait, met)) continue; | ||
| 154 | if (!w.after_gate) continue; | ||
| 155 | for (std::size_t b = 0; b < gates.size(); ++b) { | ||
| 156 | if (gates[b].queue != w.queue || gates[b].value > w.after_gate) continue; | ||
| 157 | if (gates[b].queue == target_queue || | ||
| 158 | gate_can_reach_queue(b, target_queue, writes, gates, met, visiting)) { | ||
| 159 | visiting.pop_back(); | ||
| 160 | return true; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | visiting.pop_back(); | ||
| 166 | return false; | ||
| 167 | } | ||
| 168 | |||
| 169 | } // namespace detail | ||
| 170 | |||
| 171 | // Decides whether the timeout clock of `gates[target_idx]` should pause on this pump pass. | ||
| 172 | // `met(comparison, value, reference, wide)` must implement the same predicate as `console_value_met`. | ||
| 173 | template <class MetFn> | ||
| 174 | inline PauseReason pause_reason(bool releaser_busy, std::span<const PendingWriteGroup> writes, | ||
| 175 | std::span<const PendingGate> gates, std::size_t target_idx, MetFn&& met) { | ||
| 176 | if (releaser_busy) return PauseReason::releaser_busy; | ||
| 177 | if (target_idx >= gates.size()) return PauseReason::none; | ||
| 178 | const PendingGate& target = gates[target_idx]; | ||
| 179 | if (target.is_action) return PauseReason::none; | ||
| 180 | |||
| 181 | // 1. A matching in-flight WriteGroup whose preceding gates (if any) do not cycle back to `target.queue`. | ||
| 182 | std::vector<std::size_t> visiting; | ||
| 183 | for (const WaitPredicate& wait : target.unmet_waits) { | ||
| 184 | for (const PendingWriteGroup& w : writes) { | ||
| 185 | if (!detail::write_group_matches_wait(w, wait, met)) continue; | ||
| 186 | bool cyclic = false; | ||
| 187 | if (w.after_gate) { | ||
| 188 | for (std::size_t b = 0; b < gates.size(); ++b) { | ||
| 189 | if (gates[b].queue != w.queue || gates[b].value > w.after_gate) continue; | ||
| 190 | visiting.clear(); | ||
| 191 | if (gates[b].queue == target.queue || | ||
| 192 | detail::gate_can_reach_queue(b, target.queue, writes, gates, met, visiting)) { | ||
| 193 | cyclic = true; | ||
| 194 | break; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | } | ||
| 198 | if (!cyclic) return PauseReason::matching_producer; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | // 2. Unrelated unblocked submitted GPU work (an unblocked WriteGroup on any queue, or an unblocked action gate on | ||
| 203 | // another queue) that could still write memory via a shader/copy or unblock a CPU producer. | ||
| 204 | for (const PendingWriteGroup& w : writes) { | ||
| 205 | if (detail::is_unblocked(gates, w.queue, w.after_gate)) return PauseReason::active_work; | ||
| 206 | } | ||
| 207 | for (const PendingGate& g : gates) { | ||
| 208 | if (!g.is_action || g.queue == target.queue) continue; | ||
| 209 | if (g.value == 0 || detail::is_unblocked(gates, g.queue, g.value - 1)) return PauseReason::active_work; | ||
| 210 | } | ||
| 211 | return PauseReason::none; | ||
| 212 | } | ||
| 213 | |||
| 214 | } // namespace leonida::list_value_wait | ||