Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,15 @@ test-output-scale: $(B)/test_output_scale

.PHONY: test-output-scale

$(B)/test_benchmark: tests/test_benchmark.c runtime/benchmark.c runtime/benchmark.h
@mkdir -p $(B)
$(CC) -O1 $(OSFLAGS) -Iruntime -o $@ $< -lm

test-benchmark: $(B)/test_benchmark
$(B)/test_benchmark

.PHONY: test-benchmark

$(B)/test_window_size: tests/test_window_size.c runtime/window_size.c $(RT_HDRS)
@mkdir -p $(B)
$(CC) -O1 -DSDL_STATIC_LIB $(OSFLAGS) $(DEPINC) -Iruntime -o $@ tests/test_window_size.c runtime/window_size.c -lSDL3 -lpthread -lm
Expand Down
818 changes: 818 additions & 0 deletions runtime/benchmark.c

Large diffs are not rendered by default.

121 changes: 121 additions & 0 deletions runtime/benchmark.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// In-game fixed-view performance benchmark (runtime/benchmark.c, runtime/ui/benchmark_ui.mm).
// Measures the canonical scenes from tools/perf_routes.py (or the player's current spot) directly from the F1 menu:
// teleport -> hold sky/fly -> aim camera -> warm up -> sample per-frame deltas -> compute percentiles/lows/hitches -> restore.
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define BENCH_ROUTE_COUNT 7
#define BENCH_MAX_RESULTS 8
#define BENCH_SPARK_LEN 120
#define BENCH_MAX_FRAMES 32768

enum BenchMode {
BENCH_MODE_FULL = 0, // all 7 canonical routes
BENCH_MODE_QUICK = 1, // Plateau, Kakariko, Korok Forest
BENCH_MODE_SINGLE = 2, // one chosen canonical route
BENCH_MODE_SPOT = 3, // Link's current position and view (no teleport)
};

typedef enum {
BENCH_IDLE = 0,
BENCH_TELEPORTING,
BENCH_AIMING,
BENCH_SETTLING,
BENCH_MEASURING,
BENCH_RESTORING,
BENCH_DONE,
BENCH_CANCELLED,
} BenchPhase;

typedef struct {
int mode; // BenchMode
int single_route; // 0..BENCH_ROUTE_COUNT-1 (BENCH_MODE_SINGLE)
int duration_s; // sample window per route (seconds)
int settle_s; // warm-up settle window before sampling (seconds)
bool uncap_fps; // uncap fps_limit, vsync, frame_gen and perf_overlay during the run
bool camera_pan; // gently sweep camera yaw during measurement on canonical routes
} BenchConfig;

typedef struct {
const char *id;
const char *name;
float x, y, z;
double yaw, pitch;
float hour;
bool hover;
} BenchRouteInfo;

typedef struct {
char id[32];
char name[64];
bool valid;
bool failed;
char fail_reason[96];
uint32_t frames;
double duration_s;
double avg_fps;
double low_1pct_fps;
double low_01pct_fps;
double avg_ms;
double median_ms;
double p95_ms;
double p99_ms;
double min_ms;
double max_ms;
uint32_t hitches;
uint32_t hist[4]; // frame-time bins: <8.33ms (>=120fps), 8.33-16.67ms, 16.67-33.33ms, >33.33ms (<30fps)
float spark_ms[BENCH_SPARK_LEN];
int spark_n;
float vram_peak_mb;
float ram_peak_mb;
float gpu_load_avg;
float gpu_temp_peak;
float gpu_clock_avg;
} BenchRouteStats;

typedef struct {
BenchPhase phase;
BenchConfig cfg;
int route_idx; // 0-based index in the current suite
int route_total;
char current_route_name[64];
double phase_elapsed_s;
double phase_target_s;
float live_fps;
float live_ms;
float live_spark[BENCH_SPARK_LEN];
int live_spark_n;
bool has_results;
int nresults;
BenchRouteStats results[BENCH_MAX_RESULTS];
BenchRouteStats overall;
char gpu_name[96];
char cpu_name[64];
int width, height;
char settings_summary[256];
} BenchSnapshot;

int bench_route_count(void);
const BenchRouteInfo *bench_route_info(int i);

bool bench_start(const BenchConfig *cfg);
void bench_cancel(void);
bool bench_active(void);
bool bench_hud_visible(void);
void bench_snapshot(BenchSnapshot *out);
void bench_frame(uint64_t frame);
void bench_control_init(void);

void bench_compute_stats(const uint32_t *frame_us, uint32_t count, double duration_s, BenchRouteStats *out);
size_t bench_format_markdown(const BenchSnapshot *snap, char *buf, size_t n);
bool bench_save_csv(const BenchSnapshot *snap, char *out_path, size_t out_path_n);

#ifdef __cplusplus
}
#endif
6 changes: 6 additions & 0 deletions runtime/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,15 @@ int control_console_names(const char *prefix, const char **out, int max) {
return n;
}

extern void bench_frame(uint64_t frame);
extern bool bench_active(void);
extern void control_tp_frame(uint64_t frame);

void control_frame(uint64_t frame) {
if (!initialized) return;
control_place_frame();
bench_frame(frame);
if (!dev_mode && bench_active()) control_tp_frame(frame);
if (dev_mode) control_game_frame(frame);
console_pump();
if (!queued) return;
Expand Down
2 changes: 2 additions & 0 deletions runtime/control_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sys/stat.h>

#include "control.h"
#include "benchmark.h"
#include "game_flags.h"
#include "control_cells.h"
#include "control_places.h"
Expand Down Expand Up @@ -503,6 +504,7 @@ void control_game_init(void) {
};
control_register(player, (int)(sizeof player / sizeof *player));
control_settings_init(); // (get for everyone, set only in developer mode)
bench_control_init();
if (!control_enabled()) return;
static const ControlCmd dev[] = {
{"time", "[freeze | unfreeze | set <0-24> | <0-24>]", "Show or change the hour of the day", cmd_time, 0},
Expand Down
12 changes: 10 additions & 2 deletions runtime/control_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,15 @@ bool control_look_start(bool use_yaw, double yaw, bool use_pitch, double pitch,
return true;
}

bool control_look_active(void) { return look.state == LOOK_RUN; }

// Called even when no script exists. Expiry uses presented frames, never script time or poll count.
void control_input_frame(uint64_t frame) {
if (!atomic_load_explicit(&enabled, memory_order_relaxed)) return;
extern bool bench_active(void);
if (!atomic_load_explicit(&enabled, memory_order_relaxed) && !bench_active()) return;
if (look.state == LOOK_RUN) look_step(frame);
control_fly_frame(frame);
if (!atomic_load_explicit(&enabled, memory_order_relaxed)) return;
uint64_t b = 0;
for (int i = 0; i < 16; i++) if (held[i] || frame < until[i]) b |= 1ull << i;
atomic_store(&buttons, b);
Expand All @@ -233,7 +237,11 @@ void control_input_frame(uint64_t frame) {
}
static int32_t clamp(int32_t v) { return v > 32767 ? 32767 : v < -32767 ? -32767 : v; }
void control_input_apply(uint64_t *b, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) {
if (!atomic_load_explicit(&enabled, memory_order_relaxed)) return;
extern bool bench_active(void);
if (!atomic_load_explicit(&enabled, memory_order_relaxed)) {
if (bench_active()) control_fly_input(b, lx, ly);
return;
}
*b |= atomic_load(&buttons);
if (!lx) { control_fly_input(b, NULL, NULL); return; }
int32_t *x[] = {lx, rx}, *y[] = {ly, ry};
Expand Down
26 changes: 25 additions & 1 deletion runtime/services/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <zlib.h>

#include "../achievements.h"
#include "../benchmark.h"
#include "../game_sites.h"
#include "../hle.h"
#include "../ipc.h"
Expand Down Expand Up @@ -2274,6 +2275,11 @@ static bool flush_later(const char *path) {
return queued;
}

static bool is_save_path(const char *p) {
size_t n = g_save_dir ? strlen(g_save_dir) : 0;
return n && !strncmp(p, g_save_dir, n) && (p[n] == '/' || p[n] == '\\' || !p[n]);
}

// The game closed the file's session: its host descriptor (a Windows handle each) and the copy of the save go.
static void file_close(Service *s) {
HostFile *f = s->state;
Expand Down Expand Up @@ -2308,6 +2314,8 @@ static void file(Service *s, IpcReq *req, IpcResp *resp) {
if (f->patched) { // (test warp)
n = off >= 0 && (uint64_t)off < f->patched_size ? (ssize_t)((uint64_t)size < f->patched_size - (uint64_t)off ? (uint64_t)size : f->patched_size - (uint64_t)off) : 0;
memcpy((void *)(uintptr_t)b->addr, f->patched + off, (size_t)n);
} else if (f->fd < 0) {
n = 0;
} else {
uint64_t ft0 = plat_now_ns();
n = pread(f->fd, (void *)(uintptr_t)b->addr, (size_t)size, off);
Expand All @@ -2322,6 +2330,7 @@ static void file(Service *s, IpcReq *req, IpcResp *resp) {
break;
}
case 1: { // Write(option, offset, size) <- A
if (f->fd < 0 || (bench_active() && is_save_path(f->path))) break;
int64_t off = IN(int64_t, 8), size = IN(int64_t, 16);
IpcBuf *a = in_buf(req, 0);
if (strstr(f->path, "savedata"))
Expand All @@ -2336,10 +2345,12 @@ static void file(Service *s, IpcReq *req, IpcResp *resp) {
break;
}
case 2: // Flush
if (f->written && !flush_later(f->path)) fsync(f->fd); // (its result unread, as before)
if (f->fd >= 0 && f->written && !(bench_active() && is_save_path(f->path)) && !flush_later(f->path))
fsync(f->fd); // (its result unread, as before)
f->written = false;
break;
case 3: {
if (f->fd < 0 || (bench_active() && is_save_path(f->path))) break;
int tries = 0, r;
while ((r = ftruncate(f->fd, IN(int64_t, 0))) && fs_retry(&tries, "resize", f->path)) {}
if (r) resp->result = errno_result();
Expand All @@ -2348,6 +2359,7 @@ static void file(Service *s, IpcReq *req, IpcResp *resp) {
}
case 4: {
struct stat sb;
if (f->fd < 0) { OUT(int64_t, 0) = 0; resp_size(resp, 8); break; }
fstat(f->fd, &sb);
OUT(int64_t, 0) = sb.st_size;
resp_size(resp, 8);
Expand Down Expand Up @@ -2433,6 +2445,18 @@ static void filesystem(Service *s, IpcReq *req, IpcResp *resp) {
LOG("[fs] save: %s %s%s%s\n", req->cmd == 13 ? "clean dir" : what[req->cmd], p,
req->cmd == 5 || req->cmd == 6 ? " -> " : "", req->cmd == 5 || req->cmd == 6 ? p2 : "");
}
if (bench_active() && is_save_path(p) &&
(req->cmd <= 6 || req->cmd == 13 || (req->cmd == 8 && (IN(uint32_t, 0) & 2)))) {
if (req->cmd == 8) {
HostFile *f = calloc(1, sizeof *f);
f->fd = -1;
strlcpy(f->path, p, sizeof f->path);
Service *fsvc = service_new("IFile", file, f);
fsvc->on_close = file_close;
ipc_return_object(resp, fsvc);
}
return;
}
int tries = 0, r;
switch (req->cmd) {
case 0: { // CreateFile(option, size)
Expand Down
13 changes: 13 additions & 0 deletions runtime/ui/benchmark_ui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// In-game benchmark menu page and live progress HUD (runtime/ui/benchmark_ui.mm).
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

void bench_ui_tab(bool *want_close);
void bench_ui_overlay(float display_w, float display_h, float scale);

#ifdef __cplusplus
}
#endif
Loading
Loading