🐛 Let a residency set's dropped allocations go at its commit

The guest run of oracle_residency showed an allocation whose removal a commit
applied still alive after that commit, where the M3's set lets it go at the
commit: the commit listed the dropped allocations with NSSet's allObjects,
an autoreleased array, which held them until the caller's pool drained. For
the guest, an allocation a commit drops is now released at that commit, so
the buffer or texture is freed then, as on Apple silicon; the lists the
other members build are made without an autorelease too.

The test now commits outside a pool of its own, as the probe does, and
covers a view and its texture dropped at one commit. The queue's comment
records the M3's answer the probe's hold case now measures: its queue lets a
set go a moment after the last removal, on a thread of its own, where ours
lets it go at that removal.
This commit is contained in:
Anees Iqbal committed 2026-11-12 10:14:40 -08:00
1 parent fac39ba521
commit fd56430a41
4 files changed
+51 -20

No files matched your search

+5 -2
View File
@@ -1128,8 +1128,11 @@ struct RgfxBarrierSlot {
// --- 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
// (measured: the M3 Max, macOS 27.0.1, tools/oracle/oracle_residency's queue, retain and hold
// cases): the device keeps every allocation resident, so holding them is all a queue does. The
// M3's queue lets a set go a moment after its last removal, on a thread of its own (held right
// after it, gone 100 ms later, with no buffer made); this one lets it go at that removal, as no
// correct use can depend on what a moment decides. 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.
+2 -1
View File
@@ -16,7 +16,8 @@
* - 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.
* - The set retains each allocation it holds, a marked one until the commit drops it, and lets
* the dropped ones go at that commit, not when the caller's autorelease pool drains.
* - 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,
+12 -6
View File
@@ -174,7 +174,7 @@ id memoryOf(id allocation)
if (allocations[i] == nil)
endsTheProcess("removeAllocations:count:");
}
NS_VALID_UNTIL_END_OF_SCOPE NSArray *held = [NSArray arrayWithObjects:allocations count:count];
NS_VALID_UNTIL_END_OF_SCOPE NSArray *held = [[NSArray alloc] initWithObjects:allocations count:count];
os_unfair_lock_lock(&_lock);
for (id allocation in held)
[self rgfxRemove:allocation];
@@ -184,9 +184,10 @@ id memoryOf(id allocation)
- (void)removeAllAllocations
{
os_unfair_lock_lock(&_lock);
NS_VALID_UNTIL_END_OF_SCOPE NSArray *takenBack = [_added.array copy]; // let go after the lock
NS_VALID_UNTIL_END_OF_SCOPE NSOrderedSet *takenBack = [_added copy]; // let go after the lock
[_added removeAllObjects];
[_removed addObjectsFromArray:_committed.array];
for (id allocation in _committed)
[_removed addObject:allocation];
os_unfair_lock_unlock(&_lock);
}
@@ -204,7 +205,7 @@ id memoryOf(id allocation)
- (NSArray<id<MTLAllocation>> *)allAllocations
{
os_unfair_lock_lock(&_lock);
NSMutableArray *all = [NSMutableArray arrayWithCapacity:_committed.count + _added.count];
NSMutableArray *all = [[NSMutableArray alloc] initWithCapacity:_committed.count + _added.count];
for (id allocation in _committed) {
if (![_removed containsObject:allocation])
[all addObject:allocation];
@@ -225,8 +226,13 @@ id memoryOf(id allocation)
- (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
// The allocations the commit drops, let go after the lock, and as it returns: an autoreleased
// list (NSSet's allObjects) would keep them until the caller's pool drains, where the M3 Max's
// set lets them go at its commit (oracle_residency's hold case).
NS_VALID_UNTIL_END_OF_SCOPE NSMutableArray *dropped =
[[NSMutableArray alloc] initWithCapacity:_removed.count];
for (id allocation in _removed)
[dropped addObject:allocation];
for (id allocation in dropped) {
[_committed removeObject:allocation];
id memory = memoryOf(allocation);
+32 -11
View File
@@ -360,8 +360,9 @@ void test_residency_set_sizes()
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).
// The set holds each allocation it holds, a removed one until the commit drops it, and lets it
// go at that commit, before the caller's pool drains, as the M3 Max's set does (oracle_residency's
// hold case, whose commits run in no pool of their own).
void test_residency_set_holds_its_allocations()
{
Fixture fx;
@@ -373,17 +374,13 @@ void test_residency_set_holds_its_allocations()
[s addAllocation:r];
}
CHECK(w != nil && s.allocationCount == 1);
@autoreleasepool {
[s commit];
}
[s commit];
CHECK(w != nil);
@autoreleasepool {
[s removeAllocation:w];
}
CHECK(w != nil && s.allocationCount == 1);
@autoreleasepool {
[s commit];
}
[s commit];
CHECK(w == nil && s.allocationCount == 0);
@autoreleasepool {
id<MTLBuffer> r = [RgfxBuffer bufferWithLength:4096 options:0 device:nil serializer:&fx.ser];
@@ -393,10 +390,34 @@ void test_residency_set_holds_its_allocations()
[s removeAllAllocations];
}
CHECK(w != nil);
@autoreleasepool {
[s commit];
}
[s commit];
CHECK(w == nil && s.allocationCount == 0);
// A view and its texture, both dropped at one commit, both let go at it.
__weak id wt = nil;
@autoreleasepool {
MTLTextureDescriptor *d =
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm
width:64
height:64
mipmapped:NO];
d.storageMode = MTLStorageModePrivate;
id<MTLTexture> t = [RgfxTexture textureWithDescriptor:d
heap:RGFX_OBJ_NONE
heapOffset:0
device:nil
serializer:&fx.ser];
id<MTLTexture> v = [t newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm];
wt = t;
w = v;
[s addAllocation:t];
[s addAllocation:v];
[s commit];
[s removeAllocation:v];
[s removeAllocation:t];
}
CHECK(w != nil && wt != nil);
[s commit];
CHECK(w == nil && wt == 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];