mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 22:19:57 -04:00
Put the process's OS handle count on the sizes line and add a handle census by kind behind NX2NAVI_HANDLE_CENSUS.
A session of 2026-11-13 grew from 683 to 1095 OS handles in 11 minutes, rising at every area load and never falling,
while the guest's own handles stayed at 116-122 and the threads flat; the VMA block count fell back while the handle
count did not. The log could not show it: the 600-frame "mem: sizes" line had only the guest's handles, and nothing
told which kind of handle grows.
kernel.c kern_sizes now adds "process handles" (plat_handle_count, one system call every 600 frames under trace=perf)
beside the guest's "handles". platform.h gets plat_handle_census: on Windows the system's extended handle table
filtered by our process id, with the type names from NtQueryObject's ObjectTypesInformation; on Linux the
/proc/self/fd links grouped by kind. It never opens, duplicates or names a handle (a name query can block on a pipe).
With NX2NAVI_HANDLE_CENSUS=1 (default 0, read at start) and trace=perf, perf.c starts one long-lived thread that the
600-frame report wakes; when the count grew by 16 or more since its last census it logs "mem: handles frame N: T in
all (+D since the last census): Event a (+x), ...". The census reads a few MiB of the system's table in 2-5 ms, off
the drawing threads; one thread for the run keeps a new thread's own handle out of what it counts. New cfg rows:
HANDLE_CENSUS in known[], sections[] (diagnostics) and metas[] ("0", "0|1", restart), and an nx2navi.ini.example
entry.
New tests/handle_census_check.py builds the platform layer alone: 20 events and 5 sections show as Event +20 and
Section +5, the total stays within 2 of GetProcessHandleCount, the figures return once they are closed, and kinds
past the room given go into the last entry; docs/building.md describes it. Not reproduced before the change (the
growth needs a game run). handle_census_check, sync_wake_check and the cfg row check pass; every runtime file compiles
with the dev flags, and perf.c, kernel.c, win32.c and cfg.c add no -Wall -Wextra warnings.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2257,6 +2257,47 @@ uint32_t plat_handle_count(void) {
|
||||
return n ? n - 1 : 0; // less the descriptor of this walk
|
||||
}
|
||||
|
||||
// plat_handle_census: each descriptor's link, by kind: "socket:[N]" and "pipe:[N]" by their prefix, an anonymous
|
||||
// inode by its kind ("anon_inode:[eventfd]" is eventfd), a device by its /dev folder, any other path as file.
|
||||
int plat_handle_census(PlatHandleKind *out, int max) {
|
||||
DIR *d = max > 0 ? opendir("/proc/self/fd") : NULL;
|
||||
if (!d) return -1;
|
||||
int n = 0, self = dirfd(d);
|
||||
for (struct dirent *e; (e = readdir(d));) {
|
||||
if (e->d_name[0] == '.' || atoi(e->d_name) == self) continue;
|
||||
char link[64], target[256], kind[32];
|
||||
snprintf(link, sizeof link, "/proc/self/fd/%s", e->d_name);
|
||||
ssize_t len = readlink(link, target, sizeof target - 1);
|
||||
if (len < 0) continue; // closed meanwhile
|
||||
target[len] = 0;
|
||||
if (!strncmp(target, "/dev/", 5)) {
|
||||
size_t k = strcspn(target + 5, "/");
|
||||
snprintf(kind, sizeof kind, "/dev/%.*s", (int)(k < 20 ? k : 20), target + 5);
|
||||
} else if (target[0] == '/') {
|
||||
snprintf(kind, sizeof kind, "%s", "file");
|
||||
} else if (!strncmp(target, "anon_inode:", 11)) {
|
||||
const char *k = target + 11 + (target[11] == '[');
|
||||
snprintf(kind, sizeof kind, "%.*s", (int)strcspn(k, "]"), k);
|
||||
} else {
|
||||
snprintf(kind, sizeof kind, "%.*s", (int)strcspn(target, ":"), target);
|
||||
}
|
||||
int i = 0;
|
||||
while (i < n && strcmp(out[i].name, kind)) i++;
|
||||
if (i == n) {
|
||||
if (n == max) { // no room: the last entry takes the rest
|
||||
i = max - 1;
|
||||
snprintf(out[i].name, sizeof out[i].name, "%s", "(more kinds)");
|
||||
} else {
|
||||
snprintf(out[n].name, sizeof out[n].name, "%s", kind);
|
||||
out[n++].n = 0;
|
||||
}
|
||||
}
|
||||
out[i].n++;
|
||||
}
|
||||
closedir(d);
|
||||
return n;
|
||||
}
|
||||
|
||||
// glibc's mallinfo2 (2.33 and later) sums every arena: arena is what the arenas took with brk and mmap, hblkhd the
|
||||
// blocks mmapped one by one, uordblks what the arenas handed out. Other C libraries leave the figures unknown.
|
||||
void plat_heap_info(PlatHeapInfo *h) {
|
||||
|
||||
@@ -538,6 +538,13 @@ int plat_page_file_managed(void);
|
||||
void plat_process_memory(uint64_t *resident, uint64_t *private_bytes);
|
||||
// The handles (Windows) or file descriptors (Linux) this process holds; 0 when unknown.
|
||||
uint32_t plat_handle_count(void);
|
||||
// The same by kind: Windows the object type of each handle (Event, Section, Thread, File...), Linux what each file
|
||||
// descriptor's link names (socket, pipe, an anon_inode kind, a /dev folder, file). Fills out with up to max kinds,
|
||||
// unsorted, and returns how many, -1 on failure; the kinds past max are counted in the last entry, named "(more
|
||||
// kinds)". Never opens, duplicates or names a handle. Windows reads the system's whole handle table into a buffer of a
|
||||
// few MiB kept between calls: one thread calls it at a time.
|
||||
typedef struct { char name[32]; uint32_t n; } PlatHandleKind;
|
||||
int plat_handle_census(PlatHandleKind *out, int max);
|
||||
// The C runtime's malloc heap in bytes, for the memory report (vk_memory.c system_log): what it took from the system
|
||||
// (committed) and what of that it handed out (used), and how many other heaps the process has (Windows: those DLLs made
|
||||
// with HeapCreate, the graphics driver's among them; their sizes are not asked, as a heap made without serialization
|
||||
|
||||
@@ -2188,6 +2188,106 @@ uint32_t plat_handle_count(void) {
|
||||
return GetProcessHandleCount(GetCurrentProcess(), &n) ? (uint32_t)n : 0;
|
||||
}
|
||||
|
||||
// plat_handle_census: the system's handle table (NtQuerySystemInformation, class 64, the extended handle information)
|
||||
// filtered by this process's id, and the object types' names from NtQueryObject's ObjectTypesInformation, whose entries
|
||||
// carry the type index the table uses (Windows 8.1 and later). The table holds every process's handles; a census took
|
||||
// 1.5-4.7 ms over 3.6 MiB of it on the development PC.
|
||||
typedef struct {
|
||||
PVOID object;
|
||||
ULONG_PTR pid, handle;
|
||||
ULONG access;
|
||||
USHORT backtrace, type;
|
||||
ULONG attributes, reserved;
|
||||
} HcEntry;
|
||||
typedef struct {
|
||||
ULONG_PTR n, reserved;
|
||||
HcEntry e[1];
|
||||
} HcTable;
|
||||
typedef struct {
|
||||
USHORT len, max;
|
||||
PWSTR buf;
|
||||
} HcName;
|
||||
typedef struct { // one entry of ObjectTypesInformation; its name's characters follow it
|
||||
HcName name;
|
||||
ULONG totals[13]; // object and handle counts, pool use, their high-water marks, invalid attributes: not read
|
||||
GENERIC_MAPPING mapping;
|
||||
ULONG valid_access;
|
||||
BOOLEAN security, maintain;
|
||||
UCHAR index;
|
||||
CHAR reserved;
|
||||
ULONG pool, paged, nonpaged;
|
||||
} HcType;
|
||||
|
||||
static void hc_type_names(char names[256][32]) {
|
||||
typedef LONG(NTAPI * Qo)(HANDLE, ULONG, PVOID, ULONG, PULONG);
|
||||
HMODULE nt = GetModuleHandleW(L"ntdll.dll");
|
||||
Qo qo = nt ? (Qo)(void *)GetProcAddress(nt, "NtQueryObject") : NULL;
|
||||
if (!qo) return;
|
||||
size_t cap = 1u << 16;
|
||||
uint8_t *buf = NULL;
|
||||
LONG st = -1;
|
||||
for (int tries = 0; tries < 6; tries++, cap *= 2) {
|
||||
uint8_t *b = realloc(buf, cap);
|
||||
if (!b) break;
|
||||
buf = b;
|
||||
ULONG need = 0;
|
||||
st = qo(NULL, 3, buf, (ULONG)cap, &need); // ObjectTypesInformation
|
||||
if (st != (LONG)0xC0000004L) break; // not STATUS_INFO_LENGTH_MISMATCH: done, or failed
|
||||
}
|
||||
if (st >= 0 && buf) {
|
||||
ULONG count = *(ULONG *)buf;
|
||||
const uint8_t *p = buf + sizeof(void *), *end = buf + cap; // the count, then entries at pointer alignment
|
||||
for (ULONG i = 0; i < count && p + sizeof(HcType) <= end; i++) {
|
||||
const HcType *t = (const HcType *)p;
|
||||
WideCharToMultiByte(CP_UTF8, 0, t->name.buf, t->name.len / 2, names[t->index], 31, NULL, NULL);
|
||||
p += sizeof(HcType) + ((t->name.max + sizeof(void *) - 1) & ~(sizeof(void *) - 1));
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
}
|
||||
|
||||
int plat_handle_census(PlatHandleKind *out, int max) {
|
||||
typedef LONG(NTAPI * Qsi)(ULONG, PVOID, ULONG, PULONG);
|
||||
static char names[256][32];
|
||||
static int named;
|
||||
static void *table;
|
||||
static size_t cap;
|
||||
HMODULE nt = GetModuleHandleW(L"ntdll.dll");
|
||||
Qsi qsi = nt ? (Qsi)(void *)GetProcAddress(nt, "NtQuerySystemInformation") : NULL;
|
||||
if (!qsi || max <= 0) return -1;
|
||||
if (!named) hc_type_names(names), named = 1;
|
||||
LONG st = -1;
|
||||
for (int tries = 0; tries < 6; tries++) {
|
||||
if (!table && !(table = malloc(cap = 4u << 20))) return -1;
|
||||
ULONG need = 0;
|
||||
st = qsi(64, table, (ULONG)cap, &need); // SystemExtendedHandleInformation
|
||||
if (st != (LONG)0xC0000004L) break; // the table outgrew the buffer: a larger one
|
||||
size_t want = need > cap ? need + (1u << 20) : cap * 2;
|
||||
void *b = realloc(table, want);
|
||||
if (!b) return -1;
|
||||
table = b, cap = want;
|
||||
}
|
||||
if (st < 0) return -1;
|
||||
uint32_t count[256] = {0};
|
||||
const HcTable *t = table;
|
||||
ULONG_PTR pid = GetCurrentProcessId();
|
||||
for (ULONG_PTR i = 0; i < t->n && offsetof(HcTable, e) + (i + 1) * sizeof(HcEntry) <= cap; i++)
|
||||
if (t->e[i].pid == pid) count[t->e[i].type & 255]++;
|
||||
int n = 0;
|
||||
for (int k = 0; k < 256; k++) {
|
||||
if (!count[k]) continue;
|
||||
if (n == max) { // no room: the last entry takes the rest
|
||||
snprintf(out[max - 1].name, sizeof out[0].name, "%s", "(more kinds)");
|
||||
out[max - 1].n += count[k];
|
||||
continue;
|
||||
}
|
||||
if (names[k][0]) snprintf(out[n].name, sizeof out[0].name, "%s", names[k]);
|
||||
else snprintf(out[n].name, sizeof out[0].name, "type %d", k);
|
||||
out[n++].n = count[k];
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
// The UCRT's malloc takes its memory from the heap _get_heap_handle names (the process heap); HeapSummary sums it. The
|
||||
// other heaps are only counted: one made with HEAP_NO_SERIALIZE has no lock to take.
|
||||
void plat_heap_info(PlatHeapInfo *h) {
|
||||
|
||||
Reference in New Issue
Block a user