Files
reims-vgpu/guest/kext/tests/window_test.cpp
T
Anees Iqbal 7f970bfa42 🐛 Charge a process for its window's pages, not the region's size
Every guest process that brought up the Metal bundle crossed its memory high
watermark within 2 ms of its context's open, and took an EXC_RESOURCE corpse:
28 at session start on our stack against 3 on Apple's. The corpse work cost
each process about 130 ms, and left it above its high-water mark, first in
line for jetsam, and killed outright where its limit is fatal.

The context's window was a pageable IOBufferMemoryDescriptor of the region's
size (1 GiB), mapped into the process. xnu's vm_map_enter charges an IOKit
mapping of anonymous memory that is not purgeable to the task's footprint at
its whole size as it is made (vm_map_iokit_mapped_region, the iokit_mapped
ledger), read from the public xnu source. So each open added 1 GiB to the
process's footprint at once. A purgeable object is not charged that way: its
resident pages are charged to its owner, which mach_make_memory_entry makes
the task that creates it, the process whose open makes the window. The window
is now purgeable and never made volatile (only the kernel may, and nothing
does), so a process's footprint grows only by the window's pages it reads.
The spans the kext maps over it are device memory, which no footprint counts.

The guest observes nothing else: the window is the same range, read-only,
reading zeros where nothing is mapped in, with the same spans at the same
offsets. The window's option bits live in RgfxWindowSpans.hpp, where the
native test checks them; the kext asserts they are IOKit's.

Measured on a cold boot of the x86 guest with only this kext changed: every
Metal process's IOKit footprint falls from about 1 GiB to 4 KB (WindowServer's
from 709 MB to 7.9 MB, its scanout spans), and the memory high-watermark
crossings at session start from 28 to 1 (BiomeAgent, which opens no context);
none after a logout.
2026-11-05 21:59:19 -08:00

50 lines
2.3 KiB
C++

// The native test of RgfxWindowSpans.hpp: a window maps in only whole pages past the region's
// reserved head, inside the window, and its own memory costs the process only the pages it reads.
#include "RgfxWindowSpans.hpp"
#include <cstdio>
#include <cstdlib>
namespace {
int checks = 0;
void check(bool ok, const char *what)
{
checks++;
if (!ok) {
std::fprintf(stderr, "window_test: FAILED: %s\n", what);
std::exit(1);
}
}
} // namespace
int main()
{
const uint64_t window = 0x10000000; // 256 MiB
const uint64_t head = RGFX_ALLOCATIONS_OFFSET;
check(rgfxWindowSpan(window, head, 0x1000), "the first page past the head");
check(rgfxWindowSpan(window, window - 0x1000, 0x1000), "the window's last page");
check(rgfxWindowSpan(window, head, window - head), "everything past the head");
check(!rgfxWindowSpan(window, head - 0x1000, 0x1000), "no page of the head");
check(!rgfxWindowSpan(window, RGFX_DEVICE_INFO_OFFSET & ~0xfffull, 0x1000),
"nor the device info's page");
check(!rgfxWindowSpan(window, 0, 0x1000), "nor the control rings");
check(!rgfxWindowSpan(window, head - 0x1000, 0x2000), "nor a span that starts in it");
check(!rgfxWindowSpan(window, head, 0), "no empty span");
check(!rgfxWindowSpan(window, head + 0x800, 0x1000), "no span off a page");
check(!rgfxWindowSpan(window, head, 0x1800), "nor a part of a page");
check(!rgfxWindowSpan(window, window - 0x1000, 0x2000), "nor one past the window");
check(!rgfxWindowSpan(window, window, 0x1000), "nor one after it");
check(!rgfxWindowSpan(window, 0xfffffffffffff000ull, 0x2000), "nor one that wraps");
check(!rgfxWindowSpan(0, head, 0x1000), "a closed window maps nothing");
// xnu charges a mapping of pageable memory that is not purgeable at its whole size, the
// region's, to the process's footprint as the context opens.
check((kRgfxWindowMemoryOptions & kRgfxWindowPageable) != 0, "the window's memory is pageable");
check((kRgfxWindowMemoryOptions & kRgfxWindowPurgeable) != 0,
"and purgeable, so a process's footprint counts only the pages it reads there");
check((kRgfxWindowMemoryOptions & kRgfxWindowDirectionInOut) == kRgfxWindowDirectionInOut,
"and the kernel's to write, which purgeable memory must be");
std::printf("window_test: OK (%d checks)\n", checks);
return 0;
}