Batch 42's guest test (job m3-max-25789) failed under the M3's load at
serializer_tests.cpp's CHECK(ending.calls.empty()): with setBatch's timer at
1 ms, a test thread held off a CPU for 1 ms after postLater let the timer
post before the writer's end. It is the class of the room-record test's
failure: a check that holds only while a thread runs within a time window.
The other guest test sites of it, found by reading every sleep, clock read
and timer delay in the bundle's and the kext's tests:
- serializer_tests, test_held_destroys, the writer's end: a run counts only
where the look at the calls comes within the timer's 1 ms from before the
post; runs repeat, up to 50, until one does. The calls are read under the
log's lock, as the timer's thread writes them.
- serializer_tests, test_ring_post_waits_for_room: the post's time, checked
to be at least 15 ms, was counted from after the host's thread started its
20 ms; it is now counted from before, so a late start of this thread no
longer shortens it.
- queue_tests, test_submitted_waits_for_the_buffers_before: the same for the
40 ms the wait must last, against the thread that commits after 50 ms.
- shell_tests, test_submitted_at_the_doorbell_wakes_no_thread: the
completion thread's late wake came 400 ms after the doorbell, and the test
checked the call took under 200 ms and the record was still untaken. The
wake now waits for the test to let it go (or 5 s), so the test checks the
call returned before the wake and the record untaken, with no window.
Each keeps what it checks. The rest hold under any delay: lower bounds
counted from before what they wait for, negative checks of work nothing
lets run, and bounded waits of seconds.
A message too large for half its queue's ring was built in a block of the
queue's pool once it outgrew the ring, and grew from block to block. The
block is write-combined where the kext maps it so, and on an AMD host, which
applies the guest's memory type, a write-combined store still pending
stalls the encoding thread's next locked instruction until it drains. Every
retain and release on that thread, the app's and ours, paid it: on a
26.7.2 guest of a Ryzen host (RTX 3090), encode_probe's
setRenderPipelineState took 108 ns a call in a 10000-call buffer against
21.6 ns in a 400-call one, which stays on the builder's heap, and a
setVertexBuffer between a CFRetain and a CFRelease 103 ns against 17.1 ns.
Growing from block to block also read the old block back, uncached.
The message is now built in the builder's own buffer whatever its size,
which grows to the queue's guess as it outgrows the ring, and the commit
copies it once, whole, into a block from the block's start. The host reads
the same COMMAND_BUFFER bytes; only where the message lies in its block
changes. The tests that held the old placement change with it.
What changes for the guest: on an arm64 guest, newResidencySetWithDescriptor:
error: now makes a set and sets the error out to nil, where it answered nil and
an error, and allocResidencySet makes its object. Apple silicon's Metal makes
one on macOS 15 and later, and apps built for it (game ports, engines, and
QuartzCore's CAMetalLayer.residencySet on macOS 26) use it; some abort at nil.
The x86 guest answers exactly as before: nil, its error out set to nil, as the
x86 Macs answer.
The device keeps every allocation resident, so a set is guest-side
bookkeeping and sends nothing to the host. It answers what the M3 Max's set
answers (tools/oracle/oracle_residency): adds and removes pending until a
commit, a pending remove still counted, the set holding its allocations,
allocatedSize the memory the last commit left, each piece once (a heap's
resources their heap, a view its texture, a texture over a buffer its buffer),
a nil allocation ending the process. Two answers differ on purpose: the label
is the descriptor's, as Metal's documentation gives it (the M3 answers nil),
and a small buffer counts its own pages, not a shared 128 KiB block.
The arm64 queue holds its sets in a counted set and the command buffer holds
the sets it uses, and those its render and compute encoders are given, until
it completes, as Metal's do; a nil set raises the counted set's exception
there, as on the M3, so useInternalResidencySet: of nil now raises on arm64.
The register lists the set's members, and the old residency entries say they
describe the x86 bundle.
The resource SPI's isComplete and isWriteComplete answered YES, and its
waitUntilComplete returned at once, as an M3 probe measured right after a
commit. Apple's GL renderer on macOS 11.7.11 reads a drawable back with the
texture's waitUntilComplete after commitAndWaitUntilSubmitted, then a
synchronizeTexture: on a queue of its own and getBytes:. On a unified-memory
host that synchronize has nothing to copy and finished before the draw, so
about 2.5% of Safari's WebGL readPixels on a 780M returned the canvas as it
was before the draw.
What the guest sees now: once a command buffer that names a resource, or a
view of its memory, is submitted (commitAndWaitUntilSubmitted returned, or it
is scheduled), the resource answers isComplete and isWriteComplete NO and its
waitUntilComplete waits, until the bundle takes the buffer's completion and
has written back its copies; for any use. That is the Intel 630's answer once
the buffer is scheduled (macOS 26.7.1, oracle_complete, dgpu-oracle.md Q26).
The M3 answers the same, except isWriteComplete YES for a read-only use,
which arm64 guests do not get: it only adds waiting for readers, and the
register lists it as a gap. Before submission, and for a resource no buffer in
flight names, the answers stay YES at once, as all three Macs answer. The wait
ends at the completion's take, not behind an earlier buffer's handlers, as the
630's returns with the buffer still Scheduled; where no thread takes the
completions any more, waits end. A debug line counts the waits and the NO
answers.
The tests that held the old answers keep them for a resource no buffer in
flight names; test_resources_wait_for_their_buffers_in_flight holds the new
ones, with the draw's buffer unretained as the GL renderer's is, and
resource_tests holds a view's and a buffer texture's shared count.
The rest of the SPI members the AMD 5300M's and the Intel 630's objects both
answer, for the encoders, the command buffer and the indirect command buffers,
each as Metal's base classes answer it (read from code, macOS 27.0.1) and as a
fresh object of both x86 Macs answers it (measured on macOS 26.7.1):
- render, compute, blit and parallel encoders: what Metal's base encoders
raise an unrecognized selector for raises, the members that end an encoding
end it and answer no program address table, the GPU's conditionals and loops
close as none was open, a split handler is never called, the fills raise as
Metal's IOAccel blit encoder's do, and separate commits, vertex
amplification modes and kernel debug info are none (the last two logged);
- the command buffer: Metal's own residency sets are used as the caller's;
- indirect command buffers: the resource SPI a buffer answers (complete,
written, not purgeable, an allocation of its own, the options as asked), and
no driver header (logged);
- their commands: what Metal's base command raises for raises, the mesh ones
among them, and the members that read back what a command was given answer
what a fresh command answers, logged: a command keeps what it was given in
its buffer's slots or on the host (tools/oracle/oracle_icb.m spi measured
both Macs' answers fresh, encoded and reset).
No member of Metal's SPI is left pending in the register.
Metal's command queues adopt MTLCommandQueueSPI, and the frameworks send its
members to a queue without asking first. The AMD 5300M's and the Intel 630's
queues both answer 31 of them (measured with respondsToSelector: on macOS
26.7.1, tools/oracle/oracle_spi_members.m), so the bundle's queue answers them
too, as Metal's base queue, _MTLCommandQueue, answers them (read from code,
macOS 27.0.1):
- The quality of service, commit queue, completion queue and switches come from
the descriptor the queue is made with, where the guest's descriptor answers
them; a queue no descriptor names answers what both GPUs' queues of
newCommandQueue answer (quality of service 2).
- finish returns once every buffer committed before it has completed and run
its completed handlers.
- Buffers made once the queue profiles profile, as Metal's take the switch
from their queue.
- The statistics' and counters' settings are kept and answered back; the
device gathers neither, so the counters it lists are none.
- Turning execution or render work off logs as unsupported: the device
runs the buffers.
The register's lines for them move from pending to what they answer, and the
queue's tests hold the defaults, the descriptor's values and finish's wait.
Metal's command buffers adopt MTLCommandBufferSPI, and the frameworks send its
members to a buffer without asking first. The AMD 5300M's and the Intel 630's
buffers both answer 36 of them (measured with respondsToSelector: on macOS
26.7.1, tools/oracle/oracle_spi_members.m), so the bundle's buffer answers them
too, as Metal's base buffer, _MTLCommandBuffer, and its IOAccel subclass answer
them (read from code, macOS 27.0.1):
- __waitUntilScheduledAsync: and __waitUntilCompletedAsync:, the waits Metal's
Swift overlay awaits, run their block once the stage's handlers have, at once
where they have.
- profilingResults times each stage under Metal's ten keys while profiling is
enabled, and is nil without.
- The private data, its offset, the logging buffer, the logs and the shared
indirection table keep what they are given.
- commitWithDeadline: and its wait raise an unrecognized selector, the
dashboards end the process with Metal's words, and the sampling encoders are
nil, as Metal's base answers.
- Purging, a corrupting hook and a conditional abort are kernel commands the
device has none of, and log as unsupported.
The register's lines for them move from pending to what they answer, and the
queue's tests hold the waits' order and the SPI's state.
On macOS 12, Safari's web content process may call only the external
methods its sandbox lists on an IOAccelerator connection: 0-13, 15-18,
22-24, 28-31, 40, 42, 48, 256-265, 267, 268 and 512 in every Safari from
15.1 to 17.6. Its sandbox refused Features (0x10f) before the kext saw it,
and the bundle went on without the kext's features: it took 64-id chunks
where the kext granted 16, and the host refused the textures past each
chunk's first 16 ids (BadResourceId), so pages drew without their body and
text. Under Safari 15 and 16, SurfaceBlockRelease (0x10a) and
SurfaceRetire (0x10d) were refused too. macOS 11, and 13 and later, list
no numbers.
What the guest sees:
- New selector numbers, selector map 1: Features 3, PagesBlock 4,
TakeAnswers 7, SurfaceBlockRelease 8, SurfaceRetire 9, and a shared
event port's Export 10 and Import 11. 1, 5 and 6 stay reserved, 2 is
given to none, and the rest keep their numbers. No number of map 0
takes a new meaning, so a bundle or kext of map 0 is refused, not
misrouted.
- Features answers a third word, the selector map, and keeps its number
in every map. A bundle that meets another map has no Metal device.
- ResourceIds answers the chunk's size after its first id, and the bundle
takes its chunks at that size.
- Where the bundle's setup left a stubbed device (no features, the device
info unread or of another version, the context or the user client not
opened) or returned silently (the IOGPU base declined), the process has
no Metal device, with a log line that says why. A host's video caps of
another version still leave the device without video codecs, now
logged.
user_client_words_test holds the sandbox's set, checks every selector and
the kext's routing against it, and checks that every IOConnectCall*Method
in the bundle's sources names one of them. design.md's registry gives map
1 and what map 0 was. The kext set is built again.
On a host whose GPU shares the CPU's memory a surface's textures use its pages
in place, but the runtime makes a texture over pages it cannot place one over
(rows that end inside the line the GPU writes back whole, on Intel; pages that
are more than one dma-buf, on RADV) a shadow, which every command buffer copies
in before its first use and merges back after a write. Such a surface is better
kept in an IOSurface device cache, copied only at CPU locks and unlocks, as on
a GPU of its own memory.
This adds the protocol for that choice, made surface by surface:
- SURFACE_PAGES takes RGFX_SURFACE_PAGES_ASK_CACHE with the surface's planes
(RgfxSurfacePagesLayout). The host answers OK, the block in place, or
RGFX_STATUS_CACHE, after which the kext makes the surface's device cache and
posts the block again cached, taking the view the ask made.
- The platform answers whether textures over the pages lie in place for every
access (GpuDevice::surface_pages_in_place). The Vulkan platform reads the
runtime's in-place verdicts for the pages' class and row ends, and on Linux
whether each plane's pages fit one dma-buf.
- Feature bit 29, SURFACE_CACHE_REFUSED, is what a host offers in place of
SURFACE_CACHE where it answers the ask. The kext asks only where it took the
bit, and the bundle hands the kext each block's planes.
- A host whose GPU shares the CPU's memory keeps device caches too, and makes
their transfer queue at the first cache, so one that makes none makes no
queue.
No host offers bit 29 yet, so nothing changes for the guest or the host.
guest/prebuilt and the option ROM's record are rebuilt for the wire header and
the kext; the ROM's bytes are unchanged.
A buffer with a completed handler paid two hops to its queue's completion
queue, one for its CB_SCHEDULED and one for its CB_COMPLETE, though its
scheduling runs nothing of the app's when it has no scheduled handler. Such a
buffer's scheduling now runs on the completion thread as it takes the record,
where none of the queue's records still waits on the completion queue, as the
records of a buffer with no handler at all already do.
What the guest sees: for a buffer with no scheduled handler, the status turns
Scheduled and waitUntilScheduled returns from the completion thread, one
thread wake sooner. Behind a record of its queue still to run, it waits its
turn as before, so the status still flips in the ring's order among the
handlers. The completed handler still runs on the completion queue.
The car 12 probe put a lone buffer's commit-to-scheduled-handler at about
390 us p50 on the RTX 5080 host, against 198 on an AMD Mac. Part of that is the
guest's own hops: the completion thread's wake, then the queue's completion
queue for each record. shell_tests --queue-timing [rounds] times them on the
bundle's real queue, reader and completion thread, with the kext's wait stood
in for: how long after the host's post of a CB_SCHEDULED, then a CB_COMPLETE,
the handlers run and waitUntilScheduled and waitUntilCompleted return, for a
buffer with both handlers, one with a completed handler alone and one with
none. It is a measurement, run by hand, never in make test.
The copies a command buffer wrote that no surface retire waits for went
back only at its delivery, behind the queue's earlier completed handlers:
app memory under a no-copy buffer (RgfxMemoryShadow), and the copy of a
surface whose writes were not named to the kext. A handler that ran long,
or a CPU that read the memory once the GPU was done (a shared event the
buffer signalled), saw the bytes as of an earlier buffer.
Those copies now go back on the queue's write-back queue as the buffer's
CB_COMPLETE is taken, in the ring's order, as the named surfaces' copies
already did; the delivery waits for them, so the status and the waiters
still come after the write-back.
What the guest sees: a buffer's GPU writes to such memory show as soon as
its completion is taken, not after the handlers of the buffers before it.
A command buffer that wrote an IOSurface's copy, or came behind one, wrote
its copies back and retired its writes at its delivery, on its queue's
completion queue, behind the queue's earlier handlers. A completed handler
that locked a surface a later buffer of its queue writes waited in the kext
for that buffer's retire, which could not come until the handler returned:
the lock waited out the kext's 2 s, then went on without the buffer's
results. Metal's lock waits only for the GPU.
What changes for the guest: such a buffer writes its copies back and retires
its writes on a serial queue of its queue's own, one buffer at a time in the
ring's order, as soon as its completion is taken; its delivery waits for
that, so its handlers and its status still see its results in the surfaces'
pages. A lock in an earlier handler goes on once the later buffer's copies
are written back. A buffer that writes only surfaces in place and is named
retires at the take as before; one behind a copy still retires after it.
The write-back empties the buffer's named accesses under _namedLock before
it retires, as the delivery did: a held buffer whose CB_SCHEDULED did not
come, named at its delivery's scheduling, and one whose post's naming is
still to come, name no write to the kext after its retire, which would hold
the surface's CPU locks for good (agreed with fx-decoder's held naming).
queue_tests' test_in_place_writes_retire_at_the_take now has the copy and the
in-place buffer behind it retire, in order and after the copy's write-back,
while the handler before them holds their deliveries; and a held buffer
writing the copy, completed with no CB_SCHEDULED, retires there and is named
to the kext neither at its post nor after its retire.
The host now posts a signal the buffer has work after while the buffer runs,
ahead of its CB_SCHEDULED. The test posts such a value on a queue's ring the
completion thread drives, and checks that a thread waiting on the CPU for it
wakes while the buffer is still Committed, that the value it then sets goes to
the host, and that the buffer completes once the host lets its wait go.
A command buffer's scheduling and completion records ran on its queue's
completion queue whatever they held, so the waiter for a buffer with no handler
woke two wakes after the completion thread took its record off the ring: a
libdispatch worker's for the record, then its own. Metal's waiter for a buffer
with no completed handler is woken by the thread that saw it complete (on the
M3 Max the wait returned 3 to 10 us after the status read Completed).
A buffer with no scheduled or completed handler, whose surface writes are all
in place and which writes no copy of app memory, now has its records run on the
thread that takes them off the ring, where none of its queue's records is still
to run on the queue, as rgfxDeliver:here: already ran them where no thread
drives the ring. A buffer with a handler, and any behind it while its record is
still to run, runs on the queue in the ring's order as before, its waiter woken
after its status is final. The decision is made at the commit, after which no
handler can be added.
A command buffer's writes of shared IOSurfaces retired at its delivery, on its
queue's completion queue, behind the queue's earlier records and their
handlers. A CPU lock of a surface the buffer drew in place waited for that
retire, or for the kext's next look at the device's completions, though the
GPU's results were in the surface's pages from the device's completion on.
Metal's lock waits only for the GPU.
A buffer whose written surfaces are all in place, and already named to the
kext, now retires its writes on the completion thread as it takes its
CB_COMPLETE, with the record's seqno (its _seqno is written after the post,
which the host may complete first). A buffer with a copy to write back keeps
its retire at its delivery, after the write-back, and so does every later
buffer of the queue until that retire is done: the kext retires a queue's
writes up to a seqno, so an early retire behind a copy would retire the copy
before its write-back. No call is added: the one retire moves.
queue_tests' test_in_place_writes_retire_at_the_take pins both: the in-place
retire comes while an earlier handler holds the delivery (it fails with the
take's retire turned off), and a copy then an in-place buffer retire only at
their deliveries, in order.
A command buffer whose message is too large for its queue's ring puts it
in a block of the queue's body pool, and only its completion gave the
block back. One let go with no completion to come (posted on a queue
whose completion ring nothing reads) kept its block for good, so the
region lost a block a buffer. Its dealloc now gives the block back, as it
already gave back its slot and its inline bytes' chunks.
A buffer posted with a completion to come is held by its reader until
that completion, so a block given back at dealloc is one the host has
read, or never will. The test lets uncommitted and posted buffers go one
after another and checks that they reuse one block and that the queue's
going returns it to the region.
A second enqueue, an enqueue after the commit and a second commit returned
with nothing done. On Metal each ends the process at the call (measured on
the M3 Max, an AMD 5300M and an Intel 630), so code that relies on the
silence is code that dies on a Mac.
What changes for the guest: a second enqueue, or one after the commit,
ends the process with "enqueue an already enqueued command buffer" from
-[_MTLCommandBuffer enqueue]; a second commit, or a
commitAndWaitUntilSubmitted after the commit, ends it with "commit an
already committed command buffer" from -[IOGPUMetalCommandBuffer validate],
the M3's words (the AMD's are the same from MTLIOAccelCommandBuffer, and
the Intel trips its completed-handler assertion instead).
The shell test that committed twice to show nothing more was posted now
stops at the one commit; tests/queue_tests.mm checks the four deaths.
enqueue only changed the status, and each commit posted its buffer at once,
so buffers ran in commit order. An engine that encodes on several threads and
fixes the GPU order with enqueue then got a write and its read, or two
writes, in the wrong order (aud-bundle-01).
What changes for the guest: a queue's command buffers go on its submission
ring in enqueue order, as Metal runs them on the M3 Max, an AMD 5300M and an
Intel 630 (measured). The queue keeps its enqueued, unposted buffers oldest
first; a commit with no enqueue before it enqueues the buffer there. A
buffer committed behind one not yet committed stays Committed and unposted,
and commitAndWaitUntilSubmitted of it waits, until that one is committed;
that commit then posts every ready buffer behind it, in order. An enqueued
buffer let go uncommitted stays and holds back the buffers after it, as
Metal's does (measured on all three).
One thread at a time posts a queue's ready buffers. Its lock is taken again
only by a handler the poster itself runs, where no thread drives the ring,
and then the poster's loop posts the handler's buffer after the one it is
posting.
The checks are tests/queue_tests.mm's, with the audit's two enqueue checks
folded in, plus four threads committing 400 enqueued buffers in reverse.
One completion thread ran every queue's scheduled and completed handlers
inline, and setCompletionQueue: was kept but changed nothing. A completed
handler of one queue that waited on a semaphore or a shared event another
queue's handler signals held up every queue's completions in the process,
presents and waitUntilCompleted included, for good where the wait had no
timeout. WindowServer's handlers ran off the serial queue it gave, and one
queue's write-backs delayed every other queue's (aud-bundle-02).
What changes for the guest:
- A queue's records (its buffers' scheduled and completed handlers, their
write-backs, status changes and waiters) run in ring order on its
completion queue: the dispatch queue setCompletionQueue: gave, else a
serial queue of its own labelled com.Metal.CompletionQueueDispatch at
user-interactive QoS. That is where Metal runs them, one queue's one at a
time and two queues' at once, and the status changes among the handlers
(measured on the M3 Max, an AMD 5300M and an Intel 630).
- The completion thread runs no handler. It marks a buffer submitted as it
takes the CB_SUBMITTED or CB_SCHEDULED, and at the CB_COMPLETE it gives
back the queue's slot, the message's block and the inline bytes' chunks, so
commitAndWaitUntilSubmitted and -commandBuffer never wait for handlers, as
Metal's do not.
- The waits no longer take the ring inside a handler. A wait for a later
buffer of the handler's own queue, for its own buffer, or from the queue's
completion queue for that queue, now waits for good. Metal's does too on
all three GPUs (measured). A wait for another queue's buffer is released.
Where no completion thread drives the ring (the unit tests, or a thread that
ended), the thread that takes the ring runs the records, as before.
The tests follow: the thread test checks the handlers' queue, the nested
same-queue wait goes, and the check of a commitAndWaitUntilSubmitted
answered by CB_SCHEDULED takes Committed or Scheduled at the return, as
Metal may, then Scheduled after waitUntilScheduled. tests/queue_tests.mm adds
the audit's cross-queue check, the completion queue, the order of one
queue's records and the slots taken back at the read.
addCompletedHandler: and addScheduledHandler: appended to their lists with no
lock and no look at the status. An add while the completion was delivered
mutated the list the delivering thread enumerated, ending the process there
with an NSGenericException; an add after the delivery was never run
(aud-bundle-09).
What changes for the guest: a handler added after the commit, before or after
the buffer completes, ends the process at the call with Metal's words,
"Completed handler provided after commit call" or "Scheduled handler provided
after commit call", as Metal does on the M3 Max, an AMD 5300M and an Intel 630
(measured). presentDrawable: after the commit adds a scheduled handler, so it
ends there too. The adds and the commit take one lock, so the delivering
thread walks lists nothing changes any more.
One shell test added its handler after the commit; it adds it before now.
The checks are tests/queue_tests.mm's, with the audit's two late-handler
checks folded in.
An encoder held its command buffer weakly but wrote through the buffer's
RgfxEncoder, which the buffer's dealloc deletes. A buffer that went while its
encoder was open, such as a Swift makeCommandBuffer() temporary or one made in
a pool that drained, left the encoder writing freed memory (aud-bundle-10).
What changes for the guest: each encoder (render, compute, blit, parallel and
a parallel pass's sub-encoder) now holds its buffer strongly from its making
until its endEncoding, then lets it go. That is what Metal's encoders do
(measured on the M3 Max, an AMD 5300M and an Intel 630): a buffer whose only
holder is an open encoder lives on, an ended encoder no longer keeps it, and
the ended encoder's commandBuffer is nil once the buffer is gone.
The check is tests/queue_tests.mm, a new part of the shell tests, with the
audit's encoder check folded in.