diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-25 23:33:21 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 09:07:51 -0500 |
| commit | 36771ec53dc3769a75be6b0b878b9868e9648fdb (patch) | |
| tree | 909458afb9cba16f700838bc096bd417b8bd1e54 /runtime/test/xmem_test.cpp | |
| parent | acd9f98c1f751fda83df958c869bc91e2770fccc (diff) | |
d3d12: geometry copies in system memory, and retired when the title remaps their pages
Geometry copies held 4.4-6 GB of VRAM in world runs and were never evicted: a view retired only when its range was
freed, decommitted or unmapped, and the title streams by remapping the frames of its pools.
- LEONIDA_GEOMETRY_HOST (unset by default): 1 puts the copies in system memory outside VRAM, write-back where the
device offers a cached host type; 2 write-combined. The memory type is picked among the host-visible types that are
not device-local (memory_type_order alone would give the BAR first); a device with none keeps its types. Logged once
and in the geometry buffers line.
- LEONIDA_GEOMETRY_REMAP_RELEASE (on by default, 0 off): XMemMapPhysicalPages with pages, the scatter call and
CopyPageMappingsBatchX report the pages whose frame changed (or went) as released (xmem's RemapReleases, collected
under the fault lock, reported after it), so GeometryViews retires the copies over them through #2094's
drain_released and kAfterExecutes reaping. A page mapped again to its own frame is no change.
- A retired view is copied once more from its pages when they are readable: a list recorded before a remap that took
the view's address reads the new bytes, as the refresh this replaces would have given it.
- A stream-pool extent backed by a retired copy leaves pool_spans (it was kept for the device's life).
- A full release queue joins touching ranges before it collapses into one covering range.
Tests: geometry_views_test cases 14-16, xmem_test test_remap_releases.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/xmem_test.cpp')
| -rw-r--r-- | runtime/test/xmem_test.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index 991388c7f..c65ac257a 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp | |||
| @@ -31,6 +31,7 @@ | |||
| 31 | #include "startup_trace.h" | 31 | #include "startup_trace.h" |
| 32 | #include "xmem.h" | 32 | #include "xmem.h" |
| 33 | #include "../src/title_page_watch.h" | 33 | #include "../src/title_page_watch.h" |
| 34 | #include "../src/title_geometry_watch.h" | ||
| 34 | 35 | ||
| 35 | // The process heap as test_heap_lock_faults needs it (the same gate as d3d12_existing_heap_test's): an allocation that | 36 | // The process heap as test_heap_lock_faults needs it (the same gate as d3d12_existing_heap_test's): an allocation that |
| 36 | // waits while a thread holds the heap's lock. The NT heap's low-fragmentation front end serves most small sizes | 37 | // waits while a thread holds the heap's lock. The NT heap's low-fragmentation front end serves most small sizes |
| @@ -429,6 +430,78 @@ struct PhysicalApi { | |||
| 429 | XMemMapPhysicalPagesScatterFn scatter; | 430 | XMemMapPhysicalPagesScatterFn scatter; |
| 430 | }; | 431 | }; |
| 431 | 432 | ||
| 433 | // A remap that gives watched pages another frame reports them released (geometry_writes::note_released), so the | ||
| 434 | // geometry copies of what they held are retired; a page mapped again to the frame it had is no change. Through | ||
| 435 | // XMemMapPhysicalPages, the scatter call and CopyPageMappingsBatchX. | ||
| 436 | void test_remap_releases(const PhysicalApi& api) { | ||
| 437 | namespace watch = leonida::geometry_writes; | ||
| 438 | constexpr uint64_t kPages = 3; | ||
| 439 | auto* const window = static_cast<uint8_t*>(api.virtual_alloc( | ||
| 440 | nullptr, kPages * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE)); | ||
| 441 | uint64_t pages[4]{}; | ||
| 442 | uint64_t count = 4; | ||
| 443 | if (!window || api.allocate(0x280, &count, pages) != TRUE || count != 4) { | ||
| 444 | check(false, "remap releases: a window and four physical pages"); | ||
| 445 | return; | ||
| 446 | } | ||
| 447 | const auto base = reinterpret_cast<uintptr_t>(window); | ||
| 448 | watch::ReleaseQueue queue; | ||
| 449 | watch::subscribe(queue); | ||
| 450 | for (uint64_t i = 0; i < kPages; ++i) watch::acquire(base + i * kPage64k); // as a geometry copy over the pages | ||
| 451 | std::vector<std::pair<uintptr_t, uintptr_t>> released; | ||
| 452 | const auto take = [&] { | ||
| 453 | watch::take_released(queue, released); | ||
| 454 | return released; | ||
| 455 | }; | ||
| 456 | const auto covers = [&](uintptr_t from, uintptr_t to) { | ||
| 457 | for (const auto& [lo, hi] : released) | ||
| 458 | if (lo <= from && hi >= to) return true; | ||
| 459 | return false; | ||
| 460 | }; | ||
| 461 | const auto any_overlap = [&](uintptr_t from, uintptr_t to) { | ||
| 462 | for (const auto& [lo, hi] : released) | ||
| 463 | if (lo < to && hi > from) return true; | ||
| 464 | return false; | ||
| 465 | }; | ||
| 466 | |||
| 467 | const uint64_t first[kPages] = {pages[0], pages[1], pages[2]}; | ||
| 468 | check(api.map(window, kPages, first) == window && take().empty(), | ||
| 469 | "remap releases: a first mapping of unmapped pages releases nothing"); | ||
| 470 | check(api.map(window + kPage64k, 1, &pages[1]) == window + kPage64k && take().empty(), | ||
| 471 | "remap releases: a page mapped again to the frame it has is no change"); | ||
| 472 | check(api.map(window + kPage64k, 1, &pages[3]) == window + kPage64k && !take().empty() && | ||
| 473 | covers(base + kPage64k, base + 2 * kPage64k) && !any_overlap(base, base + kPage64k) && | ||
| 474 | !any_overlap(base + 2 * kPage64k, base + 3 * kPage64k), | ||
| 475 | "remap releases: a page given another frame is released, its neighbours are not"); | ||
| 476 | const uint64_t rotated[kPages] = {pages[2], pages[0], pages[1]}; | ||
| 477 | check(api.map(window, kPages, rotated) == window && !take().empty() && covers(base, base + 3 * kPage64k), | ||
| 478 | "remap releases: a run given other frames is released whole"); | ||
| 479 | const uint64_t partly[kPages] = {pages[2], pages[3], pages[1]}; | ||
| 480 | check(api.map(window, kPages, partly) == window && !take().empty() && | ||
| 481 | covers(base + kPage64k, base + 2 * kPage64k) && !any_overlap(base, base + kPage64k) && | ||
| 482 | !any_overlap(base + 2 * kPage64k, base + 3 * kPage64k), | ||
| 483 | "remap releases: of a remapped run, only the page whose frame changed is released"); | ||
| 484 | void* const last = window + 2 * kPage64k; | ||
| 485 | check(api.scatter(&last, 1, &pages[0]) == TRUE && !take().empty() && | ||
| 486 | covers(base + 2 * kPage64k, base + 3 * kPage64k) && !any_overlap(base, base + 2 * kPage64k), | ||
| 487 | "remap releases: the scatter call releases the page it gives another frame"); | ||
| 488 | check(api.scatter(&last, 1, &pages[0]) == TRUE && take().empty(), | ||
| 489 | "remap releases: the scatter call with the frame the page has releases nothing"); | ||
| 490 | // CopyPageMappingsBatchX: page 0 gets the frame at page 1 (kind 0 from a pool at the window itself). | ||
| 491 | const rt::xmem::PageRange range{0, 1, 1}; | ||
| 492 | const rt::xmem::PageBatch batch{base, 1, &range}; | ||
| 493 | check(rt::xmem::copy_page_mappings(1, &batch, base, kPages, 0) && !take().empty() && | ||
| 494 | covers(base, base + kPage64k) && !any_overlap(base + kPage64k, base + 3 * kPage64k), | ||
| 495 | "remap releases: CopyPageMappingsBatchX releases the page it gives another frame"); | ||
| 496 | |||
| 497 | check(api.map(window, kPages, nullptr) == window && !take().empty() && covers(base, base + 3 * kPage64k), | ||
| 498 | "remap releases: unmapping releases the range"); | ||
| 499 | for (uint64_t i = 0; i < kPages; ++i) watch::release(base + i * kPage64k); | ||
| 500 | watch::unsubscribe(queue); | ||
| 501 | api.free_pages(4, pages); | ||
| 502 | VirtualFree(window, 0, MEM_RELEASE); | ||
| 503 | } | ||
| 504 | |||
| 432 | // Physical pages in a 64 KiB-page window, as the measured callers use them: reserve a window, allocate pages, map them, | 505 | // Physical pages in a 64 KiB-page window, as the measured callers use them: reserve a window, allocate pages, map them, |
| 433 | // remap them elsewhere and in another order, unmap, free. | 506 | // remap them elsewhere and in another order, unmap, free. |
| 434 | void test_physical_pages(const PhysicalApi& api) { | 507 | void test_physical_pages(const PhysicalApi& api) { |
| @@ -2655,6 +2728,7 @@ int main(int argc, char** argv) { | |||
| 2655 | // Keep the direct SEC_RESERVE control ahead of the first physical allocation, which installs xmem's VEH. | 2728 | // Keep the direct SEC_RESERVE control ahead of the first physical allocation, which installs xmem's VEH. |
| 2656 | g_section_write_control = measure_section_write_control(); | 2729 | g_section_write_control = measure_section_write_control(); |
| 2657 | test_physical_pages(physical); | 2730 | test_physical_pages(physical); |
| 2731 | test_remap_releases(physical); | ||
| 2658 | if (argc == 2 && std::strcmp(argv[1], "--file-pools") == 0) { | 2732 | if (argc == 2 && std::strcmp(argv[1], "--file-pools") == 0) { |
| 2659 | test_placeholders(physical); | 2733 | test_placeholders(physical); |
| 2660 | print_result(); | 2734 | print_result(); |