Add the performance overlay's sampling (perfmon.c), system info for bug reports (sysinfo.c), their Windows calls, GPU timestamps on demand, and option row helpers: buttons, group headings, greyed-out rows, focus by key

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-10-26 20:55:08 +03:00
1 parent bd0c9dc23d
commit 05ea4f0e56
17 files changed
+882 -44

No files matched your search

+29
View File
@@ -72,6 +72,35 @@ uint32_t plat_thread_id(void); // id of the calling t
// A system font file with the glyphs of a catalog language ("ja", "ko", "zh-Hans", "zh-Hant"): the menu shows each
// language's name in its own script. Copies the UTF-8 path into out; 0 when the system has none.
int plat_font_path(const char *lang, char *out, size_t cap);
// The operating system and its build ("Windows 10.0.28000") into out; 0 when unknown.
int plat_os_version(char *out, size_t cap);
// The file the process's stderr (the log) goes to, as a UTF-8 path; 0 when stderr is not a file (a console, a pipe).
int plat_stderr_path(char *out, size_t cap);
// ---- desktop: the in-game menu's "Copy system info" and "Open log folder"
int plat_clipboard_set(const char *utf8); // replaces the clipboard's text; 1 on success
int plat_open_folder(const char *path); // shows the folder in the file manager; 1 on success
// ---- dynamic libraries: a library some machines lack (a GPU vendor's), loaded at run time. A bare name is searched
// in the system directories only. NULL when missing.
void *plat_lib_open(const char *name);
void *plat_lib_sym(void *lib, const char *name);
// ---- machine load for the performance overlay (perfmon.c's sampling thread; each call can take milliseconds). A number
// the system does not give is -1.
typedef struct PlatLoad PlatLoad;
// Opens the counters: CPU times, the thermal zones, and the GPU engine and memory counters of the adapter whose LUID is
// gpu_luid (8 bytes, Vulkan's deviceLUID; NULL: the busiest adapter). Never NULL.
PlatLoad *plat_load_open(const uint8_t *gpu_luid);
void plat_load_close(PlatLoad *l);
// Since the previous call on l (the first call gives -1): CPU use of the machine and of this process, percent of all
// logical processors, and the hottest ACPI thermal zone in degrees Celsius.
void plat_cpu_load(PlatLoad *l, float *total_pct, float *proc_pct, float *temp_c);
// Since the previous call on l: the busiest GPU engine type in percent (as Task Manager shows it), and the adapter's
// dedicated memory in use in MB. Any GPU vendor.
void plat_gpu_load(PlatLoad *l, float *load_pct, float *vram_used_mb);
// Physical memory in MB: in use and installed on the machine, and this process's working set.
void plat_mem_info(float *used_mb, float *total_mb, float *proc_mb);
// ---- child processes: a program with stdin and stdout piped to us (stderr discarded), no console window.
typedef struct PlatChild PlatChild;
+264
View File
@@ -2,9 +2,14 @@
// SRW locks and condition variables.
#include <stdio.h>
#include <windows.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <psapi.h>
#include <shellapi.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include "platform.h"
@@ -330,6 +335,265 @@ int plat_font_path(const char *lang, char *out, size_t cap) {
return 0;
}
int plat_os_version(char *out, size_t cap) {
// GetVersionEx reports the version the manifest asks for; ntdll's RtlGetVersion the real one
typedef LONG(WINAPI * RtlGetVersion_t)(OSVERSIONINFOW *);
HMODULE nt = GetModuleHandleW(L"ntdll.dll");
RtlGetVersion_t get = nt ? (RtlGetVersion_t)(void *)GetProcAddress(nt, "RtlGetVersion") : NULL;
OSVERSIONINFOW v = {.dwOSVersionInfoSize = sizeof v};
if (!get || get(&v) != 0) return 0;
DWORD ubr = 0, size = sizeof ubr; // the update build revision, the number after the build
if (RegGetValueW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", L"UBR", RRF_RT_REG_DWORD,
NULL, &ubr, &size) != ERROR_SUCCESS)
ubr = 0;
snprintf(out, cap, ubr ? "Windows %lu.%lu.%lu.%lu" : "Windows %lu.%lu.%lu", v.dwMajorVersion, v.dwMinorVersion,
v.dwBuildNumber, ubr);
return 1;
}
int plat_stderr_path(char *out, size_t cap) {
HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
wchar_t w[WPATH];
if (!h || h == INVALID_HANDLE_VALUE || GetFileType(h) != FILE_TYPE_DISK) return 0;
DWORD n = GetFinalPathNameByHandleW(h, w, WPATH, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
if (!n || n >= WPATH) return 0;
const wchar_t *p = wcsncmp(w, L"\\\\?\\", 4) ? w : w + 4; // the \\?\ prefix
return WideCharToMultiByte(CP_UTF8, 0, p, -1, out, (int)cap, NULL, NULL) != 0;
}
// ---- desktop
int plat_clipboard_set(const char *utf8) {
int n = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
if (n <= 0) return 0;
HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)n * sizeof(wchar_t));
if (!mem) return 0;
wchar_t *w = GlobalLock(mem);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, w, n);
GlobalUnlock(mem);
// A clipboard opened without a window gets no owner, and SetClipboardData may then fail: a message-only window of
// the calling thread owns it for the call.
HWND owner = CreateWindowExW(0, L"STATIC", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
int ok = 0;
if (OpenClipboard(owner)) {
ok = EmptyClipboard() && SetClipboardData(CF_UNICODETEXT, mem) != NULL;
CloseClipboard();
}
if (owner) DestroyWindow(owner);
if (!ok) GlobalFree(mem); // after a successful SetClipboardData the system owns it
return ok;
}
static DWORD WINAPI open_folder_main(void *arg) {
ShellExecuteW(NULL, L"open", arg, NULL, NULL, SW_SHOWNORMAL);
free(arg);
return 0;
}
int plat_open_folder(const char *path) {
wchar_t *w = malloc(WPATH * sizeof *w);
if (!w || !widen(path, w, WPATH)) return free(w), 0;
DWORD a = GetFileAttributesW(w);
if (a == INVALID_FILE_ATTRIBUTES || !(a & FILE_ATTRIBUTE_DIRECTORY)) return free(w), 0;
// the shell can take a while to answer: not on the caller's thread (the present path)
HANDLE t = CreateThread(NULL, 0, open_folder_main, w, 0, NULL);
if (!t) return free(w), 0;
CloseHandle(t);
return 1;
}
// ---- dynamic libraries
void *plat_lib_open(const char *name) {
wchar_t w[WPATH];
if (!widen(name, w, WPATH)) return NULL;
int bare = !wcschr(w, L'\\') && !wcschr(w, L'/');
return (void *)LoadLibraryExW(w, NULL, bare ? LOAD_LIBRARY_SEARCH_SYSTEM32 : 0);
}
void *plat_lib_sym(void *lib, const char *name) { return lib ? (void *)GetProcAddress((HMODULE)lib, name) : NULL; }
// ---- machine load: GetSystemTimes for the CPU, PDH (pdh.dll, loaded on first use) for the thermal zones and the GPU
// engine and adapter memory counters that Task Manager reads.
typedef PDH_STATUS(WINAPI *PdhOpenQueryW_t)(LPCWSTR, DWORD_PTR, PDH_HQUERY *);
typedef PDH_STATUS(WINAPI *PdhAddEnglishCounterW_t)(PDH_HQUERY, LPCWSTR, DWORD_PTR, PDH_HCOUNTER *);
typedef PDH_STATUS(WINAPI *PdhCollectQueryData_t)(PDH_HQUERY);
typedef PDH_STATUS(WINAPI *PdhGetFormattedCounterArrayW_t)(PDH_HCOUNTER, DWORD, LPDWORD, LPDWORD,
PPDH_FMT_COUNTERVALUE_ITEM_W);
typedef PDH_STATUS(WINAPI *PdhCloseQuery_t)(PDH_HQUERY);
static struct {
INIT_ONCE once;
PdhOpenQueryW_t open;
PdhAddEnglishCounterW_t add;
PdhCollectQueryData_t collect;
PdhGetFormattedCounterArrayW_t get;
PdhCloseQuery_t close;
} pdh = {INIT_ONCE_STATIC_INIT};
static BOOL CALLBACK pdh_load(INIT_ONCE *o, void *p, void **c) {
(void)o, (void)p, (void)c;
HMODULE l = plat_lib_open("pdh.dll");
if (!l) return TRUE;
pdh.add = (PdhAddEnglishCounterW_t)(void *)GetProcAddress(l, "PdhAddEnglishCounterW");
pdh.collect = (PdhCollectQueryData_t)(void *)GetProcAddress(l, "PdhCollectQueryData");
pdh.get = (PdhGetFormattedCounterArrayW_t)(void *)GetProcAddress(l, "PdhGetFormattedCounterArrayW");
pdh.close = (PdhCloseQuery_t)(void *)GetProcAddress(l, "PdhCloseQuery");
PdhOpenQueryW_t open = (PdhOpenQueryW_t)(void *)GetProcAddress(l, "PdhOpenQueryW");
if (pdh.add && pdh.collect && pdh.get && pdh.close) pdh.open = open; // open set: all of them are there
return TRUE;
}
// One PDH query with its wildcard counters ("\Object(*)\Counter"). A query whose open failed stays NULL.
typedef struct { PDH_HQUERY q; PDH_HCOUNTER c[2]; } PdhQuery;
static void pdh_query(PdhQuery *pq, const wchar_t *c0, const wchar_t *c1) {
InitOnceExecuteOnce(&pdh.once, pdh_load, NULL, NULL);
pq->q = NULL;
if (!pdh.open || pdh.open(NULL, 0, &pq->q) != ERROR_SUCCESS) {
pq->q = NULL;
return;
}
if (pdh.add(pq->q, c0, 0, &pq->c[0]) != ERROR_SUCCESS) pq->c[0] = NULL;
if (!c1 || pdh.add(pq->q, c1, 0, &pq->c[1]) != ERROR_SUCCESS) pq->c[1] = NULL;
if (!pq->c[0] && !pq->c[1]) {
pdh.close(pq->q);
pq->q = NULL;
}
}
// The counter's instances as a malloc'd array (free it), *count of them; NULL when there is no valid data yet (a rate
// counter needs two collections).
static PDH_FMT_COUNTERVALUE_ITEM_W *pdh_items(PDH_HCOUNTER c, DWORD *count) {
DWORD size = 0;
*count = 0;
if (!c || pdh.get(c, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100, &size, count, NULL) != PDH_MORE_DATA || !size) return NULL;
PDH_FMT_COUNTERVALUE_ITEM_W *items = malloc(size);
if (items && pdh.get(c, PDH_FMT_DOUBLE | PDH_FMT_NOCAP100, &size, count, items) == ERROR_SUCCESS) return items;
free(items);
*count = 0;
return NULL;
}
struct PlatLoad {
uint64_t idle, kernel, user, proc; // the previous call's times; kernel 0 before the first
PdhQuery thermal, gpu; // gpu: engine utilisation, adapter dedicated memory
wchar_t luid[40]; // "luid_0x<high>_0x<low>" of the adapter, lower case; empty for any
};
PlatLoad *plat_load_open(const uint8_t *gpu_luid) {
PlatLoad *l = calloc(1, sizeof *l);
if (!l) return NULL;
if (gpu_luid) { // Vulkan's deviceLUID is the LUID struct's memory: LowPart, then HighPart
uint32_t lo, hi;
memcpy(&lo, gpu_luid, 4);
memcpy(&hi, gpu_luid + 4, 4);
swprintf(l->luid, sizeof l->luid / sizeof *l->luid, L"luid_0x%08x_0x%08x", hi, lo);
}
pdh_query(&l->thermal, L"\\Thermal Zone Information(*)\\Temperature", NULL);
pdh_query(&l->gpu, L"\\GPU Engine(*)\\Utilization Percentage", L"\\GPU Adapter Memory(*)\\Dedicated Usage");
return l;
}
void plat_load_close(PlatLoad *l) {
if (!l) return;
if (l->thermal.q) pdh.close(l->thermal.q);
if (l->gpu.q) pdh.close(l->gpu.q);
free(l);
}
static uint64_t ft64(FILETIME f) { return (uint64_t)f.dwHighDateTime << 32 | f.dwLowDateTime; }
void plat_cpu_load(PlatLoad *l, float *total_pct, float *proc_pct, float *temp_c) {
*total_pct = *proc_pct = *temp_c = -1;
if (!l) return;
FILETIME idle, kern, user, cr, ex, pk, pu;
if (GetSystemTimes(&idle, &kern, &user) && GetProcessTimes(GetCurrentProcess(), &cr, &ex, &pk, &pu)) {
uint64_t i = ft64(idle), k = ft64(kern), u = ft64(user), p = ft64(pk) + ft64(pu);
uint64_t total = (k - l->kernel) + (u - l->user); // kernel time includes idle time
if (l->kernel && total) {
*total_pct = 100.0f * (float)(total - (i - l->idle)) / (float)total;
*proc_pct = 100.0f * (float)(p - l->proc) / (float)total;
}
l->idle = i, l->kernel = k, l->user = u, l->proc = p;
}
// the firmware's ACPI zones, near the CPU on most boards; the exact package temperature needs a kernel driver
DWORD n;
PDH_FMT_COUNTERVALUE_ITEM_W *it;
if (l->thermal.q && pdh.collect(l->thermal.q) == ERROR_SUCCESS && (it = pdh_items(l->thermal.c[0], &n))) {
for (DWORD i = 0; i < n; i++) { // the hottest zone, in kelvin
double c = it[i].FmtValue.doubleValue - 273.15;
if (it[i].FmtValue.CStatus == PDH_CSTATUS_VALID_DATA && c > *temp_c && c < 150) *temp_c = (float)c;
}
free(it);
}
}
// The adapter part of an instance name, "luid_0x..._0x...", lower case, into out (40 wide characters).
static void inst_luid(const wchar_t *name, wchar_t *out) {
const wchar_t *p = wcsstr(name, L"luid_");
size_t n = 0;
while (p && p[n] && n < 26 && n < 39) out[n] = (wchar_t)towlower(p[n]), n++; // luid_0x%08x_0x%08x: 26
out[n] = 0;
}
void plat_gpu_load(PlatLoad *l, float *load_pct, float *vram_used_mb) {
*load_pct = *vram_used_mb = -1;
if (!l || !l->gpu.q || pdh.collect(l->gpu.q) != ERROR_SUCCESS) return;
// Engine instances: "pid_<n>_luid_0x.._0x.._phys_<n>_eng_<n>_engtype_<type>". Per adapter and engine type the
// processes' shares add up; the load is the busiest engine type.
enum { TYPES = 32 };
struct { wchar_t luid[40], type[32]; double sum; } t[TYPES];
int nt = 0;
DWORD n;
PDH_FMT_COUNTERVALUE_ITEM_W *it = pdh_items(l->gpu.c[0], &n);
for (DWORD i = 0; it && i < n; i++) {
if (it[i].FmtValue.CStatus != PDH_CSTATUS_VALID_DATA) continue;
wchar_t lu[40];
inst_luid(it[i].szName, lu);
if (l->luid[0] && wcscmp(lu, l->luid)) continue;
const wchar_t *ty = wcsstr(it[i].szName, L"engtype_");
ty = ty ? ty + 8 : L"";
int k = 0;
while (k < nt && (wcscmp(t[k].luid, lu) || wcsncmp(t[k].type, ty, 31))) k++;
if (k == nt) {
if (nt == TYPES) continue;
wcscpy(t[k].luid, lu);
wcsncpy(t[k].type, ty, 31);
t[k].type[31] = 0;
t[k].sum = 0;
nt++;
}
t[k].sum += it[i].FmtValue.doubleValue;
}
free(it);
for (int k = 0; k < nt; k++)
if (t[k].sum > *load_pct) *load_pct = (float)(t[k].sum > 100 ? 100 : t[k].sum);
// Adapter memory instances: "luid_0x.._0x.._phys_<n>", bytes. Without a LUID: the adapter that uses the most.
it = pdh_items(l->gpu.c[1], &n);
double sum = 0, best = -1;
for (DWORD i = 0; it && i < n; i++) {
if (it[i].FmtValue.CStatus != PDH_CSTATUS_VALID_DATA) continue;
wchar_t lu[40];
inst_luid(it[i].szName, lu);
if (l->luid[0] ? !wcscmp(lu, l->luid) : 0) sum += it[i].FmtValue.doubleValue;
if (it[i].FmtValue.doubleValue > best) best = it[i].FmtValue.doubleValue;
}
if (it && (l->luid[0] ? sum > 0 : best >= 0)) *vram_used_mb = (float)((l->luid[0] ? sum : best) / 1048576.0);
free(it);
}
void plat_mem_info(float *used_mb, float *total_mb, float *proc_mb) {
*used_mb = *total_mb = *proc_mb = -1;
MEMORYSTATUSEX ms = {.dwLength = sizeof ms};
if (GlobalMemoryStatusEx(&ms)) {
*total_mb = (float)(ms.ullTotalPhys >> 20);
*used_mb = (float)((ms.ullTotalPhys - ms.ullAvailPhys) >> 20);
}
PROCESS_MEMORY_COUNTERS pmc;
if (K32GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof pmc)) *proc_mb = (float)(pmc.WorkingSetSize >> 20);
}
// ---- child processes
struct PlatChild {