diff options
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/address_guard.h | 163 | ||||
| -rw-r--r-- | runtime/src/conservative_raster.h | 17 | ||||
| -rw-r--r-- | runtime/src/console_heap_copy.h | 97 | ||||
| -rw-r--r-- | runtime/src/dstorage_hold.h | 23 | ||||
| -rw-r--r-- | runtime/src/heavy_pipeline_cache.h | 94 | ||||
| -rw-r--r-- | runtime/src/list_value_wait_budget.h | 251 | ||||
| -rw-r--r-- | runtime/src/title_geometry_watch.h | 18 | ||||
| -rw-r--r-- | runtime/src/title_texture_memory.h | 28 | ||||
| -rw-r--r-- | runtime/src/title_va_alias.h | 24 |
9 files changed, 712 insertions, 3 deletions
diff --git a/runtime/src/address_guard.h b/runtime/src/address_guard.h new file mode 100644 index 000000000..dab43d40a --- /dev/null +++ b/runtime/src/address_guard.h | |||
| @@ -0,0 +1,163 @@ | |||
| 1 | // The address guard's page table (docs/d3d12_vulkan.md, "Address guard"): what the runtime backs around heap_base, | ||
| 2 | // for shaders translated with `tools/rdna2spirv.py --address-guard` (LEONIDA_ADDRESS_GUARD=1). Header-only, no | ||
| 3 | // Vulkan: the device writes the words this computes into a mapped buffer. tools/address_guard.py is the Python | ||
| 4 | // reference of the same format and of the check a guarded access makes. | ||
| 5 | // | ||
| 6 | // Layout. A header at arena offset kGuardHeader (64 bytes, AddressGuardHeader), then two sinks: refused loads read | ||
| 7 | // kGuardZero, refused stores and atomics write kGuardDump, each at the access's offset within its 4 KiB page. The | ||
| 8 | // page table is its own buffer; the header holds its address minus heap_base. Table words: kGuardLevel1 level-1 | ||
| 9 | // words indexed by bits 47..32 of the 48-bit heap offset (one per 4 GiB chunk), naming the chunk's level-2 block; | ||
| 10 | // then the blocks, kGuardBlockWords words each, one bit per 4 KiB page (bits 31..12). Block 0 is empty (nothing | ||
| 11 | // backed), block 1 full (everything passes: a chunk the table has no room for), blocks 2.. are allocated. | ||
| 12 | #pragma once | ||
| 13 | |||
| 14 | #include <algorithm> | ||
| 15 | #include <cstdint> | ||
| 16 | #include <cstring> | ||
| 17 | #include <utility> | ||
| 18 | #include <vector> | ||
| 19 | |||
| 20 | namespace leonida::address_guard { | ||
| 21 | |||
| 22 | // Shared with tools/rdna2spirv.py (GUARD_*) and tools/address_guard.py; tests/semantic/test_address_guard.py reads | ||
| 23 | // these lines. | ||
| 24 | constexpr std::uint64_t kGuardHeader = 0x70000; | ||
| 25 | constexpr std::uint64_t kGuardZero = 0x71000; | ||
| 26 | constexpr std::uint64_t kGuardDump = 0x78000; | ||
| 27 | constexpr std::uint64_t kGuardEnd = 0x80000; | ||
| 28 | constexpr std::uint32_t kGuardLevel1 = 1u << 16; | ||
| 29 | constexpr std::uint32_t kGuardBlockWords = 1u << 15; | ||
| 30 | constexpr char kGuardMagicText[] = "LGUARD01"; | ||
| 31 | constexpr std::uint64_t kMask48 = (1ull << 48) - 1; | ||
| 32 | constexpr std::uint32_t kEmptyBlock = 0, kFullBlock = 1, kFirstBlock = 2; | ||
| 33 | |||
| 34 | constexpr std::uint64_t magic() { | ||
| 35 | std::uint64_t v = 0; | ||
| 36 | for (int i = 7; i >= 0; --i) v = v << 8 | std::uint8_t(kGuardMagicText[i]); | ||
| 37 | return v; | ||
| 38 | } | ||
| 39 | |||
| 40 | struct AddressGuardHeader { | ||
| 41 | std::uint64_t magic; // magic(): the shaders check only when it is there | ||
| 42 | std::uint64_t limit; // the arena's size: an access below it passes without a lookup | ||
| 43 | std::uint64_t table; // the page table's device address minus heap_base (mod 2^64) | ||
| 44 | std::uint32_t refused; // the shaders add 1 per refused access | ||
| 45 | std::uint32_t last_module; // the last refusing module's id: FNV-1a of its file stem (shaders write it; diagnostics) | ||
| 46 | std::uint64_t last_refused; // the last refused access's heap offset (written by the shaders, not atomically) | ||
| 47 | std::uint8_t pad[24]; | ||
| 48 | }; | ||
| 49 | static_assert(sizeof(AddressGuardHeader) == 64); | ||
| 50 | static_assert(kGuardHeader + sizeof(AddressGuardHeader) <= kGuardZero && kGuardZero < kGuardDump && | ||
| 51 | kGuardDump < kGuardEnd); | ||
| 52 | |||
| 53 | // The table's contents and the words that changed. Blocks are given to chunks for good: a chunk that loses its last | ||
| 54 | // backing keeps its (now empty) block, so a submission still in flight never reads a block that came to mean | ||
| 55 | // another chunk. When the blocks run out, a new chunk gets the full block (logged by the caller through | ||
| 56 | // overflowed()): its accesses then pass unchecked, as without the guard. | ||
| 57 | class AddressGuardTable { | ||
| 58 | public: | ||
| 59 | explicit AddressGuardTable(std::uint32_t blocks = 62) : blocks_(blocks) { | ||
| 60 | words_.assign(word_count(), 0); | ||
| 61 | std::fill_n(words_.begin() + kGuardLevel1 + std::size_t(kFullBlock) * kGuardBlockWords, kGuardBlockWords, | ||
| 62 | 0xFFFFFFFFu); | ||
| 63 | } | ||
| 64 | |||
| 65 | std::size_t word_count() const { return kGuardLevel1 + std::size_t(kFirstBlock + blocks_) * kGuardBlockWords; } | ||
| 66 | std::size_t bytes() const { return word_count() * sizeof(std::uint32_t); } | ||
| 67 | // The whole table, for the first upload. | ||
| 68 | const std::vector<std::uint32_t>& words() const { return words_; } | ||
| 69 | std::uint32_t blocks_used() const { return used_; } | ||
| 70 | std::uint32_t overflowed() const { return overflowed_; } | ||
| 71 | |||
| 72 | // Make the table say exactly `spans` (heap offset [start, end), 48-bit; a span may wrap through 2^48 for a | ||
| 73 | // negative offset). A page any byte of a span touches is backed: the GPU maps memory in whole pages. Each word | ||
| 74 | // that changes is passed to `write(index, value)`, level-2 words before level-1 ones, so a reader that sees a | ||
| 75 | // chunk's new block also sees its bits. Returns the number of words written. | ||
| 76 | template <class Write> | ||
| 77 | std::size_t assign(const std::vector<std::pair<std::uint64_t, std::uint64_t>>& spans, Write&& write) { | ||
| 78 | next_.assign(std::size_t(used_) * kGuardBlockWords, 0); | ||
| 79 | std::vector<std::uint32_t> level1(words_.begin(), words_.begin() + kGuardLevel1); | ||
| 80 | for (const auto& [start, end] : spans) { | ||
| 81 | if (end == start) continue; | ||
| 82 | const std::uint64_t first = (start & kMask48) >> 12; | ||
| 83 | const std::uint64_t last = ((end - 1) & kMask48) >> 12; | ||
| 84 | if (first <= last) { | ||
| 85 | mark(level1, first, last); | ||
| 86 | } else { // wraps through 2^48: two runs | ||
| 87 | mark(level1, first, (kMask48 >> 12)); | ||
| 88 | mark(level1, 0, last); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | std::size_t written = 0; | ||
| 92 | const std::size_t base = kGuardLevel1 + std::size_t(kFirstBlock) * kGuardBlockWords; | ||
| 93 | for (std::size_t i = 0; i < next_.size(); ++i) { | ||
| 94 | if (words_[base + i] == next_[i]) continue; | ||
| 95 | words_[base + i] = next_[i]; | ||
| 96 | write(base + i, next_[i]); | ||
| 97 | ++written; | ||
| 98 | } | ||
| 99 | for (std::uint32_t i = 0; i < kGuardLevel1; ++i) { | ||
| 100 | if (words_[i] == level1[i]) continue; | ||
| 101 | words_[i] = level1[i]; | ||
| 102 | write(std::size_t(i), level1[i]); | ||
| 103 | ++written; | ||
| 104 | } | ||
| 105 | return written; | ||
| 106 | } | ||
| 107 | |||
| 108 | // The table's bit for the page of heap offset `offset`. | ||
| 109 | bool page_backed(std::uint64_t offset) const { | ||
| 110 | offset &= kMask48; | ||
| 111 | const std::uint32_t block = words_[(offset >> 32) & (kGuardLevel1 - 1)]; | ||
| 112 | const std::uint64_t page = (offset >> 12) & 0xFFFFF; | ||
| 113 | return (words_[kGuardLevel1 + std::size_t(block) * kGuardBlockWords + (page >> 5)] >> (page & 31)) & 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | // What a guarded access of `extent` bytes at heap offset `offset` does (the shaders' %guard_fn): passes when | ||
| 117 | // its first and last byte are below `limit` or on backed pages. `offset` is sign-extended from 48 bits. | ||
| 118 | bool passes(std::uint64_t offset, std::uint64_t extent, std::uint64_t limit) const { | ||
| 119 | if (offset & (1ull << 47)) offset |= ~kMask48; | ||
| 120 | else offset &= kMask48; | ||
| 121 | const std::uint64_t end = offset + extent - 1; | ||
| 122 | if (offset < limit && end < limit) return true; | ||
| 123 | return page_backed(offset) && page_backed(end); | ||
| 124 | } | ||
| 125 | |||
| 126 | private: | ||
| 127 | // Set the bits of pages [first, last] (48-bit page numbers), giving their chunks blocks as needed. | ||
| 128 | void mark(std::vector<std::uint32_t>& level1, std::uint64_t first, std::uint64_t last) { | ||
| 129 | for (std::uint64_t chunk = first >> 20; chunk <= last >> 20; ++chunk) { | ||
| 130 | std::uint32_t& block = level1[chunk]; | ||
| 131 | if (block == kEmptyBlock) { | ||
| 132 | if (used_ < blocks_) { | ||
| 133 | block = kFirstBlock + used_++; | ||
| 134 | next_.resize(std::size_t(used_) * kGuardBlockWords, 0); | ||
| 135 | } else { | ||
| 136 | block = kFullBlock; | ||
| 137 | ++overflowed_; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | if (block < kFirstBlock) continue; | ||
| 141 | std::uint32_t* bits = next_.data() + std::size_t(block - kFirstBlock) * kGuardBlockWords; | ||
| 142 | const std::uint64_t lo = std::max(first, chunk << 20) & 0xFFFFF; | ||
| 143 | const std::uint64_t hi = std::min(last, (chunk << 20) | 0xFFFFF) & 0xFFFFF; | ||
| 144 | for (std::uint64_t page = lo; page <= hi;) { | ||
| 145 | if (!(page & 31) && page + 31 <= hi) { | ||
| 146 | bits[page >> 5] = 0xFFFFFFFFu; | ||
| 147 | page += 32; | ||
| 148 | } else { | ||
| 149 | bits[page >> 5] |= 1u << (page & 31); | ||
| 150 | ++page; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | std::uint32_t blocks_; | ||
| 157 | std::uint32_t used_ = 0; | ||
| 158 | std::uint32_t overflowed_ = 0; | ||
| 159 | std::vector<std::uint32_t> words_; // what the GPU's copy holds | ||
| 160 | std::vector<std::uint32_t> next_; // the allocated blocks being built by assign() | ||
| 161 | }; | ||
| 162 | |||
| 163 | } // namespace leonida::address_guard | ||
diff --git a/runtime/src/conservative_raster.h b/runtime/src/conservative_raster.h index 469066d17..82d6dfaca 100644 --- a/runtime/src/conservative_raster.h +++ b/runtime/src/conservative_raster.h | |||
| @@ -46,4 +46,21 @@ inline InnerCoverageRequirements inner_coverage_requirements(std::span<const std | |||
| 46 | result.valid = true; | 46 | result.valid = true; |
| 47 | return result; | 47 | return result; |
| 48 | } | 48 | } |
| 49 | |||
| 50 | // True when the mesh shader module outputs triangles (OutputTrianglesEXT, 5298). A module with lines (5269), points (27) | ||
| 51 | // or no output mode does not. Each mesh entry point of a valid module has exactly one of the three. | ||
| 52 | inline bool mesh_outputs_triangles(std::span<const std::uint32_t> words) { | ||
| 53 | if (words.size() < 5 || words[0] != 0x07230203) return false; | ||
| 54 | bool triangles = false; | ||
| 55 | for (std::size_t i = 5; i < words.size();) { | ||
| 56 | const auto count = words[i] >> 16, op = words[i] & 0xffff; | ||
| 57 | if (!count || count > words.size() - i) return false; | ||
| 58 | if (op == 16 && count >= 3) { // OpExecutionMode | ||
| 59 | if (words[i + 2] == 5298) triangles = true; | ||
| 60 | else if (words[i + 2] == 5269 || words[i + 2] == 27) return false; | ||
| 61 | } | ||
| 62 | i += count; | ||
| 63 | } | ||
| 64 | return triangles; | ||
| 65 | } | ||
| 49 | } // namespace leonida::conservative_raster | 66 | } // namespace leonida::conservative_raster |
diff --git a/runtime/src/console_heap_copy.h b/runtime/src/console_heap_copy.h new file mode 100644 index 000000000..96a1f1ad4 --- /dev/null +++ b/runtime/src/console_heap_copy.h | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | #pragma once | ||
| 2 | // A console resource heap's translated copy (DeviceState::ConsoleHeap::copy): the bytes shaders read, and a host-RAM | ||
| 3 | // mirror of them that the CPU reads instead. | ||
| 4 | // | ||
| 5 | // The copy lies in the GPU address arena when the import of host memory is not used (arena.mapped + offset), and the | ||
| 6 | // arena prefers DEVICE_LOCAL | HOST_VISIBLE memory (ReBAR): a CPU read of it is an uncached read across PCIe. The | ||
| 7 | // release sweep of refresh_console_heap with LEONIDA_CONSOLE_RELEASE_FILTER=1 reads words 0-2 of both V# halves of | ||
| 8 | // every live V# slot (about 470k in a game run) to decide whether the slot needs fixing, and that read cost | ||
| 9 | // 0.9-1.0 s a sweep. Every CPU write goes through write() and zero(), which write both the copy and the mirror, so | ||
| 10 | // view() (the mirror, or the copy when there is none) holds what the GPU reads. | ||
| 11 | // | ||
| 12 | // No mirror when the copy is already host RAM (the import of the heap's own scratch), when LEONIDA_HEAP_MIRROR=0, or | ||
| 13 | // once the copy is written by the GPU pass (LEONIDA_CONSOLE_HEAP_GPU: drop_mirror(); the CPU neither writes nor reads | ||
| 14 | // the copy then). The mirror covers the heap's bytes only, not the null guard after them (written once, at creation). | ||
| 15 | // | ||
| 16 | // LEONIDA_HEAP_MIRROR_CHECK=1: refresh_console_heap compares the mirror with the copy at every release sweep | ||
| 17 | // (mismatches()) and logs the first slots that differ. | ||
| 18 | |||
| 19 | #include <algorithm> | ||
| 20 | #include <cstddef> | ||
| 21 | #include <cstdint> | ||
| 22 | #include <cstring> | ||
| 23 | #include <vector> | ||
| 24 | |||
| 25 | namespace leonida { | ||
| 26 | |||
| 27 | class ConsoleHeapCopy { | ||
| 28 | public: | ||
| 29 | // `gpu`: the copy shaders read; `bytes`: the heap's bytes (the mirror's size, when `mirrored`). | ||
| 30 | void attach(std::uint8_t* gpu, std::size_t bytes, bool mirrored) { | ||
| 31 | gpu_ = gpu; | ||
| 32 | bytes_ = bytes; | ||
| 33 | mirror_.clear(); | ||
| 34 | mirror_.shrink_to_fit(); | ||
| 35 | if (gpu && mirrored) mirror_.assign(bytes, 0); | ||
| 36 | } | ||
| 37 | // The mirror goes; view() is the copy from now on. | ||
| 38 | void drop_mirror() { | ||
| 39 | mirror_.clear(); | ||
| 40 | mirror_.shrink_to_fit(); | ||
| 41 | } | ||
| 42 | |||
| 43 | explicit operator bool() const noexcept { return gpu_ != nullptr; } | ||
| 44 | std::uint8_t* gpu() const noexcept { return gpu_; } | ||
| 45 | bool mirrored() const noexcept { return !mirror_.empty(); } | ||
| 46 | std::size_t bytes() const noexcept { return bytes_; } | ||
| 47 | |||
| 48 | // What the CPU reads: the mirror, or the copy itself. | ||
| 49 | const std::uint8_t* view() const noexcept { return mirror_.empty() ? gpu_ : mirror_.data(); } | ||
| 50 | |||
| 51 | // [at, at + n) of the copy, and of the mirror where it reaches. | ||
| 52 | void write(std::size_t at, const void* bytes, std::size_t n) { | ||
| 53 | std::memcpy(gpu_ + at, bytes, n); | ||
| 54 | if (const std::size_t m = mirror_span(at, n)) std::memcpy(mirror_.data() + at, bytes, m); | ||
| 55 | } | ||
| 56 | void zero(std::size_t at, std::size_t n) { | ||
| 57 | std::memset(gpu_ + at, 0, n); | ||
| 58 | if (const std::size_t m = mirror_span(at, n)) std::memset(mirror_.data() + at, 0, m); | ||
| 59 | } | ||
| 60 | |||
| 61 | // LEONIDA_HEAP_MIRROR_CHECK: the slots of `slot_bytes` whose mirror bytes differ from the copy's, at most `limit` | ||
| 62 | // of them in `out` (in ascending order); returns how many slots differ in all. 0 without a mirror. | ||
| 63 | std::size_t mismatches(std::size_t slot_bytes, std::size_t limit, std::vector<std::size_t>& out) const { | ||
| 64 | out.clear(); | ||
| 65 | if (mirror_.empty() || !slot_bytes) return 0; | ||
| 66 | // Read the copy in large blocks: one pass over device memory, compared in host RAM. | ||
| 67 | constexpr std::size_t kBlock = std::size_t(1) << 16; | ||
| 68 | std::vector<std::uint8_t> block; | ||
| 69 | std::size_t count = 0; | ||
| 70 | const std::size_t step = std::max(slot_bytes, kBlock / slot_bytes * slot_bytes); | ||
| 71 | for (std::size_t at = 0; at < bytes_; at += step) { | ||
| 72 | const std::size_t n = std::min(step, bytes_ - at); | ||
| 73 | block.resize(n); | ||
| 74 | std::memcpy(block.data(), gpu_ + at, n); | ||
| 75 | if (!std::memcmp(block.data(), mirror_.data() + at, n)) continue; | ||
| 76 | for (std::size_t s = 0; s < n; s += slot_bytes) { | ||
| 77 | const std::size_t k = std::min(slot_bytes, n - s); | ||
| 78 | if (!std::memcmp(block.data() + s, mirror_.data() + at + s, k)) continue; | ||
| 79 | if (out.size() < limit) out.push_back((at + s) / slot_bytes); | ||
| 80 | ++count; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | return count; | ||
| 84 | } | ||
| 85 | |||
| 86 | private: | ||
| 87 | std::size_t mirror_span(std::size_t at, std::size_t n) const noexcept { | ||
| 88 | if (mirror_.empty() || at >= mirror_.size()) return 0; | ||
| 89 | return std::min(n, mirror_.size() - at); | ||
| 90 | } | ||
| 91 | |||
| 92 | std::uint8_t* gpu_ = nullptr; | ||
| 93 | std::size_t bytes_ = 0; | ||
| 94 | std::vector<std::uint8_t> mirror_; | ||
| 95 | }; | ||
| 96 | |||
| 97 | } // namespace leonida | ||
diff --git a/runtime/src/dstorage_hold.h b/runtime/src/dstorage_hold.h new file mode 100644 index 000000000..1695c1e7c --- /dev/null +++ b/runtime/src/dstorage_hold.h | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | // Counters of the DirectStorage queues' rule "reads into physical pages wait for the title's Submit" | ||
| 2 | // (shims/dstorage.cpp, docs/directstorage.md). Process-wide, so the log line and the test read the same numbers. | ||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <atomic> | ||
| 6 | #include <cstdint> | ||
| 7 | |||
| 8 | namespace leonida::dstorage { | ||
| 9 | |||
| 10 | struct HoldStats { | ||
| 11 | std::atomic<uint64_t> self_submits{0}; // a queue half full submitted itself | ||
| 12 | std::atomic<uint64_t> held_reads{0}; // reads into physical pages such a submit left for the title's Submit | ||
| 13 | std::atomic<uint64_t> full_releases{0}; // a full queue submitted its held entries (the title waits in an enqueue) | ||
| 14 | std::atomic<uint64_t> timed_releases{0}; // LEONIDA_DSTORAGE_HOLD_MS ran out before the title's Submit | ||
| 15 | std::atomic<uint64_t> page0_entries{0}; // destination page lists that named page 0 when their data was written | ||
| 16 | }; | ||
| 17 | |||
| 18 | inline HoldStats& hold_stats() { | ||
| 19 | static HoldStats stats; | ||
| 20 | return stats; | ||
| 21 | } | ||
| 22 | |||
| 23 | } // namespace leonida::dstorage | ||
diff --git a/runtime/src/heavy_pipeline_cache.h b/runtime/src/heavy_pipeline_cache.h new file mode 100644 index 000000000..8cad10e66 --- /dev/null +++ b/runtime/src/heavy_pipeline_cache.h | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | // A pipeline cache that survives a crash, for the pipelines the offline corpus does not have (RT-HEAVY-PIPELINE-CACHE). | ||
| 2 | // | ||
| 3 | // The offline seed (pipeline_cache.bin, several GiB) covers what the `pipelines` build stage creates. A pipeline it | ||
| 4 | // misses is compiled on the spot, and some of those take the driver seconds each. pipeline_cache.session.bin cannot | ||
| 5 | // keep them across a crash: it is written at device teardown only, and a cache seeded from the corpus returns the | ||
| 6 | // whole corpus, over its write limit, so it is not written at all. | ||
| 7 | // | ||
| 8 | // So a pipeline is first created against the corpus cache with VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED. | ||
| 9 | // When the driver would have to compile, it is created again against a cache of its own, seeded from | ||
| 10 | // pipeline_cache.heavy.bin. That cache holds only the corpus's misses, so it stays small. After a compile that took | ||
| 11 | // at least LEONIDA_HEAVY_PIPELINE_CACHE_MS (default 500 ms), it is written back to disk on a thread of its own: a | ||
| 12 | // staging file renamed over the old one, so a crash at any point leaves the last complete file. | ||
| 13 | // | ||
| 14 | // This header holds the pure decisions (the threshold and when to write). Vulkan and the file live in the device. | ||
| 15 | #pragma once | ||
| 16 | |||
| 17 | #include <cstdint> | ||
| 18 | #include <cstdlib> | ||
| 19 | |||
| 20 | namespace leonida::heavy_pipeline_cache { | ||
| 21 | |||
| 22 | inline constexpr std::uint32_t default_threshold_ms = 500; | ||
| 23 | |||
| 24 | struct Threshold { | ||
| 25 | std::uint32_t ms = default_threshold_ms; // 0: off | ||
| 26 | bool valid = true; // false: the text was not a number; ms stays the default | ||
| 27 | }; | ||
| 28 | |||
| 29 | // LEONIDA_HEAVY_PIPELINE_CACHE_MS: a compile at least this long schedules a write; 0 turns the heavy cache off. | ||
| 30 | inline Threshold parse_threshold(const char* text) { | ||
| 31 | if (!text || !*text) return {}; | ||
| 32 | std::uint64_t value = 0; | ||
| 33 | for (const char* p = text; *p; ++p) { | ||
| 34 | if (*p < '0' || *p > '9') return {default_threshold_ms, false}; | ||
| 35 | value = value * 10 + std::uint64_t(*p - '0'); | ||
| 36 | if (value > 3'600'000) return {default_threshold_ms, false}; // an hour: past it the text is a mistake | ||
| 37 | } | ||
| 38 | return {std::uint32_t(value), true}; | ||
| 39 | } | ||
| 40 | |||
| 41 | // When the writer thread writes. A heavy compile marks the cache dirty; the write waits `settle_ms` after the | ||
| 42 | // newest one (heavy compiles come in bursts at a load, and each write copies the whole heavy cache) but never more | ||
| 43 | // than `max_delay_ms` after the first unwritten one, so a long burst is still saved as it goes. A write that fails | ||
| 44 | // keeps the cache dirty and is tried again `retry_ms` later, the delay doubling with each failure in a row up to | ||
| 45 | // `max_retry_ms`, so a disk that stays full or a driver that keeps failing does not make the writer spin. | ||
| 46 | class WriteSchedule { | ||
| 47 | public: | ||
| 48 | static constexpr std::uint64_t settle_ms = 3000; | ||
| 49 | static constexpr std::uint64_t max_delay_ms = 20000; | ||
| 50 | static constexpr std::uint64_t retry_ms = 5000; | ||
| 51 | static constexpr std::uint64_t max_retry_ms = 120000; | ||
| 52 | |||
| 53 | void note_heavy(std::uint64_t now_ms) { | ||
| 54 | if (!dirty_) first_ = now_ms; | ||
| 55 | dirty_ = true; | ||
| 56 | last_ = now_ms; | ||
| 57 | ++generation_; | ||
| 58 | } | ||
| 59 | bool dirty() const { return dirty_; } | ||
| 60 | // The time to write at; only meaningful while dirty. | ||
| 61 | std::uint64_t due_ms() const { | ||
| 62 | const std::uint64_t settled = last_ + settle_ms, latest = first_ + max_delay_ms; | ||
| 63 | const std::uint64_t at = settled < latest ? settled : latest; | ||
| 64 | return at > retry_at_ ? at : retry_at_; | ||
| 65 | } | ||
| 66 | bool due(std::uint64_t now_ms) const { return dirty_ && now_ms >= due_ms(); } | ||
| 67 | // Taken when a write starts; the write covers every heavy compile noted up to then. | ||
| 68 | std::uint64_t generation() const { return generation_; } | ||
| 69 | // A write that started at `generation` and `started_ms` is done. A heavy compile noted after it started, even in | ||
| 70 | // the same millisecond tick, keeps the cache dirty for the next write, due as if it were first noted then. | ||
| 71 | void written(std::uint64_t generation, std::uint64_t started_ms) { | ||
| 72 | failures_ = 0; | ||
| 73 | retry_at_ = 0; | ||
| 74 | if (generation_ == generation) dirty_ = false; | ||
| 75 | else first_ = started_ms; | ||
| 76 | } | ||
| 77 | // A write that started at `started_ms` failed: the cache stays dirty and the next try waits out the backoff. | ||
| 78 | void failed(std::uint64_t started_ms) { | ||
| 79 | std::uint64_t delay = retry_ms; | ||
| 80 | for (std::uint32_t i = 0; i < failures_ && delay < max_retry_ms; ++i) delay *= 2; | ||
| 81 | if (delay > max_retry_ms) delay = max_retry_ms; | ||
| 82 | if (failures_ < 64) ++failures_; | ||
| 83 | retry_at_ = started_ms + delay; | ||
| 84 | } | ||
| 85 | std::uint32_t failures() const { return failures_; } | ||
| 86 | |||
| 87 | private: | ||
| 88 | bool dirty_ = false; | ||
| 89 | std::uint64_t first_ = 0, last_ = 0, generation_ = 0; | ||
| 90 | std::uint32_t failures_ = 0; // failed writes in a row | ||
| 91 | std::uint64_t retry_at_ = 0; // no write before this time after a failure | ||
| 92 | }; | ||
| 93 | |||
| 94 | } // namespace leonida::heavy_pipeline_cache | ||
diff --git a/runtime/src/list_value_wait_budget.h b/runtime/src/list_value_wait_budget.h new file mode 100644 index 000000000..8e1fd5219 --- /dev/null +++ b/runtime/src/list_value_wait_budget.h | |||
| @@ -0,0 +1,251 @@ | |||
| 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 `active_pause_cap_us` so a continuously streaming queue cannot starve an | ||
| 17 | // unmodelled wait. | ||
| 18 | // - `GateTimeoutState` tracks idle wait time (`elapsed_us`) separately from paused time, expiring once `elapsed_us` | ||
| 19 | // reaches `budget_us`. Every pause reason (`releaser_busy` and `matching_producer` included) is bounded by a finite | ||
| 20 | // `max_pause_cap_us` so a producer stuck behind an untracked dependency (e.g. a title fence wait or a hung Signal) | ||
| 21 | // still expires instead of hanging forever, and demoted (`cut_short`) gates scale both caps with their short budget. | ||
| 22 | // | ||
| 23 | // Pure C++20 standard library only. | ||
| 24 | |||
| 25 | #include <algorithm> | ||
| 26 | #include <cstddef> | ||
| 27 | #include <cstdint> | ||
| 28 | #include <span> | ||
| 29 | #include <vector> | ||
| 30 | |||
| 31 | namespace leonida::list_value_wait { | ||
| 32 | |||
| 33 | enum class PauseReason : std::uint8_t { | ||
| 34 | none = 0, // no submitted or held work can still satisfy this gate -> timeout clock ticks | ||
| 35 | releaser_busy = 1, // releaser thread is executing or draining opened gates | ||
| 36 | matching_producer = 2, // an acyclic in-flight WriteGroup can satisfy an unmet wait of this gate | ||
| 37 | active_work = 3, // unrelated unblocked submitted work is still executing (bounded by active_pause_cap_us) | ||
| 38 | }; | ||
| 39 | |||
| 40 | struct WaitPredicate { | ||
| 41 | std::uint64_t address = 0; | ||
| 42 | std::uint64_t reference = 0; | ||
| 43 | std::uint32_t comparison = 0; | ||
| 44 | bool wide = false; | ||
| 45 | }; | ||
| 46 | |||
| 47 | struct PendingWrite { | ||
| 48 | std::uint64_t address = 0; | ||
| 49 | std::uint64_t value = 0; | ||
| 50 | std::uint8_t bytes = 0; | ||
| 51 | bool timestamp = false; // GPU/system timestamp: final value is unknown until landing | ||
| 52 | }; | ||
| 53 | |||
| 54 | struct PendingWriteGroup { | ||
| 55 | const void* queue = nullptr; | ||
| 56 | std::uint64_t after_gate = 0; // highest pending gate value on `queue` ahead of this group when submitted (0: none) | ||
| 57 | std::span<const PendingWrite> writes; | ||
| 58 | }; | ||
| 59 | |||
| 60 | struct PendingGate { | ||
| 61 | const void* queue = nullptr; | ||
| 62 | std::uint64_t value = 0; | ||
| 63 | bool is_action = false; // waits only on earlier GPU work on `queue`, never on memory | ||
| 64 | std::span<const WaitPredicate> unmet_waits; // waits not currently satisfied in memory (empty for action gates) | ||
| 65 | }; | ||
| 66 | |||
| 67 | struct PauseCaps { | ||
| 68 | std::uint64_t active_us = 0; // cap on pausing solely for unrelated active_work | ||
| 69 | std::uint64_t max_us = 0; // hard finite cap across ALL pause reasons (releaser_busy, matching_producer, active_work) | ||
| 70 | }; | ||
| 71 | |||
| 72 | // Pause caps scaled with the gate's own timeout budget (`gate_timeout_ms`) and demotion state (`cut_short`): | ||
| 73 | // - Normal gates: `active_us = 300x` and `max_us = 1500x` of `gate_timeout_ms`, capped at 10 min. At the default | ||
| 74 | // 200 ms timeout this allows up to 60 s for unrelated GPU work and up to 300 s (5 min) while the releaser or an | ||
| 75 | // acyclic matching producer is working (covering multi-minute world-phase pipeline builds), while guaranteeing a | ||
| 76 | // finite exit if a producer is held behind an untracked dependency (title fence wait or stuck Signal). | ||
| 77 | // - Demoted (`cut_short`) gates: `active_us = 1x` and `max_us = 4x` of `gate_timeout_ms` (4 ms / 16 ms at the 4 ms | ||
| 78 | // demoted budget), so an unmodelled wait only pays a few milliseconds instead of inheriting the full timeout's cap. | ||
| 79 | inline constexpr PauseCaps default_pause_caps(unsigned gate_timeout_ms, bool cut_short = false) { | ||
| 80 | constexpr std::uint64_t kMaxCapMs = 600000u; // 10 min hard ceiling | ||
| 81 | const std::uint64_t base_ms = gate_timeout_ms ? std::uint64_t{gate_timeout_ms} : 1u; | ||
| 82 | const auto scale_ms = [base_ms](std::uint64_t mult) -> std::uint64_t { | ||
| 83 | return base_ms > kMaxCapMs / mult ? kMaxCapMs : base_ms * mult; | ||
| 84 | }; | ||
| 85 | const std::uint64_t active_ms = cut_short ? (base_ms < kMaxCapMs ? base_ms : kMaxCapMs) : scale_ms(300u); | ||
| 86 | const std::uint64_t max_ms = cut_short ? scale_ms(4u) : scale_ms(1500u); | ||
| 87 | return {active_ms * 1000u, max_ms * 1000u}; | ||
| 88 | } | ||
| 89 | |||
| 90 | inline constexpr std::uint64_t default_active_pause_cap_us(unsigned gate_timeout_ms, bool cut_short = false) { | ||
| 91 | return default_pause_caps(gate_timeout_ms, cut_short).active_us; | ||
| 92 | } | ||
| 93 | |||
| 94 | inline constexpr std::uint64_t default_max_pause_cap_us(unsigned gate_timeout_ms, bool cut_short = false) { | ||
| 95 | return default_pause_caps(gate_timeout_ms, cut_short).max_us; | ||
| 96 | } | ||
| 97 | |||
| 98 | struct GateTimeoutState { | ||
| 99 | std::uint64_t budget_us = 0; | ||
| 100 | std::uint64_t elapsed_us = 0; | ||
| 101 | std::uint64_t active_paused_us = 0; | ||
| 102 | std::uint64_t active_pause_cap_us = 0; | ||
| 103 | std::uint64_t total_paused_us = 0; | ||
| 104 | std::uint64_t max_pause_cap_us = 0; | ||
| 105 | bool paused_ever = false; | ||
| 106 | |||
| 107 | void reset(std::uint64_t budget, std::uint64_t active_cap, std::uint64_t max_cap) { | ||
| 108 | budget_us = budget; | ||
| 109 | elapsed_us = 0; | ||
| 110 | active_paused_us = 0; | ||
| 111 | total_paused_us = 0; | ||
| 112 | max_pause_cap_us = max_cap; | ||
| 113 | active_pause_cap_us = active_cap < max_cap ? active_cap : max_cap; | ||
| 114 | paused_ever = false; | ||
| 115 | } | ||
| 116 | |||
| 117 | // Advances the gate by `dt_us` under `reason`. Returns true once the idle timeout budget is exhausted. | ||
| 118 | // Every pause reason is bounded by `max_pause_cap_us`; unrelated `active_work` is additionally bounded by | ||
| 119 | // `active_pause_cap_us`. | ||
| 120 | bool advance(std::uint64_t dt_us, PauseReason reason) { | ||
| 121 | std::uint64_t room = 0; | ||
| 122 | if (reason == PauseReason::releaser_busy || reason == PauseReason::matching_producer) { | ||
| 123 | room = total_paused_us < max_pause_cap_us ? max_pause_cap_us - total_paused_us : 0u; | ||
| 124 | } else if (reason == PauseReason::active_work) { | ||
| 125 | const std::uint64_t total_room = | ||
| 126 | total_paused_us < max_pause_cap_us ? max_pause_cap_us - total_paused_us : 0u; | ||
| 127 | const std::uint64_t active_room = | ||
| 128 | active_paused_us < active_pause_cap_us ? active_pause_cap_us - active_paused_us : 0u; | ||
| 129 | room = total_room < active_room ? total_room : active_room; | ||
| 130 | } | ||
| 131 | if (room > 0) { | ||
| 132 | if (dt_us == 0) return false; | ||
| 133 | paused_ever = true; | ||
| 134 | const std::uint64_t absorb = dt_us < room ? dt_us : room; | ||
| 135 | total_paused_us += absorb; | ||
| 136 | if (reason == PauseReason::active_work) active_paused_us += absorb; | ||
| 137 | dt_us -= absorb; | ||
| 138 | } | ||
| 139 | if (dt_us > UINT64_MAX - elapsed_us) | ||
| 140 | elapsed_us = UINT64_MAX; | ||
| 141 | else | ||
| 142 | elapsed_us += dt_us; | ||
| 143 | return elapsed_us >= budget_us; | ||
| 144 | } | ||
| 145 | }; | ||
| 146 | |||
| 147 | namespace detail { | ||
| 148 | |||
| 149 | // True when `group`'s final write touching [wait.address, wait.address + need) lands a value that meets `wait` | ||
| 150 | // (or is a timestamp write to the exact address whose runtime value is unknown until landing). | ||
| 151 | template <class MetFn> | ||
| 152 | inline bool write_group_matches_wait(const PendingWriteGroup& group, const WaitPredicate& wait, MetFn&& met) { | ||
| 153 | const std::uint64_t need = wait.wide ? 8u : 4u; | ||
| 154 | if (!wait.address || wait.address > UINT64_MAX - need) return false; | ||
| 155 | const std::uint64_t wait_end = wait.address + need; | ||
| 156 | for (std::size_t i = group.writes.size(); i-- > 0;) { | ||
| 157 | const PendingWrite& w = group.writes[i]; | ||
| 158 | if (!w.bytes || w.address > UINT64_MAX - w.bytes) continue; | ||
| 159 | const std::uint64_t write_end = w.address + w.bytes; | ||
| 160 | if (w.address >= wait_end || wait.address >= write_end) continue; | ||
| 161 | if (w.address == wait.address && w.bytes >= need && | ||
| 162 | (w.timestamp || met(wait.comparison, w.value, wait.reference, wait.wide))) | ||
| 163 | return true; | ||
| 164 | // A later write in the same group overwrote or partially overlapped the waited range with a non-matching value. | ||
| 165 | return false; | ||
| 166 | } | ||
| 167 | return false; | ||
| 168 | } | ||
| 169 | |||
| 170 | // True when no pending gate in `gates` precedes `(queue, after_gate)`. | ||
| 171 | inline bool is_unblocked(std::span<const PendingGate> gates, const void* queue, std::uint64_t after_gate) { | ||
| 172 | if (!after_gate) return true; | ||
| 173 | for (const PendingGate& g : gates) | ||
| 174 | if (g.queue == queue && g.value <= after_gate) return false; | ||
| 175 | return true; | ||
| 176 | } | ||
| 177 | |||
| 178 | // True when `gate_idx` (directly or transitively via matching in-flight writes) depends on a gate on `target_queue`. | ||
| 179 | template <class MetFn> | ||
| 180 | inline bool gate_can_reach_queue(std::size_t gate_idx, const void* target_queue, | ||
| 181 | std::span<const PendingWriteGroup> writes, std::span<const PendingGate> gates, | ||
| 182 | MetFn&& met, std::vector<std::size_t>& visiting) { | ||
| 183 | const PendingGate& g = gates[gate_idx]; | ||
| 184 | if (g.queue == target_queue) return true; | ||
| 185 | if (g.is_action) return false; | ||
| 186 | if (std::find(visiting.begin(), visiting.end(), gate_idx) != visiting.end()) return false; | ||
| 187 | visiting.push_back(gate_idx); | ||
| 188 | for (const WaitPredicate& wait : g.unmet_waits) { | ||
| 189 | for (const PendingWriteGroup& w : writes) { | ||
| 190 | if (!write_group_matches_wait(w, wait, met)) continue; | ||
| 191 | if (!w.after_gate) continue; | ||
| 192 | for (std::size_t b = 0; b < gates.size(); ++b) { | ||
| 193 | if (gates[b].queue != w.queue || gates[b].value > w.after_gate) continue; | ||
| 194 | if (gates[b].queue == target_queue || | ||
| 195 | gate_can_reach_queue(b, target_queue, writes, gates, met, visiting)) { | ||
| 196 | visiting.pop_back(); | ||
| 197 | return true; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | visiting.pop_back(); | ||
| 203 | return false; | ||
| 204 | } | ||
| 205 | |||
| 206 | } // namespace detail | ||
| 207 | |||
| 208 | // Decides whether the timeout clock of `gates[target_idx]` should pause on this pump pass. | ||
| 209 | // `met(comparison, value, reference, wide)` must implement the same predicate as `console_value_met`. | ||
| 210 | template <class MetFn> | ||
| 211 | inline PauseReason pause_reason(bool releaser_busy, std::span<const PendingWriteGroup> writes, | ||
| 212 | std::span<const PendingGate> gates, std::size_t target_idx, MetFn&& met) { | ||
| 213 | if (releaser_busy) return PauseReason::releaser_busy; | ||
| 214 | if (target_idx >= gates.size()) return PauseReason::none; | ||
| 215 | const PendingGate& target = gates[target_idx]; | ||
| 216 | if (target.is_action) return PauseReason::none; | ||
| 217 | |||
| 218 | // 1. A matching in-flight WriteGroup whose preceding gates (if any) do not cycle back to `target.queue`. | ||
| 219 | std::vector<std::size_t> visiting; | ||
| 220 | for (const WaitPredicate& wait : target.unmet_waits) { | ||
| 221 | for (const PendingWriteGroup& w : writes) { | ||
| 222 | if (!detail::write_group_matches_wait(w, wait, met)) continue; | ||
| 223 | bool cyclic = false; | ||
| 224 | if (w.after_gate) { | ||
| 225 | for (std::size_t b = 0; b < gates.size(); ++b) { | ||
| 226 | if (gates[b].queue != w.queue || gates[b].value > w.after_gate) continue; | ||
| 227 | visiting.clear(); | ||
| 228 | if (gates[b].queue == target.queue || | ||
| 229 | detail::gate_can_reach_queue(b, target.queue, writes, gates, met, visiting)) { | ||
| 230 | cyclic = true; | ||
| 231 | break; | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } | ||
| 235 | if (!cyclic) return PauseReason::matching_producer; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | // 2. Unrelated unblocked submitted GPU work (an unblocked WriteGroup on any queue, or an unblocked action gate on | ||
| 240 | // another queue) that could still write memory via a shader/copy or unblock a CPU producer. | ||
| 241 | for (const PendingWriteGroup& w : writes) { | ||
| 242 | if (detail::is_unblocked(gates, w.queue, w.after_gate)) return PauseReason::active_work; | ||
| 243 | } | ||
| 244 | for (const PendingGate& g : gates) { | ||
| 245 | if (!g.is_action || g.queue == target.queue) continue; | ||
| 246 | if (g.value == 0 || detail::is_unblocked(gates, g.queue, g.value - 1)) return PauseReason::active_work; | ||
| 247 | } | ||
| 248 | return PauseReason::none; | ||
| 249 | } | ||
| 250 | |||
| 251 | } // namespace leonida::list_value_wait | ||
diff --git a/runtime/src/title_geometry_watch.h b/runtime/src/title_geometry_watch.h index ca600fb9b..dfb0ae5b5 100644 --- a/runtime/src/title_geometry_watch.h +++ b/runtime/src/title_geometry_watch.h | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include "title_page_watch.h" | 3 | #include "title_page_watch.h" |
| 4 | #include <algorithm> | ||
| 4 | #include <array> | 5 | #include <array> |
| 5 | #include <cstdint> | 6 | #include <cstdint> |
| 6 | #include <vector> | 7 | #include <vector> |
| @@ -72,7 +73,22 @@ inline void note_released(std::uintptr_t begin, std::uintptr_t end) { | |||
| 72 | std::lock_guard subscribers_lock(s.subscribers_mutex); | 73 | std::lock_guard subscribers_lock(s.subscribers_mutex); |
| 73 | for (ReleaseQueue* q : s.subscribers) { | 74 | for (ReleaseQueue* q : s.subscribers) { |
| 74 | std::lock_guard lock(q->mutex); | 75 | std::lock_guard lock(q->mutex); |
| 75 | if (q->ranges.size() < 65536) { | 76 | bool room = q->ranges.size() < 65536; |
| 77 | if (!room) { | ||
| 78 | // Full: first the ranges that overlap or touch become one (remaps report page runs, often neighbours), and | ||
| 79 | // only when that leaves it more than half full does it all become one covering range. | ||
| 80 | std::sort(q->ranges.begin(), q->ranges.end()); | ||
| 81 | std::size_t kept = 0; | ||
| 82 | for (std::size_t i = 1; i < q->ranges.size(); ++i) { | ||
| 83 | if (q->ranges[i].first <= q->ranges[kept].second) | ||
| 84 | q->ranges[kept].second = (std::max)(q->ranges[kept].second, q->ranges[i].second); | ||
| 85 | else | ||
| 86 | q->ranges[++kept] = q->ranges[i]; | ||
| 87 | } | ||
| 88 | q->ranges.resize(kept + 1); | ||
| 89 | room = q->ranges.size() <= 32768; | ||
| 90 | } | ||
| 91 | if (room) { | ||
| 76 | q->ranges.emplace_back(begin, end); | 92 | q->ranges.emplace_back(begin, end); |
| 77 | } else { | 93 | } else { |
| 78 | std::uintptr_t merged_lo = begin; | 94 | std::uintptr_t merged_lo = begin; |
diff --git a/runtime/src/title_texture_memory.h b/runtime/src/title_texture_memory.h index a97e5bed6..ff9f004e0 100644 --- a/runtime/src/title_texture_memory.h +++ b/runtime/src/title_texture_memory.h | |||
| @@ -1,14 +1,17 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | #include <windows.h> | 2 | #include <windows.h> |
| 3 | #include <algorithm> | ||
| 3 | #include <cstddef> | 4 | #include <cstddef> |
| 4 | #include <cstdint> | 5 | #include <cstdint> |
| 5 | #include <limits> | 6 | #include <limits> |
| 6 | 7 | ||
| 8 | #include "title_page_watch.h" | ||
| 9 | |||
| 7 | namespace leonida { | 10 | namespace leonida { |
| 8 | 11 | ||
| 9 | // Keep negative probes until the title changes the map or a writer reports this footprint. With cache_success, | 12 | // Keep negative probes until the title changes the map or a writer reports this footprint. With cache_success, |
| 10 | // positive ones too: a caller that is told of every map change over the footprint (retry) and reads it under a guard | 13 | // positive ones too: a caller that is told of every map change over the footprint (retry or sync_map) and reads it |
| 11 | // (a page gone since faults into a defer) need not walk the pages before every read. | 14 | // under a guard (a page gone since faults into a defer) need not walk the pages before every read. |
| 12 | class TextureReadability { | 15 | class TextureReadability { |
| 13 | public: | 16 | public: |
| 14 | template<class Probe> | 17 | template<class Probe> |
| @@ -16,16 +19,37 @@ public: | |||
| 16 | if (cache_failure && pending_ && !retry_ && generation_ == generation) return false; | 19 | if (cache_failure && pending_ && !retry_ && generation_ == generation) return false; |
| 17 | if (cache_success && probed_ && !pending_ && !retry_ && generation_ == generation) return true; | 20 | if (cache_success && probed_ && !pending_ && !retry_ && generation_ == generation) return true; |
| 18 | generation_ = generation; // snapshot before the probe so a concurrent map change is noticed | 21 | generation_ = generation; // snapshot before the probe so a concurrent map change is noticed |
| 22 | map_generation_ = title_memory_map::generation(); | ||
| 19 | retry_ = false; | 23 | retry_ = false; |
| 20 | probed_ = true; | 24 | probed_ = true; |
| 21 | pending_ = !probe(); | 25 | pending_ = !probe(); |
| 22 | return !pending_; | 26 | return !pending_; |
| 23 | } | 27 | } |
| 28 | // Between refresh passes, check whether the map journal recorded a change over [cpu, cpu + size) since both | ||
| 29 | // this texture's last probe and the last pass's sweep. | ||
| 30 | void sync_map(std::uint64_t pass_generation, std::uintptr_t cpu, std::uint64_t size) { | ||
| 31 | if (!probed_ || retry_) return; | ||
| 32 | const std::uint64_t since = (std::max)(map_generation_, pass_generation); | ||
| 33 | if (title_memory_map::generation() == since) return; | ||
| 34 | const std::uintptr_t end = size > (std::numeric_limits<std::uintptr_t>::max)() - cpu | ||
| 35 | ? (std::numeric_limits<std::uintptr_t>::max)() | ||
| 36 | : cpu + static_cast<std::uintptr_t>(size); | ||
| 37 | std::uint64_t now = 0; | ||
| 38 | bool overlapped = false; | ||
| 39 | if (!title_memory_map::changes_since( | ||
| 40 | since, now, cpu, end, [&](std::uintptr_t, std::uintptr_t) { overlapped = true; }) || | ||
| 41 | overlapped) { | ||
| 42 | retry_ = true; | ||
| 43 | } else { | ||
| 44 | map_generation_ = now; | ||
| 45 | } | ||
| 46 | } | ||
| 24 | void defer() { pending_ = true; } // mapping disappeared between the probe and the read | 47 | void defer() { pending_ = true; } // mapping disappeared between the probe and the read |
| 25 | void retry() { retry_ = true; } | 48 | void retry() { retry_ = true; } |
| 26 | bool pending() const { return pending_; } | 49 | bool pending() const { return pending_; } |
| 27 | private: | 50 | private: |
| 28 | std::uint64_t generation_ = 0; | 51 | std::uint64_t generation_ = 0; |
| 52 | std::uint64_t map_generation_ = 0; | ||
| 29 | bool pending_ = false, retry_ = false, probed_ = false; | 53 | bool pending_ = false, retry_ = false, probed_ = false; |
| 30 | }; | 54 | }; |
| 31 | 55 | ||
diff --git a/runtime/src/title_va_alias.h b/runtime/src/title_va_alias.h index f3fd2b410..66a7f5894 100644 --- a/runtime/src/title_va_alias.h +++ b/runtime/src/title_va_alias.h | |||
| @@ -45,6 +45,9 @@ public: | |||
| 45 | // memory (an arena VA, a buffer of the layer's). | 45 | // memory (an arena VA, a buffer of the layer's). |
| 46 | using Resolver = bool (*)(void* context, std::uint64_t va, std::uint64_t bytes, std::uint64_t& shader_va); | 46 | using Resolver = bool (*)(void* context, std::uint64_t va, std::uint64_t bytes, std::uint64_t& shader_va); |
| 47 | using TableScanner = void (*)(void* context, std::uint64_t table_va, std::uint32_t descriptors); | 47 | using TableScanner = void (*)(void* context, std::uint64_t table_va, std::uint32_t descriptors); |
| 48 | // Every binding resolved to a copy of the title's memory: [va, va + bytes) as they are reached at (the device keeps | ||
| 49 | // them for the copy back after the submission, see DeviceState::write_back_titles). | ||
| 50 | using Noter = void (*)(void* context, std::uint64_t va, std::uint64_t bytes); | ||
| 48 | 51 | ||
| 49 | // [title_va, title_va + bytes) is reached as [shader_va, shader_va + bytes). A range that overlaps a live one | 52 | // [title_va, title_va + bytes) is reached as [shader_va, shader_va + bytes). A range that overlaps a live one |
| 50 | // replaces it (the title has placed a new buffer over pages it released). False for an empty or wrapping range. | 53 | // replaces it (the title has placed a new buffer over pages it released). False for an empty or wrapping range. |
| @@ -180,6 +183,7 @@ public: | |||
| 180 | std::uint64_t to = shader_va(va); | 183 | std::uint64_t to = shader_va(va); |
| 181 | if (to != va) { | 184 | if (to != va) { |
| 182 | translated_[kind].fetch_add(1, std::memory_order_relaxed); | 185 | translated_[kind].fetch_add(1, std::memory_order_relaxed); |
| 186 | note_reached(va, bytes); | ||
| 183 | return to; | 187 | return to; |
| 184 | } | 188 | } |
| 185 | Resolver resolver; | 189 | Resolver resolver; |
| @@ -191,6 +195,7 @@ public: | |||
| 191 | } | 195 | } |
| 192 | if (!resolver || !resolver(context, va, bytes, to)) return va; | 196 | if (!resolver || !resolver(context, va, bytes, to)) return va; |
| 193 | (to != va ? translated_ : untranslated_)[kind].fetch_add(1, std::memory_order_relaxed); | 197 | (to != va ? translated_ : untranslated_)[kind].fetch_add(1, std::memory_order_relaxed); |
| 198 | if (to != va) note_reached(va, bytes); | ||
| 194 | return to; | 199 | return to; |
| 195 | } | 200 | } |
| 196 | 201 | ||
| @@ -200,6 +205,18 @@ public: | |||
| 200 | if (va && shader_va(va) != va) untranslated_[kind].fetch_add(1, std::memory_order_relaxed); | 205 | if (va && shader_va(va) != va) untranslated_[kind].fetch_add(1, std::memory_order_relaxed); |
| 201 | } | 206 | } |
| 202 | 207 | ||
| 208 | // [va, va + bytes) is reached as a copy of the title's memory: the device keeps it for the copy back. | ||
| 209 | void note_reached(std::uint64_t va, std::uint64_t bytes) { | ||
| 210 | Noter noter; | ||
| 211 | void* context; | ||
| 212 | { | ||
| 213 | std::lock_guard lock(hook_mutex_); | ||
| 214 | noter = noter_; | ||
| 215 | context = hook_context_; | ||
| 216 | } | ||
| 217 | if (noter) noter(context, va, bytes); | ||
| 218 | } | ||
| 219 | |||
| 203 | // Before a submission: the device rewrites the buffer V#s of the `descriptors` slots the root signature declares | 220 | // Before a submission: the device rewrites the buffer V#s of the `descriptors` slots the root signature declares |
| 204 | // for the table at `table_va` (a console VA in one of its shader-visible CBV/SRV/UAV heaps). | 221 | // for the table at `table_va` (a console VA in one of its shader-visible CBV/SRV/UAV heaps). |
| 205 | void scan_table(std::uint64_t table_va, std::uint32_t descriptors) { | 222 | void scan_table(std::uint64_t table_va, std::uint32_t descriptors) { |
| @@ -344,10 +361,16 @@ public: | |||
| 344 | hook_context_ = nullptr; | 361 | hook_context_ = nullptr; |
| 345 | resolver_ = nullptr; | 362 | resolver_ = nullptr; |
| 346 | scanner_ = nullptr; | 363 | scanner_ = nullptr; |
| 364 | noter_ = nullptr; | ||
| 347 | } | 365 | } |
| 348 | // The resolver alone, for a device that scans its descriptor tables itself. | 366 | // The resolver alone, for a device that scans its descriptor tables itself. |
| 349 | void set_resolver(void* context, Resolver resolver) { set_hooks(context, resolver, nullptr); } | 367 | void set_resolver(void* context, Resolver resolver) { set_hooks(context, resolver, nullptr); } |
| 350 | void clear_resolver(void* context) { clear_hooks(context); } | 368 | void clear_resolver(void* context) { clear_hooks(context); } |
| 369 | // Set after set_hooks, so the hook (and its context) stays the device's: what reached a copy of the title's memory. | ||
| 370 | void set_noter(Noter noter) { | ||
| 371 | std::lock_guard lock(hook_mutex_); | ||
| 372 | noter_ = noter; | ||
| 373 | } | ||
| 351 | 374 | ||
| 352 | std::size_t size() const { return count_.load(std::memory_order_acquire); } | 375 | std::size_t size() const { return count_.load(std::memory_order_acquire); } |
| 353 | std::uint64_t invalidations() const { return invalidations_.load(std::memory_order_acquire); } | 376 | std::uint64_t invalidations() const { return invalidations_.load(std::memory_order_acquire); } |
| @@ -424,6 +447,7 @@ private: | |||
| 424 | BufferMaker maker_ = nullptr; | 447 | BufferMaker maker_ = nullptr; |
| 425 | Resolver resolver_ = nullptr; | 448 | Resolver resolver_ = nullptr; |
| 426 | TableScanner scanner_ = nullptr; | 449 | TableScanner scanner_ = nullptr; |
| 450 | Noter noter_ = nullptr; | ||
| 427 | void* source_context_ = nullptr; | 451 | void* source_context_ = nullptr; |
| 428 | DescriptorSource source_ = nullptr; | 452 | DescriptorSource source_ = nullptr; |
| 429 | std::atomic<std::uint64_t> pulled_{0}; | 453 | std::atomic<std::uint64_t> pulled_{0}; |