From 2d5879f9e2864e554ff84680ed453b5825a311df Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sat, 14 Nov 2026 19:05:18 +0300 Subject: [PATCH] Host-function calls are counted only by the probes and under TOTK_TRACE=host hostfn_entry bumped a per-thread call counter on every host-function call (a TLS load, two branches, a load and a store) for readers that only the probes and the boot checkpoint line have. The count now sits beside the first-call trace in a cold hostfn_watch, behind the same single test of rt_trace: RT_TRACE_HOSTCOUNT, switched by hostfn_count, which --probe-kernel (test_lifted_ticks reads the count) and --probe-ipc turn on. The boot checkpoint line leaves the host-call count out when nothing counted. Co-Authored-By: Claude Opus 5.5 --- runtime/boot.c | 7 +++++-- runtime/host.h | 7 +++++-- runtime/hostfn.c | 29 ++++++++++++++++++++--------- runtime/ipc.c | 1 + runtime/kernel_probe.c | 1 + 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/runtime/boot.c b/runtime/boot.c index e099fe29..bd099b8c 100644 --- a/runtime/boot.c +++ b/runtime/boot.c @@ -101,8 +101,11 @@ static void cb_module_finis(Cpu *c) { // ---- checkpoints (host.h) static void stats_line(void) { RtStats s = rt_stats(); - rt_logf("boot: passed (%llu SVCs, %llu threads, %llu host calls)\n", (unsigned long long)s.svcs, - (unsigned long long)s.threads, (unsigned long long)s.host_calls); + if (rt_trace & (RT_TRACE_HOSTCALL | RT_TRACE_HOSTCOUNT)) // counted only then (hostfn.c hostfn_entry) + rt_logf("boot: passed (%llu SVCs, %llu threads, %llu host calls)\n", (unsigned long long)s.svcs, + (unsigned long long)s.threads, (unsigned long long)s.host_calls); + else + rt_logf("boot: passed (%llu SVCs, %llu threads)\n", (unsigned long long)s.svcs, (unsigned long long)s.threads); } void rt_boot_event(const char *what, Cpu *c, const char *detail) { diff --git a/runtime/host.h b/runtime/host.h index 98442f79..a5a25ed4 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -24,7 +24,8 @@ void rt_vlogf(const char *fmt, va_list ap); void rt_log_raw(const char *p, size_t n); const char *rt_log_path(void); // the log file, NULL when this run writes none enum { RT_TRACE_SVC = 1, RT_TRACE_HOSTCALL = 2, RT_TRACE_IPC = 4, RT_TRACE_FS = 8, RT_TRACE_GFX = 16, RT_TRACE_PERF = 32, - RT_TRACE_SCHED = 64, RT_TRACE_AUDIO = 128, RT_TRACE_PRESENT = 256 }; + RT_TRACE_SCHED = 64, RT_TRACE_AUDIO = 128, RT_TRACE_PRESENT = 256, + RT_TRACE_HOSTCOUNT = 512 }; // no TOTK_TRACE name of its own: hostfn_count switches it (host.h) extern unsigned rt_trace; // from TOTK_TRACE=svc,host,ipc,fs,gfx,perf,sched,audio,present (or all) // prof_win.c: TOTK_PROFILE= sampling profiler; TOTK_TRACE=perf frame timing (waits of the render thread) // PERF_TEX: texture uploads on the render thread (deswizzle, decode, staging), PERF_ASTC included @@ -120,7 +121,9 @@ void hostfn_add_override(const char *name, GuestFn fn); // an override outsid const char *hostfn_name(uint64_t addr); // name registered at a synthetic address, NULL if none void hostfn_seal(void); // build the dispatch table (rt_dispatch_init over every // linked module); idempotent; registration ends here -uint64_t hostfn_calls(void); // host-function calls so far, all threads +uint64_t hostfn_calls(void); // host-function calls counted so far, all threads: only + // while hostfn_count is on or under TOTK_TRACE=host +void hostfn_count(int on); // count host-function calls (probes); off by default void hostfn_report(void); // call count per host function to stderr, busiest first int hostfn_table_size(int observers); // linked HOST_OVERRIDE (0) / HOST_OBSERVER (1) entries const char *hostfn_table_name(int observers, int i); // symbol of linked entry i, NULL past the end diff --git a/runtime/hostfn.c b/runtime/hostfn.c index 02bfbecd..e401fd94 100644 --- a/runtime/hostfn.c +++ b/runtime/hostfn.c @@ -2,9 +2,9 @@ // Host functions at synthetic guest addresses: the HOST_OVERRIDE / HOST_OBSERVER // linker tables, the registry behind them, and the calls between host and guest (rt_call, rt_original). // -// Every registered function dispatches through hostfn_entry, which counts the call, traces the first one -// (TOTK_TRACE=host) and tail-calls the real function. The registry is fixed by hostfn_seal, which builds the dispatch -// table; guest threads start after that, so lookups need no lock. +// Every registered function dispatches through hostfn_entry, which counts the call (only while hostfn_count is on or +// under TOTK_TRACE=host), traces the first one (TOTK_TRACE=host) and tail-calls the real function. The registry is +// fixed by hostfn_seal, which builds the dispatch table; guest threads start after that, so lookups need no lock. #include #include #include @@ -46,7 +46,8 @@ static int sealed; // The call count (hostfn_calls): one counter per calling thread, each on its own cache line, written only by that // thread. A locked add on a per-function counter beside the read-mostly slot fields made the game threads share and -// bounce those lines on every host call (1.4.3 town: about 1% of the game threads' time in hostfn_entry). +// bounce those lines on every host call (1.4.3 town: about 1% of the game threads' time in hostfn_entry). Only the +// probes and TOTK_TRACE=host read it, so a game run does not count: hostfn_entry tests one rt_trace word for both. #define MAX_COUNTERS 4096 typedef struct { _Alignas(64) uint64_t n; } Counter; static Counter counters[MAX_COUNTERS]; @@ -157,18 +158,28 @@ __attribute__((noinline)) static void hostfn_blocking(Cpu *c, GuestFn fn) { kern_core_back(&away); } -static void hostfn_entry(Cpu *c) { - uint64_t i = (c->pc - RT_HOSTFN_BASE) >> 4; - if (__builtin_expect((c->pc & 15) || i >= (uint64_t)nslots, 0)) bad_entry(c); - Slot *s = &slots[i]; +void hostfn_count(int on) { + if (on) __atomic_fetch_or(&rt_trace, RT_TRACE_HOSTCOUNT, __ATOMIC_RELAXED); + else __atomic_fetch_and(&rt_trace, ~(unsigned)RT_TRACE_HOSTCOUNT, __ATOMIC_RELAXED); +} + +// The counted path of hostfn_entry: the call count, and under TOTK_TRACE=host the first call of each function. +__attribute__((noinline, cold)) static void hostfn_watch(Cpu *c, Slot *s) { uint64_t *k = thread_calls; if (__builtin_expect(!k, 0)) k = counter_new(); if (__builtin_expect(k == &shared_calls, 0)) __atomic_fetch_add(k, 1, __ATOMIC_RELAXED); else __atomic_store_n(k, *k + 1, __ATOMIC_RELAXED); - if (__builtin_expect(rt_trace & RT_TRACE_HOSTCALL, 0) && !__atomic_fetch_add(&s->calls, 1, __ATOMIC_RELAXED)) { + if ((rt_trace & RT_TRACE_HOSTCALL) && !__atomic_fetch_add(&s->calls, 1, __ATOMIC_RELAXED)) { char lr[96]; rt_log("host call %s from %s", s->name, ldr_describe(c->x[30] - 4, lr, sizeof lr)); } +} + +static void hostfn_entry(Cpu *c) { + uint64_t i = (c->pc - RT_HOSTFN_BASE) >> 4; + if (__builtin_expect((c->pc & 15) || i >= (uint64_t)nslots, 0)) bad_entry(c); + Slot *s = &slots[i]; + if (__builtin_expect(rt_trace & (RT_TRACE_HOSTCALL | RT_TRACE_HOSTCOUNT), 0)) hostfn_watch(c, s); if (s->blocking) { hostfn_blocking(c, s->fn); return; diff --git a/runtime/ipc.c b/runtime/ipc.c index d47a3620..079d41e3 100644 --- a/runtime/ipc.c +++ b/runtime/ipc.c @@ -783,6 +783,7 @@ int probe_ipc(void) { return 0; } ldr_load(rt_config()->exefs); + hostfn_count(1); // the stats line reads the host-call count kern_run_on_guest_main(guest_main); probe_unknown_service(); RtStats s = rt_stats(); diff --git a/runtime/kernel_probe.c b/runtime/kernel_probe.c index 07fdf7bc..e90515e2 100644 --- a/runtime/kernel_probe.c +++ b/runtime/kernel_probe.c @@ -855,6 +855,7 @@ int probe_kernel(void) { test_platform(); test_oom_commit(); test_crash_report(); + hostfn_count(1); // test_lifted_ticks and the stats line read the host-call count kern_run_on_guest_main(probe_main); RtStats s = rt_stats(); printf("stats: %llu SVCs, %llu threads, %llu host calls\n", (unsigned long long)s.svcs,