upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-30 04:34:17 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-30 04:34:17 -0500
commitefc1929908a728d95fa09ffef0a743d2a649b86d (patch)
tree9e2b3a70ef49d7ec265301cf85606686f371ac57 /runtime/src
parent1aefed0dacd492a040909c9a423f270ebdab84f4 (diff)
runtime: eliminate Windows 11 full-space commit_pages_locked broadcast storm, ShadowPages/TitleTextureCells guard conflict, and streaming budget bursts
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/title_geometry_watch.h106
-rw-r--r--runtime/src/title_texture_refresh.h128
2 files changed, 228 insertions, 6 deletions
diff --git a/runtime/src/title_geometry_watch.h b/runtime/src/title_geometry_watch.h
index 7681ab899..bdae70c35 100644
--- a/runtime/src/title_geometry_watch.h
+++ b/runtime/src/title_geometry_watch.h
@@ -4,6 +4,7 @@
4#include <algorithm> 4#include <algorithm>
5#include <array> 5#include <array>
6#include <cstdint> 6#include <cstdint>
7#include <cstring>
7#include <vector> 8#include <vector>
8 9
9namespace leonida::geometry_writes { 10namespace leonida::geometry_writes {
@@ -336,7 +337,8 @@ inline void release(std::uintptr_t va, bool was_hot = false) {
336 } 337 }
337 // A page reopened here may also be one a slot-133 shadow keeps read-only (both guard the title's geometry): 338 // A page reopened here may also be one a slot-133 shadow keeps read-only (both guard the title's geometry):
338 // it is writable now with no fault to tell that shadow, which then walks its pages again. 339 // it is writable now with no fault to tell that shadow, which then walks its pages again.
339 if (reopen(va, c)) title_memory_map::changed(va, va + tile_size); 340 const std::uintptr_t tile_end = va <= UINTPTR_MAX - tile_size ? va + tile_size : UINTPTR_MAX;
341 if (reopen(va, c)) title_memory_map::changed(va, tile_end);
340 s.cells.erase(it); 342 s.cells.erase(it);
341 s.count.store(s.cells.size(), std::memory_order_release); 343 s.count.store(s.cells.size(), std::memory_order_release);
342 return; 344 return;
@@ -487,6 +489,70 @@ inline bool notify(std::uintptr_t begin, std::uintptr_t end, Event event, bool*
487 return opened; 489 return opened;
488} 490}
489 491
492// Queries whether each of `count` consecutive 4 KiB pages starting at `first_page` is currently armed read-only by a
493// live, non-hot Cell in this watch with a ShadowPages-compatible writable base protection (`PAGE_READWRITE` or
494// `PAGE_EXECUTE_READWRITE`, excluding `PAGE_GUARD` and copy-on-write pages). Allocates nothing and holds State::mutex
495// once. Used by ShadowPages::guard_read_only() so pages armed by TitleTextureCells/GeometryViews/TitleMirror adopt
496// kProtected instead of being recopied as Copy::read_only on every submission.
497inline void query_pages_guarded(std::uintptr_t first_page, std::size_t count, std::uint8_t* out) {
498 if (!count) return;
499 auto& s = state();
500 if (!s.count.load(std::memory_order_acquire)) {
501 std::memset(out, 0, count);
502 return;
503 }
504 std::lock_guard lock(s.mutex);
505 auto it = s.cells.lower_bound(first_page & ~(tile_size - 1));
506 for (std::size_t k = 0; k < count; ++k) {
507 const std::uintptr_t p = first_page + k * page_size;
508 if (k > 0 && p < first_page) {
509 std::memset(out + k, 0, count - k);
510 break;
511 }
512 const std::uintptr_t tile = p & ~(tile_size - 1);
513 while (it != s.cells.end() && it->first < tile) ++it;
514 bool guarded = false;
515 if (it != s.cells.end() && it->first == tile && !it->second.hot) {
516 const DWORD r = it->second.restore[(p - tile) / page_size];
517 const DWORD base = r & 0xffu;
518 guarded = !(r & PAGE_GUARD) && (base == PAGE_READWRITE || base == PAGE_EXECUTE_READWRITE);
519 }
520 out[k] = guarded ? 1 : 0;
521 }
522}
523
524// Called when an external guard owner (ShadowPages::reopen_locked) restored write access to [begin, end): clears
525// Cell::restore[] on those sub-pages and armed_map_gen so TitleTextureCells::arm_batch()'s zero-syscall all_armed
526// check never treats now-writable pages as still armed, bumps Cell::version, and queues subscribers without bumping
527// Cell::mapping.
528inline void note_external_reopen(std::uintptr_t begin, std::uintptr_t end) {
529 auto& s = state();
530 if (!s.count.load(std::memory_order_acquire) || end <= begin) return;
531 std::lock_guard lock(s.mutex);
532 auto it = s.cells.lower_bound(begin & ~(tile_size - 1));
533 if (it == s.cells.end() || it->first >= end) return;
534 const auto version = s.serial.load(std::memory_order_relaxed) + 1;
535 for (; it != s.cells.end() && it->first < end; ++it) {
536 auto& cell = it->second;
537 cell.armed_map_gen = Cell::kNoMapGen;
538 for (unsigned i = 0; i < cell.restore.size(); ++i) {
539 const auto p = it->first + i * page_size;
540 if (p < end && p + page_size > begin) cell.restore[i] = 0;
541 }
542 cell.version.store(version, std::memory_order_release);
543 for (WriteQueue* q : s.writers) {
544 std::lock_guard queue_lock(q->mutex);
545 if (q->overflow) continue;
546 if (q->tiles.size() < WriteQueue::kMaxTiles)
547 q->tiles.push_back(it->first);
548 else
549 q->overflow = true, q->tiles.clear();
550 q->pending.store(true, std::memory_order_release);
551 }
552 }
553 s.serial.store(version, std::memory_order_release);
554}
555
490// 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 556// 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
491// it is not armed again, and hot_tiles() lists it for every subscriber. The cell must be acquired. 557// it is not armed again, and hot_tiles() lists it for every subscriber. The cell must be acquired.
492inline void set_hot(std::uintptr_t va, bool hot) { 558inline void set_hot(std::uintptr_t va, bool hot) {
@@ -497,7 +563,14 @@ inline void set_hot(std::uintptr_t va, bool hot) {
497 Cell& c = it->second; 563 Cell& c = it->second;
498 if (hot) { 564 if (hot) {
499 if (c.hot++) return; 565 if (c.hot++) return;
500 reopen(va, c); 566 // Bump Cell::version so already-armed ShadowPages::update_aliased() blocks see the peer transitioned to hot
567 // and stop treating it as clean (no WriteQueue push needed: subscribers poll hot_tiles()).
568 const auto version = s.serial.load(std::memory_order_relaxed) + 1;
569 c.version.store(version, std::memory_order_release);
570 s.serial.store(version, std::memory_order_release);
571 // A page reopened here may also be one a slot-133 shadow treats as guarded: tell shadows to requery it.
572 const std::uintptr_t tile_end = va <= UINTPTR_MAX - tile_size ? va + tile_size : UINTPTR_MAX;
573 if (reopen(va, c)) title_memory_map::changed(va, tile_end);
501 s.hot.push_back(va); 574 s.hot.push_back(va);
502 } else { 575 } else {
503 if (!c.hot || --c.hot) return; 576 if (!c.hot || --c.hot) return;
@@ -533,16 +606,36 @@ inline void set_hot_batch(const std::uintptr_t* vas, std::size_t count, bool hot
533 std::lock_guard lock(s.mutex); 606 std::lock_guard lock(s.mutex);
534 bool hot_changed = false; 607 bool hot_changed = false;
535 if (hot) { 608 if (hot) {
609 const auto version = s.serial.load(std::memory_order_relaxed) + 1;
610 std::uintptr_t reopened_lo = 0, reopened_hi = 0;
611 const auto flush_reopened = [&] {
612 if (reopened_lo < reopened_hi) {
613 title_memory_map::changed(reopened_lo, reopened_hi);
614 reopened_lo = reopened_hi = 0;
615 }
616 };
536 for (std::size_t i = 0; i < count; ++i) { 617 for (std::size_t i = 0; i < count; ++i) {
537 const std::uintptr_t va = vas[i]; 618 const std::uintptr_t va = vas[i];
538 const auto it = s.cells.find(va); 619 const auto it = s.cells.find(va);
539 if (it == s.cells.end()) continue; 620 if (it == s.cells.end()) continue;
540 Cell& c = it->second; 621 Cell& c = it->second;
541 if (c.hot++) continue; 622 if (c.hot++) continue;
542 reopen(va, c); 623 c.version.store(version, std::memory_order_release);
624 if (reopen(va, c)) {
625 const std::uintptr_t tile_end = va <= UINTPTR_MAX - tile_size ? va + tile_size : UINTPTR_MAX;
626 if (reopened_lo < reopened_hi && va == reopened_hi) {
627 reopened_hi = tile_end;
628 } else {
629 flush_reopened();
630 reopened_lo = va;
631 reopened_hi = tile_end;
632 }
633 }
543 s.hot.push_back(va); 634 s.hot.push_back(va);
544 hot_changed = true; 635 hot_changed = true;
545 } 636 }
637 if (hot_changed) s.serial.store(version, std::memory_order_release);
638 flush_reopened();
546 } else { 639 } else {
547 host_heap::vector<std::uintptr_t> cooled; 640 host_heap::vector<std::uintptr_t> cooled;
548 cooled.reserve(count); 641 cooled.reserve(count);
@@ -597,15 +690,16 @@ inline void hot_tiles(Out& out) {
597} 690}
598 691
599// Arm under the same lock as the write-fault path. A kernel destination may only be guarded again after its pin ends. 692// Arm under the same lock as the write-fault path. A kernel destination may only be guarded again after its pin ends.
600// A hot cell is not protected (true: its writes reach every subscriber through hot_tiles()). 693// A hot cell is not protected (`allow_hot == true`: returns true because subscribers like TitleTextureWatch poll
601inline bool arm(std::uintptr_t va, Cell& c) { 694// hot_tiles(); callers that do NOT poll hot_tiles(), such as ShadowPages::update_aliased(), pass `allow_hot = false`).
695inline bool arm(std::uintptr_t va, Cell& c, bool allow_hot = true) {
602 auto& s = state(); 696 auto& s = state();
603 // Querying an OVERLAPPED can fault into xmem. Never do it with the watch lock held. 697 // Querying an OVERLAPPED can fault into xmem. Never do it with the watch lock held.
604 const auto version = c.version.load(std::memory_order_acquire); 698 const auto version = c.version.load(std::memory_order_acquire);
605 bool pinned = false; 699 bool pinned = false;
606 kernel_writes::for_each_pin(va, va + tile_size, [&](auto, auto) { pinned = true; }); 700 kernel_writes::for_each_pin(va, va + tile_size, [&](auto, auto) { pinned = true; });
607 std::lock_guard lock(s.mutex); 701 std::lock_guard lock(s.mutex);
608 if (c.hot) return true; 702 if (c.hot) return allow_hot;
609 if (pinned || version != c.version.load(std::memory_order_relaxed)) return false; 703 if (pinned || version != c.version.load(std::memory_order_relaxed)) return false;
610 c.armed_map_gen = Cell::kNoMapGen; 704 c.armed_map_gen = Cell::kNoMapGen;
611 for (std::size_t i = 0; i < c.restore.size();) { 705 for (std::size_t i = 0; i < c.restore.size();) {
diff --git a/runtime/src/title_texture_refresh.h b/runtime/src/title_texture_refresh.h
index 30ae34234..8b1a9a00e 100644
--- a/runtime/src/title_texture_refresh.h
+++ b/runtime/src/title_texture_refresh.h
@@ -2,11 +2,14 @@
2#include "shadow_keep.h" 2#include "shadow_keep.h"
3 3
4#include <algorithm> 4#include <algorithm>
5#include <atomic>
5#include <bit> 6#include <bit>
6#include <cmath> 7#include <cmath>
7#include <cstdint> 8#include <cstdint>
8#include <deque> 9#include <deque>
10#include <limits>
9#include <map> 11#include <map>
12#include <memory>
10#include <utility> 13#include <utility>
11#include <vector> 14#include <vector>
12 15
@@ -414,4 +417,129 @@ inline std::uint32_t effective_min_mip(std::uint32_t first_mip, float min_lod) {
414 return (std::max)(first_mip, static_cast<std::uint32_t>(std::floor(min_lod))); 417 return (std::max)(first_mip, static_cast<std::uint32_t>(std::floor(min_lod)));
415} 418}
416 419
420// Per-submission streaming upload budget for unmapped first-time streamed textures and partial-mip upgrades.
421// Never defers CPU-mapped textures (e.g. multi-plane video frames that must land together), already-filled dynamic
422// texel updates (from == filled_from), or the first streamed texture of a pass (guaranteeing forward progress for
423// arbitrarily large textures and single-texture tests). Partial-mip upgrades already have valid lower mips clamped via
424// VK_EXT_image_view_min_lod, so they use half the stream budget to coalesce mip-by-mip arrivals across frames.
425class TitleStreamBudget {
426public:
427 explicit TitleStreamBudget(std::uint64_t budget_bytes) noexcept : budget_bytes_(budget_bytes) {}
428
429 bool allow(bool mapped, bool already_filled, bool partial_upgrade, std::uint64_t span) noexcept {
430 if (mapped) return true;
431 if (already_filled && !partial_upgrade) return true;
432 if (!budget_bytes_) return true;
433 const std::uint64_t limit = partial_upgrade ? (budget_bytes_ > 1 ? budget_bytes_ / 2 : 1) : budget_bytes_;
434 if (scheduled_bytes_ > 0 && (span > limit || scheduled_bytes_ > limit - span)) return false;
435 scheduled_bytes_ += span;
436 return true;
437 }
438
439 std::uint64_t scheduled_bytes() const noexcept { return scheduled_bytes_; }
440 std::uint64_t budget_bytes() const noexcept { return budget_bytes_; }
441
442private:
443 std::uint64_t budget_bytes_ = 0;
444 std::uint64_t scheduled_bytes_ = 0;
445};
446
447// Sorts, deduplicates, and coalesces page-aligned addresses into contiguous half-open [lo, hi) runs of `page_size`
448// bytes, emitting each run via `emit(lo, hi)`. Used by xmem::commit_pages_locked() to notify only affected title VAs.
449template<class Alloc, class Emit>
450void coalesce_page_runs(std::vector<std::uintptr_t, Alloc>& pages, std::uintptr_t page_size, Emit&& emit) {
451 if (pages.empty() || !page_size) return;
452 if (pages.size() > 1) {
453 std::sort(pages.begin(), pages.end());
454 pages.erase(std::unique(pages.begin(), pages.end()), pages.end());
455 }
456 constexpr std::uintptr_t kMax = (std::numeric_limits<std::uintptr_t>::max)();
457 for (std::size_t i = 0; i < pages.size();) {
458 const std::uintptr_t lo = pages[i];
459 std::uintptr_t hi = lo <= kMax - page_size ? lo + page_size : kMax;
460 ++i;
461 while (i < pages.size() && pages[i] == hi) {
462 hi = hi <= kMax - page_size ? hi + page_size : kMax;
463 ++i;
464 }
465 if (lo < hi) emit(lo, hi);
466 }
467}
468
469// Lock-free 2-bit-per-page commit/notification tracker for physical-page sections (xmem.cpp).
470// Bit 0 (`kCommitted`): scratch backing is committed in host memory.
471// Bit 1 (`kNotified`): all mapped/table VAs of this page have been notified via title_memory_map::changed(lo, hi).
472// Template Alloc allows xmem.cpp to back the word array from leonida::host_heap so fault-path callers never touch
473// the process heap.
474template<class Alloc = std::allocator<std::uint64_t>>
475class PageCommitBits {
476 using Word = std::atomic<std::uint64_t>;
477 using WordAlloc = typename std::allocator_traits<Alloc>::template rebind_alloc<Word>;
478 using WordTraits = std::allocator_traits<WordAlloc>;
479
480public:
481 PageCommitBits() = default;
482 ~PageCommitBits() { reset(); }
483 PageCommitBits(const PageCommitBits&) = delete;
484 PageCommitBits& operator=(const PageCommitBits&) = delete;
485
486 void init(std::size_t page_count, bool all_committed) {
487 reset();
488 if (!page_count) return;
489 const std::size_t words = (page_count - 1u) / 32u + 1u;
490 WordAlloc alloc;
491 Word* const mem = WordTraits::allocate(alloc, words);
492 const std::uint64_t fill = all_committed ? ~std::uint64_t(0) : std::uint64_t(0);
493 for (std::size_t i = 0; i < words; ++i) WordTraits::construct(alloc, mem + i, fill);
494 words_ = mem;
495 word_count_ = words;
496 page_count_ = page_count;
497 }
498
499 void reset() noexcept {
500 if (!words_) return;
501 WordAlloc alloc;
502 for (std::size_t i = 0; i < word_count_; ++i) WordTraits::destroy(alloc, words_ + i);
503 WordTraits::deallocate(alloc, words_, word_count_);
504 words_ = nullptr;
505 word_count_ = 0;
506 page_count_ = 0;
507 }
508
509 std::size_t size() const noexcept { return page_count_; }
510
511 bool is_committed(std::size_t idx) const noexcept {
512 if (idx >= page_count_) return false;
513 const std::uint64_t mask = std::uint64_t(1) << ((idx & 31u) * 2u);
514 return (words_[idx >> 5].load(std::memory_order_acquire) & mask) != 0;
515 }
516
517 bool is_notified(std::size_t idx) const noexcept {
518 if (idx >= page_count_) return false;
519 const std::uint64_t mask = std::uint64_t(2) << ((idx & 31u) * 2u);
520 return (words_[idx >> 5].load(std::memory_order_acquire) & mask) != 0;
521 }
522
523 // Marks page `idx` committed. Returns true if it transitioned from uncommitted -> committed.
524 bool mark_committed(std::size_t idx) noexcept {
525 if (idx >= page_count_) return false;
526 const std::uint64_t mask = std::uint64_t(1) << ((idx & 31u) * 2u);
527 return (words_[idx >> 5].fetch_or(mask, std::memory_order_acq_rel) & mask) == 0;
528 }
529
530 // Marks page `idx` both committed and notified. Returns true iff `kNotified` transitioned 0 -> 1.
531 bool mark_committed_and_notified(std::size_t idx) noexcept {
532 if (idx >= page_count_) return false;
533 const unsigned shift = static_cast<unsigned>((idx & 31u) * 2u);
534 const std::uint64_t both = std::uint64_t(3) << shift;
535 const std::uint64_t notified = std::uint64_t(2) << shift;
536 return (words_[idx >> 5].fetch_or(both, std::memory_order_acq_rel) & notified) == 0;
537 }
538
539private:
540 Word* words_ = nullptr;
541 std::size_t word_count_ = 0;
542 std::size_t page_count_ = 0;
543};
544
417} // namespace leonida 545} // namespace leonida