mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-18 15:19:58 -04:00
Give back what a host thread keeps for itself when it ends.
Every guest thread is a host thread, and the game makes and ends them all through play. A thread that slept through plat_wait_ns kept a waitable timer, one that read or wrote a file an event for each, and one that decoded zstd through the host a decoder context of about 100 KiB; nothing closed or freed them when the thread ended, and a detached thread's own record stayed allocated. They are now given back when the thread's function returns: the handles in win32.c thread_main, the decoder context through plat_thread_end_hook, and the record by whichever of the thread and its detach comes last. tests/thread_end_check.py runs 416 such threads and compares the process's handle count before and after. tests/sync_wake_check.c gets the two stand-ins kernel.c has needed since the service wait log, so that check builds and passes again. The Linux file only gains the hook call and was not compiled here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -12,7 +12,8 @@ static int fails, wake_all;
|
||||
// ---- the runtime kernel.c reaches
|
||||
unsigned rt_trace;
|
||||
uint64_t rt_guest_start_ns;
|
||||
volatile uint32_t prof_replay_tid;
|
||||
volatile uint32_t prof_replay_tid, prof_main_tid;
|
||||
int mem_readable(uint64_t a, uint64_t n) { (void)a, (void)n; return 0; } // NX2NAVI_SVC_WAITS walks guest frames: off here
|
||||
const char *cfg_get(const char *name) { return !strcmp(name, "NX2NAVI_SYNC_WAKE_ALL") && wake_all ? "1" : NULL; }
|
||||
const char *cfg_value(const char *name) { (void)name; return "0"; } // no asynchronous preemption, no slow-service log
|
||||
int cfg_bool(const char *name) { (void)name; return 0; }
|
||||
|
||||
91
tests/thread_end_check.c
Normal file
91
tests/thread_end_check.c
Normal file
@@ -0,0 +1,91 @@
|
||||
// runtime/platform: what a thread keeps for itself is given back when its function returns (win32.c thread_main,
|
||||
// platform.h plat_thread_end_hook). Threads that sleep through plat_wait_ns and read a file are made and ended by the
|
||||
// hundred, joined and detached; the end hook must run once on each, and on Windows the process must not hold more
|
||||
// handles afterwards than a few threads' worth. Built and run by tests/thread_end_check.py.
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
static long handles(void) {
|
||||
DWORD n = 0;
|
||||
return GetProcessHandleCount(GetCurrentProcess(), &n) ? (long)n : -1;
|
||||
}
|
||||
#else
|
||||
static long handles(void) { return 0; } // no handle per thread there; the hook is what is checked
|
||||
#endif
|
||||
|
||||
static int fails;
|
||||
#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
|
||||
|
||||
void rt_log(const char *fmt, ...) { // the platform layer's lines
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
va_end(ap);
|
||||
fputc('\n', stderr);
|
||||
}
|
||||
|
||||
static const char *path;
|
||||
static volatile int32_t ended, hook_runs, hook_other;
|
||||
static __thread int worked;
|
||||
|
||||
static void hook(void) {
|
||||
if (worked) plat_inc32(&hook_runs);
|
||||
else plat_inc32(&hook_other);
|
||||
worked = 0;
|
||||
}
|
||||
|
||||
static void worker(void *arg) {
|
||||
(void)arg;
|
||||
worked = 1;
|
||||
plat_wait_ns(1200000); // over a millisecond: the thread's own timer
|
||||
PlatFile *f = plat_file_open(path);
|
||||
char b[16];
|
||||
if (!f || plat_file_pread(f, b, sizeof b, 0) != (int64_t)sizeof b) plat_inc32(&hook_other); // counted as a failure
|
||||
if (f) plat_file_close(f);
|
||||
plat_inc32(&ended);
|
||||
}
|
||||
|
||||
static void wait_ended(int32_t want) {
|
||||
for (int i = 0; i < 20000 && __atomic_load_n(&ended, __ATOMIC_ACQUIRE) < want; i++) plat_sleep_ms(1);
|
||||
plat_sleep_ms(100); // the last ones leave their start function and the system lets go of them
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
printf("usage: thread_end_check FILE_OF_16_BYTES_OR_MORE\n");
|
||||
return 2;
|
||||
}
|
||||
path = argv[1];
|
||||
plat_thread_end_hook(hook);
|
||||
enum { WARM = 16, ROUNDS = 25, PER = 16 };
|
||||
for (int i = 0; i < WARM; i++) plat_thread_spawn(worker, NULL, "warm", 0);
|
||||
wait_ended(WARM);
|
||||
long h0 = handles();
|
||||
int32_t made = WARM;
|
||||
for (int r = 0; r < ROUNDS; r++) {
|
||||
PlatThread *t[PER / 2];
|
||||
for (int i = 0; i < PER / 2; i++) {
|
||||
t[i] = plat_thread_new(worker, NULL, "joined");
|
||||
made += t[i] != NULL;
|
||||
made += plat_thread_spawn(worker, NULL, "detached", 0);
|
||||
}
|
||||
for (int i = 0; i < PER / 2; i++)
|
||||
if (t[i]) plat_thread_join(t[i]);
|
||||
wait_ended(made);
|
||||
}
|
||||
long h1 = handles();
|
||||
CHECK(made == WARM + ROUNDS * PER, "%d threads made of %d", (int)made, WARM + ROUNDS * PER);
|
||||
CHECK(__atomic_load_n(&ended, __ATOMIC_ACQUIRE) == made, "%d of %d threads ended", (int)ended, (int)made);
|
||||
CHECK(hook_runs == made, "the end hook ran on %d of %d threads", (int)hook_runs, (int)made);
|
||||
CHECK(!hook_other, "%d threads could not read the file, or ran the hook without having worked", (int)hook_other);
|
||||
// each thread made a timer and a read event: left open, 400 threads would hold 800 more handles
|
||||
CHECK(h1 - h0 < 64, "%ld handles more after %d ended threads (%ld before, %ld after)", h1 - h0, ROUNDS * PER, h0, h1);
|
||||
printf("thread_end_check: %s (%d threads, the hook ran on %d, handles %ld before and %ld after)\n",
|
||||
fails ? "FAILED" : "ok", (int)made, (int)hook_runs, h0, h1);
|
||||
return fails ? 1 : 0;
|
||||
}
|
||||
48
tests/thread_end_check.py
Normal file
48
tests/thread_end_check.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""Build and run tests/thread_end_check.c: what a thread keeps for itself is given back when it ends
|
||||
(runtime/platform/win32.c thread_main, platform.h plat_thread_end_hook). 416 threads sleep, read a file and end, half
|
||||
joined and half detached; the end hook must run once on each, and on Windows the process's handle count must be what
|
||||
it was. No game files, no GPU.
|
||||
|
||||
Usage: thread_end_check.py
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
|
||||
|
||||
|
||||
def compiler():
|
||||
if os.environ.get('CC'):
|
||||
return os.environ['CC']
|
||||
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
|
||||
return str(MSYS_BIN / 'clang.exe')
|
||||
return shutil.which('clang') or 'clang'
|
||||
|
||||
|
||||
def main():
|
||||
work = ROOT / 'build' / 'test' / 'thread_end'
|
||||
work.mkdir(parents=True, exist_ok=True)
|
||||
win = os.name == 'nt'
|
||||
exe = work / ('thread_end_check.exe' if win else 'thread_end_check')
|
||||
env = dict(os.environ)
|
||||
if win and MSYS_BIN.exists():
|
||||
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
|
||||
rt = ROOT / 'runtime'
|
||||
plat = [rt / 'platform' / ('win32.c' if win else 'linux.c'), rt / 'platform' / 'common.c']
|
||||
libs = ['-lbcrypt', '-lws2_32', '-lpsapi', '-lwinmm', '-ldbghelp', '-lshell32', '-lole32', '-luser32', '-ladvapi32'] if win \
|
||||
else ['-lpthread']
|
||||
subprocess.check_call([compiler(), '-O1', '-g', '-Wall', '-Wno-unused-function', '-fno-strict-aliasing',
|
||||
'-march=x86-64-v3', '-I', str(rt),
|
||||
str(ROOT / 'tests' / 'thread_end_check.c'), *map(str, plat), '-o', str(exe), '-lm', *libs],
|
||||
env=env)
|
||||
data = work / 'read.bin'
|
||||
data.write_bytes(bytes(range(64)))
|
||||
return subprocess.run([str(exe), str(data)], env=env, timeout=300).returncode
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user