mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-18 12:08:38 +00:00
Merge the Windows frame time work: SleepThread spins under 1 ms, recording copies indexed by 64 KiB block, the pipeline cache and the save Commit written on background threads, shader use masks read at start, a slow-present profiler (TOTK_PROFILE_SLOW) and input recorded on the world clock
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
c5a275126e
14 files changed
+479
-90
No files matched your search
@@ -15,6 +15,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "cfg.h"
|
||||
#include "fs.h"
|
||||
#include "gfx.h"
|
||||
#include "nvn_api.h"
|
||||
#include "vk.h"
|
||||
@@ -283,6 +284,7 @@ void rt_frame_presented(const tex_t *t, uint64_t n) {
|
||||
stats_line();
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
fs_save_flush();
|
||||
vk_pipeline_save(); // no-op without the Vulkan back end
|
||||
prof_write();
|
||||
fflush(stderr);
|
||||
|
||||
+27
-2
@@ -22,6 +22,11 @@
|
||||
// One 64-bit and one 32-bit store per present; the guest reads the floats on its own threads, and each stays finite
|
||||
// and in range whichever store it sees.
|
||||
//
|
||||
// The world clock (frame_step_world_us) sums the steps from the first present that finds the player actor away from
|
||||
// the origin, where the title screen keeps it: the end of the first save load. Recorded and scripted input
|
||||
// (input_win.c) runs on it, so a replay presses at the same game time at any frame rate and after a first load of any
|
||||
// length; a later load (fast travel) still shifts what follows by its length.
|
||||
//
|
||||
// TOTK_TRACE=perf adds a line per 120 presents: fps, the game seconds stepped against wall seconds, and with
|
||||
// TOTK_SPEED_PROBE=1 the player's position and ground speed (an independent check: Link runs at the same speed at
|
||||
// any frame rate when game time follows wall time).
|
||||
@@ -201,11 +206,30 @@ static void speed_report(uint64_t now, double step, int interval) {
|
||||
memset(win_hist, 0, sizeof win_hist);
|
||||
}
|
||||
|
||||
static _Atomic int64_t world_us = -1;
|
||||
static uint64_t presents;
|
||||
|
||||
int64_t frame_step_world_us(void) { return atomic_load_explicit(&world_us, memory_order_relaxed); }
|
||||
|
||||
static void world_clock(double dt, double step) {
|
||||
presents++;
|
||||
int64_t w = atomic_load_explicit(&world_us, memory_order_relaxed);
|
||||
if (w < 0) {
|
||||
float p[3];
|
||||
if (!player_pos(p) || fabsf(p[0]) + fabsf(p[2]) < 1.0f) return; // a few guest reads, until the load ends
|
||||
rt_log("frame step: world clock starts at present %llu (the player at %.1f %.1f %.1f)",
|
||||
(unsigned long long)presents, p[0], p[1], p[2]);
|
||||
atomic_store_explicit(&world_us, 0, memory_order_relaxed);
|
||||
return;
|
||||
}
|
||||
if (!step_word) step = dt > STEP_MAX_S ? STEP_MAX_S : dt; // whole-vsync steps: the display time stands in
|
||||
atomic_store_explicit(&world_us, w + (int64_t)(step * 1e6 + 0.5), memory_order_relaxed);
|
||||
}
|
||||
|
||||
void frame_step_present(uint64_t display_ns, int interval) {
|
||||
frame_limit_live();
|
||||
double step = 0;
|
||||
double step = 0, dt = last_ns ? (double)(display_ns - last_ns) * 1e-9 : 0;
|
||||
if (step_word && last_ns) {
|
||||
double dt = (double)(display_ns - last_ns) * 1e-9;
|
||||
if (dt > STEP_RESET_S || dt < 0.001) {
|
||||
sample_count = sample_next = 0;
|
||||
} else {
|
||||
@@ -219,5 +243,6 @@ void frame_step_present(uint64_t display_ns, int interval) {
|
||||
}
|
||||
}
|
||||
last_ns = display_ns;
|
||||
world_clock(dt, step);
|
||||
if (rt_trace & RT_TRACE_PERF) speed_report(display_ns, step, interval);
|
||||
}
|
||||
@@ -48,3 +48,5 @@ void fs_host_set_profile(const char *dir); // profile directory;
|
||||
// (default rt_config()->profile)
|
||||
int fs_save_busy(void); // a save is being written: a save file open for writing,
|
||||
// or save data changed in the last 3 s and not committed
|
||||
void fs_save_flush(void); // wait for a save Commit still running in the background;
|
||||
// every exit path calls it before the process ends
|
||||
+81
-17
@@ -13,8 +13,9 @@
|
||||
// <profile>/cache/<index>/ cache storage
|
||||
//
|
||||
// The save mount is journaled as on Horizon: changes go to a working copy and become the committed save atomically at
|
||||
// Commit; a crash or an Unmount before Commit leaves the previous committed save. fs_save_busy still tells a window
|
||||
// close to wait until a save in progress is committed. Paths are UTF-8 and go to the W APIs as UTF-16.
|
||||
// Commit; a crash or an Unmount before Commit leaves the previous committed save. Commit returns at once and the copy
|
||||
// runs on a background thread (section "save transactions"); fs_save_flush lets the exit paths wait for it. Paths are
|
||||
// UTF-8 and go to the W APIs as UTF-16.
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -70,7 +71,9 @@ static DWORD attributes(const char *path) {
|
||||
// (TotK writes a slot's files back to back and then commits; between the files nothing is open for a moment).
|
||||
static volatile LONG save_writers, save_dirty;
|
||||
static volatile LONG64 save_changed_ns;
|
||||
static void save_wait_commit(void);
|
||||
static void save_touched(void) {
|
||||
save_wait_commit(); // every change to save data comes here first: none lands in a commit's snapshot
|
||||
InterlockedExchange64(&save_changed_ns, (LONG64)plat_now_ns());
|
||||
InterlockedExchange(&save_dirty, 1);
|
||||
}
|
||||
@@ -158,7 +161,10 @@ static int remove_tree(const char *path) {
|
||||
// Commit: (1) copy work to new and flush every file (FlushFileBuffers) and directory, a file equal to the committed
|
||||
// one as a hard link to it; (2) rename save/<uid> to old;
|
||||
// (3) rename new to save/<uid>, the commit point; (4) move old to save-backup/<UTC time> and keep the newest three.
|
||||
// Both renames are MoveFileExW with MOVEFILE_WRITE_THROUGH. save/<uid> is renamed away only after new is complete
|
||||
// Both renames are MoveFileExW with MOVEFILE_WRITE_THROUGH. The game's Commit returns 0 once steps (1) to (4) are
|
||||
// queued; a thread runs them (95-135 ms on the game thread before, in the 1.4.3 fight save) while every change to the
|
||||
// working copy waits in save_touched, and Unmount and the next Commit wait too. A crash in that time loses this Commit
|
||||
// only, and a failed one is logged instead of returned. save/<uid> is renamed away only after new is complete
|
||||
// on disk, so the recovery rule at start (save_recover) is:
|
||||
// save/<uid> present it is the committed save (the old one if the crash came before (3), the new one after);
|
||||
// new, if present, is an unfinished copy and is deleted (rollback);
|
||||
@@ -425,18 +431,56 @@ static uint32_t save_commit_locked(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t save_commit(void) {
|
||||
// The background commit: commit_pending from the game's Commit until the thread has run save_commit_locked.
|
||||
static SRWLOCK pend_lock = SRWLOCK_INIT;
|
||||
static CONDITION_VARIABLE pend_cv = CONDITION_VARIABLE_INIT;
|
||||
static volatile LONG commit_pending;
|
||||
|
||||
static void save_wait_commit(void) {
|
||||
if (!InterlockedCompareExchange(&commit_pending, 0, 0)) return;
|
||||
AcquireSRWLockExclusive(&pend_lock);
|
||||
while (commit_pending) SleepConditionVariableSRW(&pend_cv, &pend_lock, INFINITE, 0);
|
||||
ReleaseSRWLockExclusive(&pend_lock);
|
||||
}
|
||||
|
||||
void fs_save_flush(void) { save_wait_commit(); }
|
||||
|
||||
static uint32_t commit_run(void) {
|
||||
AcquireSRWLockExclusive(&tx_lock);
|
||||
InterlockedIncrement(&save_writers); // fs_save_busy for the duration
|
||||
static int quiet;
|
||||
uint32_t r = 0;
|
||||
if (InterlockedCompareExchange(&save_dirty, 0, 0)) r = save_commit_locked();
|
||||
else if (!quiet++) rt_log("fs commit save: nothing changed (logged once)");
|
||||
uint32_t r = save_commit_locked();
|
||||
InterlockedDecrement(&save_writers);
|
||||
ReleaseSRWLockExclusive(&tx_lock);
|
||||
AcquireSRWLockExclusive(&pend_lock);
|
||||
commit_pending = 0;
|
||||
ReleaseSRWLockExclusive(&pend_lock);
|
||||
WakeAllConditionVariable(&pend_cv);
|
||||
return r;
|
||||
}
|
||||
|
||||
static DWORD WINAPI commit_main(void *arg) {
|
||||
(void)arg;
|
||||
commit_run();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t save_commit(void) {
|
||||
save_wait_commit(); // the previous Commit
|
||||
static int quiet;
|
||||
if (!InterlockedCompareExchange(&save_dirty, 0, 0)) {
|
||||
if (!quiet++) rt_log("fs commit save: nothing changed (logged once)");
|
||||
return 0;
|
||||
}
|
||||
InterlockedIncrement(&save_writers); // fs_save_busy until the thread is done
|
||||
InterlockedExchange(&commit_pending, 1);
|
||||
HANDLE t = CreateThread(NULL, 0, commit_main, NULL, 0, NULL);
|
||||
if (t) {
|
||||
CloseHandle(t);
|
||||
return 0;
|
||||
}
|
||||
rt_log("fs: cannot start the save commit thread (error %lu); committing on the game thread", GetLastError());
|
||||
return commit_run();
|
||||
}
|
||||
|
||||
// ---- storage roots
|
||||
static BOOL CALLBACK storage_init(PINIT_ONCE once, PVOID param, PVOID *ctx) {
|
||||
(void)once, (void)param, (void)ctx;
|
||||
@@ -678,6 +722,7 @@ static uint32_t read_cache_list(int *count, CacheStorageInfo *out, uint64_t h, i
|
||||
static void close_cache_list(uint64_t h) { free((void *)(uintptr_t)h); }
|
||||
|
||||
static void unmount(const char *name) {
|
||||
save_wait_commit(); // else the working copy could go before a Commit queued just now has copied it
|
||||
AcquireSRWLockExclusive(&tx_lock);
|
||||
AcquireSRWLockExclusive(&lock);
|
||||
for (int i = 0; i < NMOUNTS; i++)
|
||||
@@ -1335,6 +1380,13 @@ static void probe_restart(const uint64_t *uid) {
|
||||
CHECK(RES(S_MOUNT_SAVE, U("save"), U(uid)) == 0, "MountSaveData after a restart");
|
||||
}
|
||||
|
||||
// Commit through the override, then wait for its background thread, as the checks read the disk next.
|
||||
static uint32_t tx_commit(void) {
|
||||
uint32_t r = RES(S_COMMIT, U("save"));
|
||||
fs_save_flush();
|
||||
return r;
|
||||
}
|
||||
|
||||
#define TX_SIZE 4096
|
||||
static uint8_t tx_buf[TX_SIZE];
|
||||
|
||||
@@ -1405,7 +1457,7 @@ static int count_dirs(const wchar_t *dir) {
|
||||
static void probe_save_tx(const uint64_t *uid) {
|
||||
const char *a = "save:/tx_a.sav", *b = "save:/tx_b.sav";
|
||||
uint64_t h = 0;
|
||||
CHECK(tx_write(a, 1) && tx_write(b, 1) && RES(S_COMMIT, U("save")) == 0 && tx_read_committed("tx_a.sav") == 1,
|
||||
CHECK(tx_write(a, 1) && tx_write(b, 1) && tx_commit() == 0 && tx_read_committed("tx_a.sav") == 1,
|
||||
"Commit of generation 1");
|
||||
|
||||
// uncommitted changes: the game reads them; the committed save and the next start do not see them
|
||||
@@ -1417,7 +1469,7 @@ static void probe_save_tx(const uint64_t *uid) {
|
||||
CHECK(tx_read(a) == 1 && tx_read("save:/tx_c.sav") == -1, "an uncommitted write survived a restart");
|
||||
|
||||
// committed changes survive a restart; an Unmount without Commit drops changes
|
||||
CHECK(tx_write(a, 3) && tx_write(b, 3) && RES(S_COMMIT, U("save")) == 0, "Commit of generation 3");
|
||||
CHECK(tx_write(a, 3) && tx_write(b, 3) && tx_commit() == 0, "Commit of generation 3");
|
||||
probe_restart(uid);
|
||||
CHECK(tx_read(a) == 3 && tx_read(b) == 3, "a commit lost in a restart");
|
||||
CHECK(tx_write(a, 4), "write before Unmount");
|
||||
@@ -1430,7 +1482,7 @@ static void probe_save_tx(const uint64_t *uid) {
|
||||
for (int k = 1; k < 64; k++) {
|
||||
int next = ++gen, ok = tx_write(a, next) && tx_write(b, next);
|
||||
tx_crash_at = k;
|
||||
uint32_t r = RES(S_COMMIT, U("save"));
|
||||
uint32_t r = tx_commit();
|
||||
int died = tx_dead;
|
||||
tx_crash_at = 0;
|
||||
if (!died) {
|
||||
@@ -1456,19 +1508,31 @@ static void probe_save_tx(const uint64_t *uid) {
|
||||
|
||||
// a Commit writes the changed file and links the unchanged one to the committed save it replaces
|
||||
int kept = committed;
|
||||
CHECK(tx_write(a, ++gen) && RES(S_COMMIT, U("save")) == 0 && tx_read_committed("tx_a.sav") == gen &&
|
||||
CHECK(tx_write(a, ++gen) && tx_commit() == 0 && tx_read_committed("tx_a.sav") == gen &&
|
||||
tx_read_committed("tx_b.sav") == kept && tx_links_committed("tx_b.sav") >= 2 &&
|
||||
tx_links_committed("tx_a.sav") == 1, "Commit of one changed file: tx_a %d links, tx_b %d links",
|
||||
tx_links_committed("tx_a.sav"), tx_links_committed("tx_b.sav"));
|
||||
committed = gen;
|
||||
|
||||
// the background commit: a write right after Commit waits for it and stays out of it; an Unmount right after
|
||||
// Commit waits for it and keeps it
|
||||
int queued = ++gen;
|
||||
CHECK(tx_write(a, queued) && RES(S_COMMIT, U("save")) == 0 && tx_write(a, ++gen) && tx_read(a) == gen &&
|
||||
tx_read_committed("tx_a.sav") == queued, "a write right after Commit: committed tx_a %d, expected %d",
|
||||
tx_read_committed("tx_a.sav"), queued);
|
||||
CHECK(RES(S_COMMIT, U("save")) == 0, "Commit before Unmount");
|
||||
CALL(S_UNMOUNT, U("save"));
|
||||
CHECK(tx_read_committed("tx_a.sav") == gen && RES(S_MOUNT_SAVE, U("save"), U(uid)) == 0 && tx_read(a) == gen,
|
||||
"an Unmount right after Commit lost it");
|
||||
committed = gen;
|
||||
|
||||
// the read-only mount shows the committed save and refuses changes
|
||||
CHECK(tx_write(a, 99) && RES(S_MOUNT_SAVE_RO, U("ro"), TOTK_APPLICATION_ID, U(uid)) == 0 &&
|
||||
tx_read("ro:/tx_a.sav") == committed && RES(S_OPEN_FILE, U(&h), U("ro:/tx_a.sav"), FS_OPEN_WRITE) ==
|
||||
FS_R_UNSUPPORTED_OPEN && RES(S_CREATE_FILE, U("ro:/x"), 0) == FS_R_UNSUPPORTED_OPEN &&
|
||||
RES(S_RENAME_FILE, U("save:/tx_a.sav"), U("ro:/y")) == FS_R_INVALID_PATH, "MountSaveDataReadOnly");
|
||||
CALL(S_UNMOUNT, U("ro"));
|
||||
CHECK(RES(S_DELETE_FILE, U(a)) == 0 && RES(S_DELETE_FILE, U(b)) == 0 && RES(S_COMMIT, U("save")) == 0 &&
|
||||
CHECK(RES(S_DELETE_FILE, U(a)) == 0 && RES(S_DELETE_FILE, U(b)) == 0 && tx_commit() == 0 &&
|
||||
tx_read_committed("tx_a.sav") == -1, "Commit of deletions");
|
||||
printf("save tx: uncommitted writes visible to the game and dropped by a restart and by Unmount, commits kept; a "
|
||||
"crash at each of %d commit steps recovers to the old save %d times, the new one %d times, mixed %d "
|
||||
@@ -1518,7 +1582,7 @@ static void probe_storage(void) {
|
||||
CHECK(fs_save_busy(), "a save file open for writing does not count as a save in progress");
|
||||
CALL(S_CLOSE_FILE, h);
|
||||
CHECK(fs_save_busy(), "closed but uncommitted save data does not count as a save in progress");
|
||||
CHECK(RES(S_COMMIT, U("save")) == 0 && !fs_save_busy(), "Commit");
|
||||
CHECK(tx_commit() == 0 && !fs_save_busy(), "Commit");
|
||||
CHECK(RES(S_COMMIT, U("nomount")) == FS_R_PATH_NOT_FOUND, "Commit of an unknown mount");
|
||||
|
||||
// paths
|
||||
@@ -1549,7 +1613,7 @@ static void probe_storage(void) {
|
||||
count == 1 && ents[0].type == FS_ENTRY_FILE, "ReadDirectory, files only");
|
||||
CALL(S_CLOSE_DIRECTORY, d);
|
||||
|
||||
CHECK(RES(S_COMMIT, U("save")) == 0, "Commit of the directories");
|
||||
CHECK(tx_commit() == 0, "Commit of the directories");
|
||||
|
||||
// reopen after unmount: the bytes come back through the overrides and are on disk
|
||||
CALL(S_UNMOUNT, U("save"));
|
||||
@@ -1588,7 +1652,7 @@ static void probe_storage(void) {
|
||||
RES(S_DELETE_FILE, U("save:/renamed.sav")) == FS_R_PATH_NOT_FOUND, "DeleteFile");
|
||||
CHECK(RES(S_DELETE_RECURSIVE, U("save:/dir")) == 0 &&
|
||||
RES(S_ENTRY_TYPE, U(&type), U("save:/dir")) == FS_R_PATH_NOT_FOUND, "DeleteDirectoryRecursively");
|
||||
CHECK(RES(S_COMMIT, U("save")) == 0 && !fs_save_busy(), "Commit after the changes");
|
||||
CHECK(tx_commit() == 0 && !fs_save_busy(), "Commit after the changes");
|
||||
probe_save_tx(uid);
|
||||
|
||||
// temporary and cache storage
|
||||
|
||||
+114
-13
@@ -191,6 +191,8 @@ static void xlate_identity(void) {
|
||||
|
||||
// The repository root is the nearest parent of totk.exe that holds build/xlate/totk_xlate.exe. The shader cache lives
|
||||
// next to totk.exe, so every build dir has its own (as the pipeline cache, vk_pipeline.c cache_path_init).
|
||||
static void use_preload_start(void);
|
||||
|
||||
static BOOL CALLBACK init_paths(PINIT_ONCE once, PVOID param, PVOID *ctx) {
|
||||
(void)once, (void)param, (void)ctx;
|
||||
char exe[PATH_CAP], root[PATH_CAP] = "", exe_dir[PATH_CAP] = "";
|
||||
@@ -216,6 +218,7 @@ static BOOL CALLBACK init_paths(PINIT_ONCE once, PVOID param, PVOID *ctx) {
|
||||
if (cache_cfg && *cache_cfg && strlen(cache_cfg) < sizeof cache_dir) strcpy(cache_dir, cache_cfg);
|
||||
else if (exe_dir[0]) snprintf(cache_dir, sizeof cache_dir, "%s\\shader-cache", exe_dir);
|
||||
xlate_identity();
|
||||
use_preload_start();
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -701,15 +704,95 @@ static UseMask use_of(const GfxShader *s) {
|
||||
}
|
||||
|
||||
// <sha256("TKSU", translator identity, code hash)>.use: masks are per translator.
|
||||
static void use_path(const uint8_t h[32], char *p) {
|
||||
uint8_t buf[4 + 32 + 32], name[32];
|
||||
char hex[65];
|
||||
static void use_name(const uint8_t h[32], uint8_t name[32]) {
|
||||
uint8_t buf[4 + 32 + 32];
|
||||
memcpy(buf, "TKSU", 4), memcpy(buf + 4, xlate_id, 32), memcpy(buf + 36, h, 32);
|
||||
rt_sha256(buf, sizeof buf, name);
|
||||
}
|
||||
|
||||
static void use_path_of(const uint8_t name[32], char *p) {
|
||||
char hex[65];
|
||||
key_hex(name, hex);
|
||||
snprintf(p, PATH_CAP + 80, "%s\\%s.use", cache_dir, hex);
|
||||
}
|
||||
|
||||
static void use_path(const uint8_t h[32], char *p) {
|
||||
uint8_t name[32];
|
||||
use_name(h, name);
|
||||
use_path_of(name, p);
|
||||
}
|
||||
|
||||
// Every .use file of the cache, read by a background thread when the paths are set (use_preload), by file name: a
|
||||
// lookup on the render thread then opens no file. Opening one took 0.1 ms alone but 11-40 ms in play, behind the
|
||||
// translator's and the pipeline cache's writes and flushes on the same volume, and each new shader of a draw opened one.
|
||||
// The table is complete and read-only once use_files_ready is 1; masks found later are in use_buckets.
|
||||
#define USE_FILE_BUCKETS 65536
|
||||
typedef struct UseFile { struct UseFile *next; uint8_t name[32]; UseMask m; } UseFile;
|
||||
static UseFile *use_files[USE_FILE_BUCKETS];
|
||||
static volatile LONG use_files_ready;
|
||||
|
||||
static UseFile **use_file_slot(const uint8_t name[32]) {
|
||||
UseFile **e = &use_files[(name[0] | name[1] << 8) % USE_FILE_BUCKETS];
|
||||
while (*e && memcmp((*e)->name, name, 32)) e = &(*e)->next;
|
||||
return e;
|
||||
}
|
||||
|
||||
static int hex_byte(const char *c) {
|
||||
int v = 0;
|
||||
for (int i = 0; i < 2; i++) {
|
||||
int d = isdigit((unsigned char)c[i]) ? c[i] - '0' : c[i] >= 'a' && c[i] <= 'f' ? c[i] - 'a' + 10 : -1;
|
||||
if (d < 0) return -1;
|
||||
v = v << 4 | d;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static DWORD WINAPI use_preload(void *arg) {
|
||||
(void)arg;
|
||||
uint64_t t0 = plat_now_ns();
|
||||
char pattern[PATH_CAP + 16], p[PATH_CAP + 80];
|
||||
snprintf(pattern, sizeof pattern, "%s\\*.use", cache_dir);
|
||||
WIN32_FIND_DATAA fd;
|
||||
HANDLE f = FindFirstFileExA(pattern, FindExInfoBasic, &fd, FindExSearchNameMatch, NULL, FIND_FIRST_EX_LARGE_FETCH);
|
||||
int n = 0, bad = 0;
|
||||
if (f != INVALID_HANDLE_VALUE) {
|
||||
do {
|
||||
UseFile e = {0};
|
||||
int ok = strlen(fd.cFileName) == 68;
|
||||
for (int i = 0; ok && i < 32; i++) {
|
||||
int v = hex_byte(fd.cFileName + 2 * i);
|
||||
ok = v >= 0, e.name[i] = (uint8_t)v;
|
||||
}
|
||||
uint64_t len = 0;
|
||||
uint32_t *u = NULL;
|
||||
if (ok) {
|
||||
snprintf(p, sizeof p, "%s\\%s", cache_dir, fd.cFileName);
|
||||
u = (uint32_t *)read_file(p, &len);
|
||||
ok = u && len == 20 && u[0] == USE_MAGIC && u[1] == USE_VERSION;
|
||||
}
|
||||
UseFile **slot = ok ? use_file_slot(e.name) : NULL, *x;
|
||||
if (slot && !*slot && (x = malloc(sizeof *x))) {
|
||||
e.m.slots = u[2], e.m.attrs = u[3], e.m.fouts = u[4];
|
||||
*x = e, *slot = x, n++;
|
||||
} else if (!ok) {
|
||||
bad++;
|
||||
}
|
||||
free(u);
|
||||
} while (FindNextFileA(f, &fd));
|
||||
FindClose(f);
|
||||
}
|
||||
InterlockedExchange(&use_files_ready, 1);
|
||||
rt_log("gfx_shader: %d use masks read in %.0f ms on a background thread (%d unreadable)", n,
|
||||
(plat_now_ns() - t0) / 1e6, bad);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void use_preload_start(void) {
|
||||
if (!cache_dir[0]) return;
|
||||
HANDLE t = CreateThread(NULL, 0, use_preload, NULL, 0, NULL);
|
||||
if (t) SetThreadPriority(t, THREAD_PRIORITY_BELOW_NORMAL), CloseHandle(t);
|
||||
}
|
||||
|
||||
static UseEnt **use_slot(const uint8_t h[32]) { // caller holds cache_lock
|
||||
UseEnt **e = &use_buckets[(h[0] | h[1] << 8) % BUCKETS];
|
||||
while (*e && memcmp((*e)->code_hash, h, 32)) e = &(*e)->next;
|
||||
@@ -732,13 +815,20 @@ static int use_find(const uint8_t h[32], UseMask *m) {
|
||||
if (known) *m = e->m;
|
||||
ReleaseSRWLockShared(&cache_lock);
|
||||
if (seen) return known;
|
||||
char p[PATH_CAP + 80];
|
||||
use_path(h, p);
|
||||
uint64_t n = 0;
|
||||
uint32_t *f = (uint32_t *)read_file(p, &n);
|
||||
known = f && n == 20 && f[0] == USE_MAGIC && f[1] == USE_VERSION;
|
||||
if (known) m->slots = f[2], m->attrs = f[3], m->fouts = f[4];
|
||||
free(f);
|
||||
uint8_t name[32];
|
||||
use_name(h, name);
|
||||
if (InterlockedCompareExchange(&use_files_ready, 0, 0)) { // complete: a name not in it has no file
|
||||
UseFile *u = *use_file_slot(name);
|
||||
if ((known = u != NULL)) *m = u->m;
|
||||
} else {
|
||||
char p[PATH_CAP + 80];
|
||||
use_path_of(name, p);
|
||||
uint64_t n = 0;
|
||||
uint32_t *f = (uint32_t *)read_file(p, &n);
|
||||
known = f && n == 20 && f[0] == USE_MAGIC && f[1] == USE_VERSION;
|
||||
if (known) m->slots = f[2], m->attrs = f[3], m->fouts = f[4];
|
||||
free(f);
|
||||
}
|
||||
AcquireSRWLockExclusive(&cache_lock);
|
||||
use_put(h, known, m);
|
||||
ReleaseSRWLockExclusive(&cache_lock);
|
||||
@@ -918,6 +1008,12 @@ void gfx_shader_drop_memory(void) {
|
||||
for (UseEnt *e = use_buckets[i], *n; e; e = n) n = e->next, free(e);
|
||||
use_buckets[i] = NULL;
|
||||
}
|
||||
// the preloaded .use table misses the masks this run wrote, which only use_buckets held: back to the disk
|
||||
InterlockedExchange(&use_files_ready, 0);
|
||||
for (int i = 0; i < USE_FILE_BUCKETS; i++) {
|
||||
for (UseFile *e = use_files[i], *n; e; e = n) n = e->next, free(e);
|
||||
use_files[i] = NULL;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&cache_lock);
|
||||
}
|
||||
|
||||
@@ -1626,18 +1722,23 @@ static int rec_append(const char *path, const char *magic, uint32_t version, uin
|
||||
return !fclose(f) && ok;
|
||||
}
|
||||
|
||||
static SRWLOCK rec_io = SRWLOCK_INIT; // the record files: appends in order, no read during an append
|
||||
|
||||
void gfx_shader_record_flush(int force) {
|
||||
static volatile LONG64 last;
|
||||
LONG64 now = (LONG64)GetTickCount64();
|
||||
if (!RC.on || (!force && now - last < 2000)) return;
|
||||
last = now;
|
||||
// The files are written outside RC.lock, which the render thread takes to record a new answer.
|
||||
AcquireSRWLockExclusive(&rec_io);
|
||||
AcquireSRWLockExclusive(&RC.lock);
|
||||
Bytes ans = RC.ans, prg = RC.prg;
|
||||
RC.ans = (Bytes){0}, RC.prg = (Bytes){0};
|
||||
uint64_t total = RC.pairs.n, added = RC.pairs_new;
|
||||
ReleaseSRWLockExclusive(&RC.lock);
|
||||
if (prg.n && !rec_append(RC.prg_path, "TOTKSPRG", PRG_VERSION, 0, &prg)) rt_log("gfx_shader: cannot write %s", RC.prg_path);
|
||||
if (ans.n && !rec_append(RC.ans_path, "TOTKSANS", ANS_VERSION, ANS_REC, &ans)) rt_log("gfx_shader: cannot write %s", RC.ans_path);
|
||||
ReleaseSRWLockExclusive(&RC.lock);
|
||||
ReleaseSRWLockExclusive(&rec_io);
|
||||
if (ans.n) rt_log("gfx_shader: answers: %zu new records appended (%llu new this run, %llu recorded)", ans.n / ANS_REC,
|
||||
(unsigned long long)added, (unsigned long long)total);
|
||||
free(ans.p), free(prg.p);
|
||||
@@ -1801,9 +1902,9 @@ size_t gfx_shader_warm_list(GfxWarmEnt **out) {
|
||||
RC.ans_path);
|
||||
uint8_t *ab = NULL, *pb = NULL;
|
||||
size_t an = 0, pn = 0;
|
||||
AcquireSRWLockExclusive(&RC.lock); // no append meanwhile
|
||||
AcquireSRWLockExclusive(&rec_io); // no append meanwhile
|
||||
rec_read(RC.ans_path, "TOTKSANS", ANS_VERSION, ANS_REC, &ab, &an);
|
||||
ReleaseSRWLockExclusive(&RC.lock);
|
||||
ReleaseSRWLockExclusive(&rec_io);
|
||||
rec_read(prp, "TOTKSPRP", PRP_VERSION, PRP_REC, &pb, &pn);
|
||||
size_t na = an / ANS_REC, np = pn / PRP_REC, n = 0;
|
||||
GfxWarmEnt *e = calloc(na + np + 1, sizeof *e);
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "cfg.h"
|
||||
#include "flight.h"
|
||||
#include "fs.h"
|
||||
#include "gfx.h"
|
||||
#include "input.h"
|
||||
#include "nvn_api.h"
|
||||
@@ -392,8 +393,9 @@ static LRESULT CALLBACK wnd_proc(HWND h, UINT m, WPARAM w, LPARAM l) {
|
||||
return 0;
|
||||
}
|
||||
case WM_CLOSE:
|
||||
// The process ends with the window, as in MNG. Save writes go straight to files (runtime/fs_host.c).
|
||||
// The process ends with the window, as in MNG, once a save Commit still running in the background is done.
|
||||
rt_log("gfx: window closed; exiting");
|
||||
fs_save_flush();
|
||||
vk_pipeline_save();
|
||||
prof_write(); // ExitProcess runs no atexit handlers
|
||||
ExitProcess(0);
|
||||
|
||||
@@ -237,6 +237,7 @@ void *rt_patch_page(void); // the page the patch
|
||||
void frame_step_init(void); // after rt_patches_init: the real-time step's first value
|
||||
int frame_step_realtime(void); // the real-time step is on: presents ignore the interval
|
||||
uint64_t frame_step_cap_ns(void); // with the real-time step: the shortest frame in ns
|
||||
int64_t frame_step_world_us(void); // game time since the loaded save's first frame, -1 before
|
||||
// (TOTK_FPS_LIMIT, live)
|
||||
void frame_step_present(uint64_t display_ns, int interval); // nvnQueuePresentTexture: the frame's display time and
|
||||
// present interval (60 Hz ticks)
|
||||
|
||||
+51
-27
@@ -41,7 +41,11 @@ static const SDL_GamepadButton pad_sdl[PAD_COUNT] = {
|
||||
};
|
||||
|
||||
// TOTK_INPUT_SCRIPT=<file>: lines "<frame> <button> [hold]" or "<frame> LSTICK|RSTICK <x> <y> [hold]", ORed into the real input.
|
||||
typedef struct { uint64_t frame, hold; int bit, stick; int32_t x, y; int logged; char text[48]; } ScriptEv;
|
||||
// A line "@<ms> ..." is timed instead: <ms> and its hold are milliseconds of the world clock (frame_step_world_us, game
|
||||
// time since the loaded save's first frame), so it lands at the same game time at any frame rate and load length. A
|
||||
// timed event shorter than a frame is applied once.
|
||||
// at, hold: presents, or microseconds of the world clock when timed.
|
||||
typedef struct { uint64_t at, hold; int bit, stick, timed; int32_t x, y; int logged; char text[48]; } ScriptEv;
|
||||
static const struct { const char *name; int bit; } script_buttons[] = {
|
||||
{"A", B_A}, {"B", B_B}, {"X", B_X}, {"Y", B_Y}, {"LCLICK", B_STICK_L}, {"RCLICK", B_STICK_R}, {"L", B_L}, {"R", B_R},
|
||||
{"ZL", B_ZL}, {"ZR", B_ZR}, {"PLUS", B_PLUS}, {"MINUS", B_MINUS}, {"LEFT", B_LEFT}, {"UP", B_UP}, {"RIGHT", B_RIGHT}, {"DOWN", B_DOWN},
|
||||
@@ -62,22 +66,25 @@ static void script_load(void) {
|
||||
char *h = strchr(line, '#');
|
||||
if (h) *h = 0;
|
||||
char name[32];
|
||||
unsigned long long fr;
|
||||
int off = 0;
|
||||
if (sscanf(line, "%llu %31s%n", &fr, name, &off) < 2) continue;
|
||||
ScriptEv e = {.frame = fr, .hold = 4, .bit = -1};
|
||||
long a, b, c;
|
||||
double at;
|
||||
int off = 0, timed = line[0] == '@';
|
||||
if (sscanf(line + timed, "%lf %31s%n", &at, name, &off) < 2 || at < 0) continue;
|
||||
off += timed;
|
||||
double unit = timed ? 1000.0 : 1.0; // ms to us, or presents
|
||||
ScriptEv e = {.at = (uint64_t)(at * unit + 0.5), .hold = timed ? 66667 : 4, .bit = -1, .timed = timed};
|
||||
long a, b;
|
||||
double c;
|
||||
if (!strcmp(name, "LSTICK") || !strcmp(name, "RSTICK")) {
|
||||
int n = sscanf(line + off, "%ld %ld %ld", &a, &b, &c);
|
||||
int n = sscanf(line + off, "%ld %ld %lf", &a, &b, &c);
|
||||
if (n < 2) { rt_log("input: script line %d: %s needs x y [hold]", ln, name); continue; }
|
||||
e.stick = name[0] == 'L' ? 1 : 2; e.x = (int32_t)a; e.y = (int32_t)b;
|
||||
if (n >= 3) e.hold = (uint64_t)c;
|
||||
if (n >= 3 && c > 0) e.hold = (uint64_t)(c * unit + 0.5);
|
||||
snprintf(e.text, sizeof e.text, "%s %d %d", name, e.x, e.y);
|
||||
} else {
|
||||
for (size_t i = 0; i < sizeof script_buttons / sizeof *script_buttons; i++)
|
||||
if (!strcmp(name, script_buttons[i].name)) e.bit = script_buttons[i].bit;
|
||||
if (e.bit < 0) { rt_log("input: script line %d: unknown button %s", ln, name); continue; }
|
||||
if (sscanf(line + off, "%ld", &a) == 1) e.hold = (uint64_t)a;
|
||||
if (sscanf(line + off, "%lf", &c) == 1 && c > 0) e.hold = (uint64_t)(c * unit + 0.5);
|
||||
snprintf(e.text, sizeof e.text, "%s", name);
|
||||
}
|
||||
if (script_n == cap) {
|
||||
@@ -95,10 +102,17 @@ static void script_apply(uint64_t *b, int32_t sl[2], int32_t sr[2]) {
|
||||
if (!script_state) script_load();
|
||||
if (!script_n) return;
|
||||
uint64_t fr = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
int64_t w = frame_step_world_us();
|
||||
for (int i = 0; i < script_n; i++) {
|
||||
ScriptEv *e = &script_ev[i];
|
||||
if (fr < e->frame || fr >= e->frame + e->hold) continue;
|
||||
if (!e->logged) { e->logged = 1; rt_log("input: script frame %llu: %s", (unsigned long long)fr, e->text); }
|
||||
if (e->timed && w < 0) continue;
|
||||
uint64_t now = e->timed ? (uint64_t)w : fr;
|
||||
if (now < e->at || (now >= e->at + e->hold && (e->logged || !e->timed))) continue;
|
||||
if (!e->logged) {
|
||||
e->logged = 1;
|
||||
if (e->timed) rt_log("input: script frame %llu at %.1f ms: %s", (unsigned long long)fr, w / 1000.0, e->text);
|
||||
else rt_log("input: script frame %llu: %s", (unsigned long long)fr, e->text);
|
||||
}
|
||||
if (e->stick) { int32_t *s = e->stick == 1 ? sl : sr; s[0] = e->x; s[1] = e->y; }
|
||||
else *b |= 1ull << e->bit;
|
||||
}
|
||||
@@ -106,29 +120,40 @@ static void script_apply(uint64_t *b, int32_t sl[2], int32_t sr[2]) {
|
||||
|
||||
// TOTK_INPUT_RECORD=<file>: writes the merged input as TOTK_INPUT_SCRIPT lines, one per button press and per stick
|
||||
// segment, so a played session replays as a script. A stick segment ends when an axis moves by more than REC_STICK_STEP.
|
||||
// An event that begins on the world clock is written timed ("@<ms>", hold in ms), one before it in presents.
|
||||
enum { REC_STICK_STEP = 2048 };
|
||||
static FILE *rec;
|
||||
static int rec_state; // 0 not opened, 1 recording, -1 off
|
||||
static uint64_t rec_down[16]; // frame + 1 at which each button went down, 0 while up
|
||||
static int64_t rec_down_w[16]; // the world clock then, -1 before it started
|
||||
static uint64_t rec_stick_from[2]; // frame + 1 at which the current non-zero stick segment began
|
||||
static int64_t rec_stick_w[2];
|
||||
static int32_t rec_stick[2][2];
|
||||
|
||||
static void rec_close_stick(int s, uint64_t fr) {
|
||||
// "<frame> <what> <hold>" or "@<ms> <what> <hold ms>": from frame from and world clock from_w to now.
|
||||
static void rec_line(uint64_t from, int64_t from_w, uint64_t fr, int64_t w, const char *what) {
|
||||
if (from_w >= 0 && w >= from_w)
|
||||
fprintf(rec, "@%.1f %s %.1f\n", from_w / 1000.0, what, (w > from_w ? w - from_w : 1000) / 1000.0);
|
||||
else
|
||||
fprintf(rec, "%llu %s %llu\n", (unsigned long long)from, what, (unsigned long long)(fr > from ? fr - from : 1));
|
||||
}
|
||||
|
||||
static void rec_close_stick(int s, uint64_t fr, int64_t w) {
|
||||
if (!rec_stick_from[s]) return;
|
||||
uint64_t from = rec_stick_from[s] - 1;
|
||||
fprintf(rec, "%llu %s %d %d %llu\n", (unsigned long long)from, s ? "RSTICK" : "LSTICK", rec_stick[s][0],
|
||||
rec_stick[s][1], (unsigned long long)(fr > from ? fr - from : 1));
|
||||
char what[48];
|
||||
snprintf(what, sizeof what, "%s %d %d", s ? "RSTICK" : "LSTICK", rec_stick[s][0], rec_stick[s][1]);
|
||||
rec_line(rec_stick_from[s] - 1, rec_stick_w[s], fr, w, what);
|
||||
rec_stick_from[s] = 0;
|
||||
}
|
||||
|
||||
static void rec_flush_all(void) {
|
||||
if (rec_state != 1) return;
|
||||
uint64_t fr = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
uint64_t fr = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1;
|
||||
int64_t w = frame_step_world_us();
|
||||
for (int i = 0; i < 16; i++)
|
||||
if (rec_down[i]) { fprintf(rec, "%llu %s %llu\n", (unsigned long long)(rec_down[i] - 1), script_buttons[i].name,
|
||||
(unsigned long long)(fr - (rec_down[i] - 1) + 1)); rec_down[i] = 0; }
|
||||
rec_close_stick(0, fr);
|
||||
rec_close_stick(1, fr);
|
||||
if (rec_down[i]) { rec_line(rec_down[i] - 1, rec_down_w[i], fr, w, script_buttons[i].name); rec_down[i] = 0; }
|
||||
rec_close_stick(0, fr, w);
|
||||
rec_close_stick(1, fr, w);
|
||||
fflush(rec);
|
||||
}
|
||||
|
||||
@@ -138,19 +163,18 @@ static void rec_apply(uint64_t b, const int32_t sl[2], const int32_t sr[2]) {
|
||||
rec = path && *path ? fopen(path, "w") : NULL;
|
||||
rec_state = rec ? 1 : -1;
|
||||
if (path && *path && !rec) rt_log("input: cannot open TOTK_INPUT_RECORD %s", path);
|
||||
if (rec) { fprintf(rec, "# Recorded input (TOTK_INPUT_RECORD); frame = presents.\n"); atexit(rec_flush_all);
|
||||
if (rec) { fprintf(rec, "# Recorded input (TOTK_INPUT_RECORD); frame = presents, @ms = world clock.\n"); atexit(rec_flush_all);
|
||||
rt_log("input: recording to %s", path); }
|
||||
}
|
||||
if (rec_state != 1) return;
|
||||
uint64_t fr = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED);
|
||||
int64_t w = frame_step_world_us();
|
||||
int wrote = 0;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
int bit = script_buttons[i].bit, down = (b >> bit & 1) != 0;
|
||||
if (down && !rec_down[i]) rec_down[i] = fr + 1;
|
||||
if (down && !rec_down[i]) rec_down[i] = fr + 1, rec_down_w[i] = w;
|
||||
else if (!down && rec_down[i]) {
|
||||
uint64_t from = rec_down[i] - 1;
|
||||
fprintf(rec, "%llu %s %llu\n", (unsigned long long)from, script_buttons[i].name,
|
||||
(unsigned long long)(fr > from ? fr - from : 1));
|
||||
rec_line(rec_down[i] - 1, rec_down_w[i], fr, w, script_buttons[i].name);
|
||||
rec_down[i] = 0;
|
||||
wrote = 1;
|
||||
}
|
||||
@@ -160,9 +184,9 @@ static void rec_apply(uint64_t b, const int32_t sl[2], const int32_t sr[2]) {
|
||||
int moved = abs(st[s][0] - rec_stick[s][0]) > REC_STICK_STEP || abs(st[s][1] - rec_stick[s][1]) > REC_STICK_STEP ||
|
||||
((st[s][0] || st[s][1]) != (rec_stick[s][0] || rec_stick[s][1]));
|
||||
if (!moved) continue;
|
||||
if (rec_stick_from[s]) { rec_close_stick(s, fr); wrote = 1; }
|
||||
if (rec_stick_from[s]) { rec_close_stick(s, fr, w); wrote = 1; }
|
||||
rec_stick[s][0] = st[s][0]; rec_stick[s][1] = st[s][1];
|
||||
if (st[s][0] || st[s][1]) rec_stick_from[s] = fr + 1;
|
||||
if (st[s][0] || st[s][1]) rec_stick_from[s] = fr + 1, rec_stick_w[s] = w;
|
||||
}
|
||||
if (wrote) fflush(rec);
|
||||
}
|
||||
|
||||
+46
-1
@@ -89,6 +89,8 @@ struct RtThread {
|
||||
uint64_t st_since, st_ns[S_N], nblk, npre, pre_since, cpu_last;
|
||||
uint64_t wobj; // what the current wait is on: owner handle, key, address or first handle
|
||||
uint32_t waker; // the thread whose signal ended the last wait (0: a host thread or a timeout)
|
||||
uint64_t wake_at, grant_at; // when a wait was signalled and a core granted, until the thread runs again
|
||||
uint64_t lw_ns, lw_n, lw_max, lg_ns, lg_n, lg_max, sl_n, sl_req, sl_act; // those latencies; SleepThread asked/took
|
||||
RtThread *target; // as a core waiter: the owner core_kick asked to give its core up for it (holding G)
|
||||
// TOTK_ASYNC_PREEMPT (holding G)
|
||||
HANDLE hthr; // the host thread, for SuspendThread / Get/SetThreadContext; open while it may own a core
|
||||
@@ -214,6 +216,7 @@ static void wake(RtThread *t, uint32_t res) {
|
||||
t->woke = 1;
|
||||
t->wres = res;
|
||||
t->waker = tcur ? tcur->handle : 0;
|
||||
if (rt_trace & RT_TRACE_SCHED) t->wake_at = kern_now_ns();
|
||||
WakeConditionVariable(&t->cv);
|
||||
}
|
||||
|
||||
@@ -255,6 +258,13 @@ static uint32_t block_inner(RtThread *t, int kind, int64_t ns) {
|
||||
uint64_t ms = (deadline - now + 999999) / 1000000;
|
||||
SleepConditionVariableSRW(&t->cv, &G, ms > 0xFFFFFFF0u ? 0xFFFFFFF0u : (DWORD)ms, 0);
|
||||
}
|
||||
if (t->wake_at) {
|
||||
uint64_t d = kern_now_ns() - t->wake_at;
|
||||
t->lw_ns += d;
|
||||
t->lw_n++;
|
||||
if (d > t->lw_max) t->lw_max = d;
|
||||
t->wake_at = 0;
|
||||
}
|
||||
return t->wres;
|
||||
}
|
||||
|
||||
@@ -747,6 +757,7 @@ static void core_grant(int c) {
|
||||
g_core_owner[c] = b;
|
||||
core_count();
|
||||
if (g_diag > 0) b->dg_rel = g_dg_rel;
|
||||
if (rt_trace & RT_TRACE_SCHED) b->grant_at = kern_now_ns();
|
||||
WakeConditionVariable(&b->cv);
|
||||
}
|
||||
|
||||
@@ -873,6 +884,13 @@ static void core_take(RtThread *t) {
|
||||
}
|
||||
}
|
||||
if (g_diag > 0) dg_done(t);
|
||||
if (t->grant_at) {
|
||||
uint64_t d = kern_now_ns() - t->grant_at;
|
||||
t->lg_ns += d;
|
||||
t->lg_n++;
|
||||
if (d > t->lg_max) t->lg_max = d;
|
||||
t->grant_at = 0;
|
||||
}
|
||||
t->target = NULL;
|
||||
acct(t, S_RUN);
|
||||
}
|
||||
@@ -988,7 +1006,11 @@ void kern_core_stats_reset(void) {
|
||||
#define REP_N 8
|
||||
void kern_sched_report(uint64_t frame) {
|
||||
static uint64_t last;
|
||||
typedef struct { uint32_t h; int prio, render; uint64_t mask, entry, ns[S_N], nblk, npre, cpu; } Row;
|
||||
typedef struct {
|
||||
uint32_t h;
|
||||
int prio, render;
|
||||
uint64_t mask, entry, ns[S_N], nblk, npre, cpu, lw_ns, lw_n, lw_max, lg_ns, lg_n, lg_max, sl_n, sl_req, sl_act;
|
||||
} Row;
|
||||
Row rows[REP_N + 1];
|
||||
WaitRec rw[2][4] = {0}; // the render and the main thread's longest waits
|
||||
int nr = 0;
|
||||
@@ -1036,6 +1058,9 @@ void kern_sched_report(uint64_t frame) {
|
||||
memcpy(r->ns, t->st_ns, sizeof r->ns);
|
||||
r->nblk = t->nblk;
|
||||
r->npre = t->npre;
|
||||
r->lw_ns = t->lw_ns, r->lw_n = t->lw_n, r->lw_max = t->lw_max;
|
||||
r->lg_ns = t->lg_ns, r->lg_n = t->lg_n, r->lg_max = t->lg_max;
|
||||
r->sl_n = t->sl_n, r->sl_req = t->sl_req, r->sl_act = t->sl_act;
|
||||
r->cpu = 0;
|
||||
HANDLE th = t->tid ? OpenThread(THREAD_QUERY_LIMITED_INFORMATION, FALSE, t->tid) : NULL;
|
||||
FILETIME a, b, k, u;
|
||||
@@ -1062,6 +1087,7 @@ void kern_sched_report(uint64_t frame) {
|
||||
for (RtThread *t = g_all; t; t = t->all) {
|
||||
memset(t->st_ns, 0, sizeof t->st_ns);
|
||||
t->nblk = t->npre = 0;
|
||||
t->lw_ns = t->lw_n = t->lw_max = t->lg_ns = t->lg_n = t->lg_max = t->sl_n = t->sl_req = t->sl_act = 0;
|
||||
}
|
||||
memset(g_rw, 0, sizeof g_rw);
|
||||
memset(g_mw, 0, sizeof g_mw);
|
||||
@@ -1084,6 +1110,11 @@ void kern_sched_report(uint64_t frame) {
|
||||
rt_log("sched: %s%#x p%d m%llx%s cpu %.0f%% blocks %llu preempted %llu (%s)", r->render ? "render " : "",
|
||||
r->h, r->prio, (unsigned long long)r->mask, s, 100.0 * r->cpu / span, (unsigned long long)r->nblk,
|
||||
(unsigned long long)r->npre, r->entry ? d : "main");
|
||||
rt_log("sched: %#x wake to run avg %.1f us max %.0f us (%llu), core grant to run avg %.1f us max %.0f us "
|
||||
"(%llu), sleep %llu asked avg %.1f us took avg %.1f us", r->h, r->lw_n ? r->lw_ns / 1e3 / r->lw_n : 0.0,
|
||||
r->lw_max / 1e3, (unsigned long long)r->lw_n, r->lg_n ? r->lg_ns / 1e3 / r->lg_n : 0.0, r->lg_max / 1e3,
|
||||
(unsigned long long)r->lg_n, (unsigned long long)r->sl_n, r->sl_n ? r->sl_req / 1e3 / r->sl_n : 0.0,
|
||||
r->sl_n ? r->sl_act / 1e3 / r->sl_n : 0.0);
|
||||
}
|
||||
for (int k = 0; k < 2; k++)
|
||||
for (int j = 0; j < 4 && rw[k][j].ns; j++) {
|
||||
@@ -1300,7 +1331,16 @@ void kern_run_on_guest_main(void (*fn)(Cpu *c)) {
|
||||
CloseHandle(h);
|
||||
}
|
||||
|
||||
// The high-resolution waitable timer ends a wait 0.3-0.5 ms late at best: the SDK's 20 us polling sleeps (main thread
|
||||
// and job workers, about 1.5 per frame each) took 460-515 us. Sleeps up to SLEEP_SPIN_NS spin on the performance
|
||||
// counter instead; the virtual core is already given up.
|
||||
#define SLEEP_SPIN_NS 1000000
|
||||
static void sleep_ns(RtThread *t, int64_t ns) {
|
||||
if (ns <= SLEEP_SPIN_NS) {
|
||||
uint64_t end = kern_now_ns() + (uint64_t)ns;
|
||||
while (kern_now_ns() < end) YieldProcessor();
|
||||
return;
|
||||
}
|
||||
if (!t->timer) {
|
||||
t->timer = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
|
||||
if (!t->timer) t->timer = CreateWaitableTimerExW(NULL, NULL, 0, TIMER_ALL_ACCESS);
|
||||
@@ -1444,8 +1484,13 @@ int kern_svc(Cpu *c, uint32_t imm) {
|
||||
t->wobj = (uint64_t)ns;
|
||||
t->nblk++;
|
||||
unlock();
|
||||
uint64_t t0 = kern_now_ns();
|
||||
sleep_ns(t, ns);
|
||||
uint64_t took = kern_now_ns() - t0;
|
||||
lock();
|
||||
t->sl_n++;
|
||||
t->sl_req += (uint64_t)ns;
|
||||
t->sl_act += took;
|
||||
core_take(t);
|
||||
unlock();
|
||||
return 1;
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
#define MIN_COMMAND_SIZE 512 // NVN_DEVICE_INFO_COMMAND_BUFFER_MIN_COMMAND_SIZE (nvn_api_core.c devinfo)
|
||||
#define MIN_CONTROL_SIZE 256 // NVN_DEVICE_INFO_COMMAND_BUFFER_MIN_CONTROL_SIZE
|
||||
#define CTL_PER_RECORDING 64 // control memory one recording is reported to use
|
||||
#define COPY_PAGE_SHIFT 12
|
||||
#define COPY_PAGE_SHIFT 16 // recording copy index granularity (release_copies)
|
||||
|
||||
static cmd_t *cmd_of(const Cpu *c) { return arg_p(c, 0); }
|
||||
|
||||
@@ -158,8 +158,10 @@ static const tex_t *tex_pin(const NVNtexture *nt) {
|
||||
}
|
||||
|
||||
// ---------- recording copies
|
||||
// A handle points just past a copy_prefix. Copies are indexed by the 4 KiB page of their control-memory position, so
|
||||
// adding control memory again releases the copies recorded there without walking every live copy.
|
||||
// A handle points just past a copy_prefix. Copies are indexed by the 64 KiB block of their control-memory position, so
|
||||
// adding control memory again releases the copies recorded there without walking every live copy. 4 KiB pages cost
|
||||
// about 500 lookups per AddControlMemory under the exclusive lock (1.4.3 field: 53 calls a frame, 2 MiB ranges, 3.7
|
||||
// copies per page), and the recording threads waited on it.
|
||||
typedef struct { uint64_t magic, bytes, made_at, used_at, deferred; } copy_prefix; // deferred: 0 unknown, 1 none, 2 some
|
||||
_Static_assert(sizeof(copy_prefix) % 8 == 0, "records stay 8-byte aligned");
|
||||
typedef struct { uintptr_t lo, hi; uint8_t *copy; } cmd_copy;
|
||||
|
||||
+113
-22
@@ -9,6 +9,13 @@
|
||||
// thread's stack and, after
|
||||
// ResumeThread, unwinds the copy with the .pdata tables (RtlVirtualUnwind), so it takes no lock while the thread is
|
||||
// suspended. The unwind stops at the first frame outside the copy; deeper callers are cut.
|
||||
// TOTK_PROFILE_SLOW=<ms> keeps every thread's chains of the last half second in a ring (a thread outside the exe, mostly
|
||||
// waiting, copies 8 KiB of stack, one in it STK_COPY; a thread that ran no cycle since its previous sample is not
|
||||
// suspended and repeats its previous chain; a sample that took over 2 ms to stop its thread, which happens while the
|
||||
// thread is in a long kernel call, is written as `tstall <tid> <ms into the window> <ms stopping> <rip>...` with the
|
||||
// chain where the thread stopped) and, after a present at least <ms> after the previous one, writes
|
||||
// that interval's samples (plus the 20 ms before) to <TOTK_PROFILE>.f<frame>: the profile format, one `stack 1` line per
|
||||
// sample, and `tstack <tid> <ms into the window> <rip>...` lines that keep the thread and the time. At most 64 dumps.
|
||||
#include <windows.h>
|
||||
#include <psapi.h>
|
||||
#include <tlhelp32.h>
|
||||
@@ -25,14 +32,15 @@
|
||||
#define PROF_SLOTS (1u << 16) // Rip hash table, open addressing; a full table drops new Rips
|
||||
#define PROF_TOP 4000 // more than the 200 asked for: the report sums per function
|
||||
#define PROF_THREADS 512
|
||||
#define PROF_DEPTH 24 // frames kept per call chain, the sampled Rip first
|
||||
#define STK_SLOTS (1u << 15) // call-chain hash table, open addressing; a full table drops new chains
|
||||
#define PROF_STACKS 6000
|
||||
#define STK_COPY (64u << 10) // stack bytes copied per sample
|
||||
#define STK_SLACK (64u << 10) // zeros after the copy: a frame reaching past it reads zeros, and a zero Rip ends the chain
|
||||
|
||||
typedef struct { uint64_t rip, n; } RipCount;
|
||||
typedef struct { DWORD tid; HANDLE h; uint64_t n, in_exe; } ThreadSlot;
|
||||
#define PROF_DEPTH 24 // frames kept per call chain, the sampled Rip first
|
||||
// cycles, depth, last: TOTK_PROFILE_SLOW only, the thread's cycle count and chain at its previous sample
|
||||
typedef struct { DWORD tid; HANDLE h; uint64_t n, in_exe, cycles; uint32_t depth; uint64_t last[PROF_DEPTH]; } ThreadSlot;
|
||||
|
||||
static RipCount table[PROF_SLOTS];
|
||||
typedef struct { uint64_t hash, n; uint32_t depth; uint64_t rip[PROF_DEPTH]; } StackCount;
|
||||
@@ -54,6 +62,18 @@ static int chains; // call chains are kept: TOTK_PROFILE_RENDER
|
||||
static volatile uint64_t cur_frame; // last presented frame
|
||||
static uint64_t frame_lo, frame_hi; // TOTK_PROFILE_FRAMES=<lo>-<hi>: sample only while presents lo..hi-1 are pending
|
||||
|
||||
#define SLOW_RING (1u << 15) // TOTK_PROFILE_SLOW: about 0.5 s of samples of every thread
|
||||
#define SLOW_DUMPS 64
|
||||
typedef struct { uint64_t t; DWORD tid; uint32_t depth; uint64_t rip[PROF_DEPTH]; } Sample;
|
||||
static Sample ring[SLOW_RING];
|
||||
static uint64_t ring_n;
|
||||
#define STALL_RING 1024
|
||||
static Sample stalls[STALL_RING]; // t: when the stop began; rip[PROF_DEPTH - 1] holds the stop's ns
|
||||
static uint64_t stall_n;
|
||||
static uint64_t slow_ns; // TOTK_PROFILE_SLOW in ns, 0 off
|
||||
static volatile LONG64 dump_lo, dump_hi, dump_frame; // the render thread asks for a window, the sampler writes it
|
||||
static volatile DWORD replay_tid; // asynchronous replay (vk_backend.c): its waits hold up the frame too
|
||||
|
||||
static void count_rip(uint64_t rip) {
|
||||
uint32_t i = (uint32_t)((rip * 0x9E3779B97F4A7C15ull) >> 48);
|
||||
for (uint32_t k = 0; k < 64; k++, i = (i + 1) & (PROF_SLOTS - 1)) {
|
||||
@@ -138,6 +158,54 @@ static void refresh_threads(DWORD self) {
|
||||
n_threads = n;
|
||||
}
|
||||
|
||||
static void write_modules(FILE *f) {
|
||||
HMODULE mods[1024];
|
||||
DWORD need = 0;
|
||||
if (!EnumProcessModules(GetCurrentProcess(), mods, sizeof mods, &need)) return;
|
||||
for (DWORD i = 0; i < need / sizeof(HMODULE) && i < 1024; i++) {
|
||||
MODULEINFO mi;
|
||||
char path[MAX_PATH];
|
||||
if (GetModuleInformation(GetCurrentProcess(), mods[i], &mi, sizeof mi) && GetModuleFileNameA(mods[i], path, sizeof path))
|
||||
fprintf(f, "module %llx %lx %s\n", (unsigned long long)(uintptr_t)mi.lpBaseOfDll, mi.SizeOfImage, path);
|
||||
}
|
||||
}
|
||||
|
||||
// The sampler, between ticks: the samples of the window the render thread asked for.
|
||||
static void slow_dump(void) {
|
||||
uint64_t lo = (uint64_t)dump_lo, hi = (uint64_t)dump_hi, frame = (uint64_t)dump_frame;
|
||||
static int dumps;
|
||||
char path[1100];
|
||||
snprintf(path, sizeof path, "%s.f%llu", out_path, (unsigned long long)frame);
|
||||
FILE *f = dumps < SLOW_DUMPS ? fopen(path, "w") : NULL;
|
||||
if (f) {
|
||||
dumps++;
|
||||
fprintf(f, "totk-prof 1\nbase %llx\nperiod_us 1000\nframe %llu interval_ms %.1f\nmain %lu render %lu replay %lu\n",
|
||||
(unsigned long long)(uintptr_t)GetModuleHandleW(NULL), (unsigned long long)frame, (hi - lo) / 1e6 - 20,
|
||||
main_tid, render_tid, replay_tid);
|
||||
write_modules(f);
|
||||
uint64_t first = ring_n > SLOW_RING ? ring_n - SLOW_RING : 0;
|
||||
for (int pass = 0; pass < 2; pass++)
|
||||
for (uint64_t k = first; k < ring_n; k++) {
|
||||
const Sample *s = &ring[k & (SLOW_RING - 1)];
|
||||
if (s->t < lo || s->t > hi) continue;
|
||||
if (pass) fprintf(f, "tstack %lu %.1f", s->tid, (s->t - lo) / 1e6);
|
||||
else fprintf(f, "stack 1");
|
||||
for (uint32_t d = 0; d < s->depth; d++) fprintf(f, " %llx", (unsigned long long)s->rip[d]);
|
||||
fputc('\n', f);
|
||||
}
|
||||
for (uint64_t k = stall_n > STALL_RING ? stall_n - STALL_RING : 0; k < stall_n; k++) {
|
||||
const Sample *s = &stalls[k & (STALL_RING - 1)];
|
||||
if (s->t + s->rip[PROF_DEPTH - 1] < lo || s->t > hi) continue;
|
||||
fprintf(f, "tstall %lu %.1f %.1f", s->tid, ((double)s->t - (double)lo) / 1e6, s->rip[PROF_DEPTH - 1] / 1e6);
|
||||
for (uint32_t d = 0; d < s->depth; d++) fprintf(f, " %llx", (unsigned long long)s->rip[d]);
|
||||
fputc('\n', f);
|
||||
}
|
||||
fclose(f);
|
||||
rt_log("prof: slow frame %llu (%.1f ms) -> %s", (unsigned long long)frame, (hi - lo) / 1e6 - 20, path);
|
||||
}
|
||||
InterlockedExchange64(&dump_hi, 0);
|
||||
}
|
||||
|
||||
static DWORD WINAPI sampler_main(void *arg) {
|
||||
(void)arg;
|
||||
DWORD self = GetCurrentThreadId();
|
||||
@@ -153,27 +221,54 @@ static DWORD WINAPI sampler_main(void *arg) {
|
||||
Sleep(1);
|
||||
}
|
||||
if (tick % 64 == 0) refresh_threads(self);
|
||||
if (dump_hi) slow_dump();
|
||||
if (frame_hi && (cur_frame < frame_lo || cur_frame >= frame_hi)) continue;
|
||||
uint64_t now = slow_ns ? kern_now_ns() : 0;
|
||||
for (int i = 0; i < n_threads; i++) {
|
||||
ThreadSlot *t = &threads[i];
|
||||
if ((only_render && t->tid != render_tid) || (only_main && t->tid != main_tid)) continue;
|
||||
uint64_t cyc = 0;
|
||||
if (slow_ns && t->depth && QueryThreadCycleTime(t->h, &cyc) && cyc == t->cycles) { // still waiting
|
||||
Sample *s = &ring[ring_n++ & (SLOW_RING - 1)];
|
||||
s->t = now, s->tid = t->tid, s->depth = t->depth;
|
||||
memcpy(s->rip, t->last, t->depth * sizeof *t->last);
|
||||
continue;
|
||||
}
|
||||
ctx.ContextFlags = chains ? CONTEXT_CONTROL | CONTEXT_INTEGER : CONTEXT_CONTROL;
|
||||
uint64_t stop0 = slow_ns ? kern_now_ns() : 0;
|
||||
if (SuspendThread(t->h) == (DWORD)-1) continue;
|
||||
BOOL ok = GetThreadContext(t->h, &ctx);
|
||||
BOOL ok = GetThreadContext(t->h, &ctx); // returns once the thread has stopped in user mode
|
||||
uint64_t stop_ns = slow_ns ? kern_now_ns() - stop0 : 0;
|
||||
size_t n = 0;
|
||||
MEMORY_BASIC_INFORMATION mb;
|
||||
if (ok && chains && VirtualQuery((void *)ctx.Rsp, &mb, sizeof mb) && mb.State == MEM_COMMIT) {
|
||||
// a thread stopped between its sub rsp and the stack probe has Rsp in the guard page: touching it would fault
|
||||
if (ok && chains && VirtualQuery((void *)ctx.Rsp, &mb, sizeof mb) && mb.State == MEM_COMMIT &&
|
||||
!(mb.Protect & (PAGE_GUARD | PAGE_NOACCESS))) {
|
||||
n = (uintptr_t)mb.BaseAddress + mb.RegionSize - ctx.Rsp; // the committed stack above Rsp
|
||||
if (n > STK_COPY) n = STK_COPY;
|
||||
size_t cap = slow_ns && (ctx.Rip < exe_lo || ctx.Rip >= exe_hi) ? 8u << 10 : STK_COPY;
|
||||
if (n > cap) n = cap;
|
||||
memcpy(stk_buf, (void *)ctx.Rsp, n);
|
||||
}
|
||||
ResumeThread(t->h);
|
||||
if (!ok) continue;
|
||||
count_rip(ctx.Rip);
|
||||
if (n) {
|
||||
uint64_t chain[PROF_DEPTH];
|
||||
uint32_t d = unwind_copy(&ctx, ctx.Rsp, n, chain);
|
||||
if (d) count_stack(chain, d);
|
||||
uint64_t chain[PROF_DEPTH] = {ctx.Rip};
|
||||
uint32_t d = n ? unwind_copy(&ctx, ctx.Rsp, n, chain) : 0;
|
||||
if (d) count_stack(chain, d);
|
||||
if (slow_ns) {
|
||||
Sample *s = &ring[ring_n++ & (SLOW_RING - 1)];
|
||||
s->t = now, s->tid = t->tid, s->depth = d ? d : 1;
|
||||
memcpy(s->rip, chain, s->depth * sizeof *chain);
|
||||
if (stop_ns > 2000000) {
|
||||
Sample *w = &stalls[stall_n++ & (STALL_RING - 1)];
|
||||
*w = *s;
|
||||
w->t = stop0;
|
||||
if (w->depth > PROF_DEPTH - 1) w->depth = PROF_DEPTH - 1;
|
||||
w->rip[PROF_DEPTH - 1] = stop_ns;
|
||||
}
|
||||
QueryThreadCycleTime(t->h, &t->cycles); // after ResumeThread: the cycles the sample itself cost
|
||||
t->depth = s->depth;
|
||||
memcpy(t->last, chain, s->depth * sizeof *chain);
|
||||
}
|
||||
total++;
|
||||
t->n++;
|
||||
@@ -207,16 +302,7 @@ void prof_write(void) {
|
||||
(unsigned long long)total, (unsigned long long)dropped);
|
||||
for (int i = 0; i < n_threads; i++)
|
||||
fprintf(f, "thread %lu %llu %llu\n", threads[i].tid, (unsigned long long)threads[i].n, (unsigned long long)threads[i].in_exe);
|
||||
HMODULE mods[1024];
|
||||
DWORD need = 0;
|
||||
if (EnumProcessModules(GetCurrentProcess(), mods, sizeof mods, &need)) {
|
||||
for (DWORD i = 0; i < need / sizeof(HMODULE) && i < 1024; i++) {
|
||||
MODULEINFO mi;
|
||||
char path[MAX_PATH];
|
||||
if (GetModuleInformation(GetCurrentProcess(), mods[i], &mi, sizeof mi) && GetModuleFileNameA(mods[i], path, sizeof path))
|
||||
fprintf(f, "module %llx %lx %s\n", (unsigned long long)(uintptr_t)mi.lpBaseOfDll, mi.SizeOfImage, path);
|
||||
}
|
||||
}
|
||||
write_modules(f);
|
||||
int cnt = 0;
|
||||
for (uint32_t i = 0; i < PROF_SLOTS; i++)
|
||||
if (table[i].rip) table[cnt++] = table[i];
|
||||
@@ -267,7 +353,9 @@ void prof_start(void) {
|
||||
only_render = cfg_get("TOTK_PROFILE_RENDER") != NULL;
|
||||
only_main = !only_render && cfg_get("TOTK_PROFILE_MAIN") != NULL;
|
||||
const char *ch = cfg_get("TOTK_PROFILE_CHAINS");
|
||||
chains = only_render || only_main || (ch && *ch && strcmp(ch, "0"));
|
||||
const char *sl = cfg_get("TOTK_PROFILE_SLOW");
|
||||
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;
|
||||
MODULEINFO mi;
|
||||
@@ -290,8 +378,6 @@ static uint64_t wait_ns[PERF_N], frame_ns[PERF_N], worst_ns[PERF_N]; // window
|
||||
|
||||
uint64_t perf_begin(void) { return kern_now_ns(); }
|
||||
|
||||
static volatile DWORD replay_tid; // asynchronous replay (vk_backend.c): its waits hold up the frame too
|
||||
|
||||
void perf_replay_thread(void) { replay_tid = GetCurrentThreadId(); }
|
||||
void perf_main_thread(void) { main_tid = GetCurrentThreadId(); }
|
||||
|
||||
@@ -448,6 +534,11 @@ void perf_present(uint64_t frame) {
|
||||
cur_frame = frame;
|
||||
if (rt_trace & RT_TRACE_SCHED) kern_sched_report(frame);
|
||||
uint64_t now = kern_now_ns(), d = last ? now - last : 0;
|
||||
if (slow_ns && last && d >= slow_ns && !dump_hi) {
|
||||
dump_lo = (LONG64)(now - d - 20000000ull);
|
||||
dump_frame = (LONG64)frame;
|
||||
InterlockedExchange64(&dump_hi, (LONG64)now);
|
||||
}
|
||||
uint64_t rep = ft_frame(frame, now, d); // d is 0 on the first present: it only starts the period
|
||||
if (__atomic_load_n(&overlay, __ATOMIC_RELAXED)) // the overlay's only cost while it is off: this branch
|
||||
perfmon_frame(now, d, frame_ns, rep, __atomic_load_n(&counts[PERF_CNT_TEX_UP], __ATOMIC_RELAXED));
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "fs.h"
|
||||
#include "host.h"
|
||||
#include "kernel.h"
|
||||
#include "vk.h"
|
||||
@@ -220,6 +221,7 @@ static void svc_body(Cpu *c, uint32_t imm, uint64_t pc) {
|
||||
// profile saved, logs flushed, exit code 0
|
||||
char at[160];
|
||||
rt_log("guest ExitProcess at %s; exiting", ldr_describe(pc, at, sizeof at));
|
||||
fs_save_flush();
|
||||
vk_pipeline_save();
|
||||
prof_write();
|
||||
fflush(NULL);
|
||||
|
||||
@@ -84,7 +84,7 @@ static DWORD WINAPI present_main(void *arg) {
|
||||
j.at && t1 > j.at ? (t1 - j.at) / 1e6 : 0.0);
|
||||
last = t1;
|
||||
last_queued = j.queued;
|
||||
vk_pipeline_autosave(); // off the render thread, outside the present lock
|
||||
vk_pipeline_autosave(); // starts the autosave thread once
|
||||
AcquireSRWLockExclusive(&pl);
|
||||
}
|
||||
return 0;
|
||||
|
||||
+31
-3
@@ -3009,6 +3009,8 @@ void vk_pipeline_stats(VkPipeStats *s) {
|
||||
ReleaseSRWLockShared(&P.lock);
|
||||
}
|
||||
|
||||
static SRWLOCK save_lock = SRWLOCK_INIT; // one cache save at a time; shutdown takes it to destroy the cache
|
||||
|
||||
void vk_pipeline_save(void) {
|
||||
if (P.ready) keys_flush();
|
||||
if (P.ready && P.gpl) {
|
||||
@@ -3036,7 +3038,13 @@ void vk_pipeline_save(void) {
|
||||
}
|
||||
gfx_shader_record_flush(1);
|
||||
if (!P.ready || !P.cache || !P.cache_path[0]) return;
|
||||
AcquireSRWLockExclusive(&P.lock);
|
||||
// Not under P.lock: the driver synchronises P.cache, and holding P.lock through the copy and the file write (about
|
||||
// 60 ms on the 1.4.3 fight) stopped every pipeline lookup of the replay, a freeze at each 20 s autosave.
|
||||
AcquireSRWLockExclusive(&save_lock);
|
||||
if (!P.cache) { // vk_pipeline_shutdown destroyed it
|
||||
ReleaseSRWLockExclusive(&save_lock);
|
||||
return;
|
||||
}
|
||||
size_t size = 0;
|
||||
void *data = NULL;
|
||||
if (vkGetPipelineCacheData(P.device, P.cache, &size, NULL) == VK_SUCCESS && size && (data = malloc(size)) &&
|
||||
@@ -3044,12 +3052,12 @@ void vk_pipeline_save(void) {
|
||||
if (!plat_file_write_atomic(P.cache_path, data, size)) rt_log("vk_pipeline: cannot write %s", P.cache_path);
|
||||
}
|
||||
free(data);
|
||||
ReleaseSRWLockExclusive(&P.lock);
|
||||
ReleaseSRWLockExclusive(&save_lock);
|
||||
}
|
||||
|
||||
// The game never shuts the device down (boot.c and the window end the process), so the cache is saved while it
|
||||
// runs: at most every 20 s, and only after new pipelines were built. New pipeline keys are appended every 2 s.
|
||||
void vk_pipeline_autosave(void) {
|
||||
static void autosave_once(void) {
|
||||
static uint64_t last_n, last_ms, last_keys;
|
||||
if (!P.ready) return;
|
||||
uint64_t now = GetTickCount64();
|
||||
@@ -3063,6 +3071,24 @@ void vk_pipeline_autosave(void) {
|
||||
vk_pipeline_save();
|
||||
}
|
||||
|
||||
// On its own thread: the save (vkGetPipelineCacheData and the file write, about 60 ms) and the key appends (a flush to
|
||||
// the disk each) ran on the present thread, and the render thread waited for it to take the next frame.
|
||||
static DWORD WINAPI autosave_main(void *arg) {
|
||||
(void)arg;
|
||||
for (;;) {
|
||||
Sleep(500);
|
||||
autosave_once();
|
||||
}
|
||||
}
|
||||
|
||||
void vk_pipeline_autosave(void) {
|
||||
static LONG started;
|
||||
if (!P.ready || InterlockedExchange(&started, 1)) return;
|
||||
HANDLE t = CreateThread(NULL, 0, autosave_main, NULL, 0, NULL);
|
||||
if (t) SetThreadPriority(t, THREAD_PRIORITY_BELOW_NORMAL), CloseHandle(t);
|
||||
else rt_log("vk_pipeline: cannot start the autosave thread; the cache is saved at exit only");
|
||||
}
|
||||
|
||||
void vk_pipeline_shutdown(void) {
|
||||
if (!P.ready) return;
|
||||
lb_stop();
|
||||
@@ -3094,8 +3120,10 @@ void vk_pipeline_shutdown(void) {
|
||||
}
|
||||
layouts[i] = NULL;
|
||||
}
|
||||
AcquireSRWLockExclusive(&save_lock); // not while the autosave thread copies it
|
||||
if (P.cache) vkDestroyPipelineCache(P.device, P.cache, NULL);
|
||||
P.cache = VK_NULL_HANDLE;
|
||||
ReleaseSRWLockExclusive(&save_lock);
|
||||
memset(&P.stats, 0, sizeof P.stats);
|
||||
InterlockedExchange(&P.ready, 0);
|
||||
ReleaseSRWLockExclusive(&P.lock);
|
||||
|
||||
Reference in new issue
Block a user