🆕 Log how long block releases and dormant reaps wait

The release worker posts one BLOCK_RELEASE at a time and waits for its answer,
which the device gives once the block's context's earlier command buffers have
finished (aud-r2-fixes-02). What that costs was not visible: the kext now logs,
at most every 10 s and only where more releases were answered, the time from a
block's free to its release's answer (p50, p99, max), the releases kept, the
most in flight at once, how long a dormant block's ask to go waited for the
worker's reap, and how many new surface blocks waited for the region's room
and found none after.

The histogram (RgfxLatency.hpp) keeps eight buckets an octave, so a percentile
reads within an eighth; its native test is latency_test. Diagnostics only:
nothing a process sees changes.
This commit is contained in:
Anees Iqbal committed 2026-11-05 21:59:24 -08:00
1 parent 6fa484cd78
commit 3a80d42f4b
5 files changed
+251 -12

No files matched your search

+1 -1
View File
@@ -96,7 +96,7 @@ TESTS = $(BUILD)/tests/spans_test $(BUILD)/tests/blocks_test $(BUILD)/tests/surf
$(BUILD)/tests/bundle_paths_test $(BUILD)/tests/surface_caches_test \
$(BUILD)/tests/vblank_clock_test $(BUILD)/tests/submit_gate_test $(BUILD)/tests/memory_size_test \
$(BUILD)/tests/queue_limits_test $(BUILD)/tests/user_client_words_test \
$(BUILD)/tests/features_test
$(BUILD)/tests/features_test $(BUILD)/tests/latency_test
$(BUILD)/tests/%: tests/%.cpp $(HEADERS) ../../crates/reims-graphics/include/reims_graphics_wire.h Makefile
@mkdir -p $(@D)
+97 -6
View File
@@ -51,6 +51,7 @@
#include "ReimsGraphics.hpp"
#include "RgfxSurfaceBlocks.hpp"
#include "RgfxSurfaceCaches.hpp"
#include "RgfxLatency.hpp"
#include <IOKit/IOLib.h>
#include <IOKit/IOMemoryDescriptor.h>
@@ -92,6 +93,8 @@ constexpr UInt32 kDormantLogMs = 10000;
constexpr UInt32 kDormantRoomMs = 100;
// How many dormant blocks a new block that finds no room in the region lets go.
constexpr UInt32 kDormantRoomBlocks = 16;
// How often the releases' line is logged at most.
constexpr UInt32 kReleaseLogMs = 10000;
UInt64 alignUp(UInt64 value, UInt64 align)
{
@@ -176,6 +179,32 @@ struct DormantStats {
};
DormantStats dormantStats = {};
// What the releases took, under contextLock, logged once more were answered and kReleaseLogMs
// passed since the last line (noteReleases): from a block's free to its release's answer
// (`latency`), and from a dormant block's ask to go to the worker's reap of it (`reapWait`, while
// the worker is busy with releases); the releases kept, the most in flight at once, and the new
// surface blocks that waited for the region's room and the ones that found none after.
struct ReleaseStats {
RgfxLatency latency;
RgfxLatency reapWait;
UInt64 kept;
UInt32 inFlightMost;
UInt64 roomWaits;
UInt64 roomWaitsFailed;
UInt64 reapAskedAt;
UInt64 loggedCount;
UInt64 loggedAt;
};
ReleaseStats releaseStats = {};
UInt64 nanosSince(UInt64 then, UInt64 now)
{
UInt64 ns = 0;
if (then && now > then)
absolutetime_to_nanoseconds(now - then, &ns);
return ns;
}
UInt64 loadHeader64(const volatile UInt8 *header, UInt32 field)
{
return __atomic_load_n(reinterpret_cast<const volatile UInt64 *>(header + field),
@@ -191,6 +220,7 @@ void queueInterruptHook(OSObject *target)
bool reapDormant();
void noteDormant();
void noteReserve();
void noteReleases();
void unwire(void *pages);
// The release thread call: lets go of the dormant blocks their surfaces asked to go
@@ -201,6 +231,11 @@ void releaseFreedBlocks(thread_call_param_t accelerator, thread_call_param_t)
{
RgfxAccelerator *accel = static_cast<RgfxAccelerator *>(accelerator);
IOLockLock(contextLock);
if (releaseStats.reapAskedAt) {
rgfxLatencyAdd(releaseStats.reapWait,
nanosSince(releaseStats.reapAskedAt, mach_absolute_time()));
releaseStats.reapAskedAt = 0;
}
reapDormant();
IOLockUnlock(contextLock);
noteDormant();
@@ -208,9 +243,13 @@ void releaseFreedBlocks(thread_call_param_t accelerator, thread_call_param_t)
RgfxBlock block;
IOLockLock(contextLock);
bool any = rgfxBlockNextRelease(blockTable, &block);
if (any && !releaseStats.inFlightMost)
releaseStats.inFlightMost = 1;
IOLockUnlock(contextLock);
if (!any)
if (!any) {
noteReleases();
return;
}
RgfxBlockRelease release = {};
release.context = block.context;
release.offset = block.offset;
@@ -242,6 +281,8 @@ void releaseFreedBlocks(thread_call_param_t accelerator, thread_call_param_t)
}
void *wired = nullptr;
rgfxBlockReleased(blockTable, block.offset, block.serial, ok, &wired);
rgfxLatencyAdd(releaseStats.latency, nanosSince(block.freedAt, mach_absolute_time()));
releaseStats.kept += !ok;
// A new block waiting for the region's room looks again.
IOLockWakeup(contextLock, &surfaceTable, false);
IOLockUnlock(contextLock);
@@ -493,7 +534,7 @@ IOReturn RgfxAccelerator::freeBlock(RgfxAcceleratorUserClient *owner, UInt64 off
RgfxBlockResult r = rgfxBlockFreeable(blockTable, owner, offset, &size);
if (r == kRgfxBlockOk) {
owner->unmapSpan(offset, size);
r = rgfxBlockFree(blockTable, owner, offset);
r = rgfxBlockFree(blockTable, owner, offset, mach_absolute_time());
}
IOLockUnlock(contextLock);
if (r == kRgfxBlockBusy)
@@ -659,7 +700,7 @@ bool letGoBlock(RgfxSurfaceEntry &e, UInt32 context, bool keep, void *wired = nu
if (!keep) {
if (!e.dormant)
rgfxBlockSetContext(blockTable, e.offset, context);
release = rgfxBlockFree(blockTable, &e, e.offset) == kRgfxBlockOk;
release = rgfxBlockFree(blockTable, &e, e.offset, mach_absolute_time()) == kRgfxBlockOk;
}
if (!release)
rgfxBlockKeepAt(blockTable, e.offset);
@@ -719,6 +760,47 @@ bool reapDormant()
return release;
}
// Logs what the releases took (ReleaseStats), where more were answered and kReleaseLogMs passed
// since the last line. Not under contextLock.
void noteReleases()
{
if (!contextLock)
return;
const uint64_t now = mach_absolute_time();
uint64_t interval = 0;
nanoseconds_to_absolutetime(static_cast<uint64_t>(kReleaseLogMs) * 1000000ull, &interval);
IOLockLock(contextLock);
const bool log = releaseStats.latency.count != releaseStats.loggedCount &&
now - releaseStats.loggedAt >= interval;
UInt64 count = 0, p50 = 0, p99 = 0, most = 0, reaps = 0, r50 = 0, r99 = 0, rmost = 0;
UInt64 kept = 0, waits = 0, failed = 0;
UInt32 inFlight = 0;
if (log) {
const ReleaseStats &s = releaseStats;
count = s.latency.count;
p50 = rgfxLatencyPercentile(s.latency, 500);
p99 = rgfxLatencyPercentile(s.latency, 990);
most = s.latency.maxNs;
reaps = s.reapWait.count;
r50 = rgfxLatencyPercentile(s.reapWait, 500);
r99 = rgfxLatencyPercentile(s.reapWait, 990);
rmost = s.reapWait.maxNs;
kept = s.kept;
inFlight = s.inFlightMost;
waits = s.roomWaits;
failed = s.roomWaitsFailed;
releaseStats.loggedCount = count;
releaseStats.loggedAt = now;
}
IOLockUnlock(contextLock);
if (log)
IOLog("RgfxAccelerator: releases: %llu answered (%llu kept), %u in flight at most; freed "
"to answered p50 %llu us, p99 %llu us, max %llu us; dormant reaps %llu, waited p50 "
"%llu us, p99 %llu us, max %llu us; room waits %llu (%llu found none)\n",
count, kept, inFlight, p50 / 1000, p99 / 1000, most / 1000, reaps, r50 / 1000,
r99 / 1000, rmost / 1000, waits, failed);
}
// The most WindowServer's blocks came to at the last line of noteReserve. Under contextLock.
UInt32 reserveLoggedCount = 0;
UInt64 reserveLoggedBytes = 0;
@@ -921,6 +1003,7 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
if (made != kRgfxBlockNoSpace || waitedForRoom || !table.dormantCount)
break;
waitedForRoom = true;
releaseStats.roomWaits++;
for (UInt32 n = 0; n < kDormantRoomBlocks; n++) {
const int32_t oldest = rgfxSurfaceOldestDormant(table);
if (oldest < 0)
@@ -934,6 +1017,8 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
clock_interval_to_deadline(kDormantRoomMs, kMillisecondScale, &deadline);
IOLockSleepDeadline(contextLock, &surfaceTable, deadline, THREAD_UNINT);
}
if (waitedForRoom && made == kRgfxBlockNoSpace)
releaseStats.roomWaitsFailed++;
if (release)
thread_call_enter(releaseCall);
if (at >= 0) {
@@ -1086,8 +1171,14 @@ IOReturn RgfxAccelerator::acquireSurfaceBlock(RgfxAcceleratorUserClient *client,
void RgfxAccelerator::wakeReleases()
{
if (__atomic_load_n(&contextLock, __ATOMIC_ACQUIRE))
thread_call_enter(releaseCall);
if (!__atomic_load_n(&contextLock, __ATOMIC_ACQUIRE))
return;
// When the first dormant block asked to go, for the worker's reap of it (ReleaseStats).
IOLockLock(contextLock);
if (!releaseStats.reapAskedAt)
releaseStats.reapAskedAt = mach_absolute_time();
IOLockUnlock(contextLock);
thread_call_enter(releaseCall);
}
void RgfxAccelerator::retryRefusedBlocks()
@@ -1095,7 +1186,7 @@ void RgfxAccelerator::retryRefusedBlocks()
if (!contextLock)
return;
IOLockLock(contextLock);
UInt32 n = rgfxBlockRetryRefused(blockTable);
UInt32 n = rgfxBlockRetryRefused(blockTable, mach_absolute_time());
IOLockUnlock(contextLock);
if (n) {
thread_call_enter(releaseCall);
+12 -5
View File
@@ -54,6 +54,9 @@ struct RgfxBlock {
// Made for a client that may take the reserve (rgfxBlockAlloc's `mayReserve`): counted in
// the table's reservedCount and reservedBytes while it is in the table.
bool reserved;
// When it was freed for its release (the caller's clock, 0 where not given), for the time a
// release takes.
uint64_t freedAt;
};
// A pin of one block (rgfxBlockPin), to give back with rgfxBlockUnpin.
@@ -173,6 +176,7 @@ inline RgfxBlockResult rgfxBlockAlloc(RgfxBlockTable &t, const void *owner, uint
b.refused = false;
b.wired = nullptr;
b.reserved = mayReserve;
b.freedAt = 0;
t.count++;
if (mayReserve) {
t.reservedCount++;
@@ -298,9 +302,10 @@ inline RgfxBlockResult rgfxBlockFreeable(const RgfxBlockTable &t, const void *ow
return kRgfxBlockOk;
}
// `owner` frees its live block at `offset`: it is freed, for its release to be posted. Busy while
// a create naming it is in flight.
inline RgfxBlockResult rgfxBlockFree(RgfxBlockTable &t, const void *owner, uint64_t offset)
// `owner` frees its live block at `offset`, at `now`: it is freed, for its release to be posted.
// Busy while a create naming it is in flight.
inline RgfxBlockResult rgfxBlockFree(RgfxBlockTable &t, const void *owner, uint64_t offset,
uint64_t now = 0)
{
int64_t at = rgfxBlockIndex(t, offset);
if (at < 0 || t.blocks[at].owner != owner || t.blocks[at].state != kRgfxBlockLive)
@@ -308,6 +313,7 @@ inline RgfxBlockResult rgfxBlockFree(RgfxBlockTable &t, const void *owner, uint6
if (t.blocks[at].pins)
return kRgfxBlockBusy;
t.blocks[at].state = kRgfxBlockFreed;
t.blocks[at].freedAt = now;
return kRgfxBlockOk;
}
@@ -349,14 +355,15 @@ inline void rgfxBlockReleased(RgfxBlockTable &t, uint64_t offset, uint64_t seria
// Posts again the release of every block kept after the device refused it (an object of some
// context lay in it): each goes back to freed, for rgfxBlockNextRelease, since a context close
// may have dropped that object. A block kept for good (a close not answered) is not `refused`,
// and is left. How many are posted again.
inline uint32_t rgfxBlockRetryRefused(RgfxBlockTable &t)
// and is left. How many are posted again, freed at `now`.
inline uint32_t rgfxBlockRetryRefused(RgfxBlockTable &t, uint64_t now = 0)
{
uint32_t n = 0;
for (uint32_t i = 0; i < t.count; i++) {
if (t.blocks[i].state == kRgfxBlockKept && t.blocks[i].refused) {
t.blocks[i].state = kRgfxBlockFreed;
t.blocks[i].refused = false;
t.blocks[i].freedAt = now;
n++;
}
}
+72
View File
@@ -0,0 +1,72 @@
// RgfxLatency: a histogram of latencies in nanoseconds, for the kext's own log lines: eight buckets
// an octave (each within an eighth of its lower edge), so a percentile reads within 12.5%.
//
// Header-only and free of the kernel's headers, so its native test (tests/latency_test.cpp)
// builds it as the kext does.
//
// Our own code.
#ifndef RGFX_LATENCY_HPP_
#define RGFX_LATENCY_HPP_
#include <stdint.h>
static constexpr uint32_t kRgfxLatencySubBuckets = 8;
// The 8 single nanoseconds, then octaves 3 to 63.
static constexpr uint32_t kRgfxLatencyBuckets = 62 * kRgfxLatencySubBuckets;
struct RgfxLatency {
uint64_t count;
uint64_t maxNs;
uint32_t buckets[kRgfxLatencyBuckets];
};
// The bucket of `ns`: below 8 ns its own, else its octave's eighth.
inline uint32_t rgfxLatencyBucket(uint64_t ns)
{
if (ns < kRgfxLatencySubBuckets)
return static_cast<uint32_t>(ns);
// Octaves 3 (8 to 15 ns) and up follow the 8 single nanoseconds.
const uint32_t octave = 63 - static_cast<uint32_t>(__builtin_clzll(ns));
const uint32_t sub = static_cast<uint32_t>(ns >> (octave - 3)) & (kRgfxLatencySubBuckets - 1);
return (octave - 2) * kRgfxLatencySubBuckets + sub;
}
// The least latency of bucket `b`.
inline uint64_t rgfxLatencyFloor(uint32_t b)
{
if (b < kRgfxLatencySubBuckets)
return b;
const uint32_t octave = b / kRgfxLatencySubBuckets + 2;
const uint64_t sub = b % kRgfxLatencySubBuckets;
return (kRgfxLatencySubBuckets + sub) << (octave - 3);
}
inline void rgfxLatencyAdd(RgfxLatency &l, uint64_t ns)
{
l.count++;
if (ns > l.maxNs)
l.maxNs = ns;
l.buckets[rgfxLatencyBucket(ns)]++;
}
// The latency under which `permille` thousandths of the samples lie: the next bucket's floor
// (the most measured where that is past it), 0 with no samples.
inline uint64_t rgfxLatencyPercentile(const RgfxLatency &l, uint32_t permille)
{
if (!l.count)
return 0;
uint64_t want = (l.count * permille + 999) / 1000;
if (want == 0)
want = 1;
uint64_t seen = 0;
for (uint32_t b = 0; b < kRgfxLatencyBuckets; b++) {
seen += l.buckets[b];
if (seen >= want) {
const uint64_t top = b + 1 < kRgfxLatencyBuckets ? rgfxLatencyFloor(b + 1) : l.maxNs;
return top < l.maxNs ? top : l.maxNs;
}
}
return l.maxNs;
}
#endif // RGFX_LATENCY_HPP_
+69
View File
@@ -0,0 +1,69 @@
// The native test of RgfxLatency.hpp: a latency lands in a bucket within an eighth of it, and a
// percentile reads within that of the samples'.
#include "RgfxLatency.hpp"
#include <cstdio>
#include <cstdlib>
namespace {
int checks = 0;
void check(bool ok, const char *what)
{
checks++;
if (!ok) {
std::fprintf(stderr, "latency_test: FAILED: %s\n", what);
std::exit(1);
}
}
RgfxLatency fresh()
{
static RgfxLatency l;
l = {};
return l;
}
} // namespace
int main()
{
// Every bucket's floor is in it, and the next bucket starts past it.
bool rising = true;
for (uint32_t b = 0; b + 1 < kRgfxLatencyBuckets; b++)
rising &= rgfxLatencyFloor(b + 1) > rgfxLatencyFloor(b);
check(rising, "the buckets' floors rise");
check(rgfxLatencyBucket(~0ull) == kRgfxLatencyBuckets - 1, "the longest latency is the last's");
const uint64_t samples[] = {0, 1, 7, 8, 9, 15, 16, 1000, 1023, 1024, 123456789, 1ull << 40,
~0ull >> 1};
for (uint64_t ns : samples) {
const uint32_t b = rgfxLatencyBucket(ns);
check(b < kRgfxLatencyBuckets, "a bucket in range");
check(rgfxLatencyFloor(b) <= ns, "a latency is at or past its bucket's floor");
if (b + 1 < kRgfxLatencyBuckets)
check(ns < rgfxLatencyFloor(b + 1), "and below the next bucket's");
if (ns >= 8)
check(ns - rgfxLatencyFloor(b) <= rgfxLatencyFloor(b) / 8,
"within an eighth of its floor");
}
RgfxLatency l = fresh();
check(rgfxLatencyPercentile(l, 500) == 0, "no samples read 0");
// 1000 samples, 1 us to 1000 us.
for (uint64_t us = 1; us <= 1000; us++)
rgfxLatencyAdd(l, us * 1000);
check(l.count == 1000 && l.maxNs == 1000000, "every sample counted, the most kept");
const uint64_t p50 = rgfxLatencyPercentile(l, 500), p99 = rgfxLatencyPercentile(l, 990);
check(p50 >= 500000 && p50 <= 500000 + 500000 / 8 + 1000, "p50 within an eighth of 500 us");
check(p99 >= 990000 && p99 <= 1000000, "p99 within an eighth of 990 us, no more than the most");
check(rgfxLatencyPercentile(l, 1000) == 1000000, "p100 is the most measured");
// One slow sample among fast ones shows at the top, not at p50.
RgfxLatency m = fresh();
for (int i = 0; i < 99; i++)
rgfxLatencyAdd(m, 20000);
rgfxLatencyAdd(m, 4000000);
check(rgfxLatencyPercentile(m, 500) <= 20000 + 20000 / 8, "p50 is the fast ones'");
check(rgfxLatencyPercentile(m, 999) == 4000000, "the slow one is past p99");
std::printf("latency_test: OK (%d checks)\n", checks);
return 0;
}