mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-19 12:59:58 -04:00
Write the flight dump and the memory map through a temporary file and a rename.
The window's close ends the process under the detached threads, so a dump file the flight recorder's thread was writing stayed cut short under its own name. Its three files are now written as .part and renamed when complete (runtime/part_file.h, with a check that kills a writer in the middle), and the allocator's map goes through plat_file_write_cached. The shader identities and the other cache files already went through an atomic write. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include "gfx.h"
|
||||
#include "host.h"
|
||||
#include "input.h"
|
||||
#include "part_file.h"
|
||||
#include "vk.h"
|
||||
#include "vk_exec.h"
|
||||
#include "platform/platform.h"
|
||||
@@ -200,11 +201,12 @@ static void copy_events(DumpJob *j, uint64_t first) {
|
||||
static const char *const tex_kind_names[FLIGHT_TEX_N] = {"new", "up", "reup", "release", "takeback", "drop", "bytes",
|
||||
"reg", "blank", "toblank", "blanksub", "direct", "unreg"};
|
||||
|
||||
// <dir>/events.tsv: one line per texture event of the dump's frames (flight_tex).
|
||||
// <dir>/events.tsv: one line per texture event of the dump's frames (flight_tex). Like the dump's other files it is
|
||||
// written as a part file and renamed when complete (part_file.h): the game may close under the dump thread.
|
||||
static void write_events(const DumpJob *j) {
|
||||
char path[PLAT_PATH_MAX + 32];
|
||||
snprintf(path, sizeof path, "%s/events.tsv", j->dir);
|
||||
FILE *f = fopen(path, "w");
|
||||
FILE *f = part_file_open(path, "w");
|
||||
if (!f) {
|
||||
rt_log("flight: cannot write %s", path);
|
||||
return;
|
||||
@@ -253,7 +255,7 @@ static void write_events(const DumpJob *j) {
|
||||
e->kind < FLIGHT_TEX_N ? tex_kind_names[e->kind] : "?", (unsigned long long)e->addr, e->format, e->w,
|
||||
e->h, e->layers, fl, detail, e->us);
|
||||
}
|
||||
fclose(f);
|
||||
if (!part_file_close(f, path, 1)) rt_log("flight: cannot write %s", path);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- PNG (RGB, Paeth rows, fixed-Huffman deflate)
|
||||
@@ -369,7 +371,7 @@ static int write_png(const char *path, const uint8_t *rgba, PngBufs *pb) {
|
||||
}
|
||||
}
|
||||
size_t zn = deflate_fixed(pb->raw, (size_t)row * h, pb->z, pb->head);
|
||||
FILE *f = fopen(path, "wb");
|
||||
FILE *f = part_file_open(path, "wb");
|
||||
if (!f) return 0;
|
||||
static const uint8_t sig[8] = {0x89, 'P', 'N', 'G', 13, 10, 26, 10};
|
||||
uint8_t ihdr[13] = {0, 0, (uint8_t)(w >> 8), (uint8_t)w, 0, 0, (uint8_t)(h >> 8), (uint8_t)h, 8, 2, 0, 0, 0};
|
||||
@@ -377,7 +379,7 @@ static int write_png(const char *path, const uint8_t *rgba, PngBufs *pb) {
|
||||
chunk(f, "IHDR", ihdr, 13);
|
||||
chunk(f, "IDAT", pb->z, (uint32_t)zn);
|
||||
chunk(f, "IEND", NULL, 0);
|
||||
return fclose(f) == 0;
|
||||
return part_file_close(f, path, 1);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- dump thread
|
||||
@@ -419,7 +421,7 @@ static void dump_main(void *arg) {
|
||||
}
|
||||
char path[PLAT_PATH_MAX + 32];
|
||||
snprintf(path, sizeof path, "%s/frames.tsv", j->dir);
|
||||
FILE *f = fopen(path, "w");
|
||||
FILE *f = part_file_open(path, "w");
|
||||
if (!f) {
|
||||
rt_log("flight: cannot write %s", path);
|
||||
free(have), free(j->ev), free(j);
|
||||
@@ -439,7 +441,7 @@ static void dump_main(void *arg) {
|
||||
fprintf(f, "\t%.4f\t%.4f\t%llu", r->step_page, r->step_game, (unsigned long long)r->guest_presents);
|
||||
fprintf(f, "\t%d\n", have ? have[i] : 0);
|
||||
}
|
||||
fclose(f);
|
||||
if (!part_file_close(f, path, 1)) rt_log("flight: cannot write %s", path);
|
||||
rt_log("flight: dump %s: %s at frame %llu (%.1f ms), frames %llu-%llu, %d thumbnails, %zu texture events, written "
|
||||
"in %llu ms; recorder %.1f us per frame (max %.0f us)", j->dir,
|
||||
j->why, (unsigned long long)j->trig_frame, j->trig_interval_us / 1000.0, (unsigned long long)j->r[0].frame,
|
||||
|
||||
41
runtime/part_file.h
Normal file
41
runtime/part_file.h
Normal file
@@ -0,0 +1,41 @@
|
||||
// A file written piece by piece on a thread nobody waits for when the process ends.
|
||||
//
|
||||
// The window's close ends the process with plat_exit while the detached threads still run (gfx_present.c
|
||||
// gfx_window_closed), and a kill or a crash ends it anywhere. A file such a thread was in the middle of then stays
|
||||
// cut short under its own name, and whoever opens it later takes it for a whole one. So the file is written as
|
||||
// <path>.part and gets its name with one rename once it is complete: a reader finds the whole file or none, and a
|
||||
// .part file says what it is. A file made in memory first goes through plat_file_write_cached or
|
||||
// plat_file_write_atomic instead, which do the same in one call.
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#define PART_FILE_SUFFIX ".part"
|
||||
|
||||
// <path>.part into out; 0 when it does not fit.
|
||||
static inline int part_file_name(const char *path, char *out, size_t cap) {
|
||||
int n = snprintf(out, cap, "%s" PART_FILE_SUFFIX, path);
|
||||
return n > 0 && (size_t)n < cap;
|
||||
}
|
||||
|
||||
// Opens <path>.part for writing (mode: "w" or "wb"). NULL when it cannot be made.
|
||||
static inline FILE *part_file_open(const char *path, const char *mode) {
|
||||
char part[PLAT_PATH_MAX + 64];
|
||||
return part_file_name(path, part, sizeof part) ? fopen(part, mode) : NULL;
|
||||
}
|
||||
|
||||
// Closes f, the file part_file_open(path) gave, and renames it to path, over an older file of that name. With ok 0,
|
||||
// after a write error on f or when the close or the rename fails, the part is deleted and path stays as it was.
|
||||
// 1 when path now holds the whole new file.
|
||||
static inline int part_file_close(FILE *f, const char *path, int ok) {
|
||||
char part[PLAT_PATH_MAX + 64];
|
||||
if (!f) return 0;
|
||||
ok = ok && !ferror(f);
|
||||
ok = !fclose(f) && ok;
|
||||
if (!part_file_name(path, part, sizeof part)) return 0;
|
||||
if (ok && plat_file_replace(part, path, 0)) return 1;
|
||||
plat_file_delete(part);
|
||||
return 0;
|
||||
}
|
||||
@@ -4497,9 +4497,9 @@ static void census_main(void *arg) {
|
||||
snprintf(path, sizeof path, "%.*svma-frame%llu.json", sl ? (int)(sl - lp) + 1 : 0, lp, (unsigned long long)j.frame);
|
||||
char *json = NULL;
|
||||
vmaBuildStatsString(j.c->vma, &json, VK_TRUE);
|
||||
FILE *f = json ? fopen(path, "wb") : NULL;
|
||||
int ok = f && fputs(json, f) >= 0;
|
||||
if (f && fclose(f)) ok = 0;
|
||||
// Through a temporary file and a rename: nobody waits for this thread when the game closes, and a map cut short
|
||||
// there would read as a whole one
|
||||
int ok = json && plat_file_write_cached(path, json, strlen(json));
|
||||
rt_log("mem: vma detailed map %s %s", ok ? "written to" : "not written:", path);
|
||||
if (json) vmaFreeStatsString(j.c->vma, json);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user