diff --git a/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp b/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp index 4a68f0ca31..6923468a79 100644 --- a/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp +++ b/core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp @@ -1969,10 +1969,13 @@ static bool PopulatesDirectMappings() { return populates; } -static bool HostKeepsResident(std::size_t bytes) { +static std::size_t ResidentRoom(std::size_t bytes, std::size_t pageBytes) { MEMORYSTATUSEX status{}; status.dwLength = sizeof(status); - return GlobalMemoryStatusEx(&status) != 0 && status.ullAvailPhys >= bytes + status.ullTotalPhys / 4; + if (GlobalMemoryStatusEx(&status) == 0) return 0; + const std::uint64_t reserve = status.ullTotalPhys / 16; + const std::uint64_t room = status.ullAvailPhys > reserve ? (status.ullAvailPhys - reserve) / pageBytes * pageBytes : 0; + return static_cast(std::min(bytes, room)); } static bool TouchPages(std::uintptr_t start, std::uintptr_t end, std::size_t pageBytes) { @@ -1984,16 +1987,17 @@ static bool TouchPages(std::uintptr_t start, std::uintptr_t end, std::size_t pag return true; } -static void PopulateDirectMapping(std::uintptr_t address, std::size_t bytes) { +static void PopulateDirectMapping(std::uintptr_t address, std::size_t mappedBytes) { if (!PopulatesDirectMappings()) return; - if (!HostKeepsResident(bytes)) { - static std::once_flag noted; - std::call_once(noted, [bytes] { std::fprintf(stderr, "[memory] direct memory of %zu MiB and later mappings that do not fit in a quarter of host memory stay paged on demand\n", bytes >> 20); }); - return; - } SYSTEM_INFO system{}; GetSystemInfo(&system); const std::size_t pageBytes = system.dwPageSize; + const std::size_t bytes = ResidentRoom(mappedBytes, pageBytes); + if (bytes < mappedBytes) { + static std::once_flag noted; + std::call_once(noted, [bytes, mappedBytes] { std::fprintf(stderr, "[memory] direct memory: %zu MiB of a %zu MiB mapping populated, so a sixteenth of host memory stays available; the rest, and what later mappings do not fit, stays paged on demand\n", bytes >> 20, mappedBytes >> 20); }); + } + if (bytes == 0) return; constexpr std::size_t chunkBytes = std::size_t{256} << 20; const auto chunks = (bytes + chunkBytes - 1) / chunkBytes; const auto workers = std::min(chunks, std::max(1u, std::thread::hardware_concurrency())); diff --git a/docs/dev/THIRD_PARTY_KYTY.md b/docs/dev/THIRD_PARTY_KYTY.md index 7b2e6b1157..22b19f7a01 100644 --- a/docs/dev/THIRD_PARTY_KYTY.md +++ b/docs/dev/THIRD_PARTY_KYTY.md @@ -223,7 +223,7 @@ The table draws are GPU-driven draws of a shader pair's table programs. | Variable | Values | Default | Effect | Category | Platform | Origin | |---|---|---|---|---|---|---| -| `KYTY_DIRECT_MEMORY_PAGES` | `private` (Linux only), `populate` or `demand`, else stops | Linux: `private` where it works, else `populate`; Windows: `populate` | How the host backs direct memory, so a game's first write to fresh direct memory does not fault once per 4 KiB page on its own thread: `private` puts it in a file on a tmpfs mounted with `huge=within_size` in a private user and mount namespace (2 MiB pages, memory resident only where written), `populate` pre-reads each new mapping while a quarter of host memory stays available afterwards (resident memory grows by everything mapped; Linux keeps the memfd and uses `MADV_POPULATE_READ`, Windows reads one byte of each page of the new view with `ReadProcessMemory` on several threads and stops at the first page that is no longer readable, so a mapping that another thread removes or protects meanwhile is left as it is), `demand` is the old 4 KiB demand paging. On Linux, unset tries `private` and falls back to `populate` where the namespace or the mount is refused, for example by Ubuntu's AppArmor policy or `shmem_enabled=deny`, while `private` set explicitly stops there instead; on Windows, unset is `populate` and `private` stops; the log names the backing | perf | all | AnyKyty | +| `KYTY_DIRECT_MEMORY_PAGES` | `private` (Linux only), `populate` or `demand`, else stops | Linux: `private` where it works, else `populate`; Windows: `populate` | How the host backs direct memory, so a game's first write to fresh direct memory does not fault once per 4 KiB page on its own thread: `private` puts it in a file on a tmpfs mounted with `huge=within_size` in a private user and mount namespace (2 MiB pages, memory resident only where written), `populate` pre-reads each new mapping while a quarter of host memory (Linux) or a sixteenth of it (Windows, so a 16 GB host still populates a 6 GiB pool) stays available afterwards (resident memory grows by everything mapped; Linux keeps the memfd and uses `MADV_POPULATE_READ`, Windows reads one byte of each page of the new view with `ReadProcessMemory` on several threads and stops at the first page that is no longer readable, so a mapping that another thread removes or protects meanwhile is left as it is), `demand` is the old 4 KiB demand paging. On Linux, unset tries `private` and falls back to `populate` where the namespace or the mount is refused, for example by Ubuntu's AppArmor policy or `shmem_enabled=deny`, while `private` set explicitly stops there instead; on Windows, unset is `populate` and `private` stops; the log names the backing | perf | all | AnyKyty | | `KYTY_WRITE_BACK_IMAGES` | `0` or `1`, empty counts as unset, else stops | on | Copies the queued readbacks back at each `ACQUIRE_MEM` or `RELEASE_MEM` with GL2 write-back and at each flip; `0` is KytyPS5's behaviour. See [below](#details) | compat | all | `9d957fd81e` | | `KYTY_URGENT_WRITEBACK_WAIT` | `0` turns it off, any other value or unset leaves it on | on | A guest thread that faults on a page whose texture write-back is still in the open command buffer sends the flush on the urgent queue and waits for that submission itself, instead of queueing the flush behind every operation the command processor has queued; `0` sends it through the ordinary command queue | perf | all | AnyKyty | | `KYTY_TILE_VIA_VRAM` | `0` or `1`, empty counts as unset, else stops | on | Tiles a tiled image's readback in device-local memory, then copies it once to the host; `0` is KytyPS5's behaviour. See [below](#details) | perf | all | `08c666a175` |