mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 16:08:37 +00:00
geometry watch: a page the mirror leaves writable is hot in its cell, written for every copy of it, and written once more when it turns cold (RT-TITLE-TEXTURE-WRITE-TRACKED)
Two trackers guarded the same 64 KiB pages through the same cells: the page mirror's HotColdTracking and the title texture watch. When the mirror found a page hot it reopened the cell behind the other's back; the texture watch then armed it again, so the page faulted at every write for one tracker while the other meant it to be writable (two guards racing on one page), and when the mirror turned a hot page cold for a probe, a write made while it was hot reached no other tracker at all (the video plane's write of that interval was never uploaded). One owner of the guard now: the cell. An owner that leaves a page writable holds it hot (geometry_writes:: set_hot); a hot cell is not armed by anyone and hot_tiles() lists it, so the texture watch takes it as written at every pass; when the last owner lets go, the cell counts as written once more for every subscriber. HotColdTracking holds its hot pages that way and ignores its own hot-to-cold turn (a cell that has not moved since it was armed was not written). Also: the FSDRAW census reset the refresh line's passes, changed and filled counters, which then read 0 uploads. Test: title_mirror_hot_cold now runs the texture watch over the same cells as the mirror's tracking: the plane written every interval is named at every interval, probes included, and its page stops faulting once hot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -1632,6 +1632,7 @@ struct TitleTextureCells {
|
||||
void take_written(std::vector<std::uintptr_t>& out, bool& overflow) {
|
||||
leonida::geometry_writes::take_written(queue, out, overflow);
|
||||
}
|
||||
void hot_tiles(std::vector<std::uintptr_t>& out) { leonida::geometry_writes::hot_tiles(out); }
|
||||
bool enabled() const { return watch_shadow_writes(); }
|
||||
};
|
||||
|
||||
@ -18888,7 +18889,9 @@ private:
|
||||
static_cast<unsigned long long>(s.probes[1]), double(s.probe_ns[1]) * 1e-6,
|
||||
static_cast<unsigned long long>(s.rechecks[1]),
|
||||
static_cast<unsigned long long>(s.readable_now[1]));
|
||||
s.passes = s.checked = s.hashed = s.changed = s.deferred = s.filled_bytes = s.max_pass_bytes = 0;
|
||||
// passes, checked, changed and filled_bytes belong to the refresh's own 10 s line, which resets them: reset
|
||||
// here too (LEONIDA_FSDRAW_PROBE prints this census far more often) they read 0 uploads there (run 64).
|
||||
s.hashed = s.deferred = 0;
|
||||
std::fprintf(stderr,
|
||||
"[rt] title texture pass split: written ranges and map changes %.3f ms/pass (%.1f map ranges a "
|
||||
"pass, %llu passes probed all), mapped and stale textures %.3f ms/pass (%.1f visited a pass), "
|
||||
|
||||
@ -16,6 +16,13 @@ struct Cell {
|
||||
std::atomic<std::uint64_t> version{1}, mapping{1};
|
||||
std::array<DWORD, tile_size / page_size> restore{};
|
||||
unsigned refs = 0;
|
||||
// Owners that leave the tile writable and copy it at every submission instead (the page mirror's hot pages,
|
||||
// src/title_write_policy.h). While it is nonzero the cell guards nothing (arm() does not protect it) and its tile
|
||||
// counts as written at every look: a subscriber asks for the hot tiles (hot_tiles()) as it takes its queue. One
|
||||
// guard per page, owned here, whatever the number of copies that read it: an owner that stopped guarding a page
|
||||
// without saying so left every other copy of it blind (run 64: video planes uploaded until the mirror found their
|
||||
// pages hot, then never again).
|
||||
unsigned hot = 0;
|
||||
};
|
||||
struct ReleaseQueue {
|
||||
std::mutex mutex;
|
||||
@ -34,6 +41,8 @@ struct WriteQueue {
|
||||
struct State {
|
||||
std::mutex mutex;
|
||||
host_heap::map<std::uintptr_t, Cell> cells;
|
||||
host_heap::vector<std::uintptr_t> hot; // the tiles of the cells with hot > 0, under mutex
|
||||
std::atomic<std::size_t> hot_count{0};
|
||||
host_heap::vector<WriteQueue*> writers; // under mutex
|
||||
std::atomic<std::uint64_t> serial{1};
|
||||
std::atomic<std::size_t> count{0};
|
||||
@ -214,6 +223,10 @@ inline void release(std::uintptr_t va) {
|
||||
std::lock_guard lock(s.mutex);
|
||||
auto it = s.cells.find(va);
|
||||
if (it != s.cells.end() && !--it->second.refs) {
|
||||
if (it->second.hot) {
|
||||
std::erase(s.hot, va);
|
||||
s.hot_count.store(s.hot.size(), std::memory_order_release);
|
||||
}
|
||||
// A page reopened here may also be one a slot-133 shadow keeps read-only (both guard the title's geometry):
|
||||
// it is writable now with no fault to tell that shadow, which then walks its pages again.
|
||||
if (reopen(va, it->second)) title_memory_map::changed(va, va + tile_size);
|
||||
@ -265,7 +278,51 @@ inline bool notify(std::uintptr_t begin, std::uintptr_t end, Event event, bool*
|
||||
return opened;
|
||||
}
|
||||
|
||||
// The tile at `va` is hot for one more owner (`hot`) or one fewer: while any owner holds it hot, its guard is off and
|
||||
// it is not armed again, and hot_tiles() lists it for every subscriber. The cell must be acquired.
|
||||
inline void set_hot(std::uintptr_t va, bool hot) {
|
||||
auto& s = state();
|
||||
std::lock_guard lock(s.mutex);
|
||||
const auto it = s.cells.find(va);
|
||||
if (it == s.cells.end()) return;
|
||||
Cell& c = it->second;
|
||||
if (hot) {
|
||||
if (c.hot++) return;
|
||||
reopen(va, c);
|
||||
s.hot.push_back(va);
|
||||
} else {
|
||||
if (!c.hot || --c.hot) return;
|
||||
std::erase(s.hot, va);
|
||||
// Writes made while it was hot faulted nowhere and the subscribers that looked before this saw it in
|
||||
// hot_tiles(): it counts as written once more, for all of them, as a write does.
|
||||
const auto version = s.serial.load(std::memory_order_relaxed) + 1;
|
||||
c.version.store(version, std::memory_order_release);
|
||||
for (WriteQueue* q : s.writers) {
|
||||
std::lock_guard queue_lock(q->mutex);
|
||||
if (q->overflow) continue;
|
||||
if (q->tiles.size() < WriteQueue::kMaxTiles)
|
||||
q->tiles.push_back(va);
|
||||
else
|
||||
q->overflow = true, q->tiles.clear();
|
||||
q->pending.store(true, std::memory_order_release);
|
||||
}
|
||||
s.serial.store(version, std::memory_order_release);
|
||||
}
|
||||
s.hot_count.store(s.hot.size(), std::memory_order_release);
|
||||
}
|
||||
|
||||
// The tiles some owner holds hot: written, for every subscriber, at every look.
|
||||
template<class Out>
|
||||
inline void hot_tiles(Out& out) {
|
||||
out.clear();
|
||||
auto& s = state();
|
||||
if (!s.hot_count.load(std::memory_order_acquire)) return;
|
||||
std::lock_guard lock(s.mutex);
|
||||
out.assign(s.hot.begin(), s.hot.end());
|
||||
}
|
||||
|
||||
// Arm under the same lock as the write-fault path. A kernel destination may only be guarded again after its pin ends.
|
||||
// A hot cell is not protected (true: its writes reach every subscriber through hot_tiles()).
|
||||
inline bool arm(std::uintptr_t va, Cell& c) {
|
||||
auto& s = state();
|
||||
// Querying an OVERLAPPED can fault into xmem. Never do it with the watch lock held.
|
||||
@ -273,6 +330,7 @@ inline bool arm(std::uintptr_t va, Cell& c) {
|
||||
bool pinned = false;
|
||||
kernel_writes::for_each_pin(va, va + tile_size, [&](auto, auto) { pinned = true; });
|
||||
std::lock_guard lock(s.mutex);
|
||||
if (c.hot) return true;
|
||||
if (pinned || version != c.version.load(std::memory_order_relaxed)) return false;
|
||||
for (std::size_t i = 0; i < c.restore.size();) {
|
||||
auto p = va + i * page_size;
|
||||
|
||||
@ -19,6 +19,7 @@ public:
|
||||
HotColdTracking() : policy_(guard_) { geometry_writes::subscribe_writes(queue_); }
|
||||
~HotColdTracking() override {
|
||||
geometry_writes::unsubscribe_writes(queue_);
|
||||
for (const auto& [page, held] : guard_.hot) geometry_writes::set_hot(std::uintptr_t(page), false);
|
||||
for (const auto& [page, cell] : guard_.cells) geometry_writes::release(std::uintptr_t(page));
|
||||
}
|
||||
HotColdTracking(const HotColdTracking&) = delete;
|
||||
@ -34,6 +35,7 @@ public:
|
||||
if (it == guard_.cells.end()) return;
|
||||
policy_.remove(std::uintptr_t(page), std::uintptr_t(page) + Policy::kPage);
|
||||
guard_.armed.erase(page);
|
||||
if (guard_.hot.erase(page)) geometry_writes::set_hot(std::uintptr_t(page), false);
|
||||
geometry_writes::release(std::uintptr_t(page));
|
||||
guard_.cells.erase(it);
|
||||
}
|
||||
@ -44,8 +46,17 @@ public:
|
||||
if (overflow)
|
||||
for (const auto& [page, cell] : guard_.cells) policy_.written(std::uintptr_t(page));
|
||||
else
|
||||
for (const std::uintptr_t tile : tiles_)
|
||||
if (guard_.cells.count(tile)) policy_.written(tile);
|
||||
for (const std::uintptr_t tile : tiles_) {
|
||||
const auto cell = guard_.cells.find(tile);
|
||||
if (cell == guard_.cells.end()) continue;
|
||||
// A guarded page whose cell has not moved since it was armed was not written since: the entry is
|
||||
// older than the arming, or this tracking's own turn of the page from hot to cold.
|
||||
const auto armed = guard_.armed.find(tile);
|
||||
if (armed != guard_.armed.end() &&
|
||||
armed->second == cell->second->version.load(std::memory_order_acquire))
|
||||
continue;
|
||||
policy_.written(tile);
|
||||
}
|
||||
policy_.collect([&](std::uintptr_t page) { out.push_back(page); });
|
||||
policy_.frame(); // one interval a collect
|
||||
}
|
||||
@ -68,9 +79,12 @@ private:
|
||||
struct CellGuard {
|
||||
std::unordered_map<std::uint64_t, geometry_writes::Cell*> cells;
|
||||
std::unordered_map<std::uint64_t, std::uint64_t> armed; // page -> its cell's version when last armed
|
||||
std::unordered_map<std::uint64_t, bool> hot; // pages this tracking holds hot in their cells
|
||||
// Guarded (cold, or a hot page's probe): no longer hot for this owner, then armed.
|
||||
bool arm(std::uintptr_t page) {
|
||||
const auto it = cells.find(page);
|
||||
if (it == cells.end()) return false;
|
||||
if (hot.erase(page)) geometry_writes::set_hot(page, false);
|
||||
const std::uint64_t version = it->second->version.load(std::memory_order_acquire);
|
||||
if (!geometry_writes::arm(page, *it->second)) {
|
||||
armed.erase(page);
|
||||
@ -79,9 +93,11 @@ private:
|
||||
armed[page] = version;
|
||||
return true;
|
||||
}
|
||||
// Writable again (a page turning hot): the cell reopens what it guarded.
|
||||
// Hot: the cell is held hot (writable, and written at every look for every other copy of the page), not
|
||||
// just reopened behind the other copies' backs (which left them blind: run 64's frozen video).
|
||||
void disarm(std::uintptr_t page) {
|
||||
geometry_writes::notify(page, page + 1, geometry_writes::Event::open);
|
||||
armed.erase(page);
|
||||
if (cells.count(page) && hot.emplace(page, true).second) geometry_writes::set_hot(page, true);
|
||||
}
|
||||
};
|
||||
using Policy = TitleWritePolicy<CellGuard>;
|
||||
|
||||
@ -33,6 +33,7 @@
|
||||
// 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
|
||||
// void hot_tiles(std::vector<std::uintptr_t>&); // tiles another owner leaves writable: written at every look
|
||||
//
|
||||
// Not thread-safe: the caller holds the title texture registry's lock.
|
||||
#include <algorithm>
|
||||
@ -223,6 +224,14 @@ public:
|
||||
if (const auto it = tiles_.find(owner); it != tiles_.end()) changed(owner, it->second);
|
||||
}
|
||||
}
|
||||
// Tiles another owner keeps writable (the page mirror's hot pages): written, as far as anyone can tell.
|
||||
watch_.hot_tiles(hot_);
|
||||
for (const std::uintptr_t a : hot_) {
|
||||
if (const auto it = tiles_.find(a); it != tiles_.end()) mark(it->second);
|
||||
if (const auto peer = by_peer_.find(a); peer != by_peer_.end())
|
||||
for (const std::uintptr_t owner : peer->second)
|
||||
if (const auto it = tiles_.find(owner); it != tiles_.end()) mark(it->second);
|
||||
}
|
||||
// Tiles not armed: whatever their version says, a write there need not have faulted.
|
||||
for (const std::uintptr_t a : unarmed_)
|
||||
if (const auto it = tiles_.find(a); it != tiles_.end()) {
|
||||
@ -366,7 +375,7 @@ private:
|
||||
std::set<Key> dirty_, over_;
|
||||
std::set<std::uintptr_t> pass_written_; // the tiles written as the last collect() found them
|
||||
std::vector<Key> named_;
|
||||
std::vector<std::uintptr_t> written_, aliases_;
|
||||
std::vector<std::uintptr_t> written_, aliases_, hot_;
|
||||
Stats stats_;
|
||||
};
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include "../src/title_mirror_hot_cold.h"
|
||||
#include "../src/title_texture_watch.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
@ -39,6 +40,24 @@ int failures = 0;
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
// The title texture refresh's view of the same cells (as the D3D12 layer's TitleTextureCells): a second copy of a page
|
||||
// the mirror tracks, which must hear of every write the mirror's tracking lets through unguarded.
|
||||
struct TextureCells {
|
||||
using Cell = leonida::geometry_writes::Cell;
|
||||
leonida::geometry_writes::WriteQueue queue;
|
||||
TextureCells() { leonida::geometry_writes::subscribe_writes(queue); }
|
||||
~TextureCells() { leonida::geometry_writes::unsubscribe_writes(queue); }
|
||||
Cell* acquire(std::uintptr_t tile) { return leonida::geometry_writes::acquire(tile); }
|
||||
void release(std::uintptr_t tile) { leonida::geometry_writes::release(tile); }
|
||||
int arm(std::uintptr_t tile, Cell& cell) { return leonida::geometry_writes::arm(tile, cell) ? 1 : -1; }
|
||||
void aliases(std::uintptr_t, std::vector<std::uintptr_t>&) {}
|
||||
void take_written(std::vector<std::uintptr_t>& out, bool& overflow) {
|
||||
leonida::geometry_writes::take_written(queue, out, overflow);
|
||||
}
|
||||
void hot_tiles(std::vector<std::uintptr_t>& out) { leonida::geometry_writes::hot_tiles(out); }
|
||||
bool enabled() const { return true; }
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
@ -54,6 +73,13 @@ int main() {
|
||||
{
|
||||
leonida::HotColdTracking tracking;
|
||||
for (std::size_t p = 0; p < kPages; ++p) tracking.watch(base + p * kPage);
|
||||
// A texture over page 0 (a video plane the title writes every interval) and one over page 2, watched by the
|
||||
// texture refresh's watch over the same cells.
|
||||
TextureCells cells;
|
||||
leonida::TitleTextureWatch<TextureCells> textures(cells);
|
||||
textures.track(1, base, kPage);
|
||||
textures.track(2, base + 2 * kPage, kPage);
|
||||
unsigned plane_missed = 0;
|
||||
std::vector<std::uint64_t> out;
|
||||
std::uint64_t hot_faults_late = 0;
|
||||
for (unsigned interval = 0; interval < 200; ++interval) {
|
||||
@ -66,7 +92,20 @@ int main() {
|
||||
for (const std::uint64_t page : out)
|
||||
std::memcpy(copy.data() + (page - base), reinterpret_cast<const void*>(page), kPage);
|
||||
EXPECT(!std::memcmp(copy.data(), memory, copy.size()));
|
||||
// The texture refresh's pass after the mirror's: the plane, written this interval, must be named.
|
||||
bool plane = false;
|
||||
textures.collect([&](std::uint64_t key) {
|
||||
plane |= key == 1;
|
||||
textures.examine(key);
|
||||
});
|
||||
if (interval > 0 && !plane) {
|
||||
++plane_missed;
|
||||
if (plane_missed <= 8)
|
||||
std::printf("plane not named at interval %u (hot %d)\n", interval, int(tracking.stats().hot));
|
||||
}
|
||||
}
|
||||
EXPECT(tracking.stats().hot >= 1); // the plane's page turned hot in the mirror's tracking
|
||||
EXPECT(plane_missed == 0); // and the texture watch still heard of every write to it
|
||||
EXPECT(hot_faults_late == 0);
|
||||
EXPECT(g_faults[1].load() <= 21);
|
||||
const auto& s = tracking.stats();
|
||||
|
||||
@ -42,6 +42,8 @@ struct FakeWatch {
|
||||
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;
|
||||
}
|
||||
std::vector<std::uintptr_t> hot;
|
||||
void hot_tiles(std::vector<std::uintptr_t>& out) { out = hot; }
|
||||
void take_written(std::vector<std::uintptr_t>& out, bool& lost) {
|
||||
out = std::move(queue);
|
||||
queue.clear();
|
||||
|
||||
Reference in New Issue
Block a user