diff --git a/guest/bundle/Makefile b/guest/bundle/Makefile index 5224d8aee..b8363c925 100644 --- a/guest/bundle/Makefile +++ b/guest/bundle/Makefile @@ -111,7 +111,7 @@ CORE_SRCS = src/RgfxObjectModel.cpp src/RgfxCaps.cpp src/RgfxRingWriter.cpp \ src/RgfxRegion.cpp src/RgfxReplyReader.cpp src/RgfxLibraryKey.cpp \ src/RgfxCompletionThread.cpp src/RgfxFunctionConstants.cpp \ src/RgfxBinaryArchiveFile.cpp src/RgfxAirArguments.cpp src/RgfxKnownPipelines.cpp \ - src/RgfxSlabs.cpp src/RgfxIcbSlotTable.cpp + src/RgfxSlabs.cpp src/RgfxIcbSlotTable.cpp src/RgfxLibraryStream.cpp # The video driver AppleGVA loads from this bundle (C++, src/RgfxVideoDriver.cpp): bundle only. VIDEO_SRCS = src/RgfxVideoDriver.cpp src/RgfxVideoH264.cpp src/RgfxVideoH265.cpp @@ -167,11 +167,11 @@ $(patsubst src/%.mm,$(BUILD)/obj/$(ARCH)/shell/%.o,$(wildcard src/RgfxMetalDevic # --- the serialiser check (x86_64, Rosetta) ---------------------------------- -$(SER_TESTBIN): $(CORE_OBJ_ARCH) tests/serializer_tests.cpp tests/slab_tests.cpp $(HEADERS) \ - $(TESTHEADERS) Makefile +$(SER_TESTBIN): $(CORE_OBJ_ARCH) tests/serializer_tests.cpp tests/slab_tests.cpp \ + tests/library_stream_tests.cpp $(HEADERS) $(TESTHEADERS) Makefile @mkdir -p $(@D) $(CXX) -arch $(ARCH) $(CXXFLAGS) -I tests $(CORE_OBJ_ARCH) tests/serializer_tests.cpp \ - tests/slab_tests.cpp -o $@ + tests/slab_tests.cpp tests/library_stream_tests.cpp -o $@ # --- the Metal-shell wrapper/routing check (native arch) --------------------- # The NSObject wrappers + serialiser core + the routing test; no device shell. diff --git a/guest/bundle/src/RgfxLibrary.h b/guest/bundle/src/RgfxLibrary.h index 8c4dad61b..37b0f4546 100644 --- a/guest/bundle/src/RgfxLibrary.h +++ b/guest/bundle/src/RgfxLibrary.h @@ -94,14 +94,14 @@ static const MTLFunctionType kRgfxFunctionTypeExtern = (MTLFunctionType)4; serializer:(nullable RgfxSerializer *)serializer; /// Creates a library from `length` bytes of .metallib/AIR at `bytes` by streaming -/// them through the process's staging window in `region` (two blocks, used in -/// turn, so the copy of one chunk overlaps the host's copy-out of the one before), -/// one RGFX_CMD_LIBRARY_FROM_REGION per chunk, for a library larger than one ring -/// message. Waits for the host to have copied a block out before writing it again. -/// The chunks carry the content key where `key` (32 bytes) is not null. Returns nil -/// if `serializer` or `region` is null, the region has no room for the window, the -/// host did not take a chunk (`*status` kRgfxNoAnswer), or the device refused the -/// library (`*status` its answer to the last chunk). +/// them through a staging window of `region` made for this library and given back +/// once the host has copied it out (RgfxLibraryStream.hpp), one +/// RGFX_CMD_LIBRARY_FROM_REGION per chunk, for a library larger than one ring +/// message. The chunks carry the content key where `key` (32 bytes) is not null. +/// Returns nil if `serializer` or `region` is null, the region has no room for a +/// window even of page-sized chunks, the host did not take a chunk (`*status` +/// kRgfxNoAnswer), or the device refused the library (`*status` its answer to the +/// last chunk). + (nullable instancetype)libraryStreamingBytes:(const void *)bytes length:(NSUInteger)length key:(nullable const uint8_t *)key diff --git a/guest/bundle/src/RgfxLibrary.mm b/guest/bundle/src/RgfxLibrary.mm index 96ca7056f..ec679c40a 100644 --- a/guest/bundle/src/RgfxLibrary.mm +++ b/guest/bundle/src/RgfxLibrary.mm @@ -29,6 +29,7 @@ #include "RgfxAirArguments.hpp" #include "RgfxFunctionConstants.hpp" +#include "RgfxLibraryStream.hpp" #include "RgfxRegion.hpp" #include "RgfxSerializer.hpp" @@ -164,98 +165,6 @@ bool readMetallibFunctions(const uint8_t *p, size_t n, std::vector(i); - st.ack[i] = reinterpret_cast(b.ptr + 64 * i); - __atomic_store_n(st.ack[i], 0, __ATOMIC_RELEASE); - st.blockOffset[i] = b.offset + 0x1000 + chunk * static_cast(i); - st.block[i] = b.ptr + 0x1000 + chunk * i; - st.expected[i] = 0; - } - st.made = true; - return true; - } - return false; -} - -// Waits, about ten seconds at most, for the host to have copied block `i`'s last chunk. -bool awaitBlock(LibraryStaging &st, int i) -{ - for (int t = 0; t < 50000; t++) { - if (__atomic_load_n(st.ack[i], __ATOMIC_ACQUIRE) == st.expected[i]) - return true; - usleep(200); - } - return false; -} - -// Streams `length` bytes to the host as one library: each chunk copied into the next -// staging block once the host has copied out what it last held, then posted. -// The last chunk's answer is `*outStatus`. -bool streamLibrary(RgfxRegion ®ion, RgfxSerializer &ser, const uint8_t *bytes, uint64_t length, - const uint8_t *key, RgfxObjId *outObjId, uint32_t *outStatus) -{ - LibraryStaging &st = libraryStaging(); - std::lock_guard guard(st.lock); - if (!st.made && !makeStaging(st, region)) - return false; - const RgfxObjId objId = ser.allocLibraryId(); - int i = 0; - for (uint64_t at = 0; at < length; at += st.chunk, i ^= 1) { - const uint64_t n = length - at < st.chunk ? length - at : st.chunk; - if (!awaitBlock(st, i)) - return false; - std::memcpy(const_cast(st.block[i]), bytes + at, n); - const uint64_t token = st.nextToken++; - st.expected[i] = token; - bool posted = false; - for (int r = 0; r < 2000 && !posted; r++) { - posted = ser.postLibraryChunk(objId, st.blockOffset[i], n, length, at, st.ackOffset[i], - token, key, outStatus); - if (!posted) - usleep(1000); - } - if (!posted) - return false; - } - *outObjId = objId; - return true; -} - // An `NSArray` a `new`-family SPI hands back, retained. using NewArrayFn = NSArray *__attribute__((ns_returns_retained)) (*)(id, SEL); @@ -519,8 +428,8 @@ static bool rgfxMade(uint32_t status) return nil; } RgfxObjId objId = 0; - if (!streamLibrary(*region, *serializer, static_cast(bytes), length, key, - &objId, status) || + if (!rgfxStreamLibrary(*region, *serializer, static_cast(bytes), length, key, + &objId, status) || !rgfxMade(*status)) { return nil; } diff --git a/guest/bundle/src/RgfxLibraryStream.cpp b/guest/bundle/src/RgfxLibraryStream.cpp new file mode 100644 index 000000000..a8d4aa929 --- /dev/null +++ b/guest/bundle/src/RgfxLibraryStream.cpp @@ -0,0 +1,172 @@ +/* + * RgfxLibraryStream: a library streamed through a staging window made for it + * (RgfxLibraryStream.hpp). + */ + +#include "RgfxLibraryStream.hpp" + +#include +#include +#include +#include +#include +#include + +#include "RgfxRegion.hpp" +#include "RgfxSerializer.hpp" + +RgfxStagingShape rgfxStagingShape(uint64_t length, uint64_t chunkMax) +{ + const uint64_t chunks = (length + chunkMax - 1) / chunkMax; + const uint64_t even = (length + chunks - 1) / chunks; + RgfxStagingShape s = {}; + s.chunk = (even + kRgfxStagingPage - 1) / kRgfxStagingPage * kRgfxStagingPage; + s.blocks = chunks > 1 ? 2 : 1; + s.bytes = kRgfxStagingPage + s.blocks * s.chunk; + return s; +} + +bool rgfxStagingSmaller(uint64_t *chunkMax) +{ + if (*chunkMax <= kRgfxStagingPage) + return false; + const uint64_t next = *chunkMax / 4; + *chunkMax = next < kRgfxStagingPage ? kRgfxStagingPage : next; + return true; +} + +namespace { + +// One stream's window: its block, its chunks' size and blocks, and each block's place, its ack +// slot and the ack its last chunk asked for (0: none). +struct Window { + uint64_t offset = 0; + RgfxStagingShape shape = {}; + uint64_t blockOffset[2] = {}; + volatile uint8_t *block[2] = {}; + uint64_t ackOffset[2] = {}; + volatile uint64_t *ack[2] = {}; + uint64_t expected[2] = {}; +}; + +// Streams in the process go one at a time, so it holds one window at most. +std::mutex &streamLock() +{ + static std::mutex *lock = new std::mutex(); // never destroyed: libraries come at any time + return *lock; +} + +// Each chunk's ack value, unique in the process. +uint64_t g_nextToken = 1; + +// The window for `length` bytes: the shape for the largest chunks the region has room for. +bool makeWindow(RgfxRegion ®ion, uint64_t length, Window *w) +{ + uint64_t chunkMax = kRgfxStagingChunkMax; + uint64_t lastBytes = 0; + do { + const RgfxStagingShape s = rgfxStagingShape(length, chunkMax); + if (s.bytes == lastBytes) + continue; // a library smaller than the chunk: the same window again + lastBytes = s.bytes; + RgfxRegion::Block b = region.allocate(s.bytes); + if (!b.ok) + continue; + w->offset = b.offset; + w->shape = s; + for (uint32_t i = 0; i < 2; i++) { + w->ackOffset[i] = b.offset + 64 * static_cast(i); + w->ack[i] = reinterpret_cast(b.ptr + 64 * i); + __atomic_store_n(w->ack[i], 0, __ATOMIC_RELEASE); + w->blockOffset[i] = b.offset + kRgfxStagingPage + s.chunk * i; + w->block[i] = b.ptr + kRgfxStagingPage + s.chunk * i; + } + if (s.chunk < rgfxStagingShape(length, kRgfxStagingChunkMax).chunk) { + static std::atomic logged{false}; + if (!logged.exchange(true)) + os_log(OS_LOG_DEFAULT, + "RGFX: a library of %llu bytes streams in chunks of %llu bytes: the region " + "had no room for a larger window (logged once)", + (unsigned long long)length, (unsigned long long)s.chunk); + } + return true; + } while (rgfxStagingSmaller(&chunkMax)); + static std::atomic logged{false}; + if (!logged.exchange(true)) + os_log_error(OS_LOG_DEFAULT, + "RGFX: no room in the region for a library's staging window, even of %llu " + "bytes: the library of %llu bytes is not made (logged once)", + (unsigned long long)rgfxStagingShape(length, kRgfxStagingPage).bytes, + (unsigned long long)length); + return false; +} + +// Waits, `awaitUs` at most, for the host to have copied block `i`'s last chunk out. +bool awaitBlock(const Window &w, uint32_t i, uint64_t awaitUs) +{ + const auto end = std::chrono::steady_clock::now() + std::chrono::microseconds(awaitUs); + for (;;) { + if (__atomic_load_n(w.ack[i], __ATOMIC_ACQUIRE) == w.expected[i]) + return true; + if (std::chrono::steady_clock::now() >= end) + return false; + usleep(200); + } +} + +} // namespace + +bool rgfxStreamLibrary(RgfxRegion ®ion, RgfxSerializer &ser, const uint8_t *bytes, + uint64_t length, const uint8_t *key, RgfxObjId *outObjId, + uint32_t *outStatus, uint64_t awaitUs) +{ + if (bytes == nullptr || length == 0) + return false; + std::lock_guard guard(streamLock()); + Window w; + if (!makeWindow(region, length, &w)) + return false; + const uint64_t chunk = w.shape.chunk; + const RgfxObjId objId = ser.allocLibraryId(); + bool streamed = true, copiedOut = true; + uint32_t i = 0; + for (uint64_t at = 0; at < length; at += chunk, i = (i + 1) % w.shape.blocks) { + const uint64_t n = length - at < chunk ? length - at : chunk; + if (!awaitBlock(w, i, awaitUs)) { + streamed = copiedOut = false; + break; + } + std::memcpy(const_cast(w.block[i]), bytes + at, n); + const uint64_t token = g_nextToken++; + bool posted = false; + for (int r = 0; r < 2000 && !posted; r++) { + posted = ser.postLibraryChunk(objId, w.blockOffset[i], n, length, at, w.ackOffset[i], + token, key, outStatus); + if (!posted) + usleep(1000); + } + if (!posted) { + streamed = false; + break; + } + w.expected[i] = token; + } + // The window goes back once the host has copied out every chunk it took (the last chunk's + // answer comes once the library is made from them all, so this rarely waits). + for (uint32_t b = 0; copiedOut && b < w.shape.blocks; b++) + copiedOut = awaitBlock(w, b, awaitUs); + if (copiedOut) { + region.release(w.offset); + } else { + static std::atomic logged{false}; + if (!logged.exchange(true)) + os_log_error(OS_LOG_DEFAULT, + "RGFX: the host did not copy a library's chunk out of its staging window " + "in time: the window's %llu bytes are kept (logged once)", + (unsigned long long)w.shape.bytes); + } + if (!streamed) + return false; + *outObjId = objId; + return true; +} diff --git a/guest/bundle/src/RgfxLibraryStream.hpp b/guest/bundle/src/RgfxLibraryStream.hpp new file mode 100644 index 000000000..f7c0d10b9 --- /dev/null +++ b/guest/bundle/src/RgfxLibraryStream.hpp @@ -0,0 +1,73 @@ +/* + * RgfxLibraryStream: a library too large for one ring message, streamed to the host in chunks + * (RGFX_CMD_LIBRARY_FROM_REGION) through a staging window of the device region. + * + * THE WINDOW. One block of the region: a page of ack slots (one a cache line), then one chunk + * block, or two used in turn where the library takes more than one chunk, so the copy of one + * chunk overlaps the host's copy-out of the one before. The host writes a block's ack slot once + * it has copied its chunk out, and the block is written again only then. The window is made for + * one stream, sized to its library: chunks of the library split evenly into as few chunks of at + * most kRgfxStagingChunkMax as hold it, each a whole number of pages, so a window is never more + * than the library and three pages. It goes back to the region once the host has copied out every + * chunk, so a process holds no window between streams. + * + * Where the region has no room for that window, smaller chunks are asked for, down to a page + * each, so a library is streamed while any room for a window of two pages and an ack page is + * left; only then is it refused. + * + * Streams are serialised in the process, so it holds at most one window at a time. + * + * Our own code; depends only on the committed wire header. + */ + +#ifndef RGFX_LIBRARY_STREAM_HPP_ +#define RGFX_LIBRARY_STREAM_HPP_ + +#include + +#include "RgfxObjectModel.hpp" + +class RgfxRegion; +class RgfxSerializer; + +/// A page: the ack slots' block and the unit every chunk is a multiple of. +constexpr uint64_t kRgfxStagingPage = 0x1000; +/// The most bytes one chunk carries: 32 MiB, so QuartzCore's 160 MB library goes in five chunks +/// per block. +constexpr uint64_t kRgfxStagingChunkMax = 32ull << 20; + +/// A staging window's shape: its chunks' size, how many chunk blocks it holds (1 or 2), and its +/// bytes, the ack page with them. +struct RgfxStagingShape { + uint64_t chunk; + uint32_t blocks; + uint64_t bytes; +}; + +/// The shape of the window that streams `length` bytes (more than 0) in chunks of at most +/// `chunkMax` bytes (a multiple of a page, at least one): as few chunks as hold the library, +/// split evenly and rounded up to whole pages, and a second block only where there are two or +/// more. +RgfxStagingShape rgfxStagingShape(uint64_t length, uint64_t chunkMax); + +/// The chunk sizes a stream asks for in turn where the region refuses the window before: from +/// kRgfxStagingChunkMax down to a page, each a quarter of the one before. `*chunkMax` becomes the +/// next; false after the last. +bool rgfxStagingSmaller(uint64_t *chunkMax); + +/// How long a stream waits for the host to copy a block's chunk out, at most: ten seconds. +constexpr uint64_t kRgfxStagingAwaitUs = 10ull * 1000 * 1000; + +/// Streams the `length` bytes at `bytes` to the host as one library, through a window of +/// `region` made for it: each chunk copied into the next block once the host has copied out what +/// it last held, then posted, carrying the content key where `key` (32 bytes) is not null. The +/// last chunk's answer is `*outStatus`, and the library's id `*outObjId`. The window goes back +/// once every chunk posted is copied out, which the host's acks say; one whose acks do not come +/// within `awaitUs` is kept, so nothing the host may still read is given out again. False where +/// the region has room for no window, where the host did not take a chunk, or where it did not +/// copy one out within `awaitUs`. +bool rgfxStreamLibrary(RgfxRegion ®ion, RgfxSerializer &ser, const uint8_t *bytes, + uint64_t length, const uint8_t *key, RgfxObjId *outObjId, + uint32_t *outStatus, uint64_t awaitUs = kRgfxStagingAwaitUs); + +#endif // RGFX_LIBRARY_STREAM_HPP_ diff --git a/guest/bundle/tests/library_stream_tests.cpp b/guest/bundle/tests/library_stream_tests.cpp new file mode 100644 index 000000000..0c3fb1d06 --- /dev/null +++ b/guest/bundle/tests/library_stream_tests.cpp @@ -0,0 +1,280 @@ +/* + * library_stream_tests: a library streamed through a staging window made for it + * (RgfxLibraryStream.hpp), against a host of the test's own on the kext's forwarding route: the + * window's shape for each library, its fallbacks where the region refuses it, the chunks' bytes + * and order, and the window going back once every chunk is copied out, or kept where one is not. + * Part of the serialiser check (serializer_tests.cpp calls rgfxLibraryStreamTests). No Metal. + */ + +#include +#include +#include + +#include "RgfxLibraryStream.hpp" +#include "RgfxObjectModel.hpp" +#include "RgfxRegion.hpp" +#include "RgfxRingWriter.hpp" +#include "RgfxSerializer.hpp" +#include "reims_graphics_wire.h" +#include "rgfx_test_ring.hpp" // CHECK, g_checks + +void rgfxLibraryStreamTests(); + +namespace { + +constexpr uint64_t kPage = 0x1000; +constexpr uint64_t kMiB = 1ull << 20; + +// The region's memory and the kext's blocks over it: first fit from the floor, each take and give +// logged; a take larger than `largest` refused, as a fragmented region refuses it. +struct Blocks { + std::vector mem; + struct Held { + uint64_t offset, size; + }; + std::vector held; + std::vector takes, gives; // sizes asked for, offsets given back + uint64_t largest = ~0ull; + + explicit Blocks(uint64_t bytes) : mem(RGFX_ALLOCATIONS_OFFSET + bytes, 0xee) {} + + static bool alloc(void *context, uint64_t size, uint64_t align, uint64_t *offset) + { + Blocks *b = static_cast(context); + b->takes.push_back(size); + if (size > b->largest) + return false; + uint64_t at = RGFX_ALLOCATIONS_OFFSET; + for (bool moved = true; moved;) { + moved = false; + at = (at + align - 1) & ~(align - 1); + for (const Held &h : b->held) + if (at < h.offset + h.size && h.offset < at + size) { + at = h.offset + h.size; + moved = true; + } + } + if (at + size > b->mem.size()) + return false; + b->held.push_back({at, size}); + *offset = at; + return true; + } + + static void release(void *context, uint64_t offset) + { + Blocks *b = static_cast(context); + b->gives.push_back(offset); + for (size_t i = 0; i < b->held.size(); i++) + if (b->held[i].offset == offset) { + b->held.erase(b->held.begin() + i); + return; + } + } +}; + +// The host: takes each LIBRARY_FROM_REGION off the forward, appends its chunk's bytes, and writes +// its ack (unless `ackless` names the chunk's place, which it never acks), answering the last +// chunk with `answer`. +struct Host { + Blocks *blocks; + std::vector library; + std::vector chunks; + uint32_t answer = RGFX_STATUS_OK; + uint64_t ackless = ~0ull; + uint64_t nextSeqno = 1; + + static bool forward(void *context, const void *message, uint32_t size, uint64_t *out_seqno, + uint32_t *out_status) + { + Host *h = static_cast(context); + *out_seqno = h->nextSeqno++; + if (out_status) + *out_status = kRgfxNoAnswer; + RgfxLibraryFromRegion m = {}; + CHECK(size == sizeof(m)); + std::memcpy(&m, message, sizeof(m)); + CHECK(m.header.type == RGFX_CMD_LIBRARY_FROM_REGION); + CHECK(m.chunk_offset == h->library.size()); // in order + h->chunks.push_back(m); + const uint8_t *base = h->blocks->mem.data(); + h->library.insert(h->library.end(), base + m.region_offset, + base + m.region_offset + m.byte_length); + if (m.chunk_offset != h->ackless) + std::memcpy(h->blocks->mem.data() + m.ack_offset, &m.ack_value, sizeof(m.ack_value)); + if (m.chunk_offset + m.byte_length >= m.total_length && out_status) + *out_status = h->answer; + return true; + } +}; + +std::vector pattern(uint64_t n) +{ + std::vector v(n); + for (uint64_t i = 0; i < n; i++) + v[i] = static_cast(i * 131 + (i >> 12)); + return v; +} + +// Streams `length` bytes through a region of `regionBytes` whose largest take is `largest`: +// what the host made, the window's takes and gives, and the stream's answer. +struct Run { + bool ok; + uint32_t status; + Host host; + std::vector takes, gives; + size_t heldAfter; +}; + +Run stream(uint64_t length, uint64_t regionBytes, uint64_t largest, uint64_t ackless = ~0ull, + uint64_t awaitUs = kRgfxStagingAwaitUs) +{ + Blocks blocks(regionBytes); + blocks.largest = largest; + RgfxRegion region(blocks.mem.data(), blocks.mem.size()); + region.setBlockSource(&Blocks::alloc, &Blocks::release, &blocks); + Run run = {}; + run.host.blocks = &blocks; + run.host.ackless = ackless; + RgfxRingWriter writer(&Host::forward, &run.host, 4096); + RgfxIdSpace ids; + RgfxSerializer ser(writer, ids); + const std::vector lib = pattern(length); + RgfxObjId objId = 0; + run.status = kRgfxNoAnswer; + run.ok = rgfxStreamLibrary(region, ser, lib.data(), lib.size(), nullptr, &objId, &run.status, + awaitUs); + if (run.ok) { + CHECK(objId != 0); + CHECK(run.host.library == lib); // every byte, in order + for (const RgfxLibraryFromRegion &c : run.host.chunks) + CHECK(c.obj_id == objId && c.total_length == length); + } + run.takes = blocks.takes; + run.gives = blocks.gives; + run.heldAfter = blocks.held.size(); + return run; +} + +// The window's shape: one chunk block for a library that fits a chunk, two for more, each chunk +// the library split evenly in whole pages, so the window is never more than the library and three +// pages; smaller chunk sizes in turn, down to a page. +void test_staging_shapes() +{ + RgfxStagingShape s = rgfxStagingShape(12296, kRgfxStagingChunkMax); // Photo Booth's library + CHECK(s.chunk == 4 * kPage && s.blocks == 1 && s.bytes == 5 * kPage); + s = rgfxStagingShape(kPage, kRgfxStagingChunkMax); + CHECK(s.chunk == kPage && s.blocks == 1 && s.bytes == 2 * kPage); + s = rgfxStagingShape(kRgfxStagingChunkMax, kRgfxStagingChunkMax); + CHECK(s.chunk == kRgfxStagingChunkMax && s.blocks == 1); + s = rgfxStagingShape(40 * kMiB, kRgfxStagingChunkMax); // two chunks of 20 MiB + CHECK(s.chunk == 20 * kMiB && s.blocks == 2 && s.bytes == kPage + 40 * kMiB); + s = rgfxStagingShape(160000000, kRgfxStagingChunkMax); // QuartzCore's: five chunks + CHECK(s.blocks == 2 && s.chunk <= kRgfxStagingChunkMax && 5 * s.chunk >= 160000000 && + 4 * s.chunk < 160000000); + for (uint64_t length = 1; length < 300 * kMiB; length = length * 3 + 7) { + for (uint64_t max = kRgfxStagingChunkMax;;) { + const RgfxStagingShape t = rgfxStagingShape(length, max); + CHECK(t.chunk % kPage == 0 && t.chunk >= kPage && t.chunk <= max); + CHECK(t.blocks == (length > t.chunk ? 2u : 1u)); + CHECK(t.bytes == kPage + t.blocks * t.chunk); + CHECK(t.bytes <= length + 3 * kPage || t.bytes <= kPage + 2 * max); + if (!rgfxStagingSmaller(&max)) + break; + } + } + uint64_t max = kRgfxStagingChunkMax; + std::vector sizes = {max}; + while (rgfxStagingSmaller(&max)) + sizes.push_back(max); + CHECK((sizes == std::vector{32 * kMiB, 8 * kMiB, 2 * kMiB, 512 * 1024, 128 * 1024, + 32 * 1024, 8 * 1024, kPage})); +} + +// A library through a window made for it: the window is its size, not a process-long 64 MiB, and +// goes back once the stream is done, so a second stream takes a window of its own. +void test_window_made_and_given_back() +{ + Run small = stream(12296, 128 * kMiB, ~0ull); + CHECK(small.ok && small.status == RGFX_STATUS_OK); + CHECK((small.takes == std::vector{5 * kPage})); + CHECK(small.gives.size() == 1 && small.heldAfter == 0); + CHECK(small.host.chunks.size() == 1 && small.host.chunks[0].byte_length == 12296); + + Run big = stream(70 * kMiB, 128 * kMiB, ~0ull); // three chunks through two blocks + CHECK(big.ok && big.status == RGFX_STATUS_OK); + const RgfxStagingShape s = rgfxStagingShape(70 * kMiB, kRgfxStagingChunkMax); + CHECK((big.takes == std::vector{s.bytes}) && s.blocks == 2); + CHECK(big.host.chunks.size() == 3 && big.gives.size() == 1 && big.heldAfter == 0); + // The two blocks in turn, each ack slot in the window's first page. + CHECK(big.host.chunks[0].region_offset != big.host.chunks[1].region_offset); + CHECK(big.host.chunks[0].region_offset == big.host.chunks[2].region_offset); + CHECK(big.host.chunks[0].ack_offset == big.host.chunks[2].ack_offset); + CHECK(big.host.chunks[1].ack_offset - big.host.chunks[0].ack_offset == 64); + CHECK(big.host.chunks[2].ack_value > big.host.chunks[1].ack_value); +} + +// Where the region refuses the window, smaller chunks are asked for in turn, and the library still +// streams whole; a region with no room even for page-sized chunks refuses the library, holding +// nothing. +void test_window_falls_back() +{ + Run frag = stream(10 * kMiB, 128 * kMiB, 80 * 1024); // gaps of 80 KiB at most + CHECK(frag.ok && frag.status == RGFX_STATUS_OK); + CHECK(frag.takes.size() > 1 && frag.takes.back() <= 80 * 1024); + for (size_t i = 1; i < frag.takes.size(); i++) + CHECK(frag.takes[i] < frag.takes[i - 1]); + CHECK(frag.host.chunks.size() > 2 && frag.gives.size() == 1 && frag.heldAfter == 0); + + Run tiny = stream(12296, 128 * kMiB, 3 * kPage); // a page of acks and one page a chunk + CHECK(tiny.ok && tiny.host.chunks.size() == 4 && tiny.takes.back() == 3 * kPage); + + Run none = stream(12296, 128 * kMiB, 2 * kPage - 1); + CHECK(!none.ok && none.host.chunks.empty() && none.gives.empty() && none.heldAfter == 0); + CHECK(none.takes.back() == 3 * kPage); // the last ask: two page-sized chunk blocks +} + +// The device's refusal of the library is its answer; the window still goes back. A chunk the host +// never acks keeps the window, so nothing it may still read is given out again. +void test_refused_and_ackless() +{ + { + Blocks blocks(16 * kMiB); + RgfxRegion region(blocks.mem.data(), blocks.mem.size()); + region.setBlockSource(&Blocks::alloc, &Blocks::release, &blocks); + Host host; + host.blocks = &blocks; + host.answer = RGFX_STATUS_REFUSED; + RgfxRingWriter writer(&Host::forward, &host, 4096); + RgfxIdSpace ids; + RgfxSerializer ser(writer, ids); + const std::vector lib = pattern(20000); + RgfxObjId objId = 0; + uint32_t status = 0; + CHECK(rgfxStreamLibrary(region, ser, lib.data(), lib.size(), nullptr, &objId, &status)); + CHECK(status == RGFX_STATUS_REFUSED && blocks.gives.size() == 1 && blocks.held.empty()); + } + // Room for chunks of 512 KiB: six, the fifth (at 2 MiB) never acked. The stream is done, the + // device made the library, and the window is kept. + Run lastAckless = stream(3 * kMiB, 16 * kMiB, 2 * kMiB + kPage, /*ackless*/ 2 * kMiB, + /*awaitUs*/ 2000); + CHECK(lastAckless.ok && lastAckless.status == RGFX_STATUS_OK); + CHECK(lastAckless.host.chunks.size() == 6 && lastAckless.host.chunks[0].byte_length == 512 * 1024); + CHECK(lastAckless.gives.empty() && lastAckless.heldAfter == 1); + // The first chunk never acked: its block is never written again, so the stream stops at the + // third chunk, answers false and keeps the window. + Run firstAckless = stream(3 * kMiB, 16 * kMiB, 2 * kMiB + kPage, /*ackless*/ 0, + /*awaitUs*/ 2000); + CHECK(!firstAckless.ok && firstAckless.host.chunks.size() == 2); + CHECK(firstAckless.gives.empty() && firstAckless.heldAfter == 1); +} + +} // namespace + +void rgfxLibraryStreamTests() +{ + test_staging_shapes(); + test_window_made_and_given_back(); + test_window_falls_back(); + test_refused_and_ackless(); +} diff --git a/guest/bundle/tests/serializer_tests.cpp b/guest/bundle/tests/serializer_tests.cpp index 0cb9a2b87..ec3fed44e 100644 --- a/guest/bundle/tests/serializer_tests.cpp +++ b/guest/bundle/tests/serializer_tests.cpp @@ -40,6 +40,7 @@ #include "rgfx_test_ring.hpp" // CHECK, g_checks, TestRing, expectMessage void rgfxSlabTests(); +void rgfxLibraryStreamTests(); namespace { @@ -2457,6 +2458,7 @@ int main() test_argument_layout_queries(); test_argument_layout_parse(); rgfxSlabTests(); + rgfxLibraryStreamTests(); std::printf("serializer_tests: OK (%d checks)\n", g_checks); return 0; }