From 5b783728e53440964e92440ac9df522c9ad3267b Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 27 Oct 2026 16:29:32 +0300 Subject: [PATCH] Count NVN calls per thread and add them to the shared counter 1024 at a time: the main thread and the job workers record command buffers, and a locked add per call kept the counter's cache line moving between their cores Co-Authored-By: Claude Opus 5.5 --- runtime/nvn_api_core.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/runtime/nvn_api_core.c b/runtime/nvn_api_core.c index 1b33e525..f0490004 100644 --- a/runtime/nvn_api_core.c +++ b/runtime/nvn_api_core.c @@ -24,8 +24,15 @@ #define EVENT_MAGIC MAGIC('E', 'V', 'N', 'T') // ---------- shared helpers (nvn_api.h) +// The call count goes to the shared counter 1024 at a time: the main thread and the job workers all record command +// buffers, and one locked add per call kept the counter's cache line moving between their cores (0.8k samples a +// fight). The count lags by at most 1023 calls per thread. void nvn_called(uint8_t *seen, const char *name) { - __atomic_fetch_add(&gfx_counters.nvn_calls, 1, __ATOMIC_RELAXED); + static _Thread_local uint32_t calls; + if (++calls == 1024) { + __atomic_fetch_add(&gfx_counters.nvn_calls, calls, __ATOMIC_RELAXED); + calls = 0; + } if (__atomic_load_n(seen, __ATOMIC_RELAXED) || __atomic_exchange_n(seen, 1, __ATOMIC_RELAXED)) return; uint64_t n = __atomic_add_fetch(&gfx_counters.nvn_distinct, 1, __ATOMIC_RELAXED); NVN_LOG("first call %llu: %s", (unsigned long long)n, name);