- watchdog: read the running function's position through its call record's pc pointer (+0x10, CI_HASFRAME) instead of the stale savedpc copy, which gave wrong lines or ":-1". - alloctrack: hook adjust_varargs (0x6F6200) while tracking is on, so the `arg` table of a vararg function is charged to that function rather than to its caller (wrappers no longer pay for their callees). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
281 lines
11 KiB
C++
281 lines
11 KiB
C++
// Test for the per-frame counters and the freeze watchdog
|
|
// (src/modules/framestats.cpp, watchdog.cpp), outside the game.
|
|
// Built as 32-bit with stubbed core services: a fake "main thread" renders
|
|
// short frames, then one long frame spinning in a known function; the
|
|
// watchdog must report it to <exe dir>\Logs\ducielperf_freezes.log with that
|
|
// function on top and the frame's counters.
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <windows.h>
|
|
|
|
#include "../src/core/core.h"
|
|
#include "../src/modules/framestats.h"
|
|
|
|
// --- Stubs for the core ---------------------------------------------------------------
|
|
void log_write(const char *fmt, ...) {
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
printf(" [log] ");
|
|
vprintf(fmt, ap);
|
|
printf("\n");
|
|
va_end(ap);
|
|
}
|
|
bool hook_create(uintptr_t, void *, void **, const char *) { return true; }
|
|
int config_int(const char *, int def) { return def; }
|
|
bool hook_set_enabled(uintptr_t, void *, void **, bool, const char *) { return true; }
|
|
|
|
static int g_failures;
|
|
static void check(const char *what, bool ok) {
|
|
printf(" %-60s %s\n", what, ok ? "ok" : "FAIL");
|
|
if (!ok) g_failures++;
|
|
}
|
|
|
|
static volatile uint32_t g_sink;
|
|
|
|
__attribute__((noinline)) static void spinFor(double ms_) {
|
|
LARGE_INTEGER f, t0, t;
|
|
QueryPerformanceFrequency(&f);
|
|
QueryPerformanceCounter(&t0);
|
|
do {
|
|
for (int i = 0; i < 1000; i++) g_sink += i;
|
|
QueryPerformanceCounter(&t);
|
|
} while ((t.QuadPart - t0.QuadPart) * 1000.0 / f.QuadPart < ms_);
|
|
}
|
|
|
|
__attribute__((noinline)) static void fakeFileRead(double ms_) {
|
|
int64_t t = fstats::begin();
|
|
spinFor(ms_);
|
|
fstats::end(fstats::FILE_READ, t, 65536);
|
|
}
|
|
|
|
// Fake Lua state for the collector: L+0x10 -> global_State (+0x24 threshold,
|
|
// +0x28 totalbytes), L+0x60 = GC enabled flag.
|
|
alignas(16) static uint8_t gcmem[256];
|
|
static uint32_t gcL() { return (uint32_t)(uintptr_t)gcmem; }
|
|
static void __fastcall fakeCollect(uint32_t L) {
|
|
spinFor(700);
|
|
*(uint32_t *)(gcmem + 0x80 + 0x28) = 3 << 20; // 5 MB -> 3 MB
|
|
}
|
|
|
|
// Fast fake collection for the GC deferral checks: counts calls, frees half.
|
|
static int g_fast_collects;
|
|
static void __fastcall fakeCollectFast(uint32_t L) {
|
|
g_fast_collects++;
|
|
uint32_t *mem = (uint32_t *)(gcmem + 0x80 + 0x28);
|
|
*mem /= 2;
|
|
*(uint32_t *)(gcmem + 0x80 + 0x24) = *mem * 2; // like Lua: threshold = 2 x live
|
|
}
|
|
namespace fstats { extern volatile bool g_test_in_combat; }
|
|
using fstats::g_test_in_combat;
|
|
static uint32_t &gcThreshold() { return *(uint32_t *)(gcmem + 0x80 + 0x24); }
|
|
static uint32_t &gcMemory() { return *(uint32_t *)(gcmem + 0x80 + 0x28); }
|
|
|
|
static void frame(double work_ms) {
|
|
fakeFileRead(work_ms);
|
|
fstats::onFrameEnd();
|
|
}
|
|
|
|
// Wait in short frames, so the wait itself is not a long frame.
|
|
static void idle(int ms_) {
|
|
for (int i = 0; i < ms_ / 10; i++) {
|
|
Sleep(10);
|
|
fstats::onFrameEnd();
|
|
}
|
|
}
|
|
|
|
// --- Fake client Lua state (built with DP_TEST: the watchdog reads lua_State* from here) --
|
|
uint32_t g_test_lua_state_ptr;
|
|
// Layout as in alloctrack_core.h; a Lua function (pfQuest, line 42) that called
|
|
// a C function (spinFor).
|
|
alignas(16) static uint8_t mem[8192];
|
|
static uint32_t at(int off) { return (uint32_t)(uintptr_t)(mem + off); }
|
|
static void put32(uint32_t a, uint32_t v) { *(uint32_t *)(uintptr_t)a = v; }
|
|
|
|
static bool fakeLuaState(void *cfunc) {
|
|
const char *src = "@Interface\\AddOns\\pfQuest\\database.lua";
|
|
uint32_t ts = at(0);
|
|
put32(ts + 0x0C, (uint32_t)strlen(src));
|
|
memcpy(mem + 0x10, src, strlen(src) + 1);
|
|
uint32_t code = at(256), lineinfo = at(512);
|
|
for (int i = 0; i < 8; i++) put32(lineinfo + i * 4, 40 + i);
|
|
uint32_t proto = at(1024);
|
|
put32(proto + 0x0C, code);
|
|
put32(proto + 0x14, lineinfo);
|
|
put32(proto + 0x20, ts);
|
|
put32(proto + 0x3C, 40);
|
|
uint32_t lcl = at(1200), ccl = at(1300);
|
|
mem[1200 + 6] = 0;
|
|
put32(lcl + 0x0C, proto);
|
|
mem[1300 + 6] = 1;
|
|
put32(ccl + 0x0C, (uint32_t)(uintptr_t)cfunc);
|
|
uint32_t stack = at(4096);
|
|
put32(stack + 0 * 16, 6);
|
|
put32(stack + 0 * 16 + 8, lcl);
|
|
put32(stack + 4 * 16, 6);
|
|
put32(stack + 4 * 16 + 8, ccl);
|
|
uint32_t ci = at(2048);
|
|
put32(ci + 1 * 0x18, stack + 1 * 16);
|
|
// Running function (CI_HASFRAME): savedpc is stale (first instruction),
|
|
// the VM's pc variable (pointed to by +0x10) is at instruction 2 -> line 42.
|
|
uint32_t pcvar = at(3500);
|
|
put32(pcvar, code + 3 * 4);
|
|
put32(ci + 1 * 0x18 + 0x08, 2);
|
|
put32(ci + 1 * 0x18 + 0x0C, code);
|
|
put32(ci + 1 * 0x18 + 0x10, pcvar);
|
|
put32(ci + 2 * 0x18, stack + 5 * 16);
|
|
uint32_t L = at(3000);
|
|
put32(L + 0x14, ci + 2 * 0x18);
|
|
put32(L + 0x28, ci);
|
|
put32(L + 0x24, ci + 3 * 0x18);
|
|
put32(L + 0x1C, stack);
|
|
put32(L + 0x18, stack + 60 * 16);
|
|
g_test_lua_state_ptr = L;
|
|
return true;
|
|
}
|
|
|
|
// Worker threads for the "other threads" part of the report.
|
|
static volatile int g_worker_run;
|
|
static DWORD WINAPI busyWorker(void *) {
|
|
while (g_worker_run) spinFor(5);
|
|
return 0;
|
|
}
|
|
static DWORD WINAPI idleWorker(void *ev) {
|
|
WaitForSingleObject((HANDLE)ev, INFINITE);
|
|
return 0;
|
|
}
|
|
|
|
int main() {
|
|
char log_path[MAX_PATH];
|
|
GetModuleFileNameA(nullptr, log_path, MAX_PATH);
|
|
strcpy(strrchr(log_path, '\\') + 1, "Logs");
|
|
CreateDirectoryA(log_path, nullptr);
|
|
strcat(log_path, "\\ducielperf_freezes.log");
|
|
DeleteFileA(log_path);
|
|
|
|
printf("Setup:\n");
|
|
check("fake Lua state", fakeLuaState((void *)&spinFor));
|
|
fstats::install();
|
|
fstats::applyConfig(true, true, 300);
|
|
fstats::onFrameEnd(); // first call: captures the main thread
|
|
|
|
printf("Normal frames, then a 900 ms frame:\n");
|
|
for (int i = 0; i < 20; i++) frame(5);
|
|
frame(900);
|
|
for (int i = 0; i < 20; i++) frame(5);
|
|
idle(300); // let the watchdog write its report
|
|
check("one report written", fstats::wd::reportCount() == 1);
|
|
|
|
printf("A 700 ms garbage collection, then a call with GC disabled:\n");
|
|
*(uint32_t *)(gcmem + 0x10) = gcL() + 0x80;
|
|
*(uint32_t *)(gcmem + 0x80 + 0x28) = 5 << 20;
|
|
*(uint32_t *)(gcmem + 0x60) = 1;
|
|
fstats::gc::collect(gcL(), &fakeCollect);
|
|
fstats::onFrameEnd();
|
|
idle(300);
|
|
check("GC frame reported", fstats::wd::reportCount() == 2);
|
|
*(uint32_t *)(gcmem + 0x60) = 0;
|
|
*(uint32_t *)(gcmem + 0x80 + 0x28) = 5 << 20;
|
|
fstats::gc::collect(gcL(), &fakeCollect); // must run the original (spins) but not count
|
|
fstats::onFrameEnd();
|
|
idle(300);
|
|
|
|
printf("A 200 ms frame (under the threshold):\n");
|
|
frame(200);
|
|
for (int i = 0; i < 5; i++) frame(5);
|
|
idle(300);
|
|
check("no new report", fstats::wd::reportCount() == 3);
|
|
|
|
printf("Main thread sleeping 800 ms while a worker thread works:\n");
|
|
HANDLE idle_ev = CreateEventA(nullptr, TRUE, FALSE, nullptr);
|
|
g_worker_run = 1;
|
|
HANDLE busy_t = CreateThread(nullptr, 0, &busyWorker, nullptr, 0, nullptr);
|
|
HANDLE idle_t = CreateThread(nullptr, 0, &idleWorker, idle_ev, 0, nullptr);
|
|
Sleep(50);
|
|
for (int i = 0; i < 5; i++) frame(5);
|
|
Sleep(800);
|
|
fstats::onFrameEnd();
|
|
idle(300);
|
|
g_worker_run = 0;
|
|
SetEvent(idle_ev);
|
|
WaitForSingleObject(busy_t, 2000);
|
|
WaitForSingleObject(idle_t, 2000);
|
|
check("sleeping frame reported", fstats::wd::reportCount() == 4);
|
|
|
|
char buf[65536] = {0};
|
|
HANDLE f = CreateFileA(log_path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0,
|
|
nullptr);
|
|
DWORD got = 0;
|
|
if (f != INVALID_HANDLE_VALUE) {
|
|
ReadFile(f, buf, sizeof buf - 1, &got, nullptr);
|
|
CloseHandle(f);
|
|
}
|
|
printf("Report:\n%s", buf);
|
|
check("report has the frame duration", strstr(buf, "frame 9") != nullptr);
|
|
check("report has the frame counters", strstr(buf, "fileRead 9") != nullptr);
|
|
check("GC counter: time and 2 MB freed", strstr(buf, "gc 70") && strstr(buf, "ms/1/2048 KB"));
|
|
check("disabled GC not counted (third report has no gc)",
|
|
strstr(buf, "gc 70") && !strstr(strstr(buf, "gc 70") + 5, " gc "));
|
|
check("EIP inside test_watchdog.exe", strstr(buf, "main thread at (EIP):\n") &&
|
|
strstr(strstr(buf, "main thread at (EIP):\n"), "test_watchdog.exe+"));
|
|
check("Lua source, current line and function line",
|
|
strstr(buf, "@Interface\\AddOns\\pfQuest\\database.lua:42 (function at line 40)") != nullptr);
|
|
check("C function called from Lua", strstr(buf, "in C function test_watchdog.exe+") != nullptr);
|
|
const char *last = buf, *p;
|
|
while ((p = strstr(last + 1, "=== ")) != nullptr) last = p;
|
|
check("sleeping frame: category 'waiting / sleeping' first",
|
|
strstr(last, "main thread time by category: waiting / sleeping") != nullptr);
|
|
check("sleeping frame: system function named from exports",
|
|
strstr(last, "DelayExecution+0x") != nullptr || strstr(last, "SleepEx+0x") != nullptr);
|
|
check("worker threads sampled: busy worker listed",
|
|
strstr(last, "other threads") && strstr(last, "(started at test_watchdog.exe+"));
|
|
check("idle worker counted as idle", strstr(last, " idle the whole time") && !strstr(last, " 0 idle the whole"));
|
|
|
|
printf("GC deferral in combat:\n");
|
|
{
|
|
*(uint32_t *)(gcmem + 0x10) = gcL() + 0x80;
|
|
*(uint32_t *)(gcmem + 0x60) = 1;
|
|
gcMemory() = 40u << 20;
|
|
g_fast_collects = 0;
|
|
fstats::gc::collect(gcL(), &fakeCollectFast);
|
|
check("out of combat: collects normally", g_fast_collects == 1);
|
|
g_test_in_combat = true;
|
|
for (int i = 0; i < 16; i++) fstats::onFrameEnd(); // combat is read every 8th frame
|
|
gcMemory() = 50u << 20;
|
|
fstats::gc::collect(gcL(), &fakeCollectFast);
|
|
check("in combat: collection skipped", g_fast_collects == 1);
|
|
check("in combat: threshold raised by 2 MB", gcThreshold() == (52u << 20));
|
|
gcMemory() = (50u + 100u) << 20;
|
|
fstats::gc::collect(gcL(), &fakeCollectFast);
|
|
check("in combat, +100 MB (under the 128 MB cap): still skipped", g_fast_collects == 1);
|
|
gcMemory() = (50u + 130u) << 20;
|
|
fstats::gc::collect(gcL(), &fakeCollectFast);
|
|
check("in combat, +130 MB (over the cap): collects", g_fast_collects == 2);
|
|
gcMemory() += 10u << 20;
|
|
fstats::gc::collect(gcL(), &fakeCollectFast);
|
|
check("after a cap hit: deferring again from the new baseline", g_fast_collects == 2);
|
|
g_test_in_combat = false;
|
|
for (int i = 0; i < 2; i++) fstats::onFrameEnd(); // checked every frame while deferring
|
|
check("combat over: threshold set to 0 (next check collects)", gcThreshold() == 0);
|
|
fstats::gc::collect(gcL(), &fakeCollectFast);
|
|
check("combat over: collects", g_fast_collects == 3);
|
|
}
|
|
|
|
printf("Cost:\n");
|
|
const int N = 1000000;
|
|
LARGE_INTEGER fq, t0, t1;
|
|
QueryPerformanceFrequency(&fq);
|
|
QueryPerformanceCounter(&t0);
|
|
for (int i = 0; i < N; i++) fstats::end(fstats::PACKET, fstats::begin());
|
|
QueryPerformanceCounter(&t1);
|
|
printf(" counted call (begin+end), counting on: %.1f ns\n", (t1.QuadPart - t0.QuadPart) * 1e9 / fq.QuadPart / N);
|
|
fstats::wd::setEnabled(false, 300);
|
|
QueryPerformanceCounter(&t0);
|
|
for (int i = 0; i < N; i++) fstats::onFrameEnd();
|
|
QueryPerformanceCounter(&t1);
|
|
printf(" onFrameEnd: %.1f ns\n", (t1.QuadPart - t0.QuadPart) * 1e9 / fq.QuadPart / N);
|
|
|
|
printf("%s\n", g_failures ? "FAIL" : "PASS");
|
|
return g_failures ? 1 : 0;
|
|
}
|