Files
reims-vgpu/guest/kext/tests/spans_test.cpp
T
Anees Iqbal 628b03fb37 🐎 Hold heap-placed buffers with the destroys around them
A heap's buffer at an offset (BUFFER_AT_OFFSET, each small buffer of a slab) is
a create no one waits for, and cost its own kext call: 3079 a Vision process,
12 to 15 us each, 40 to 48 ms a process on the RTX 3090 host, between the
destroys, which broke each held run of destroys into one call of about 11.
SubmitBatch now takes BUFFER_AT_OFFSET too, of its one size, checked as Submit
checks it, and a buffer made for a context that closed since its check is
destroyed behind it, as Submit does (RgfxSubmitGate's rgfxSubmitBatchSize;
spans_test checks no message the batch takes names a block). The bundle holds
each buffer with the destroys, in their order, and a command buffer's commit
posts what is held before the buffer goes on its ring, as the host reads a
command buffer after the control ring's messages posted before it.

This changes what the guest sees under transport exhaustion only (the control
ring full for 5 s, or the device stopped):
- before, the buffer's create was refused there and the buffer was nil; now
  it is answered as made, held, as Metal makes objects on a GPU that is gone;
- before, a command buffer committed on a stopped device was never answered,
  and the run hung at its wait; now a commit that finds a buffer still held
  fails the command buffer, unposted, with MTLCommandBufferErrorInternal
  ("Discarded (victim of GPU error/recovery)"), the host's own recovery error,
  as the buffer may name it;
- the next post retries what is held and is refused where it is refused
  again, and a buffer that needs the held gone to make room is refused at
  once, holding nothing more.
Tested by serializer_tests' test_held_buffers and protocol_tests'
test_commit_behind_an_untaken_buffer_fails.


Measured, Vision cold request, RTX 3090 host (rts2, the destroys held,
against rts4, this commit and the next two; load 3.4 and 3.0): destroys and
heap-placed buffers together 62.0 -> 17.6 ms in the first process (3079
buffers and 294 destroy calls become 688 SubmitBatch calls), 51.6 -> 16.1 ms
in the second.
2026-11-13 04:02:30 -08:00

100 lines
4.2 KiB
C++

// The span rule of RgfxRegionSpans.hpp, built natively: which spans each message names, and
// that a short message of a type that names one is malformed.
#include <cstdio>
#include <cstring>
#include "RgfxRegionSpans.hpp"
#include "RgfxSubmitGate.hpp"
static int g_checks = 0, g_failed = 0;
#define CHECK(e) \
do { \
g_checks++; \
if (!(e)) { \
g_failed++; \
std::fprintf(stderr, "%s:%d: CHECK(%s) failed\n", __FILE__, __LINE__, #e); \
} \
} while (0)
template <typename T> static int spansOf(uint32_t type, const T &m, uint64_t size, RgfxRegionSpan *s)
{
return rgfxRegionSpans(type, reinterpret_cast<const uint8_t *>(&m), size, s);
}
int main()
{
RgfxRegionSpan s[kRgfxMaxRegionSpans] = {};
RgfxBufferOverHostMemory buffer = {};
buffer.region_offset = 0x8400000;
buffer.length = 0x1000;
CHECK(spansOf(RGFX_CMD_BUFFER_OVER_HOST_MEMORY, buffer, sizeof(buffer), s) == 1);
CHECK(s[0].offset == 0x8400000 && s[0].length == 0x1000);
CHECK(spansOf(RGFX_CMD_BUFFER_OVER_HOST_MEMORY, buffer, sizeof(buffer) - 8, s) == -1);
// A buffer over pages names its pages block, not the bytes of the view it lies at.
RgfxBufferOverPages pages = {};
pages.block_offset = 0x8800000;
pages.block_size = 0x2000;
pages.offset = 0x123;
pages.length = 0x40000;
CHECK(spansOf(RGFX_CMD_BUFFER_OVER_PAGES, pages, sizeof(pages), s) == 1);
CHECK(s[0].offset == 0x8800000 && s[0].length == 0x2000);
CHECK(spansOf(RGFX_CMD_BUFFER_OVER_PAGES, pages, sizeof(pages) - 8, s) == -1);
// A heap over pages names its pages block too, not the guest RAM it lies over.
RgfxHeapOverPages pagesHeap = {};
pagesHeap.block_offset = 0x8c00000;
pagesHeap.block_size = 0x2000;
pagesHeap.size_bytes = 0x200000;
CHECK(spansOf(RGFX_CMD_HEAP_OVER_PAGES, pagesHeap, sizeof(pagesHeap), s) == 1);
CHECK(s[0].offset == 0x8c00000 && s[0].length == 0x2000);
CHECK(spansOf(RGFX_CMD_HEAP_OVER_PAGES, pagesHeap, sizeof(pagesHeap) - 8, s) == -1);
RgfxHeapOverHostMemory heap = {};
heap.region_offset = 0x9000000;
heap.size_bytes = 0x20000;
CHECK(spansOf(RGFX_CMD_HEAP_OVER_HOST_MEMORY, heap, sizeof(heap), s) == 1);
CHECK(s[0].offset == 0x9000000 && s[0].length == 0x20000);
CHECK(spansOf(RGFX_CMD_HEAP_OVER_HOST_MEMORY, heap, 16, s) == -1);
RgfxLibraryFromRegion chunk = {};
chunk.region_offset = 0xa000000;
chunk.byte_length = 0x4000;
chunk.ack_offset = 0x9fff000;
CHECK(spansOf(RGFX_CMD_LIBRARY_FROM_REGION, chunk, sizeof(chunk), s) == 2);
CHECK(s[0].offset == 0xa000000 && s[0].length == 0x4000);
CHECK(s[1].offset == 0x9fff000 && s[1].length == 8);
CHECK(spansOf(RGFX_CMD_LIBRARY_FROM_REGION, chunk, 72, s) == -1);
RgfxArgumentLayoutQuery layout = {};
layout.reply_offset = 0xb000000;
layout.reply_length = 0x4000;
CHECK(spansOf(RGFX_CMD_ARGUMENT_LAYOUT, layout, sizeof(layout), s) == 1);
CHECK(s[0].offset == 0xb000000 && s[0].length == 0x4000);
CHECK(spansOf(RGFX_CMD_ARGUMENT_LAYOUT, layout, 40, s) == -1);
// Messages that name no span of the region, whatever their size.
RgfxObjectCreate event = {};
CHECK(spansOf(RGFX_CMD_EVENT_CREATE, event, sizeof(event), s) == 0);
RgfxBufferAtOffset atOffset = {};
CHECK(spansOf(RGFX_CMD_BUFFER_AT_OFFSET, atOffset, sizeof(atOffset), s) == 0);
RgfxLibraryCreate library = {};
CHECK(spansOf(RGFX_CMD_LIBRARY_CREATE, library, 8, s) == 0);
// Nor does any message SubmitBatch takes, which it posts with no block pinned.
uint8_t batched[256] = {};
for (uint32_t type = 0; type <= RGFX_CMD_CONTEXT_CLOSE + 64; type++) {
const uint32_t size = rgfxSubmitBatchSize(type);
CHECK(size <= sizeof(batched));
if (size != 0 && size <= sizeof(batched))
CHECK(rgfxRegionSpans(type, batched, size, s) == 0);
}
if (g_failed) {
std::fprintf(stderr, "spans_test: %d of %d checks failed\n", g_failed, g_checks);
return 1;
}
std::printf("spans_test: OK (%d checks)\n", g_checks);
return 0;
}