diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 23b758897d..53ead6b552 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -1704,9 +1704,12 @@ The copies: the copy and the title would never see it ([#1537](https://github.com/ubiquitarianism/leonida/issues/1537), the `WRITE_INVALID` stop at the world start). Every title range a submission's bindings reach is kept, and the next refresh copies those ranges from the copy to the title's pages *before* it copies the pages forward, so a page the - title itself wrote in between keeps the title's newer bytes and its GPU result is dropped. Up to 8,192 ranges and - 64 MiB a submission (a V# has no read/write bit, so a read-only view costs a copy too; past the caps the ranges are - left behind, logged once). `LEONIDA_TITLE_WRITEBACK=0` turns it off. A batch is fenced: it is kept with the ticket + title itself wrote in between keeps the title's newer bytes and its GPU result is dropped. The copy back asks the + write watch first (the refresh's own asking comes after it, so a hot page's last answer is a submission old), passes + over pages whose mapping changed, that a kernel write may be filling or that xmem maps at another address too, and + writes only the 256-byte pieces where the copy differs from the title's bytes: a read-only view writes nothing and + its pages stay guarded. Up to 8,192 ranges and 64 MiB a submission (a V# has no read/write bit, so every view + reached is compared; past the caps the ranges are left behind, logged once). `LEONIDA_TITLE_WRITEBACK=0` turns it off. A batch is fenced: it is kept with the ticket of the submission that will use it and copied back only when the context reports that ticket retired, so a range another submission is still writing is never read half done. A queue held deep (console ordering) can leave batches waiting: past 64 of them the oldest go back without their ticket, logged once. diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 77ef22741e..ce2da55142 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -397,21 +397,50 @@ class ShadowPages { ShadowPages(const ShadowPages&) = delete; ShadowPages& operator=(const ShadowPages&) = delete; - // What the GPU writes into the copies of [from, to) goes back to the title's pages: the pages are made writable - // first (the tracker guards them) and every target is copied from its shadow. Returns the bytes written. Called - // before the next refresh, so a page the title writes again is copied forward over the result, as on a GPU that - // shares the memory. + // What the GPU writes into the copies of [from, to) goes back to the title's pages. Returns the bytes written. + // Called before the next refresh, so a page the title writes again is copied forward over the result, as on a GPU + // that shares the memory. + // + // Only the bytes a copy holds differently from the title's page go back, and only on a page known unchanged since + // it was copied: guarded (read-only since that copy), or write-watched and found unwritten by an asking made here. + // The asking comes first because write_back runs before refresh() asks: a hot page still carried kClean from the + // last asking, so the title's writes since then were copied over with the older copy. Comparing first leaves a + // read-only binding's pages alone (guarded, so a write the title makes meanwhile faults and waits on the lock), + // where opening them for a whole copy lost a write made between the opening and the copy; and of two copies of + // the same bytes only the one that differs is written, where the last one won. std::size_t write_back(std::uintptr_t from, std::uintptr_t to) { from = std::max(from, first_), to = std::min(to, last_); if (from >= to) return 0; AcquireSRWLockExclusive(&lock_); + // What changed since the last refresh, as refresh() learns it: mappings (the journal), kernel-write pins, and + // the write watch. A journal that no longer reaches back makes nothing known: nothing goes back. + std::uint64_t map_generation = leonida::title_memory_map::generation(); + bool known = map_generation_ != ~std::uint64_t(0); + if (known && map_generation != map_generation_) + known = leonida::title_memory_map::changes_since( + map_generation_, map_generation, first_, last_, [&](std::uintptr_t a, std::uintptr_t b) { + for (std::size_t i = (a - first_) / kPage, i1 = (b - first_ + kPage - 1) / kPage; + i < i1 && i < state_.size(); ++i) + state_[i] |= kRequery; + }); + if (!known) { + ReleaseSRWLockExclusive(&lock_); + return 0; + } + leonida::kernel_writes::for_each_pin(from, to, [&](std::uintptr_t a, std::uintptr_t b) { + for (std::uintptr_t p = std::max(a, from) & ~(kPage - 1); p < std::min(b, to); p += kPage) + state_[(p - first_) / kPage] |= kPinned; + pinned_ = true; + }); + ask_written_locked(); // A page the title wrote since it was last copied holds the newer bytes already: its GPU result is dropped and - // the refresh's own copy carries the title's. A guarded page (read-only since that copy) or one asked about and - // found unchanged takes the GPU's. + // the refresh's own copy carries the title's. So is a page whose mapping changed, one a kernel write may be + // filling, and one xmem maps at another address too (a write there is seen by no guard and no watch here). const auto held = [&](std::size_t i) { const std::uint8_t s = state_[i]; - return (s & kZero) || (!(s & kProtected) && !(s & kClean)); + return (s & (kZero | kRequery | kPinned | kAliased)) || (!(s & kProtected) && !(s & kClean)); }; + constexpr std::uintptr_t kPiece = 256; // compared and copied in pieces this size std::size_t written = 0; for (std::uintptr_t at = from & ~(kPage - 1); at < ((to + kPage - 1) & ~(kPage - 1));) { std::size_t pages = 0; @@ -420,12 +449,23 @@ class ShadowPages { ++pages; if (!held((at - first_) / kPage)) { const std::uintptr_t a = std::max(at, from), b = std::min(at + pages * kPage, to); - reopen_locked(at, at + pages * kPage); + bool opened = false; for (const Target& t : targets_) { const std::uintptr_t x = std::max(a, t.begin), y = std::min(b, t.end); if (x >= y || !t.shadow) continue; - std::memcpy(reinterpret_cast(x), t.shadow + (x - t.begin), y - x); - written += y - x; + for (std::uintptr_t c = x; c < y;) { + const std::uintptr_t e = std::min(y, (c + kPiece) & ~(kPiece - 1)); + const std::uint8_t* copy = t.shadow + (c - t.begin); + if (std::memcmp(reinterpret_cast(c), copy, e - c) != 0) { + if (!opened) { + reopen_locked(at, at + pages * kPage); + opened = true; + } + std::memcpy(reinterpret_cast(c), copy, e - c); + written += e - c; + } + c = e; + } } } at += pages * kPage; @@ -518,38 +558,8 @@ class ShadowPages { }); // The write-watched pages written since the last asking are no longer clean: asked before the walk, so a write // after this is in the next asking. - const std::uint64_t watch_generation = leonida::title_write_watch::generation(); - if (watch_generation != watch_generation_) { - watch_generation_ = watch_generation; - update_watched(); - } - if (runs_stale_) rebuild_watch_runs(); refresh_asks_ = refresh_written_ = 0; - if (!watch_runs_.empty()) { - if (!reader_) { - reader_ = leonida::title_write_watch::open_reader(); - forget_clean(); - } - refresh_asks_ = watch_runs_.size(); - leonida::title_write_watch::ask( - reader_, watch_runs_, - [&](std::uintptr_t from, std::uintptr_t to) { - for (std::uintptr_t p = from; p < to; p += kPage) { - std::uint8_t& s = state_[(p - first_) / kPage]; - if (!(s & kHot)) continue; // asked over a gap between hot pages: guarded - refresh_written_ += (s & kClean) != 0; - s &= ~kClean; - warm(p); - } - }, - [&](std::uintptr_t from, std::uintptr_t to) { - // Not write-watched after all (released and reserved again): guarded from now on. - for (std::uintptr_t p = from; p < to; p += kPage) - state_[(p - first_) / kPage] &= ~(kWatched | kClean | kHot); - runs_stale_ = true; - }); - cool(); // a block it guards again leaves the runs at the next refresh - } + if (ask_written_locked()) cool(); // a block it guards again leaves the runs at the next refresh for (std::uintptr_t at = first_; at < last_;) { const std::size_t i0 = (at - first_) / kPage; if (!cover_[i0]) { // no copy reads it: left as the title set it @@ -953,6 +963,42 @@ class ShadowPages { return s == kProtected || s == (kWatched | kProtected) || s == (kWatched | kHot | kClean); } + // One asking of the write watch, under lock_: the hot pages written since the last asking lose kClean (counted in + // refresh_written_). From refresh() before its walk, and from write_back() before it trusts kClean. False when no + // page is write-watched here. + bool ask_written_locked() { + const std::uint64_t watch_generation = leonida::title_write_watch::generation(); + if (watch_generation != watch_generation_) { + watch_generation_ = watch_generation; + update_watched(); + } + if (runs_stale_) rebuild_watch_runs(); + if (watch_runs_.empty()) return false; + if (!reader_) { + reader_ = leonida::title_write_watch::open_reader(); + forget_clean(); + } + refresh_asks_ += watch_runs_.size(); + leonida::title_write_watch::ask( + reader_, watch_runs_, + [&](std::uintptr_t from, std::uintptr_t to) { + for (std::uintptr_t p = from; p < to; p += kPage) { + std::uint8_t& s = state_[(p - first_) / kPage]; + if (!(s & kHot)) continue; // asked over a gap between hot pages: guarded + refresh_written_ += (s & kClean) != 0; + s &= ~kClean; + warm(p); + } + }, + [&](std::uintptr_t from, std::uintptr_t to) { + // Not write-watched after all (released and reserved again): guarded from now on. + for (std::uintptr_t p = from; p < to; p += kPage) + state_[(p - first_) / kPage] &= ~(kWatched | kClean | kHot); + runs_stale_ = true; + }); + return true; + } + // A hot page `p` was found written: its block is not idle. void warm(std::uintptr_t p) { const std::uintptr_t block = p & ~(kBlock - 1);