d3d12: title texture tiles over memory not committed yet are armed, and a kernel write in progress is waited out, not read (RT-TITLE-TEXTURE-WRITE-TRACKED)

Run 57 showed 99.9 % of the arms refused, 60,637 tiles not armed and about 8,700 textures looked at per pass
(60-68 ms a pass). The refusals were this watch's own: a tile with any page not committed was refused, so
every streamed texture registered before its memory arrived was named and its arming retried at every pass,
and a texture never filled because its memory was not readable was named again at every pass too.

Now pages not committed are left as they are: xmem's commit or mapping bumps the tile's cell, and the mapping
change makes the textures over it probe their memory again. A texture whose memory is not readable is not
named again until then, except one the title mapped (its pages may be committed under the Map pointer). A tile
pinned for a kernel write is busy: the texture is not read (which uploaded half-written textures at every pass
until the write ended) and is read once the pin is gone. Only a tile whose guard cannot be set is read at every
pass. The log line counts busy looks and arms apart from refusals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jan Trencansky
2026-11-26 18:00:13 -05:00
parent 8c579b7d41
commit 9aed60a222
3 changed files with 99 additions and 46 deletions

View File

@ -1448,8 +1448,8 @@ bool watch_shadow_writes() {
// A ShadowPages the fault handler knows of for its whole life: registered before it protects anything, removed once
// its destructor has made every page writable again.
// The geometry watch's cells as TitleTextureWatch uses them: the title texture refresh's own queue of written tiles,
// and arming only while the write-fault handler is installed. A tile with a page not committed is not armed (a commit
// that does not come through xmem would not be seen): its textures are looked at each refresh until it is.
// and arming only while the write-fault handler is installed. Pages not committed are left as they are (xmem's commit
// or mapping of them bumps the cell); a tile pinned for a kernel write is busy until the write ends.
struct TitleTextureCells {
using Cell = leonida::geometry_writes::Cell;
leonida::geometry_writes::WriteQueue queue;
@ -1459,13 +1459,13 @@ struct TitleTextureCells {
TitleTextureCells& operator=(const TitleTextureCells&) = delete;
Cell* acquire(std::uintptr_t tile) { return leonida::geometry_writes::acquire(tile); }
void release(std::uintptr_t tile) { leonida::geometry_writes::release(tile); }
bool arm(std::uintptr_t tile, Cell& cell) {
for (std::uintptr_t at = tile; at < tile + leonida::geometry_writes::tile_size;) {
MEMORY_BASIC_INFORMATION m{};
if (!VirtualQuery(reinterpret_cast<const void*>(at), &m, sizeof m) || m.State != MEM_COMMIT) return false;
at = reinterpret_cast<std::uintptr_t>(m.BaseAddress) + m.RegionSize;
}
return leonida::geometry_writes::arm(tile, cell);
int arm(std::uintptr_t tile, Cell& cell) {
bool pinned = false;
leonida::kernel_writes::for_each_pin(tile, tile + leonida::geometry_writes::tile_size,
[&](auto, auto) { pinned = true; });
if (pinned) return 0;
// Refused otherwise: a write raced the arming (it is in the queue), or VirtualQuery or VirtualProtect failed.
return leonida::geometry_writes::arm(tile, cell) ? 1 : -1;
}
void aliases(std::uintptr_t tile, std::vector<std::uintptr_t>& out) {
if (const auto f = leonida::geometry_writes::aliases.load(std::memory_order_acquire)) f(tile, out);
@ -15546,15 +15546,24 @@ public:
const std::shared_ptr<TitleTexture> entry = it->second;
TitleTexture& t = *entry;
if (t.gpu_written || !t.layout_known) continue;
// Armed before it is read: a write from now on is in the next pass. A kernel write still in progress
// over it: read once it is done (its tile stays unarmed, so the next pass names it again).
if (watch.examine(key) == leonida::TitleTextureWatch<TitleTextureCells>::Look::busy) {
++stats.busy;
continue;
}
++stats.checked;
watch.examine(key); // armed before it is read: a write from now on is in the next pass
const auto fill_start = clock::now();
const std::uint64_t uploaded = fill_title_texture(t);
stats.fill_ns += std::chrono::duration_cast<std::chrono::nanoseconds>(clock::now() - fill_start).count();
if (uploaded) ++stats.changed;
filled += uploaded;
// Not filled (no backing yet, a failed upload, memory not readable): looked at again next pass.
if (!uploaded && (t.stale || !t.hashed) && t.resource) watch.touch(key);
// Not filled: with no backing yet or after a failed upload it is looked at again next pass. Memory not
// readable yet waits for its mapping (xmem's change names it), except a texture the title mapped, whose
// pages may be committed under the pointer Map gave without xmem (the probe answers each pass). Its tiles
// are armed again then: pages committed since the last arming have no guard yet.
if (!uploaded && (t.stale || !t.hashed) && t.resource && (!t.memory.pending() || t.mapped))
watch.note_changed(t.cpu, t.cpu + t.layout.layout.size);
}
stats.filled_bytes += filled;
stats.max_pass_bytes = std::max(stats.max_pass_bytes, filled);
@ -15574,23 +15583,25 @@ public:
const auto& w = watch.stats();
const auto& l = title_watch_logged_;
std::fprintf(stderr,
"[rt] d3d12: title texture refresh: %llu passes, %llu textures looked at (%llu of them over "
"memory not watched), %llu uploaded (%.1f MiB), %llu unchanged; %llu written tiles, %llu arms "
"(%llu refused), %llu queue overflows; %zu textures over %zu tiles watched, %zu not armed; "
"%.3f ms a pass, %.3f at most\n",
"[rt] d3d12: title texture refresh: %llu passes, %llu textures looked at (%llu named over "
"tiles not armed, %llu left for a kernel write in progress), %llu uploaded (%.1f MiB), %llu "
"unchanged; %llu written tiles, %llu arms (%llu busy, %llu refused), %llu queue overflows; %zu "
"textures over %zu tiles watched, %zu tiles not armed; %.3f ms a pass, %.3f at most\n",
static_cast<unsigned long long>(stats.passes), static_cast<unsigned long long>(stats.checked),
static_cast<unsigned long long>(w.unarmed_named - l.unarmed_named),
static_cast<unsigned long long>(stats.busy),
static_cast<unsigned long long>(stats.changed), double(stats.filled_bytes) / 1048576.0,
static_cast<unsigned long long>(stats.unchanged),
static_cast<unsigned long long>(w.written_tiles - l.written_tiles),
static_cast<unsigned long long>(w.arms - l.arms),
static_cast<unsigned long long>(w.arm_busy - l.arm_busy),
static_cast<unsigned long long>(w.arm_failures - l.arm_failures),
static_cast<unsigned long long>(w.overflows - l.overflows), watch.textures(), watch.tiles(),
watch.unarmed(), double(stats.refresh_ns) * 1e-6 / double(stats.passes),
double(stats.max_refresh_ns) * 1e-6);
title_watch_logged_ = w;
if (leonida::fsdraw::enabled()) log_title_census("refresh");
stats.passes = stats.checked = stats.changed = stats.unchanged = stats.filled_bytes = 0;
stats.passes = stats.checked = stats.busy = stats.changed = stats.unchanged = stats.filled_bytes = 0;
stats.refresh_ns = stats.max_refresh_ns = stats.max_pass_bytes = 0;
}
inside.reset();
@ -15607,7 +15618,7 @@ private:
std::map<std::string, std::uint64_t> refused; // why a view is a null view -> views
std::uint64_t views = 0, null_views = 0, of_slot133 = 0, of_cache = 0, made = 0, retired = 0, registered = 0,
refusals = 0, written_ranges = 0, unmaps = 0, unreadable = 0, pending_views = 0;
std::uint64_t fills = 0, fill_bytes = 0, fill_failures = 0, unchanged = 0;
std::uint64_t fills = 0, fill_bytes = 0, fill_failures = 0, unchanged = 0, busy = 0;
std::uint64_t staged_fills = 0, staged_ns = 0;
// refresh_title_textures, since the last log line
std::uint64_t passes = 0, checked = 0, hashed = 0, changed = 0, deferred = 0, filled_bytes = 0,

View File

@ -11,10 +11,14 @@
// looked at. Before a texture is read for its upload, examine() arms its changed tiles again, so a write that lands
// during or after the read faults again and is in the next collect().
//
// A tile that cannot be armed (not committed or not readable yet, pinned for a kernel write, a protection the watch
// does not own) stays `unarmed`: the textures over it are named at every collect() until it is armed, and their bytes
// are compared with the last upload's (the caller's hash). That is the one fallback, for memory a guard cannot watch.
// The same holds for every tile when the watch is off (no write-fault handler): nothing is ever armed.
// Memory not committed yet is armed like the rest: a page that arrives through xmem bumps the tile's mapping, so the
// tile is in the queue then. A tile whose arming fails stays `unarmed`, and the textures over it are named at every
// collect() until it is armed, but what examine() says decides whether they are read:
// - busy: a kernel write into the tile is in progress (a ReadFile or a DirectStorage request pinned it). Reading now
// would upload a half-written texture, again at every pass until the write ends (in run 57, ~1.5 GiB of such
// uploads per 10 s); the texture is read once the pin is gone and the tile could be armed.
// - unwatched: the guard cannot be set (VirtualProtect refused, no write-fault handler): read, and uploaded only when
// the bytes differ from the last upload's. The one fallback, for memory a guard cannot watch.
//
// A texture the title writes every frame (a video plane, a dynamic atlas) is not treated apart: its tiles fault once
// each per frame the title writes them, which costs far less than reading the texture back at every submission
@ -24,7 +28,8 @@
// using Cell = ...; // has std::atomic<std::uint64_t> version, mapping
// Cell* acquire(std::uintptr_t tile); // a reference to the tile's cell
// void release(std::uintptr_t tile); // drops it (and the tile's guard with the last one)
// bool arm(std::uintptr_t tile, Cell&); // makes the tile read-only; false when it could not
// int arm(std::uintptr_t tile, Cell&); // makes the tile read-only: 1 done, 0 busy (a kernel write in
// // progress), -1 cannot be guarded
// void aliases(std::uintptr_t tile, std::vector<std::uintptr_t>&); // the tile's other addresses
// void take_written(std::vector<std::uintptr_t>& tiles, bool& overflow); // the queue since the last call
// bool enabled(); // a write-fault handler is installed: arming is allowed
@ -55,8 +60,8 @@ public:
TitleTextureWatch& operator=(const TitleTextureWatch&) = delete;
struct Stats {
std::uint64_t collects = 0, named = 0, unarmed_named = 0, arms = 0, arm_failures = 0, written_tiles = 0,
overflows = 0;
std::uint64_t collects = 0, named = 0, unarmed_named = 0, arms = 0, arm_failures = 0, arm_busy = 0,
written_tiles = 0, overflows = 0;
};
const Stats& stats() const { return stats_; }
std::size_t textures() const { return textures_.size(); }
@ -158,19 +163,24 @@ public:
for (const Key k : named_) f(k);
}
// What examine() found: every tile armed (read it now; it is known unchanged afterwards until a tile is written),
// a kernel write in progress over it (do not read it yet), or a tile that cannot be guarded (read it every time).
enum class Look { armed, busy, unwatched };
// Before the texture's bytes are read for an upload: its tiles that changed (or were never armed) are armed
// again, so a write from now on is seen by the next collect(). True when every tile is armed: the texture is then
// known unchanged until a tile is written. False leaves the unarmed tiles to be tried at the next collect().
bool examine(Key key) {
// again, so a write from now on is seen by the next collect(). Tiles not armed are tried at the next collect().
Look examine(Key key) {
const auto it = textures_.find(key);
if (it == textures_.end()) return false;
bool armed = true;
if (it == textures_.end()) return Look::unwatched;
Look look = Look::armed;
for (std::uintptr_t a = it->second.first; a < it->second.last; a += kTile) {
const auto t = tiles_.find(a);
if (t == tiles_.end()) continue;
armed &= arm(a, t->second);
const int armed = arm(a, t->second);
if (!armed) look = Look::busy;
else if (armed < 0 && look == Look::armed) look = Look::unwatched;
}
return armed;
return look;
}
private:
@ -219,9 +229,13 @@ private:
}
}
bool arm(std::uintptr_t address, Tile& tile) {
if (!tile.stale && !unarmed_.count(address)) return true;
if (!watch_.enabled()) return false;
int arm(std::uintptr_t address, Tile& tile) {
if (!tile.stale && !unarmed_.count(address)) return 1;
if (!watch_.enabled()) {
unarmed_.insert(address);
tile.stale = true;
return -1;
}
// The tile's other addresses follow its mapping: looked up again when xmem remapped it.
const std::uint64_t mapping = tile.primary->mapping.load(std::memory_order_acquire);
if (mapping != tile.mapping) {
@ -244,18 +258,18 @@ private:
std::uint64_t version = tile.primary->version.load(std::memory_order_acquire);
for (const auto& [peer, cell] : tile.peers) version = (std::max)(version, cell->version.load(std::memory_order_acquire));
++stats_.arms;
bool ok = watch_.arm(address, *tile.primary);
for (const auto& [peer, cell] : tile.peers) ok = watch_.arm(peer, *cell) && ok;
if (!ok) {
++stats_.arm_failures;
int result = watch_.arm(address, *tile.primary);
for (const auto& [peer, cell] : tile.peers) result = (std::min)(result, watch_.arm(peer, *cell) + 0);
if (result <= 0) {
++(result ? stats_.arm_failures : stats_.arm_busy);
unarmed_.insert(address);
tile.stale = true;
return false;
return result;
}
tile.version = version;
tile.stale = false;
unarmed_.erase(address);
return true;
return 1;
}
Watch& watch_;

View File

@ -20,7 +20,7 @@ struct FakeWatch {
std::map<std::uintptr_t, Cell> cells;
std::vector<std::uintptr_t> queue;
std::map<std::uintptr_t, std::vector<std::uintptr_t>> alias; // tile -> its other addresses
std::set<std::uintptr_t> unarmable;
std::set<std::uintptr_t> unarmable, pinned;
bool on = true, overflow = false;
std::uint64_t serial = 1;
@ -33,10 +33,11 @@ struct FakeWatch {
auto it = cells.find(tile);
if (it != cells.end() && !--it->second.refs) cells.erase(it);
}
bool arm(std::uintptr_t tile, Cell& c) {
if (unarmable.count(tile)) return false;
int arm(std::uintptr_t tile, Cell& c) {
if (pinned.count(tile)) return 0;
if (unarmable.count(tile)) return -1;
c.armed = true;
return true;
return 1;
}
void aliases(std::uintptr_t tile, std::vector<std::uintptr_t>& out) {
if (const auto it = alias.find(tile); it != alias.end()) out = it->second;
@ -167,6 +168,32 @@ int main() {
EXPECT((collect(w) == V{6}));
EXPECT((collect(w) == V{6}));
}
{ // a kernel write in progress: named, but busy (not to be read) until the pin ends; then read once
FakeWatch f;
Watch w(f);
w.track(2, base, 2 * kTile);
collect(w);
f.write(base + kTile);
f.pinned.insert(base + kTile);
std::vector<Watch::Look> looks;
const auto pass = [&] {
looks.clear();
w.collect([&](std::uint64_t k) { looks.push_back(w.examine(k)); });
};
pass();
EXPECT(looks.size() == 1 && looks[0] == Watch::Look::busy);
pass();
EXPECT(looks.size() == 1 && looks[0] == Watch::Look::busy);
f.pinned.clear();
pass();
EXPECT(looks.size() == 1 && looks[0] == Watch::Look::armed);
pass();
EXPECT(looks.empty());
f.unarmable.insert(base); // a guard that cannot be set: read every time
f.write(base);
pass();
EXPECT(looks.size() == 1 && looks[0] == Watch::Look::unwatched);
}
{ // a lost queue names every texture once; untrack releases the tiles
FakeWatch f;
Watch w(f);
@ -182,6 +209,7 @@ int main() {
EXPECT(w.tiles() == 0 && w.textures() == 0);
}
if (failures) return 1;
std::puts("title texture watch: new, untouched, one page, two addresses, remap, every frame, unarmable, lost queue");
std::puts("title texture watch: new, untouched, one page, two addresses, remap, every frame, unarmable, kernel "
"write in progress, lost queue");
return 0;
}