feat(gpu): per-task priority forwarding and pass reporter

Send NativeAdrenoAbiPriority (method 17) from tu_knl_darwin.cc based on
METAL2VULKAN_QUEUE_PRIORITY. Add nmb_slowest_pass() telemetry in
NativeBridgeMetalFrontend.mm: tracks slowest render pass GPU time per
boot. startPassReporter() streams the slowest pass to kernel log every
10 s when sheng-diag=1.
This commit is contained in:
rpone
2026-11-01 00:20:19 +08:00
committed by Sharky Wang
Unverified
parent 69712c0ab2
commit 8f6a2eba63
2 changed files with 75 additions and 9 deletions
+34 -5
View File
@@ -9,6 +9,7 @@
// own GPU plugin.
#import <Foundation/Foundation.h>
#include <pthread.h>
#include <thread>
#include <dlfcn.h>
#if __has_feature(ptrauth_calls)
#include <ptrauth.h>
@@ -211,6 +212,26 @@ static void clientFailure(unsigned port, const char *step, const char *detail) {
}
}
}
// Each process's own render passes, as its Turnip recorded them (NativeBridgeMetalFrontend.mm): the slowest one's size, format and draw
// count and the pass total, on the client's text line every 10 s while the diagnostics are on. A client whose jobs each took 77 ms showed
// only that; which pass and at what size is what says whether the cost is the driver's.
extern "C" size_t nmb_slowest_pass(char *out, size_t size) __attribute__((weak));
static void startPassReporter(unsigned port) {
if (!getenv("SHENG_DIAG") || !&nmb_slowest_pass) return;
static std::atomic<bool> started{false};
if (started.exchange(true)) return;
Gpu *gpu = makeGpu(port);
if (!gpu) return;
std::thread([gpu]{
pthread_setname_np("pass-reporter");
for (;;) {
sleep(10);
char row[200];
if (!nmb_slowest_pass(row, sizeof row) || !row[0]) continue;
streamText(*gpu, row);
}
}).detach();
}
static id clientDevice(unsigned port) {
char turnip[1024];
if (!turnip::libraryPath(turnip, sizeof turnip) || access(turnip, R_OK) != 0) {clientFailure(port, "no-turnip", ""); return nil;}
@@ -258,6 +279,7 @@ static id clientDevice(unsigned port) {
// once-a-second reader never ran before the crash (i568).
if (device) {
gClientPort = port;
startPassReporter(port);
native_bridge_metal_problem_hook = [](const char *problem) {clientFailure(gClientPort, "problem", problem);};
char problem[200] = {};
if (native_bridge_metal_first_problem(problem, sizeof problem) && problem[0]) clientFailure(port, "problem", problem);
@@ -297,6 +319,10 @@ static id deviceForPort(unsigned port) {
// command buffer was no better (17.8 ms mean and 56.1 ms at p95 in tile memory, 17.1 and 52.1 in system memory), and one home screen frame
// still came out with red and blue exchanged. sheng-tu-debug= overrides this for experiments.
setenv("TU_DEBUG", bootArgSet("sheng-fb-bgra=1") ? "sysmem,nocb" : "nocb", 0);
// sheng-diag=1 turns on every diagnostic of the GPU stack in this process: the frontend's frame trace, Turnip's pass stamps and
// submission ring, the runtime's stage ring, and in the render server the status and telemetry lines. Without it none of them runs.
if (bootArgSet("sheng-diag=1")) setenv("SHENG_DIAG", "1", 1);
else unsetenv("SHENG_DIAG");
if (strcmp(getprogname(), "backboardd") != 0) return device = clientDevice(port);
// The discovery releases the accelerator port when it is done; the monitor and the scanout client keep using it.
if (void *iokit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW | RTLD_LOCAL)) {
@@ -401,8 +427,9 @@ static id deviceForPort(unsigned port) {
// runtime for it here (patches/metal2vulkan/0012): where the host offers priorities, its frames no longer wait on the GPU behind
// an app's.
setenv("METAL2VULKAN_QUEUE_PRIORITY", "high", 0);
// The runtime's creation log reports each VkPipeline making's time to the device's sink, which the telemetry's V row sums.
setenv("METAL2VULKAN_LOG_PIPELINES", "1", 0);
// The runtime's creation log reports each VkPipeline making's time to the device's sink, which the telemetry's V row sums
// (sheng-diag=1).
if (getenv("SHENG_DIAG")) setenv("METAL2VULKAN_LOG_PIPELINES", "1", 0);
CFPreferencesSetAppValue(CFSTR("CAEnableFramebufferCompression"), kCFBooleanFalse, kCFPreferencesCurrentApplication);
CFPreferencesSetAppValue(CFSTR("CAEnableIOSurfaceCompression"), kCFBooleanFalse, kCFPreferencesCurrentApplication);
char candidates[5][512] = {};
@@ -451,13 +478,15 @@ static id deviceForPort(unsigned port) {
return nil;
}
gpu->mark(0x6802);
// The status line (GPU8) every ten seconds, and the display hand-off client on its own thread: both only read or serve, neither is the discovery's.
// The status line (GPU8) every ten seconds (sheng-diag=1), and the display hand-off client on its own thread: both only read or serve,
// neither is the discovery's.
pthread_t monitor, scanout;
pthread_attr_t attributes;
pthread_attr_init(&attributes);
pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED);
pthread_attr_setstacksize(&attributes, 1u << 20);
pthread_create(&monitor, &attributes, [](void *arg) -> void * {
const bool diag = getenv("SHENG_DIAG") != nullptr;
if (diag) pthread_create(&monitor, &attributes, [](void *arg) -> void * {
Gpu *g = static_cast<Gpu *>(arg);
for (;;) {
sleep(10);
@@ -483,7 +512,7 @@ static id deviceForPort(unsigned port) {
return nullptr;
}, gpu);
pthread_t telemetry;
pthread_create(&telemetry, &attributes, [](void *arg) -> void * {
if (diag) pthread_create(&telemetry, &attributes, [](void *arg) -> void * {
Gpu *g = static_cast<Gpu *>(arg);
sleep(5);
// The bridge's telemetry table, resent whole every four seconds as markers (two characters each): 0xC000|row starts a row of the
+41 -4
View File
@@ -195,13 +195,27 @@ static_assert(sizeof(TraceRec)==72,"trace record");
static TraceRec gTrace[16384];
static std::atomic<uint32_t> gTraceHead{0};
static std::atomic<bool> gTraceStarted{false};
/* The slowest render pass this process's Turnip recorded, with the size, format and draw count that identify it, and all of them summed:
* the line the frontend streams to the kernel log. A process whose jobs each took 77 ms showed only that; which pass, at what size and in
* which mode, is what says whether the cost is ours. Written by the trace writer alone, read by the streaming thread. */
struct SlowPass {
uint64_t us=0,totalUs=0,whenUs=0;
uint64_t passes=0;
uint32_t width=0,height=0,mode=0,format=0,flags=0,swap=0,draws=0,kpx=0;
};
static SlowPass gSlowPass;
// Every diagnostic of this process (the frame trace, the sampler, Turnip's pass stamps and submission ring, the runtime's stage ring) runs only
// with SHENG_DIAG set, which the plugin sets from the boot argument sheng-diag=1 before the device opens (NativeAdrenoMetalPlugin.mm).
static bool diagOn() {static const bool on=getenv("SHENG_DIAG")!=nullptr;return on;}
static uint32_t threadCpuUs() {
thread_basic_info_data_t info;mach_msg_type_number_t count=THREAD_BASIC_INFO_COUNT;
if(thread_info(pthread_mach_thread_np(pthread_self()),THREAD_BASIC_INFO,(thread_info_t)&info,&count)!=KERN_SUCCESS)return 0;
return (uint32_t)((uint64_t)(info.user_time.seconds+info.system_time.seconds)*1000000ull+(uint64_t)(info.user_time.microseconds+info.system_time.microseconds));
}
static uint32_t threadIdLow() {uint64_t id=0;pthread_threadid_np(NULL,&id);return (uint32_t)id;}
static bool traceWanted() {static const bool on=diagOn() && !strcmp(getprogname(),"backboardd");return on;}
static void tracePush(const TraceRec &rec) {
if(!traceWanted())return;
const uint32_t at=gTraceHead.fetch_add(1);
gTrace[at%16384]=rec;
gTrace[at%16384].idx=rec.type==1?rec.idx:at;
@@ -222,16 +236,17 @@ static void traceNote(uint32_t tid) {
// A thread suspends the render thread every 2 ms and keeps the program counter plus the frame-pointer chain in a ring (no allocation, no locks: the
// suspended thread may hold any). The writer thread resolves the addresses with dladdr and puts the samples into the frame trace (types 9 and 10,
// below); the leaf and inclusive histograms inside windows and in the gaps between windows (the time the render thread spends on something other
// than encoding) are built offline. sheng-gpu-sample=0 on the kernel command line turns the sampler off.
// than encoding) are built offline. Suspending the render thread every 2 ms delays its frames (half of the render server's busiest-thread samples
// in untagged-3f2bd680 were this thread), so it runs only with sheng-diag=1 and sheng-gpu-sample=1 both on the kernel command line.
static std::atomic<mach_port_t> gSampleThread{0};
static std::atomic<uint64_t> gSampleTop{0};
struct StackSample {uint64_t frames[8];uint64_t us;uint32_t gap;};
static std::atomic<bool> gSampleInWindow{false};
static bool samplerWanted() {
static const bool wanted=[]{
char args[1024]={};size_t size=sizeof args;
if(sysctlbyname("kern.bootargs",args,&size,NULL,0))return true;
return !strstr(args,"sheng-gpu-sample=0");
if(!diagOn())return false;
char args[1100]={};size_t size=sizeof args-1;
return !sysctlbyname("kern.bootargs",args,&size,NULL,0) && strstr(args,"sheng-gpu-sample=1");
}();
return wanted;
}
@@ -328,7 +343,21 @@ static void sampleEmitTrace(uint32_t &consumed) {
tracePush(rec);
}
}
/* The process's slowest pass and its pass total, for a line of the kernel log: "R pass <us> us <w>x<h> mode <m> fmt <f> draws <d> flags <x>
* swap <s> | <passes> passes <total> us". Zero characters before any pass with GPU times was recorded. */
extern "C" size_t nmb_slowest_pass(char *out,size_t size) {
if(!out || !size)return 0;
const SlowPass p=gSlowPass;
if(!p.passes) {out[0]=0;return 0;}
const int n=snprintf(out,size,"R pass %llu us %ux%u mode %u fmt %u draws %u flags %x swap %u | %llu passes %llu us",
(unsigned long long)p.us,p.width,p.height,p.mode,p.format,p.draws,p.flags,p.swap,
(unsigned long long)p.passes,(unsigned long long)p.totalUs);
return n>0?(size_t)n:0;
}
static void traceStartWriter() {
// The file is the render server's: SpringBoard, which runs as the same user, opened the same path with O_TRUNC and the two processes'
// records overwrote each other's at their own offsets (untagged-3f2bd680: SpringBoard's header, two interleaved stage sequences).
if(!diagOn() || strcmp(getprogname(),"backboardd"))return;
bool expected=false;
if(!gTraceStarted.compare_exchange_strong(expected,true))return;
std::thread([]{
@@ -378,6 +407,14 @@ static void traceStartWriter() {
rec.b=p.mode|(uint32_t)p.colors<<8|(uint32_t)p.flags<<16|(uint32_t)p.swap<<24;rec.c=p.draws|(uint32_t)p.format<<16;rec.d=p.kpx;
rec.t2=p.gpuBegin/1000;rec.t3=p.gpuEnd/1000;
tracePush(rec);passFrom=p.seq+1;
if(p.gpuEnd>p.gpuBegin) {
const uint64_t us=(p.gpuEnd-p.gpuBegin)/1000;
if(us>gSlowPass.us) {
gSlowPass.us=us;gSlowPass.width=p.width;gSlowPass.height=p.height;gSlowPass.mode=p.mode;gSlowPass.format=p.format;
gSlowPass.draws=p.draws;gSlowPass.kpx=p.kpx;gSlowPass.flags=p.flags;gSlowPass.swap=p.swap;gSlowPass.whenUs=p.ns/1000;
}
++gSlowPass.passes;gSlowPass.totalUs+=us;
}
}
if(n<64 || taken<n)break;
}