🆕 Make residency sets on the arm64 device
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.
This commit is contained in:
1 parent
cc3c03cb75
commit
864a468059
13 files changed
+1174
-50
No files matched your search
@@ -197,7 +197,7 @@ $(SER_TESTBIN): $(CORE_OBJ_ARCH) tests/serializer_tests.cpp tests/slab_tests.cpp
|
||||
SHELL_TESTSRCS = tests/shell_tests.mm tests/protocol_tests.mm tests/archive_tests.mm \
|
||||
tests/resource_tests.mm tests/queue_tests.mm \
|
||||
tests/create_tests.mm tests/known_pipeline_tests.mm tests/icb_tests.mm tests/gl_tests.mm \
|
||||
tests/cl_mode_tests.mm
|
||||
tests/cl_mode_tests.mm tests/residency_tests.mm
|
||||
|
||||
$(SHELL_TESTBIN): $(CORE_OBJ_NAT) $(WRAP_OBJ_NAT) $(SHELL_TESTSRCS) $(HEADERS) $(TESTHEADERS) Makefile
|
||||
@mkdir -p $(@D)
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
#import "RgfxMetalError.h"
|
||||
#import "RgfxMetalPrivate.h"
|
||||
#import "RgfxRenderPipelineState.h"
|
||||
#import "RgfxResidencySet.h"
|
||||
#import "RgfxUnsupported.h"
|
||||
#import "RgfxUploads.h"
|
||||
|
||||
@@ -775,6 +776,9 @@ struct RgfxBarrierSlot {
|
||||
// finish's: each command buffer committed to it with a reader is in it until it has
|
||||
// completed (RgfxCmdBuffer's commit and rgfxDeliverCompletion:).
|
||||
dispatch_group_t _committedGroup;
|
||||
// The residency sets it is given, each as many times as it was added (RESIDENCY SETS, below),
|
||||
// under @synchronized(self): made with the queue on the arm64 device, none on the x86 one.
|
||||
NSCountedSet *_residencySets;
|
||||
}
|
||||
|
||||
+ (instancetype)queueWithSubmitRing:(RgfxRingWriter *)submitRing
|
||||
@@ -814,6 +818,9 @@ struct RgfxBarrierSlot {
|
||||
// completion queue is set: Metal's has this label and user-interactive QoS, one queue's
|
||||
// handlers run one at a time and two queues' at once (measured on the M3 Max, an AMD
|
||||
// 5300M and an Intel 630).
|
||||
#if RgfxMakesResidencySets
|
||||
q->_residencySets = [NSCountedSet set];
|
||||
#endif
|
||||
q->_ownCompletionQueue = dispatch_queue_create(
|
||||
"com.Metal.CompletionQueueDispatch",
|
||||
dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE,
|
||||
@@ -1118,29 +1125,62 @@ struct RgfxBarrierSlot {
|
||||
{
|
||||
}
|
||||
|
||||
// --- unsupported: the device makes no residency set (newResidencySetWithDescriptor:error:) ---
|
||||
// --- RESIDENCY SETS ---------------------------------------------------------------------------
|
||||
// The arm64 device's queue holds the sets it is given, each as many times as it was added, and
|
||||
// lets one go once it is removed as many times, as Metal's queue counts them in an NSCountedSet
|
||||
// (measured: the M3 Max, macOS 27.0.1, tools/oracle/oracle_residency's queue and retain cases):
|
||||
// the device keeps every allocation resident, so holding them is all a queue does. A nil set
|
||||
// raises the counted set's NSInvalidArgumentException, as Metal's queue does; a set never added
|
||||
// is removed with nothing done. The x86 device makes no set (RgfxMakesResidencySets), and its
|
||||
// queue logs one it is given.
|
||||
|
||||
- (void)addResidencySet:(id<MTLResidencySet>)residencySet
|
||||
{
|
||||
#if RgfxMakesResidencySets
|
||||
@synchronized(self) {
|
||||
[_residencySets addObject:residencySet];
|
||||
}
|
||||
#else
|
||||
if (residencySet != nil)
|
||||
RGFX_UNSUPPORTED("residency sets");
|
||||
#endif
|
||||
}
|
||||
- (void)addResidencySets:(const id<MTLResidencySet> _Nonnull[_Nonnull])residencySets
|
||||
count:(NSUInteger)count
|
||||
{
|
||||
#if RgfxMakesResidencySets
|
||||
@synchronized(self) {
|
||||
for (NSUInteger i = 0; i < count; i++)
|
||||
[_residencySets addObject:residencySets[i]];
|
||||
}
|
||||
#else
|
||||
if (count != 0)
|
||||
RGFX_UNSUPPORTED("residency sets");
|
||||
#endif
|
||||
}
|
||||
- (void)removeResidencySet:(id<MTLResidencySet>)residencySet
|
||||
{
|
||||
#if RgfxMakesResidencySets
|
||||
@synchronized(self) {
|
||||
[_residencySets removeObject:residencySet];
|
||||
}
|
||||
#else
|
||||
if (residencySet != nil)
|
||||
RGFX_UNSUPPORTED("residency sets");
|
||||
#endif
|
||||
}
|
||||
- (void)removeResidencySets:(const id<MTLResidencySet> _Nonnull[_Nonnull])residencySets
|
||||
count:(NSUInteger)count
|
||||
{
|
||||
#if RgfxMakesResidencySets
|
||||
@synchronized(self) {
|
||||
for (NSUInteger i = 0; i < count; i++)
|
||||
[_residencySets removeObject:residencySets[i]];
|
||||
}
|
||||
#else
|
||||
if (count != 0)
|
||||
RGFX_UNSUPPORTED("residency sets");
|
||||
#endif
|
||||
}
|
||||
|
||||
// --- dispatch queues and priority (MTLCommandQueue SPI WindowServer sets) -----------
|
||||
@@ -1634,6 +1674,9 @@ enum RgfxStage : unsigned {
|
||||
std::atomic<MTLCommandBufferStatus> _status;
|
||||
NSString *_cbLabel;
|
||||
NSMutableDictionary *_userDictionary; // userDictionary, made when first asked
|
||||
// The residency sets it uses, each as many times as it was used, under @synchronized(self):
|
||||
// made at the first use, let go once it completes (useResidencySet:).
|
||||
NSCountedSet *_residencySets;
|
||||
// What the client asked of the buffer's content protection (setProtectionOptions:).
|
||||
uint64_t _protectionOptions;
|
||||
// The error options its descriptor asked for (commandBufferWithDescriptor:).
|
||||
@@ -2552,6 +2595,7 @@ static NSError *rgfxCommandBufferError(NSInteger code)
|
||||
// wake, as Metal's buffer lets its retained references go; the blocks
|
||||
// __waitUntilCompletedAsync: was given run between the two, as Metal's do.
|
||||
_kept.clear();
|
||||
[self rgfxLetResidencySetsGo];
|
||||
[self rgfxRunWaiters:_completedWaiters done:&_completedWaitersDone];
|
||||
if (_done != nil && !_left.exchange(true)) {
|
||||
dispatch_group_leave(_done);
|
||||
@@ -2943,17 +2987,50 @@ static NSError *rgfxCommandBufferError(NSInteger code)
|
||||
RGFX_UNSUPPORTED("sparse textures");
|
||||
return nil;
|
||||
}
|
||||
// The device makes no residency set (newResidencySetWithDescriptor:error:).
|
||||
// The arm64 device's buffer holds the residency sets it uses, each as many times as it was used,
|
||||
// from the use until it completes, as Metal's buffer counts them in an NSCountedSet and lets
|
||||
// them go at its completion (measured: the M3 Max, macOS 27.0.1, tools/oracle/oracle_residency's
|
||||
// cb, retain and hold cases): the device keeps every allocation resident, so holding them is all
|
||||
// a buffer does. A nil set raises the counted set's NSInvalidArgumentException, as Metal's
|
||||
// buffer does, and a use after the commit is taken. The x86 device makes no set
|
||||
// (RgfxMakesResidencySets), and its buffer logs one it is given.
|
||||
- (void)useResidencySet:(id<MTLResidencySet>)residencySet
|
||||
{
|
||||
#if RgfxMakesResidencySets
|
||||
@synchronized(self) {
|
||||
if (_residencySets == nil)
|
||||
_residencySets = [NSCountedSet set];
|
||||
[_residencySets addObject:residencySet];
|
||||
}
|
||||
#else
|
||||
if (residencySet != nil)
|
||||
RGFX_UNSUPPORTED("residency sets");
|
||||
#endif
|
||||
}
|
||||
- (void)useResidencySets:(const id<MTLResidencySet> _Nonnull[_Nonnull])residencySets
|
||||
count:(NSUInteger)count
|
||||
{
|
||||
#if RgfxMakesResidencySets
|
||||
@synchronized(self) {
|
||||
if (_residencySets == nil)
|
||||
_residencySets = [NSCountedSet set];
|
||||
for (NSUInteger i = 0; i < count; i++)
|
||||
[_residencySets addObject:residencySets[i]];
|
||||
}
|
||||
#else
|
||||
if (count != 0)
|
||||
RGFX_UNSUPPORTED("residency sets");
|
||||
#endif
|
||||
}
|
||||
|
||||
// At its completion: the residency sets it used go, after the lock.
|
||||
- (void)rgfxLetResidencySetsGo
|
||||
{
|
||||
NS_VALID_UNTIL_END_OF_SCOPE NSCountedSet *used = nil;
|
||||
@synchronized(self) {
|
||||
used = _residencySets;
|
||||
_residencySets = nil;
|
||||
}
|
||||
}
|
||||
|
||||
// --- what Core Image and MPS send a command buffer beyond MTLCommandBuffer ------------
|
||||
@@ -4998,18 +5075,33 @@ RGFX_LOG_UNRECOGNIZED
|
||||
RGFX_UNSUPPORTED("tile dispatches over a region");
|
||||
}
|
||||
|
||||
// Metal takes nil and its own sets; the device makes no residency set
|
||||
// (newResidencySetWithDescriptor:error:), so nil is the only one this can be sent.
|
||||
// Metal's render and compute encoders take a residency set and nil (measured: the M3 Max, macOS
|
||||
// 27.0.1, tools/oracle/oracle_residency's render-use and compute-use cases). The arm64 device's
|
||||
// encoder has its command buffer hold a set it is given until the buffer completes (useResidencySet:
|
||||
// there), which is all the device needs, as it keeps every allocation resident, and passes over
|
||||
// nil. The x86 device makes no set (RgfxMakesResidencySets), and its encoder logs one it is given.
|
||||
- (void)useResidencySet:(id<MTLResidencySet>)residencySet
|
||||
{
|
||||
#if RgfxMakesResidencySets
|
||||
if (residencySet != nil)
|
||||
[_commandBuffer useResidencySet:residencySet];
|
||||
#else
|
||||
if (residencySet != nil)
|
||||
RGFX_UNSUPPORTED("residency sets");
|
||||
#endif
|
||||
}
|
||||
- (void)useResidencySets:(const id<MTLResidencySet> _Nonnull[_Nonnull])residencySets
|
||||
count:(NSUInteger)count
|
||||
{
|
||||
#if RgfxMakesResidencySets
|
||||
for (NSUInteger i = 0; i < count; i++) {
|
||||
if (residencySets[i] != nil)
|
||||
[_commandBuffer useResidencySet:residencySets[i]];
|
||||
}
|
||||
#else
|
||||
if (count != 0)
|
||||
RGFX_UNSUPPORTED("residency sets");
|
||||
#endif
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -5447,18 +5539,29 @@ RGFX_LOG_UNRECOGNIZED
|
||||
|
||||
// --- what MPS sends a compute encoder beyond its protocol ----------------------------
|
||||
|
||||
// Metal takes nil (and its own sets); the device makes no residency set, as the render
|
||||
// encoder's.
|
||||
// As the render encoder's.
|
||||
- (void)useResidencySet:(id<MTLResidencySet>)residencySet
|
||||
{
|
||||
#if RgfxMakesResidencySets
|
||||
if (residencySet != nil)
|
||||
[_commandBuffer useResidencySet:residencySet];
|
||||
#else
|
||||
if (residencySet != nil)
|
||||
RGFX_UNSUPPORTED("residency sets");
|
||||
#endif
|
||||
}
|
||||
- (void)useResidencySets:(const id<MTLResidencySet> _Nonnull[_Nonnull])residencySets
|
||||
count:(NSUInteger)count
|
||||
{
|
||||
#if RgfxMakesResidencySets
|
||||
for (NSUInteger i = 0; i < count; i++) {
|
||||
if (residencySets[i] != nil)
|
||||
[_commandBuffer useResidencySet:residencySets[i]];
|
||||
}
|
||||
#else
|
||||
if (count != 0)
|
||||
RGFX_UNSUPPORTED("residency sets");
|
||||
#endif
|
||||
}
|
||||
|
||||
// A loop the GPU runs while a value in `buffer` compares true: none on the wire, so it is
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
* depth/stencil states and heaps — each a thin forwarder to the matching wrapper
|
||||
* factory (RgfxBuffer/RgfxTexture/RgfxHeap), routing through the device's
|
||||
* `-serializer`, or, for samplers and depth/stencil states, to the device's
|
||||
* RgfxStateCache, which answers equal descriptors with one object.
|
||||
* RgfxStateCache, which answers equal descriptors with one object. It also makes
|
||||
* the arm64 device's residency sets (RgfxResidencySet), which the host never sees.
|
||||
*
|
||||
* Like the device class itself, this category is COMPILED into the bundle but not
|
||||
* linked into the user-space unit test: it names RgfxMetalDevice, which subclasses
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#import "RgfxIndirectCommandBuffer.h"
|
||||
#import "RgfxMetalPrivate.h"
|
||||
#import "RgfxRasterizationRateMap.h"
|
||||
#import "RgfxResidencySet.h"
|
||||
#import "RgfxStateCache.h"
|
||||
#import "RgfxTexture.h"
|
||||
#import "RgfxUnsupported.h"
|
||||
@@ -397,6 +398,39 @@ const char *bufferCause(NSUInteger length, MTLResourceOptions options, NSUIntege
|
||||
return [self newRasterizationRateMapWithDescriptor:descriptor];
|
||||
}
|
||||
|
||||
// --- residency sets --------------------------------------------------------------------------
|
||||
|
||||
// The arm64 device makes a set, as Apple silicon's Metal does, and sets the error out to nil,
|
||||
// whatever it held (measured: macOS 27.0.1, the M3 Max, AGXG15XFamilyResidencySet, a nil
|
||||
// descriptor's among them; RgfxResidencySet.h). The x86 device answers nil and sets the error out
|
||||
// to nil, as the x86 Macs do (measured: macOS 26.7.1, the AMD Radeon Pro 5300M and the Intel UHD
|
||||
// 630: an error out holding an error before the call holds nil after it).
|
||||
- (id<MTLResidencySet>)newResidencySetWithDescriptor:(MTLResidencySetDescriptor *)desc
|
||||
error:(NSError **)error
|
||||
{
|
||||
if (error != nullptr)
|
||||
*error = nil;
|
||||
#if RgfxMakesResidencySets
|
||||
return [[RgfxResidencySet alloc] initWithDevice:self descriptor:desc];
|
||||
#else
|
||||
RGFX_UNSUPPORTED("residency sets on the x86 device, as the x86 Macs make none");
|
||||
return nil;
|
||||
#endif
|
||||
}
|
||||
|
||||
// The object Metal's base device inits a residency set of (`@16@0:8`; initWithDevice:descriptor:,
|
||||
// as the M3 Max's AGXResidencySet answers it): the arm64 device's set, and nil on the x86 device,
|
||||
// as its public form above.
|
||||
- (id)allocResidencySet
|
||||
{
|
||||
#if RgfxMakesResidencySets
|
||||
return [RgfxResidencySet alloc];
|
||||
#else
|
||||
RGFX_UNSUPPORTED("residency sets on the x86 device, as the x86 Macs make none");
|
||||
return nil;
|
||||
#endif
|
||||
}
|
||||
|
||||
// --- heaps -------------------------------------------------------------------
|
||||
|
||||
- (id<MTLHeap>)newHeapWithDescriptor:(MTLHeapDescriptor *)descriptor
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
* What each waits on is listed in the effort's protocol-gaps-wire.md. Ray tracing, function
|
||||
* pointers, dynamic libraries, Metal 4, placement sparse and counters are what the device answers
|
||||
* no for (RgfxCaps); indirect command buffers, argument encoders,
|
||||
* binary archives, tile pipelines, residency sets, shared textures, log states and
|
||||
* binary archives, tile pipelines, shared textures, log states and
|
||||
* tensors are what the wire does not carry yet.
|
||||
*
|
||||
* Without these the IOGPU base's own creators answer, which build on IOGPU objects the device
|
||||
|
||||
@@ -125,24 +125,6 @@ NSError *metal4Refusal()
|
||||
return nil;
|
||||
}
|
||||
|
||||
// The x86 Macs answer nil and set the error out to nil, whatever it held (measured: macOS 26.7.1,
|
||||
// the AMD Radeon Pro 5300M and the Intel UHD 630: an error out holding an error before the call
|
||||
// holds nil after it), and so does the x86 device. An M3 makes a set (measured: macOS 27.0.1, the
|
||||
// M3 Max, AGXG15XFamilyResidencySet, with no error), which the device cannot: the arm64 device
|
||||
// answers nil and an error, as a use the device does not support.
|
||||
- (id<MTLResidencySet>)newResidencySetWithDescriptor:(MTLResidencySetDescriptor *)desc
|
||||
error:(NSError **)error
|
||||
{
|
||||
#if defined(__arm64__)
|
||||
RGFX_UNSUPPORTED_ERROR(error, "residency sets (none on the wire)");
|
||||
#else
|
||||
RGFX_UNSUPPORTED("residency sets (none on the wire)");
|
||||
if (error != nullptr)
|
||||
*error = nil;
|
||||
#endif
|
||||
return nil;
|
||||
}
|
||||
|
||||
// --- counters and shader logging ------------------------------------------------------
|
||||
|
||||
- (id<MTLCounterSampleBuffer>)newCounterSampleBufferWithDescriptor:
|
||||
@@ -437,14 +419,8 @@ NSError *metal4Refusal()
|
||||
return nil;
|
||||
}
|
||||
|
||||
// Residency sets (`@16@0:8`), counters' resolve (`@40@0:8@16{_NSRange=QQ}24`) and textures
|
||||
// shared by handle at a resource index (`@32@0:8@16Q24`), as their public forms above.
|
||||
- (id)allocResidencySet
|
||||
{
|
||||
RGFX_UNSUPPORTED("residency sets (none on the wire)");
|
||||
return nil;
|
||||
}
|
||||
|
||||
// Counters' resolve (`@40@0:8@16{_NSRange=QQ}24`) and textures shared by handle at a resource
|
||||
// index (`@32@0:8@16Q24`), as their public forms above.
|
||||
- (NSData *)resolveCounters:(id<MTLCounterSampleBuffer>)buffer withRange:(NSRange)range
|
||||
{
|
||||
RGFX_UNSUPPORTED("counter sampling");
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* RgfxResidencySet: the reims-graphics bundle's MTLResidencySet (macOS 15), the arm64 device's
|
||||
* answer to newResidencySetWithDescriptor:error:, as Apple silicon's Metal makes one; the x86
|
||||
* device makes none, as the x86 Macs make none (RgfxMakesResidencySets).
|
||||
*
|
||||
* The device keeps every allocation resident, so a set is bookkeeping alone: it sends nothing to
|
||||
* the host, requestResidency and endResidency do nothing, and a queue or a command buffer only
|
||||
* holds the sets it is given. What the set answers is what the M3 Max's answers
|
||||
* (docs/graphics-kext/dgpu-oracle.md, Q31; measured: macOS 27.0.1, tools/oracle/oracle_residency):
|
||||
* - It holds its allocations as three groups: the committed ones, the adds since the last
|
||||
* commit (none of them committed), and the committed ones a remove since then marks. An add
|
||||
* of a marked one takes its mark away, and a remove of an uncommitted add takes the add back;
|
||||
* an add of a committed one, or a remove of one the set does not hold, does nothing.
|
||||
* removeAllAllocations takes back every add and marks every committed one. A commit drops
|
||||
* the marked ones and commits the adds.
|
||||
* - allocationCount counts the committed allocations and the adds, the marked ones among them
|
||||
* until the commit drops them; containsAllocation: and allAllocations answer the adds and the
|
||||
* committed ones not marked (allAllocations in the order they were committed, then added).
|
||||
* - The set retains each allocation it holds, a marked one until the commit drops it.
|
||||
* - allocatedSize is what the last commit left: 0 before one, then the bytes of the memory the
|
||||
* committed allocations lie in, each piece of memory once however many of them lie there: a
|
||||
* heap's resources count their heap, a view its texture, a texture over a buffer its buffer,
|
||||
* and every other allocation its own allocatedSize. An allocation of no device of ours
|
||||
* counts nothing. (The M3's buffers count the block of 128 KiB its small buffers share; the
|
||||
* device's buffers lie in slabs, and each counts its own pages.)
|
||||
* - A nil allocation added or removed ends the process, as the M3's set does (SIGSEGV there);
|
||||
* containsAllocation: of nil answers NO.
|
||||
* - The label is the descriptor's, as Metal's documentation gives it ("The label specified at
|
||||
* creation"), and setLabel: changes it, as the M3's set answers it. The M3's set answers nil
|
||||
* whatever the descriptor's label: a documented answer is kept whatever a Mac does (class 1).
|
||||
* - The descriptor's initialCapacity is a hint the set does not need, and a nil descriptor makes
|
||||
* a set of none, as the M3 makes one. (The M3 spins without end at a capacity of
|
||||
* NSUIntegerMax.)
|
||||
*
|
||||
* Its members are safe to call from any thread, as a CAMetalLayer's set is changed from
|
||||
* QuartzCore's threads while the app reads it.
|
||||
*
|
||||
* Our own ObjC++: conforms to MTLResidencySet but subclasses only NSObject, so it instantiates
|
||||
* and is unit-tested on the Mac.
|
||||
*/
|
||||
|
||||
#ifndef RGFX_RESIDENCY_SET_H_
|
||||
#define RGFX_RESIDENCY_SET_H_
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <Metal/Metal.h>
|
||||
|
||||
// Whether the device makes residency sets: the arm64 device does, as Apple silicon's Metal makes
|
||||
// them on macOS 15 and later; the x86 device does not, as the x86 Macs answer nil (measured:
|
||||
// macOS 26.7.1, the AMD Radeon Pro 5300M and the Intel UHD 630), so no queue, command buffer or
|
||||
// encoder of it holds one.
|
||||
#if defined(__arm64__)
|
||||
#define RgfxMakesResidencySets 1
|
||||
#else
|
||||
#define RgfxMakesResidencySets 0
|
||||
#endif
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface RgfxResidencySet : NSObject <MTLResidencySet>
|
||||
|
||||
/// A set of `device` (weak, nil in a unit test) labelled with `descriptor`'s label, holding no
|
||||
/// allocation. `descriptor` may be nil. Its selector is the one Metal's base device inits the
|
||||
/// object of allocResidencySet with.
|
||||
- (instancetype)initWithDevice:(nullable id<MTLDevice>)device
|
||||
descriptor:(nullable MTLResidencySetDescriptor *)descriptor;
|
||||
|
||||
- (instancetype)init NS_UNAVAILABLE;
|
||||
|
||||
/// Changes the set's label (the descriptor's until then) to a copy of `label`, as Metal's set
|
||||
/// answers it (_MTLObjectWithLabel's).
|
||||
- (void)setLabel:(nullable NSString *)label;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
#endif // RGFX_RESIDENCY_SET_H_
|
||||
@@ -0,0 +1,253 @@
|
||||
/*
|
||||
* RgfxResidencySet: the bundle's MTLResidencySet (RgfxResidencySet.h says what it answers).
|
||||
*/
|
||||
|
||||
#import "RgfxResidencySet.h"
|
||||
|
||||
#include <os/lock.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#import "RgfxResource.h"
|
||||
#import "RgfxTexture.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// Ends the process at a nil allocation added or removed, as the M3 Max's set does (SIGSEGV;
|
||||
// measured: macOS 27.0.1, tools/oracle/oracle_residency's add-nil, remove-nil, adds-nil and
|
||||
// removes-nil).
|
||||
[[noreturn]] void endsTheProcess(const char *call)
|
||||
{
|
||||
NSLog(@"RGFX: -[RgfxResidencySet %s] of a nil allocation ends the process, as Metal's set does",
|
||||
call);
|
||||
abort();
|
||||
}
|
||||
|
||||
// The memory `allocation` lies in, which allocatedSize counts once however many of the set's
|
||||
// allocations lie there: a view's texture, a texture's buffer, and the heap either lies in;
|
||||
// nil for an allocation of no device of ours, which counts nothing, as the M3 Max's set counts
|
||||
// an allocation of none (measured: tools/oracle/oracle_residency, add-foreign).
|
||||
id memoryOf(id allocation)
|
||||
{
|
||||
if (![allocation isKindOfClass:[RgfxResource class]])
|
||||
return nil;
|
||||
id memory = allocation;
|
||||
while ([memory isKindOfClass:[RgfxTexture class]]) {
|
||||
RgfxTexture *texture = memory;
|
||||
id under = texture.parentTexture;
|
||||
if (under == nil)
|
||||
under = texture.buffer;
|
||||
if (under == nil)
|
||||
break;
|
||||
memory = under;
|
||||
}
|
||||
id<MTLHeap> heap = ((RgfxResource *)memory).heap;
|
||||
return heap != nil ? heap : memory;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@implementation RgfxResidencySet {
|
||||
__weak id<MTLDevice> _device;
|
||||
// Guards everything below.
|
||||
os_unfair_lock _lock;
|
||||
NSString *_label;
|
||||
// The committed allocations, in the order they were committed.
|
||||
NSMutableOrderedSet *_committed;
|
||||
// The adds since the last commit, none of them committed, in the order they were added.
|
||||
NSMutableOrderedSet *_added;
|
||||
// The committed allocations a remove since the last commit marks.
|
||||
NSMutableSet *_removed;
|
||||
// The memory the committed allocations lie in (memoryOf), each counted once an allocation.
|
||||
NSCountedSet *_memory;
|
||||
// The bytes of `_memory`'s objects: allocatedSize.
|
||||
uint64_t _allocatedSize;
|
||||
}
|
||||
|
||||
- (instancetype)initWithDevice:(id<MTLDevice>)device descriptor:(MTLResidencySetDescriptor *)descriptor
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
_device = device;
|
||||
_label = [descriptor.label copy];
|
||||
_lock = OS_UNFAIR_LOCK_INIT;
|
||||
_committed = [NSMutableOrderedSet orderedSet];
|
||||
_added = [NSMutableOrderedSet orderedSet];
|
||||
_removed = [NSMutableSet set];
|
||||
_memory = [NSCountedSet set];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id<MTLDevice>)device
|
||||
{
|
||||
return _device;
|
||||
}
|
||||
|
||||
- (NSString *)label
|
||||
{
|
||||
os_unfair_lock_lock(&_lock);
|
||||
NSString *label = _label;
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
return label;
|
||||
}
|
||||
|
||||
- (void)setLabel:(NSString *)label
|
||||
{
|
||||
NSString *copied = [label copy];
|
||||
os_unfair_lock_lock(&_lock);
|
||||
NS_VALID_UNTIL_END_OF_SCOPE NSString *was = _label; // let go after the lock
|
||||
_label = copied;
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
}
|
||||
|
||||
- (uint64_t)allocatedSize
|
||||
{
|
||||
os_unfair_lock_lock(&_lock);
|
||||
const uint64_t size = _allocatedSize;
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
return size;
|
||||
}
|
||||
|
||||
// The device keeps every allocation resident, so neither a request nor its end has anything to
|
||||
// do, and neither changes what the set answers, as on the M3 Max (measured).
|
||||
- (void)requestResidency
|
||||
{
|
||||
}
|
||||
|
||||
- (void)endResidency
|
||||
{
|
||||
}
|
||||
|
||||
// Under the lock: `allocation` added, as the header gives it.
|
||||
- (void)rgfxAdd:(id)allocation
|
||||
{
|
||||
if ([_removed containsObject:allocation])
|
||||
[_removed removeObject:allocation];
|
||||
else if (![_committed containsObject:allocation])
|
||||
[_added addObject:allocation];
|
||||
}
|
||||
|
||||
// Under the lock: `allocation` removed, as the header gives it.
|
||||
- (void)rgfxRemove:(id)allocation
|
||||
{
|
||||
if ([_added containsObject:allocation])
|
||||
[_added removeObject:allocation];
|
||||
else if ([_committed containsObject:allocation])
|
||||
[_removed addObject:allocation];
|
||||
}
|
||||
|
||||
- (void)addAllocation:(id<MTLAllocation>)allocation
|
||||
{
|
||||
if (allocation == nil)
|
||||
endsTheProcess("addAllocation:");
|
||||
os_unfair_lock_lock(&_lock);
|
||||
[self rgfxAdd:allocation];
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
}
|
||||
|
||||
- (void)addAllocations:(const id<MTLAllocation> _Nonnull[_Nonnull])allocations count:(NSUInteger)count
|
||||
{
|
||||
for (NSUInteger i = 0; i < count; i++) {
|
||||
if (allocations[i] == nil)
|
||||
endsTheProcess("addAllocations:count:");
|
||||
}
|
||||
os_unfair_lock_lock(&_lock);
|
||||
for (NSUInteger i = 0; i < count; i++)
|
||||
[self rgfxAdd:allocations[i]];
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
}
|
||||
|
||||
- (void)removeAllocation:(id<MTLAllocation>)allocation
|
||||
{
|
||||
if (allocation == nil)
|
||||
endsTheProcess("removeAllocation:");
|
||||
// An add taken back lets the allocation go here, after the lock, where the set held it last.
|
||||
NS_VALID_UNTIL_END_OF_SCOPE id held = allocation;
|
||||
os_unfair_lock_lock(&_lock);
|
||||
[self rgfxRemove:held];
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
}
|
||||
|
||||
- (void)removeAllocations:(const id<MTLAllocation> _Nonnull[_Nonnull])allocations
|
||||
count:(NSUInteger)count
|
||||
{
|
||||
for (NSUInteger i = 0; i < count; i++) {
|
||||
if (allocations[i] == nil)
|
||||
endsTheProcess("removeAllocations:count:");
|
||||
}
|
||||
NS_VALID_UNTIL_END_OF_SCOPE NSArray *held = [NSArray arrayWithObjects:allocations count:count];
|
||||
os_unfair_lock_lock(&_lock);
|
||||
for (id allocation in held)
|
||||
[self rgfxRemove:allocation];
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
}
|
||||
|
||||
- (void)removeAllAllocations
|
||||
{
|
||||
os_unfair_lock_lock(&_lock);
|
||||
NS_VALID_UNTIL_END_OF_SCOPE NSArray *takenBack = [_added.array copy]; // let go after the lock
|
||||
[_added removeAllObjects];
|
||||
[_removed addObjectsFromArray:_committed.array];
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
}
|
||||
|
||||
- (BOOL)containsAllocation:(id<MTLAllocation>)allocation
|
||||
{
|
||||
if (allocation == nil)
|
||||
return NO;
|
||||
os_unfair_lock_lock(&_lock);
|
||||
const BOOL contains = [_added containsObject:allocation] ||
|
||||
([_committed containsObject:allocation] && ![_removed containsObject:allocation]);
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
return contains;
|
||||
}
|
||||
|
||||
- (NSArray<id<MTLAllocation>> *)allAllocations
|
||||
{
|
||||
os_unfair_lock_lock(&_lock);
|
||||
NSMutableArray *all = [NSMutableArray arrayWithCapacity:_committed.count + _added.count];
|
||||
for (id allocation in _committed) {
|
||||
if (![_removed containsObject:allocation])
|
||||
[all addObject:allocation];
|
||||
}
|
||||
[all addObjectsFromArray:_added.array];
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
return all.count != 0 ? [all copy] : @[];
|
||||
}
|
||||
|
||||
- (NSUInteger)allocationCount
|
||||
{
|
||||
os_unfair_lock_lock(&_lock);
|
||||
const NSUInteger count = _committed.count + _added.count;
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
return count;
|
||||
}
|
||||
|
||||
- (void)commit
|
||||
{
|
||||
os_unfair_lock_lock(&_lock);
|
||||
NS_VALID_UNTIL_END_OF_SCOPE NSArray *dropped =
|
||||
_removed.count != 0 ? _removed.allObjects : nil; // let go after the lock
|
||||
for (id allocation in dropped) {
|
||||
[_committed removeObject:allocation];
|
||||
id memory = memoryOf(allocation);
|
||||
if (memory == nil)
|
||||
continue;
|
||||
[_memory removeObject:memory];
|
||||
if ([_memory countForObject:memory] == 0)
|
||||
_allocatedSize -= [(id<MTLAllocation>)memory allocatedSize];
|
||||
}
|
||||
[_removed removeAllObjects];
|
||||
for (id allocation in _added) {
|
||||
[_committed addObject:allocation];
|
||||
id memory = memoryOf(allocation);
|
||||
if (memory == nil)
|
||||
continue;
|
||||
if ([_memory countForObject:memory] == 0)
|
||||
_allocatedSize += [(id<MTLAllocation>)memory allocatedSize];
|
||||
[_memory addObject:memory];
|
||||
}
|
||||
[_added removeAllObjects];
|
||||
os_unfair_lock_unlock(&_lock);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -45,6 +45,9 @@
|
||||
%spi-answers what a fresh object of the kind of the AMD 5300M's and of the Intel 630's answers, for a member that takes no argument (measured: macOS 26.7.1, tools/oracle/oracle_spi_members.m answers)
|
||||
%in-flight NO while a command buffer that names the resource's memory is submitted and its completion is yet to be taken, for any use, and YES before and after, as the Intel 630 answers (measured: macOS 26.7.1, tools/oracle/oracle_complete.m; docs/graphics-kext/dgpu-oracle.md, Q26; RGFX_RESOURCE_SPI)
|
||||
%old-only a member only older guest releases' protocols carry (tests/protocol_members_releases.txt), with no public counterpart; no app pass on the releases that carry it showed a guest framework sending it to the bundle's objects (the bundle logs each selector it does not recognize, RGFX: unrecognized), so a call raises an unrecognized selector, as it did before
|
||||
%x86-residency residency sets on the x86 device, which makes none, as the x86 Macs make none (measured: macOS 26.7.1, the AMD 5300M and the Intel 630; RgfxMakesResidencySets, src/RgfxResidencySet.h): this register's bundle is the x86 one; the arm64 device makes them and its queue, command buffer and encoders hold them
|
||||
%residency-held the device keeps every allocation resident, so a residency set's request of residency, and its end, have nothing to do, and neither changes what the set answers, as on the M3 Max (measured: macOS 27.0.1, tools/oracle/oracle_residency)
|
||||
%residency-spi Metal's IOGPU queue and command buffer send it to their own sets (measured: the M3 Max, macOS 27.0.1, tools/oracle/oracle_residency layer-spy, which saw setCurrentGeneration: and setExpiredGeneration: of these, from IOGPU alone); the device's queue and command buffer are its own and send none, and QuartzCore, which changes a CAMetalLayer's set, sends none of them
|
||||
%old-only-device a member only macOS 11's or 12's MTLDeviceSPI carries (tests/protocol_members_releases.txt): on those releases the device's IOGPU base answers it, the guest's own class (measured: macOS 11.7.11, the device answers it); on the Mac the base lacks it
|
||||
|
||||
[RgfxArgumentEncoder]
|
||||
@@ -176,21 +179,21 @@ resourceStateCommandEncoderWithDescriptor: unsupported sparse textures
|
||||
retainedReferences implemented
|
||||
setLabel: implemented
|
||||
status implemented
|
||||
useResidencySet: partial residency sets
|
||||
useResidencySets:count: partial residency sets
|
||||
useResidencySet: partial %x86-residency
|
||||
useResidencySets:count: partial %x86-residency
|
||||
waitUntilCompleted implemented
|
||||
waitUntilScheduled implemented
|
||||
|
||||
[RgfxCommandQueue]
|
||||
addResidencySet: partial residency sets
|
||||
addResidencySets:count: partial residency sets
|
||||
addResidencySet: partial %x86-residency
|
||||
addResidencySets:count: partial %x86-residency
|
||||
commandBuffer implemented
|
||||
commandBufferWithDescriptor: implemented
|
||||
commandBufferWithUnretainedReferences implemented
|
||||
insertDebugCaptureBoundary metal %debug
|
||||
label implemented
|
||||
removeResidencySet: partial residency sets
|
||||
removeResidencySets:count: partial residency sets
|
||||
removeResidencySet: partial %x86-residency
|
||||
removeResidencySets:count: partial %x86-residency
|
||||
setLabel: implemented
|
||||
|
||||
[RgfxComputeCommandEncoder]
|
||||
@@ -707,7 +710,7 @@ newRenderPipelineStateWithTileDescriptor:completionHandler: implemented
|
||||
newRenderPipelineStateWithTileDescriptor:error: implemented
|
||||
newRenderPipelineStateWithTileDescriptor:options:completionHandler: partial tile pipelines (no tile pipeline create on the wire)
|
||||
newRenderPipelineStateWithTileDescriptor:options:reflection:error: partial tile pipelines (no tile pipeline create on the wire)
|
||||
newResidencySetWithDescriptor:error: unsupported residency sets (none on the wire)
|
||||
newResidencySetWithDescriptor:error: partial %x86-residency
|
||||
newResourceGroupFromResources:count: unsupported resource groups
|
||||
newSamplerStateWithDescriptor: implemented
|
||||
newSharedEvent implemented
|
||||
@@ -1241,6 +1244,22 @@ shaderValidation metal %no-validation
|
||||
supportIndirectCommandBuffers implemented
|
||||
threadgroupSizeMatchesTileSize implemented
|
||||
|
||||
[RgfxResidencySet]
|
||||
addAllocation: implemented
|
||||
addAllocations:count: implemented
|
||||
allAllocations implemented
|
||||
allocatedSize implemented
|
||||
allocationCount implemented
|
||||
commit implemented
|
||||
containsAllocation: implemented
|
||||
device implemented
|
||||
endResidency metal %residency-held
|
||||
label implemented
|
||||
removeAllAllocations implemented
|
||||
removeAllocation: implemented
|
||||
removeAllocations:count: implemented
|
||||
requestResidency metal %residency-held
|
||||
|
||||
[RgfxResource]
|
||||
device implemented
|
||||
heap implemented
|
||||
@@ -1512,8 +1531,8 @@ setThreadgroupDistributionModeWithClusterGroupIndex: metal does nothing, as Meta
|
||||
setThreadgroupPackingDisabled: metal raises an unrecognized selector, as Metal's base encoder does: it sends itself doesNotRecognizeSelector: (read from code, %enc-base)
|
||||
setToolsDispatchBufferSPI:atIndex: metal raises an unrecognized selector, as Metal's IOAccel compute encoder does: it sends itself doesNotRecognizeSelector: (read from code, %enc-base)
|
||||
signalProgress: absent %spi-neither
|
||||
useResidencySet: partial residency sets
|
||||
useResidencySets:count: partial residency sets
|
||||
useResidencySet: partial %x86-residency
|
||||
useResidencySets:count: partial %x86-residency
|
||||
useResourceGroup:usage: absent %spi-neither
|
||||
waitForProgress: absent %spi-neither
|
||||
waitForVirtualSubstream: absent %spi-neither
|
||||
@@ -1784,8 +1803,8 @@ setTriangleFrontFillMode:backFillMode: absent %spi-neither
|
||||
setVertexAmplificationMode:value: unsupported a vertex amplification mode
|
||||
setVertexSamplerState:lodMinClamp:lodMaxClamp:lodBias:atIndex: absent %spi-neither
|
||||
setViewportTransformEnabled: absent %spi-neither
|
||||
useResidencySet: partial residency sets
|
||||
useResidencySets:count: partial residency sets
|
||||
useResidencySet: partial %x86-residency
|
||||
useResidencySets:count: partial %x86-residency
|
||||
useResourceGroup:usage:stages: absent %spi-neither
|
||||
[RgfxRenderPipelineState]
|
||||
emulationMeshMaxPrimitiveCount metal 0, as both x86 Macs' fresh pipeline answers (%spi-answers) and Metal's base pipeline answers until its emulation is set up (read from code, %pipe-base): no pipeline of the device emulates mesh shaders
|
||||
@@ -1861,6 +1880,13 @@ vertexDebugInstrumentationData metal nil, as Metal's base pipeline answers for o
|
||||
vertexFunctionHandleWithFunction: unsupported function pointers
|
||||
vertexGlobalConstantsBase metal 0, as Metal's base pipeline answers for every pipeline (read from code, %pipe-base; %spi-answers)
|
||||
vertexGlobalConstantsSize metal 0, as Metal's base pipeline answers for every pipeline (read from code, %pipe-base; %spi-answers)
|
||||
[RgfxResidencySet]
|
||||
allCommittedAllocations absent %residency-spi
|
||||
currentGeneration absent %residency-spi
|
||||
expiredGeneration absent %residency-spi
|
||||
generationForAllocation: absent %residency-spi
|
||||
setCurrentGeneration: absent %residency-spi
|
||||
setExpiredGeneration: absent %residency-spi
|
||||
[RgfxResource]
|
||||
protectionOptions metal the content protection the resource was made with: the device protects no content and no call sets it, so none
|
||||
responsibleProcess implemented
|
||||
|
||||
@@ -1363,8 +1363,8 @@ void test_queue_barriers()
|
||||
|
||||
// --- the command buffer and the queue ---------------------------------------------
|
||||
|
||||
// The encoders of features the device does not support are nil, logged; residency sets
|
||||
// (which the device does not make) change nothing; a capture boundary marks nothing.
|
||||
// The encoders of features the device does not support are nil, logged; no residency sets
|
||||
// change nothing; a capture boundary marks nothing.
|
||||
void test_command_buffer_and_queue_members()
|
||||
{
|
||||
QueueFixture fx;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <IOSurface/IOSurface.h>
|
||||
#import <Metal/Metal.h>
|
||||
#import <objc/message.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
@@ -32,6 +33,7 @@
|
||||
#import "RgfxFunction.h"
|
||||
#import "RgfxIOSurface.h"
|
||||
#import "RgfxMetalPrivate.h"
|
||||
#import "RgfxResidencySet.h"
|
||||
#import "RgfxResource.h"
|
||||
#import "RgfxSharedEvent.h"
|
||||
#import "RgfxUnsupported.h"
|
||||
@@ -2012,6 +2014,20 @@ void test_async_waits_run_after_their_handlers()
|
||||
}
|
||||
}
|
||||
|
||||
// Whether `body` raises the NSInvalidArgumentException of a counted set given nil to insert, as
|
||||
// Metal's queue and command buffer raise at a nil residency set (measured: the M3 Max, macOS
|
||||
// 27.0.1, oracle_residency).
|
||||
bool raisesInsertingNil(void (^body)(void))
|
||||
{
|
||||
@try {
|
||||
body();
|
||||
} @catch (NSException *e) {
|
||||
return [e.name isEqualToString:NSInvalidArgumentException] &&
|
||||
[e.reason isEqualToString:@"*** -[NSCountedSet addObject:]: attempt to insert nil"];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// The SPI's state a buffer keeps, as Metal's base buffer keeps it, and the answers a fresh buffer
|
||||
// of the AMD 5300M's and the Intel 630's gives; profilingResults times each stage the buffer
|
||||
// reached while profiling was enabled, under Metal's keys.
|
||||
@@ -2042,10 +2058,18 @@ void test_command_buffer_spi_state()
|
||||
[spi configWithCommandBufferDescriptor:[MTLCommandBufferDescriptor new]];
|
||||
const int tasks[] = {1, 2};
|
||||
[spi setResponsibleTaskIDs:tasks count:2];
|
||||
// Metal's own residency sets are used as the caller's: none, and nothing logged.
|
||||
[spi useInternalResidencySet:nil];
|
||||
// Metal's own residency sets are used as the caller's: none, and nothing logged; nil
|
||||
// raises where the device makes sets, as on the M3 Max (oracle_residency's
|
||||
// cb-use-internal-nil).
|
||||
id noSets[1] = {nil};
|
||||
[spi useInternalResidencySets:noSets count:0];
|
||||
#if RgfxMakesResidencySets
|
||||
CHECK(raisesInsertingNil(^{
|
||||
[spi useInternalResidencySet:nil];
|
||||
}));
|
||||
#else
|
||||
[spi useInternalResidencySet:nil];
|
||||
#endif
|
||||
CHECK(!RgfxUnsupportedWasLogged([RgfxCmdBuffer class], @selector(useResidencySet:)) &&
|
||||
!RgfxUnsupportedWasLogged([RgfxCmdBuffer class], @selector(useResidencySets:count:)));
|
||||
[spi setProfilingEnabled:YES];
|
||||
@@ -2202,6 +2226,93 @@ void test_finish_waits_for_committed_buffers()
|
||||
}
|
||||
}
|
||||
|
||||
#if RgfxMakesResidencySets
|
||||
// The arm64 device's queue holds a residency set as many times as it was added, and its command
|
||||
// buffer holds the sets it uses, and those its render and compute encoders are given, until it
|
||||
// completes, as Metal's do on the M3 Max (oracle_residency's queue, retain, hold, cb and
|
||||
// render-use and compute-use cases). A nil set raises the counted set's
|
||||
// NSInvalidArgumentException at the queue and the buffer, and the encoders pass over it.
|
||||
void test_residency_sets_are_held_until_let_go()
|
||||
{
|
||||
Queue q(true);
|
||||
__weak id held = nil;
|
||||
@autoreleasepool {
|
||||
RgfxResidencySet *set = [[RgfxResidencySet alloc] initWithDevice:nil descriptor:nil];
|
||||
held = set;
|
||||
[q.queue addResidencySet:set];
|
||||
[q.queue addResidencySet:set];
|
||||
}
|
||||
CHECK(held != nil);
|
||||
@autoreleasepool {
|
||||
[q.queue removeResidencySet:held];
|
||||
}
|
||||
CHECK(held != nil);
|
||||
@autoreleasepool {
|
||||
[q.queue removeResidencySet:held];
|
||||
}
|
||||
CHECK(held == nil);
|
||||
@autoreleasepool {
|
||||
RgfxResidencySet *a = [[RgfxResidencySet alloc] initWithDevice:nil descriptor:nil];
|
||||
RgfxResidencySet *b = [[RgfxResidencySet alloc] initWithDevice:nil descriptor:nil];
|
||||
held = a;
|
||||
id<MTLResidencySet> both[] = {a, b};
|
||||
[q.queue removeResidencySet:a]; // never added: nothing
|
||||
[q.queue addResidencySets:both count:2];
|
||||
[q.queue addResidencySets:both count:0];
|
||||
[q.queue removeResidencySets:both count:0];
|
||||
[q.queue removeResidencySets:both count:2];
|
||||
[q.queue removeResidencySets:both count:2];
|
||||
}
|
||||
CHECK(held == nil);
|
||||
CHECK(raisesInsertingNil(^{
|
||||
[q.queue addResidencySet:(id<MTLResidencySet> _Nonnull)nil];
|
||||
}));
|
||||
@try {
|
||||
[q.queue removeResidencySet:(id<MTLResidencySet> _Nonnull)nil];
|
||||
CHECK(false);
|
||||
} @catch (NSException *e) {
|
||||
CHECK([e.name isEqualToString:NSInvalidArgumentException] &&
|
||||
[e.reason isEqualToString:@"*** -[NSCountedSet removeObject:]: attempt to remove nil"]);
|
||||
}
|
||||
|
||||
__weak id used = nil, viaRender = nil, viaCompute = nil;
|
||||
id<MTLCommandBuffer> cb = [q.queue commandBuffer];
|
||||
@autoreleasepool {
|
||||
RgfxResidencySet *u = [[RgfxResidencySet alloc] initWithDevice:nil descriptor:nil];
|
||||
RgfxResidencySet *r = [[RgfxResidencySet alloc] initWithDevice:nil descriptor:nil];
|
||||
RgfxResidencySet *c = [[RgfxResidencySet alloc] initWithDevice:nil descriptor:nil];
|
||||
used = u;
|
||||
viaRender = r;
|
||||
viaCompute = c;
|
||||
[cb useResidencySet:u];
|
||||
id<MTLResidencySet> twice[] = {u, u};
|
||||
[cb useResidencySets:twice count:2];
|
||||
id<MTLRenderCommandEncoder> renc = [cb renderCommandEncoderWithDescriptor:pass()];
|
||||
id renderSPI = renc;
|
||||
((void (*)(id, SEL, id))objc_msgSend)(renderSPI, @selector(useResidencySet:), nil);
|
||||
((void (*)(id, SEL, id))objc_msgSend)(renderSPI, @selector(useResidencySet:), r);
|
||||
[renc endEncoding];
|
||||
id<MTLComputeCommandEncoder> cenc = [cb computeCommandEncoder];
|
||||
id<MTLResidencySet> withNil[] = {c, nil};
|
||||
((void (*)(id, SEL, const id *, NSUInteger))objc_msgSend)(
|
||||
cenc, @selector(useResidencySets:count:), withNil, 2);
|
||||
[cenc endEncoding];
|
||||
}
|
||||
CHECK(used != nil && viaRender != nil && viaCompute != nil);
|
||||
CHECK(raisesInsertingNil(^{
|
||||
[cb useResidencySet:(id<MTLResidencySet> _Nonnull)nil];
|
||||
}));
|
||||
@autoreleasepool {
|
||||
[cb commit];
|
||||
}
|
||||
CHECK(used != nil && viaRender != nil && viaCompute != nil);
|
||||
complete(q, cb);
|
||||
CHECK(used == nil && viaRender == nil && viaCompute == nil);
|
||||
CHECK(!RgfxUnsupportedWasLogged([RgfxCmdBuffer class], @selector(useResidencySet:)) &&
|
||||
!RgfxUnsupportedWasLogged([RgfxCommandQueue class], @selector(addResidencySet:)));
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
void rgfxQueueTests(const char *self)
|
||||
@@ -2230,4 +2341,7 @@ void rgfxQueueTests(const char *self)
|
||||
test_command_queue_spi_state();
|
||||
test_queue_takes_its_descriptor();
|
||||
test_finish_waits_for_committed_buffers();
|
||||
#if RgfxMakesResidencySets
|
||||
test_residency_sets_are_held_until_let_go();
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,534 @@
|
||||
/*
|
||||
* residency_tests: the residency set's answers (src/RgfxResidencySet.h), part of the Metal
|
||||
* shell's check (shell_tests.mm calls rgfxResidencyTests; guest/bundle/Makefile `test`). Each
|
||||
* answer checked is the M3 Max's (macOS 27.0.1, tools/oracle/oracle_residency;
|
||||
* docs/graphics-kext/dgpu-oracle.md, Q31), but where the header says the set answers otherwise:
|
||||
* the order allAllocations lists them in (the M3's changes from run to run), the bytes of a
|
||||
* small buffer and the label, which is the descriptor's.
|
||||
*
|
||||
* Our own ObjC++; built on the Mac's native arch with the NSObject-based wrappers.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <Metal/Metal.h>
|
||||
|
||||
#include <crt_externs.h>
|
||||
#include <csignal>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <spawn.h>
|
||||
#include <string>
|
||||
#include <sys/wait.h>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
#import "RgfxBuffer.h"
|
||||
#import "RgfxHeap.h"
|
||||
#import "RgfxResidencySet.h"
|
||||
#import "RgfxTexture.h"
|
||||
|
||||
#include "RgfxObjectModel.hpp"
|
||||
#include "RgfxRingWriter.hpp"
|
||||
#include "RgfxSerializer.hpp"
|
||||
#include "reims_graphics_wire.h"
|
||||
#include "rgfx_test_ring.hpp" // CHECK, g_checks, TestRing
|
||||
|
||||
void rgfxResidencyTests(const char *self);
|
||||
int rgfxResidencyDiesCase(const char *name);
|
||||
void drainRing(TestRing &ring); // shell_tests.mm
|
||||
|
||||
// An allocation of no device: Metal's set takes it and counts nothing of it.
|
||||
@interface RgfxTestForeignAllocation : NSObject <MTLAllocation>
|
||||
@end
|
||||
@implementation RgfxTestForeignAllocation
|
||||
- (NSUInteger)allocatedSize
|
||||
{
|
||||
return 4096;
|
||||
}
|
||||
@end
|
||||
|
||||
namespace {
|
||||
|
||||
// This binary's path, which the death checks run again with --residency-dies CASE.
|
||||
const char *g_self = nullptr;
|
||||
|
||||
// The exit status of a death case that raised SIGABRT: its handler exits with it, so the death
|
||||
// writes no crash report.
|
||||
const int kAborted = 128 + SIGABRT;
|
||||
|
||||
// A ring, an id space and a serialiser for the allocations, as resource_tests.mm's fixture. The
|
||||
// transport is never freed: an object a test lets go may dealloc after the test and post its
|
||||
// destroy here.
|
||||
struct Fixture {
|
||||
TestRing ˚
|
||||
RgfxRingWriter &writer;
|
||||
RgfxIdSpace &ids;
|
||||
RgfxSerializer &ser;
|
||||
|
||||
Fixture()
|
||||
: ring(*new TestRing(0x40000)),
|
||||
writer(*new RgfxRingWriter(ring.base(), ring.dataSize, RGFX_CONTROL_RING_ID,
|
||||
TestRing::doorbell, nullptr)),
|
||||
ids(*new RgfxIdSpace()), ser(*new RgfxSerializer(writer, ids))
|
||||
{
|
||||
ring.setConsumerIdle(1);
|
||||
}
|
||||
};
|
||||
|
||||
// The allocations of oracle_residency's cases: B, a Shared buffer of 4096 bytes; T, a Private
|
||||
// 64 x 64 RGBA8 texture; H, a Private heap of 1 MiB; HB, a buffer of 4096 bytes of H; TV, a view
|
||||
// of T; X, a Shared buffer of 4096 bytes.
|
||||
struct Allocations {
|
||||
id<MTLBuffer> B, HB, X;
|
||||
id<MTLTexture> T, TV;
|
||||
id<MTLHeap> H;
|
||||
std::vector<std::pair<const char *, id>> named;
|
||||
|
||||
explicit Allocations(Fixture &fx)
|
||||
{
|
||||
B = [RgfxBuffer bufferWithLength:4096 options:0 device:nil serializer:&fx.ser];
|
||||
X = [RgfxBuffer bufferWithLength:4096 options:0 device:nil serializer:&fx.ser];
|
||||
MTLTextureDescriptor *td =
|
||||
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
|
||||
width:64
|
||||
height:64
|
||||
mipmapped:NO];
|
||||
td.storageMode = MTLStorageModePrivate;
|
||||
td.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
|
||||
T = [RgfxTexture textureWithDescriptor:td
|
||||
heap:RGFX_OBJ_NONE
|
||||
heapOffset:0
|
||||
device:nil
|
||||
serializer:&fx.ser];
|
||||
TV = [T newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm];
|
||||
MTLHeapDescriptor *hd = [MTLHeapDescriptor new];
|
||||
hd.size = 1 << 20;
|
||||
hd.storageMode = MTLStorageModePrivate;
|
||||
H = [RgfxHeap heapWithDescriptor:hd device:nil serializer:&fx.ser];
|
||||
HB = [H newBufferWithLength:4096 options:MTLResourceStorageModePrivate];
|
||||
CHECK(B != nil && X != nil && T != nil && TV != nil && H != nil && HB != nil);
|
||||
named = {{"B", B}, {"T", T}, {"H", H}, {"HB", HB}, {"TV", TV}, {"X", X}};
|
||||
}
|
||||
|
||||
// The names of `objects`, space-separated, in their order ("?" for one of none of them).
|
||||
std::string names(NSArray *objects) const
|
||||
{
|
||||
std::string out;
|
||||
for (id o in objects) {
|
||||
const char *name = "?";
|
||||
for (const auto &[n, a] : named) {
|
||||
if (a == o)
|
||||
name = n;
|
||||
}
|
||||
out += (out.empty() ? "" : " ") + std::string(name);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// The names `set` contains (containsAllocation:), space-separated, in the order of `named`.
|
||||
std::string contained(id<MTLResidencySet> set) const
|
||||
{
|
||||
std::string out;
|
||||
for (const auto &[n, a] : named) {
|
||||
if ([set containsAllocation:a])
|
||||
out += (out.empty() ? "" : " ") + std::string(n);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
RgfxResidencySet *newSet(NSString *label = nil)
|
||||
{
|
||||
MTLResidencySetDescriptor *d = [MTLResidencySetDescriptor new];
|
||||
d.label = label;
|
||||
return [[RgfxResidencySet alloc] initWithDevice:nil descriptor:d];
|
||||
}
|
||||
|
||||
// One step of oracle_residency's steps case: the set's count, the allocations it contains, the
|
||||
// ones allAllocations lists, in its order, and its allocatedSize.
|
||||
void expectStep(id<MTLResidencySet> set, const Allocations &a, NSUInteger count, const char *contains,
|
||||
const char *all, uint64_t size)
|
||||
{
|
||||
const std::string has = a.contained(set), listed = a.names(set.allAllocations);
|
||||
if (set.allocationCount != count || has != contains || listed != all || set.allocatedSize != size)
|
||||
std::fprintf(stderr, "step: count %lu has [%s] all [%s] size %llu; want %lu [%s] [%s] %llu\n",
|
||||
(unsigned long)set.allocationCount, has.c_str(), listed.c_str(),
|
||||
(unsigned long long)set.allocatedSize, (unsigned long)count, contains, all,
|
||||
(unsigned long long)size);
|
||||
CHECK(set.allocationCount == count);
|
||||
CHECK(has == contains);
|
||||
CHECK(listed == all);
|
||||
CHECK(set.allocatedSize == size);
|
||||
}
|
||||
|
||||
// A fresh set: no allocation, no size, the descriptor's label (which setLabel: changes), and an
|
||||
// empty allocations array; a nil descriptor and an initial capacity of NSUIntegerMax each make a
|
||||
// set too.
|
||||
void test_residency_set_answers()
|
||||
{
|
||||
RgfxResidencySet *set = newSet(@"oracle-rs");
|
||||
CHECK(set.device == nil);
|
||||
CHECK([set.label isEqualToString:@"oracle-rs"]);
|
||||
CHECK(set.allocatedSize == 0 && set.allocationCount == 0);
|
||||
// Empty, the same array each time, as the M3 Max's (__NSArray0, oracle_residency).
|
||||
CHECK(set.allAllocations.count == 0 && set.allAllocations == set.allAllocations);
|
||||
[set setLabel:@"set-later"];
|
||||
CHECK([set.label isEqualToString:@"set-later"]);
|
||||
[set setLabel:nil];
|
||||
CHECK(set.label == nil);
|
||||
CHECK([set conformsToProtocol:@protocol(MTLResidencySet)]);
|
||||
|
||||
RgfxResidencySet *none = [[RgfxResidencySet alloc] initWithDevice:nil descriptor:nil];
|
||||
CHECK(none != nil && none.label == nil && none.allocationCount == 0);
|
||||
MTLResidencySetDescriptor *huge = [MTLResidencySetDescriptor new];
|
||||
huge.initialCapacity = NSUIntegerMax;
|
||||
RgfxResidencySet *big = [[RgfxResidencySet alloc] initWithDevice:nil descriptor:huge];
|
||||
CHECK(big != nil && big.allocationCount == 0);
|
||||
|
||||
// Neither a request of residency nor its end changes what the set answers.
|
||||
Fixture fx;
|
||||
@autoreleasepool {
|
||||
Allocations a(fx);
|
||||
[set endResidency];
|
||||
[set requestResidency];
|
||||
[set addAllocation:a.B];
|
||||
[set requestResidency];
|
||||
expectStep(set, a, 1, "B", "B", 0);
|
||||
[set commit];
|
||||
[set endResidency];
|
||||
[set endResidency];
|
||||
expectStep(set, a, 1, "B", "B", a.B.allocatedSize);
|
||||
}
|
||||
drainRing(fx.ring);
|
||||
}
|
||||
|
||||
// oracle_residency's steps case, step by step, as the M3 Max answers each (the order of
|
||||
// allAllocations and the bytes of a small buffer as the header gives them).
|
||||
void test_residency_set_steps()
|
||||
{
|
||||
Fixture fx;
|
||||
@autoreleasepool {
|
||||
Allocations a(fx);
|
||||
const uint64_t b = a.B.allocatedSize, t = a.T.allocatedSize, h = a.H.allocatedSize,
|
||||
x = a.X.allocatedSize;
|
||||
CHECK(h == 1 << 20 && t != 0 && b != 0);
|
||||
RgfxResidencySet *s = newSet(@"oracle-rs");
|
||||
expectStep(s, a, 0, "", "", 0);
|
||||
[s addAllocation:a.B];
|
||||
expectStep(s, a, 1, "B", "B", 0);
|
||||
[s addAllocation:a.T];
|
||||
expectStep(s, a, 2, "B T", "B T", 0);
|
||||
[s addAllocation:a.H];
|
||||
expectStep(s, a, 3, "B T H", "B T H", 0);
|
||||
[s commit];
|
||||
expectStep(s, a, 3, "B T H", "B T H", b + t + h);
|
||||
[s addAllocation:a.B];
|
||||
expectStep(s, a, 3, "B T H", "B T H", b + t + h);
|
||||
[s addAllocation:a.HB];
|
||||
expectStep(s, a, 4, "B T H HB", "B T H HB", b + t + h);
|
||||
[s addAllocation:a.TV];
|
||||
expectStep(s, a, 5, "B T H HB TV", "B T H HB TV", b + t + h);
|
||||
// A heap's buffer counts its heap, a view its texture: neither adds.
|
||||
[s commit];
|
||||
expectStep(s, a, 5, "B T H HB TV", "B T H HB TV", b + t + h);
|
||||
// A remove is counted until the commit drops it.
|
||||
[s removeAllocation:a.B];
|
||||
expectStep(s, a, 5, "T H HB TV", "T H HB TV", b + t + h);
|
||||
[s commit];
|
||||
expectStep(s, a, 4, "T H HB TV", "T H HB TV", t + h);
|
||||
[s removeAllocation:a.X];
|
||||
expectStep(s, a, 4, "T H HB TV", "T H HB TV", t + h);
|
||||
[s commit];
|
||||
expectStep(s, a, 4, "T H HB TV", "T H HB TV", t + h);
|
||||
id two[] = {a.B, a.B, a.X};
|
||||
[s addAllocations:two count:3];
|
||||
expectStep(s, a, 6, "B T H HB TV X", "T H HB TV B X", t + h);
|
||||
[s commit];
|
||||
expectStep(s, a, 6, "B T H HB TV X", "T H HB TV B X", t + h + b + x);
|
||||
// T goes, but its view keeps its memory counted.
|
||||
id rm[] = {a.X, a.T};
|
||||
[s removeAllocations:rm count:2];
|
||||
expectStep(s, a, 6, "B H HB TV", "H HB TV B", t + h + b + x);
|
||||
[s commit];
|
||||
expectStep(s, a, 4, "B H HB TV", "H HB TV B", t + h + b);
|
||||
[s addAllocations:two count:0];
|
||||
[s removeAllocations:rm count:0];
|
||||
expectStep(s, a, 4, "B H HB TV", "H HB TV B", t + h + b);
|
||||
[s removeAllAllocations];
|
||||
expectStep(s, a, 4, "", "", t + h + b);
|
||||
[s commit];
|
||||
expectStep(s, a, 0, "", "", 0);
|
||||
[s commit];
|
||||
expectStep(s, a, 0, "", "", 0);
|
||||
// removeAllAllocations takes back an add, and marks what is committed.
|
||||
[s addAllocation:a.B];
|
||||
[s commit];
|
||||
[s addAllocation:a.T];
|
||||
expectStep(s, a, 2, "B T", "B T", b);
|
||||
[s removeAllAllocations];
|
||||
expectStep(s, a, 1, "", "", b);
|
||||
[s commit];
|
||||
expectStep(s, a, 0, "", "", 0);
|
||||
// An add takes a remove's mark away; a remove takes an add back.
|
||||
[s addAllocation:a.B];
|
||||
[s commit];
|
||||
[s removeAllocation:a.B];
|
||||
[s addAllocation:a.B];
|
||||
expectStep(s, a, 1, "B", "B", b);
|
||||
[s commit];
|
||||
expectStep(s, a, 1, "B", "B", b);
|
||||
[s addAllocation:a.T];
|
||||
[s removeAllocation:a.T];
|
||||
expectStep(s, a, 1, "B", "B", b);
|
||||
[s commit];
|
||||
expectStep(s, a, 1, "B", "B", b);
|
||||
[s removeAllocation:a.B];
|
||||
[s removeAllocation:a.B];
|
||||
expectStep(s, a, 1, "", "", b);
|
||||
[s commit];
|
||||
expectStep(s, a, 0, "", "", 0);
|
||||
for (const auto &[n, o] : a.named)
|
||||
[s addAllocation:o];
|
||||
expectStep(s, a, 6, "B T H HB TV X", "B T H HB TV X", 0);
|
||||
[s commit];
|
||||
expectStep(s, a, 6, "B T H HB TV X", "B T H HB TV X", b + t + h + x);
|
||||
}
|
||||
drainRing(fx.ring);
|
||||
}
|
||||
|
||||
// allocatedSize counts each piece of memory once: a heap's resources their heap, a view its
|
||||
// texture, a texture over a buffer its buffer, with or without what they lie in, as oracle_residency's
|
||||
// sizes case measures on the M3 Max; an allocation of no device counts nothing, and is held as any.
|
||||
void test_residency_set_sizes()
|
||||
{
|
||||
Fixture fx;
|
||||
@autoreleasepool {
|
||||
Allocations a(fx);
|
||||
RgfxBuffer *lb = [RgfxBuffer bufferWithLength:65536 options:0 device:nil serializer:&fx.ser];
|
||||
MTLTextureDescriptor *td =
|
||||
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
|
||||
width:64
|
||||
height:64
|
||||
mipmapped:NO];
|
||||
td.storageMode = MTLStorageModeShared;
|
||||
td.usage = MTLTextureUsageShaderRead;
|
||||
RgfxTexture *bt = [RgfxTexture textureWithDescriptor:td
|
||||
buffer:lb
|
||||
offset:0
|
||||
bytesPerRow:256
|
||||
device:nil
|
||||
serializer:&fx.ser];
|
||||
td.storageMode = MTLStorageModePrivate;
|
||||
id<MTLTexture> ht = [a.H newTextureWithDescriptor:td];
|
||||
CHECK(lb != nil && bt != nil && ht != nil);
|
||||
auto sizeOf = [](NSArray *objects) {
|
||||
RgfxResidencySet *s = newSet();
|
||||
for (id o in objects)
|
||||
[s addAllocation:o];
|
||||
[s commit];
|
||||
return s.allocatedSize;
|
||||
};
|
||||
const uint64_t b = a.B.allocatedSize, t = a.T.allocatedSize, h = a.H.allocatedSize;
|
||||
CHECK(sizeOf(@[ a.B ]) == b);
|
||||
CHECK(sizeOf(@[ a.B, a.X ]) == b + a.X.allocatedSize);
|
||||
CHECK(sizeOf(@[ a.HB ]) == h);
|
||||
CHECK(sizeOf(@[ ht ]) == h);
|
||||
CHECK(sizeOf(@[ a.HB, ht ]) == h);
|
||||
CHECK(sizeOf(@[ a.H, a.HB, ht ]) == h);
|
||||
CHECK(sizeOf(@[ a.T ]) == t);
|
||||
CHECK(sizeOf(@[ a.TV ]) == t);
|
||||
CHECK(sizeOf(@[ a.T, a.TV ]) == t);
|
||||
CHECK(sizeOf(@[ bt ]) == lb.allocatedSize);
|
||||
CHECK(sizeOf(@[ lb, bt ]) == lb.allocatedSize);
|
||||
|
||||
RgfxTestForeignAllocation *foreign = [RgfxTestForeignAllocation new];
|
||||
RgfxResidencySet *s = newSet();
|
||||
[s addAllocation:foreign];
|
||||
CHECK(s.allocationCount == 1 && [s containsAllocation:foreign] &&
|
||||
s.allAllocations.firstObject == foreign);
|
||||
[s commit];
|
||||
CHECK(s.allocationCount == 1 && s.allocatedSize == 0);
|
||||
[s addAllocation:a.T];
|
||||
[s commit];
|
||||
CHECK(s.allocatedSize == t);
|
||||
[s removeAllocation:foreign];
|
||||
[s commit];
|
||||
CHECK(s.allocationCount == 1 && s.allocatedSize == t);
|
||||
CHECK(![s containsAllocation:(id<MTLAllocation> _Nonnull)nil]);
|
||||
}
|
||||
drainRing(fx.ring);
|
||||
}
|
||||
|
||||
// The set holds each allocation it holds, a removed one until the commit drops it, as the M3
|
||||
// Max's set does (oracle_residency's hold case).
|
||||
void test_residency_set_holds_its_allocations()
|
||||
{
|
||||
Fixture fx;
|
||||
RgfxResidencySet *s = newSet();
|
||||
__weak id w = nil;
|
||||
@autoreleasepool {
|
||||
id<MTLBuffer> r = [RgfxBuffer bufferWithLength:4096 options:0 device:nil serializer:&fx.ser];
|
||||
w = r;
|
||||
[s addAllocation:r];
|
||||
}
|
||||
CHECK(w != nil && s.allocationCount == 1);
|
||||
@autoreleasepool {
|
||||
[s commit];
|
||||
}
|
||||
CHECK(w != nil);
|
||||
@autoreleasepool {
|
||||
[s removeAllocation:w];
|
||||
}
|
||||
CHECK(w != nil && s.allocationCount == 1);
|
||||
@autoreleasepool {
|
||||
[s commit];
|
||||
}
|
||||
CHECK(w == nil && s.allocationCount == 0);
|
||||
@autoreleasepool {
|
||||
id<MTLBuffer> r = [RgfxBuffer bufferWithLength:4096 options:0 device:nil serializer:&fx.ser];
|
||||
w = r;
|
||||
[s addAllocation:r];
|
||||
[s commit];
|
||||
[s removeAllAllocations];
|
||||
}
|
||||
CHECK(w != nil);
|
||||
@autoreleasepool {
|
||||
[s commit];
|
||||
}
|
||||
CHECK(w == nil && s.allocationCount == 0);
|
||||
// An add taken back lets the allocation go.
|
||||
@autoreleasepool {
|
||||
id<MTLBuffer> r = [RgfxBuffer bufferWithLength:4096 options:0 device:nil serializer:&fx.ser];
|
||||
w = r;
|
||||
[s addAllocation:r];
|
||||
}
|
||||
CHECK(w != nil);
|
||||
@autoreleasepool {
|
||||
[s removeAllocation:w];
|
||||
}
|
||||
CHECK(w == nil && s.allocationCount == 0);
|
||||
drainRing(fx.ring);
|
||||
}
|
||||
|
||||
// Threads that add, remove and commit at once, as an app and QuartzCore change a layer's set,
|
||||
// leave the set what their changes add up to.
|
||||
void test_residency_set_threads()
|
||||
{
|
||||
Fixture fx;
|
||||
@autoreleasepool {
|
||||
RgfxResidencySet *s = newSet();
|
||||
NSMutableArray *kept = [NSMutableArray array];
|
||||
for (int i = 0; i < 64; i++)
|
||||
[kept addObject:[RgfxBuffer bufferWithLength:4096 options:0 device:nil serializer:&fx.ser]];
|
||||
std::vector<std::thread> threads;
|
||||
for (int k = 0; k < 4; k++) {
|
||||
threads.emplace_back([s, kept, k] {
|
||||
for (int round = 0; round < 200; round++) {
|
||||
@autoreleasepool {
|
||||
for (int i = k; i < 64; i += 4)
|
||||
[s addAllocation:kept[i]];
|
||||
[s commit];
|
||||
(void)s.allAllocations;
|
||||
(void)s.allocatedSize;
|
||||
for (int i = k; i < 64; i += 8)
|
||||
[s removeAllocation:kept[i]];
|
||||
[s commit];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
for (std::thread &t : threads)
|
||||
t.join();
|
||||
// Each thread's last round leaves its odd half of its quarter.
|
||||
CHECK(s.allocationCount == 32);
|
||||
uint64_t size = 0;
|
||||
for (NSUInteger i = 0; i < 64; i++) {
|
||||
const bool held = (i % 8) >= 4;
|
||||
CHECK([s containsAllocation:kept[i]] == held);
|
||||
if (held)
|
||||
size += [kept[i] allocatedSize];
|
||||
}
|
||||
CHECK(s.allocatedSize == size);
|
||||
}
|
||||
drainRing(fx.ring);
|
||||
}
|
||||
|
||||
// Runs this binary again with --residency-dies `name`: it must end the process (SIGABRT) with
|
||||
// `words` on its stderr.
|
||||
void checkDies(const char *name, const char *words)
|
||||
{
|
||||
int fds[2];
|
||||
CHECK(pipe(fds) == 0);
|
||||
posix_spawn_file_actions_t fa;
|
||||
posix_spawn_file_actions_init(&fa);
|
||||
posix_spawn_file_actions_adddup2(&fa, fds[1], 2);
|
||||
posix_spawn_file_actions_addclose(&fa, fds[0]);
|
||||
char *args[] = {const_cast<char *>(g_self), const_cast<char *>("--residency-dies"),
|
||||
const_cast<char *>(name), nullptr};
|
||||
pid_t pid = 0;
|
||||
CHECK(posix_spawn(&pid, g_self, &fa, nullptr, args, *_NSGetEnviron()) == 0);
|
||||
posix_spawn_file_actions_destroy(&fa);
|
||||
close(fds[1]);
|
||||
std::string err;
|
||||
char chunk[512];
|
||||
ssize_t n;
|
||||
while ((n = read(fds[0], chunk, sizeof chunk)) > 0)
|
||||
err.append(chunk, static_cast<size_t>(n));
|
||||
close(fds[0]);
|
||||
int status = 0;
|
||||
CHECK(waitpid(pid, &status, 0) == pid);
|
||||
const bool aborted = WIFEXITED(status) && WEXITSTATUS(status) == kAborted;
|
||||
if (!aborted || err.find(words) == std::string::npos)
|
||||
std::fprintf(stderr, "--residency-dies %s ended with status %d:\n%s\n", name, status,
|
||||
err.c_str());
|
||||
CHECK(aborted);
|
||||
CHECK(err.find(words) != std::string::npos);
|
||||
}
|
||||
|
||||
// A nil allocation added or removed ends the process, as it does on the M3 Max (SIGSEGV there:
|
||||
// oracle_residency's add-nil, remove-nil, adds-nil and removes-nil).
|
||||
void test_residency_set_nil_allocations_end_the_process()
|
||||
{
|
||||
checkDies("add", "-[RgfxResidencySet addAllocation:] of a nil allocation ends the process");
|
||||
checkDies("adds", "-[RgfxResidencySet addAllocations:count:] of a nil allocation ends the process");
|
||||
checkDies("remove", "-[RgfxResidencySet removeAllocation:] of a nil allocation ends the process");
|
||||
checkDies("removes",
|
||||
"-[RgfxResidencySet removeAllocations:count:] of a nil allocation ends the process");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int rgfxResidencyDiesCase(const char *name)
|
||||
{
|
||||
signal(SIGABRT, [](int) { _exit(kAborted); });
|
||||
@autoreleasepool {
|
||||
RgfxResidencySet *s = newSet();
|
||||
id buffer = [RgfxTestForeignAllocation new];
|
||||
[s addAllocation:buffer];
|
||||
[s commit];
|
||||
id withNil[] = {buffer, nil};
|
||||
if (std::strcmp(name, "add") == 0)
|
||||
[s addAllocation:(id<MTLAllocation> _Nonnull)nil];
|
||||
else if (std::strcmp(name, "adds") == 0)
|
||||
[s addAllocations:withNil count:2];
|
||||
else if (std::strcmp(name, "remove") == 0)
|
||||
[s removeAllocation:(id<MTLAllocation> _Nonnull)nil];
|
||||
else if (std::strcmp(name, "removes") == 0)
|
||||
[s removeAllocations:withNil count:2];
|
||||
else
|
||||
return 2;
|
||||
}
|
||||
return 0; // it went on: the check fails
|
||||
}
|
||||
|
||||
void rgfxResidencyTests(const char *self)
|
||||
{
|
||||
g_self = self;
|
||||
test_residency_set_answers();
|
||||
test_residency_set_steps();
|
||||
test_residency_set_sizes();
|
||||
test_residency_set_holds_its_allocations();
|
||||
test_residency_set_threads();
|
||||
test_residency_set_nil_allocations_end_the_process();
|
||||
}
|
||||
@@ -17758,6 +17758,8 @@ void rgfxArchiveTests();
|
||||
void rgfxResourceTests();
|
||||
void rgfxQueueTests(const char *self);
|
||||
int rgfxQueueDiesCase(const char *name);
|
||||
void rgfxResidencyTests(const char *self);
|
||||
int rgfxResidencyDiesCase(const char *name);
|
||||
int rgfxQueueTimings(int rounds);
|
||||
void rgfxCreateTests();
|
||||
void rgfxKnownPipelineTests();
|
||||
@@ -17821,6 +17823,8 @@ int main(int argc, char **argv)
|
||||
return dies_case(argv[2]);
|
||||
if (argc == 3 && std::strcmp(argv[1], "--queue-dies") == 0)
|
||||
return rgfxQueueDiesCase(argv[2]);
|
||||
if (argc == 3 && std::strcmp(argv[1], "--residency-dies") == 0)
|
||||
return rgfxResidencyDiesCase(argv[2]);
|
||||
// A measurement, run by hand: the guest's share of a buffer's scheduling and completion
|
||||
// (tests/queue_tests.mm).
|
||||
if (argc >= 2 && std::strcmp(argv[1], "--queue-timing") == 0)
|
||||
@@ -17994,6 +17998,7 @@ int main(int argc, char **argv)
|
||||
rgfxArchiveTests(); // tests/archive_tests.mm
|
||||
rgfxResourceTests(); // tests/resource_tests.mm
|
||||
rgfxQueueTests(argv[0]); // tests/queue_tests.mm
|
||||
rgfxResidencyTests(argv[0]); // tests/residency_tests.mm
|
||||
rgfxCreateTests(); // tests/create_tests.mm
|
||||
rgfxKnownPipelineTests(); // tests/known_pipeline_tests.mm
|
||||
rgfxIcbTests(); // tests/icb_tests.mm
|
||||
|
||||
Reference in new issue
Block a user