diff --git a/runtime/kernel.c b/runtime/kernel.c index 4ff43ba5..d3032da3 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -126,14 +126,17 @@ uint64_t kern_now_ns(void) { return plat_now_ns(); } // ---- TOTK_CRIT=- with TOTK_TRACE=sched: every state change and wake of the guest threads and // every present over those frames, written to TOTK_CRIT_OUT (default crit.bin) at the last one, for -// tools/crit_path.py, which walks the chain of waits back from each present (holding G). +// tools/crit_path.py, which walks the chain of waits back from each present (holding G). TOTK_CRIT=w- takes +// world-clock seconds instead (frame_step_world_us): from the first present at or after lo to the first after hi, so a +// session covers the same game time at any frame rate. typedef struct { uint64_t ts, obj; uint32_t a, b; uint16_t kind, st; uint32_t pad; } CritEv; enum { CRIT_STATE, CRIT_WAKE, CRIT_FRAME }; // a: thread; b: its waker (STATE), the waker (WAKE); st: new S_* state #define CRIT_N (1u << 22) // 128 MiB, about 2000 fight frames static CritEv *g_crit; static uint32_t g_crit_n; static int g_crit_on; -static uint64_t g_crit_lo, g_crit_hi; +static uint64_t g_crit_lo, g_crit_hi; // frames; UINT64_MAX in world mode until the world clock fixes them +static int64_t g_crit_wlo, g_crit_whi; // world mode: the window in microseconds of world clock, else 0 static void crit_add(int kind, uint32_t a, uint32_t b, int st, uint64_t obj, uint64_t ts) { if (g_crit_n < CRIT_N) g_crit[g_crit_n++] = (CritEv){ts, obj, a, b, (uint16_t)kind, (uint16_t)st, 0}; @@ -1084,12 +1087,26 @@ static void crit_frame(RtThread *self, uint64_t frame) { parsed = 1; const char *e = cfg_get("TOTK_CRIT"); unsigned long long lo, hi; - if (e && sscanf(e, "%llu-%llu", &lo, &hi) == 2 && lo < hi && (g_crit = malloc(CRIT_N * sizeof *g_crit))) { + double wl, wh; + if (e && e[0] == 'w' && sscanf(e + 1, "%lf-%lf", &wl, &wh) == 2 && wl >= 0 && wl < wh && + (g_crit = malloc(CRIT_N * sizeof *g_crit))) { + g_crit_wlo = (int64_t)(wl * 1e6), g_crit_whi = (int64_t)(wh * 1e6); + g_crit_lo = g_crit_hi = UINT64_MAX; + rt_log("crit: recording world seconds %g-%g", wl, wh); + } else if (e && sscanf(e, "%llu-%llu", &lo, &hi) == 2 && lo < hi && (g_crit = malloc(CRIT_N * sizeof *g_crit))) { g_crit_lo = lo; g_crit_hi = hi; rt_log("crit: recording frames %llu-%llu", lo, hi); } } + if (g_crit && g_crit_whi && g_crit_hi == UINT64_MAX) { // world mode: fix the frames as the clock passes lo and hi + int64_t w = frame_step_world_us(); + if (g_crit_lo == UINT64_MAX) { + if (w < g_crit_wlo) return; + g_crit_lo = frame; + } + if (w > g_crit_whi) g_crit_hi = frame; + } if (!g_crit || frame < g_crit_lo || frame > g_crit_hi) return; g_crit_on = 1; crit_add(CRIT_FRAME, self ? self->handle : 0, 0, 0, frame, kern_now_ns()); diff --git a/runtime/prof_win.c b/runtime/prof_win.c index c1f1ed21..969be816 100644 --- a/runtime/prof_win.c +++ b/runtime/prof_win.c @@ -61,6 +61,7 @@ static int only_main; // TOTK_PROFILE_MAIN=1: sample the guest mai static int chains; // call chains are kept: TOTK_PROFILE_RENDER or TOTK_PROFILE_CHAINS static volatile uint64_t cur_frame; // last presented frame static uint64_t frame_lo, frame_hi; // TOTK_PROFILE_FRAMES=-: sample only while presents lo..hi-1 are pending +static int64_t world_lo, world_hi; // TOTK_PROFILE_FRAMES=w-: only while the world clock is in [lo, hi) s (us) #define SLOW_RING (1u << 15) // TOTK_PROFILE_SLOW: about 0.5 s of samples of every thread #define SLOW_DUMPS 64 @@ -223,6 +224,10 @@ static DWORD WINAPI sampler_main(void *arg) { if (tick % 64 == 0) refresh_threads(self); if (dump_hi) slow_dump(); if (frame_hi && (cur_frame < frame_lo || cur_frame >= frame_hi)) continue; + if (world_hi) { // -1 before the world clock starts + int64_t w = frame_step_world_us(); + if (w < world_lo || w >= world_hi) continue; + } uint64_t now = slow_ns ? kern_now_ns() : 0; for (int i = 0; i < n_threads; i++) { ThreadSlot *t = &threads[i]; @@ -357,7 +362,10 @@ void prof_start(void) { slow_ns = sl && atoi(sl) > 0 ? (uint64_t)atoi(sl) * 1000000ull : 0; chains = only_render || only_main || slow_ns || (ch && *ch && strcmp(ch, "0")); const char *fr = cfg_get("TOTK_PROFILE_FRAMES"); - if (fr && sscanf(fr, "%llu-%llu", (unsigned long long *)&frame_lo, (unsigned long long *)&frame_hi) != 2) frame_hi = 0; + double wl, wh; + if (fr && fr[0] == 'w' && sscanf(fr + 1, "%lf-%lf", &wl, &wh) == 2 && wl >= 0 && wl < wh) + world_lo = (int64_t)(wl * 1e6), world_hi = (int64_t)(wh * 1e6); + else if (fr && sscanf(fr, "%llu-%llu", (unsigned long long *)&frame_lo, (unsigned long long *)&frame_hi) != 2) frame_hi = 0; MODULEINFO mi; if (GetModuleInformation(GetCurrentProcess(), GetModuleHandleW(NULL), &mi, sizeof mi)) { exe_lo = (uintptr_t)mi.lpBaseOfDll;