From 25cd5a178f7fabdacc3f52c737481c8b400064a3 Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:49:54 +0100 Subject: [PATCH 1/9] Leave the sound sync's wait for the audio thread out of the work time With frameskip set to manual or none, synchronize_sound sleeps until the audio thread takes a block, and nothing recorded that sleep, so a paced capture counted it in work-time, frames-work-over-budget and phase-main-update-gba-us. The sleep now goes into the frame's waits as its own site, which no VBlank wait counter shows. --- docs/PERFORMANCE.md | 5 +++-- source/src/perf_metrics.c | 7 ++++++- source/src/perf_metrics.h | 7 +++++-- source/src/sound.c | 6 ++++++ tests/test_perf_metrics.c | 23 +++++++++++++++++++++++ 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/docs/PERFORMANCE.md b/docs/PERFORMANCE.md index f885312..64764c6 100644 --- a/docs/PERFORMANCE.md +++ b/docs/PERFORMANCE.md @@ -62,8 +62,9 @@ for the time per frame: - `phase-main-update-gba-us`: update_gba's own time (scheduler, input, presenting the frame, DMA), without the two phases below and without the - waits the work time leaves out (the VBlank waits and the GE wait). During - a lockstep link it also holds the link waits. + waits the work time leaves out (the VBlank waits, the sound sync's wait + for the audio thread and the GE wait). During a lockstep link it also + holds the link waits. - `phase-main-scanline-us`: update_scanline. - `phase-main-gbc-sound-us`: the PSG channels (update_gbc_sound). Direct sound, hundreds of calls a frame, is not timed. diff --git a/source/src/perf_metrics.c b/source/src/perf_metrics.c index 2c53e58..3d47c37 100644 --- a/source/src/perf_metrics.c +++ b/source/src/perf_metrics.c @@ -396,7 +396,8 @@ void perf_metrics_observe_memory_available(PerfMetrics *metrics, } /* Adds one VBlank wait to the count and total of its site and to the waits - of the current frame; an unknown site is ignored. */ + of the current frame; the audio drain, no VBlank wait, goes only to the + frame's waits, and an unknown site is ignored. */ void perf_metrics_record_wait(PerfMetrics *metrics, PerfMetricsWaitSite site, uint32_t wait_us) { @@ -420,6 +421,10 @@ void perf_metrics_record_wait(PerfMetrics *metrics, count = &metrics->vblank_audio_count; total = &metrics->vblank_audio_total_us; break; + case PERF_METRICS_WAIT_AUDIO_DRAIN: + metrics->frame_wait_us = perf_metrics_saturating_add( + metrics->frame_wait_us, wait_us); + return; default: return; } diff --git a/source/src/perf_metrics.h b/source/src/perf_metrics.h index 4f3810e..8a9c4d2 100644 --- a/source/src/perf_metrics.h +++ b/source/src/perf_metrics.h @@ -74,12 +74,15 @@ typedef struct } PerfMetricsHistogram; /* Where the emulator thread waited for the PSP VBlank: frame pacing in - synchronize(), before presenting a frame, or in the sound sync. */ + synchronize(), before presenting a frame, or in the sound sync. The + last site is no VBlank wait: the sound sync sleeping until the audio + thread takes a block, which only leaves the frame's work time. */ typedef enum { PERF_METRICS_WAIT_BACKPRESSURE, PERF_METRICS_WAIT_PRESENT, - PERF_METRICS_WAIT_AUDIO + PERF_METRICS_WAIT_AUDIO, + PERF_METRICS_WAIT_AUDIO_DRAIN } PerfMetricsWaitSite; /* What a phase timer measured. UPDATE_GBA is the whole call; the scanline diff --git a/source/src/sound.c b/source/src/sound.c index 1acf590..21afff3 100644 --- a/source/src/sound.c +++ b/source/src/sound.c @@ -1098,6 +1098,7 @@ void synchronize_sound(void) u64 started; u32 factor; u32 limit; + int drained = 0; __builtin_allegrex_sync(); if(sound_perf_underrun_pending) @@ -1153,8 +1154,13 @@ void synchronize_sound(void) perf_monitor_record_audio_recovery(); break; } + drained = 1; sceKernelDelayThread(100); } + // The loop slept until the audio thread took a block: not work time. + if(drained && perf_monitor_active()) + perf_monitor_record_wait(PERF_METRICS_WAIT_AUDIO_DRAIN, + (u32)(sceKernelGetSystemTimeWide() - started)); } } } diff --git a/tests/test_perf_metrics.c b/tests/test_perf_metrics.c index 954607b..6cbaf91 100644 --- a/tests/test_perf_metrics.c +++ b/tests/test_perf_metrics.c @@ -332,6 +332,28 @@ static void test_work_time_subtracts_waits(void) assert(result.frame_time_total_us == 26700); } +/* The sound sync's sleep until the audio thread takes a block leaves the + frame's work time like a wait, but it is no VBlank wait: no site counts + it, and the frame still counts as late. */ +static void test_audio_drain_leaves_work_only(void) +{ + PerfMetrics metrics; + PerfMetricsSnapshot result; + + perf_metrics_init(&metrics); + assert(perf_metrics_start(&metrics)); + perf_metrics_record_wait(&metrics, PERF_METRICS_WAIT_AUDIO, 500); + perf_metrics_record_wait(&metrics, PERF_METRICS_WAIT_AUDIO_DRAIN, 8000); + perf_metrics_record_frame(&metrics, 16700, 200, 0, 0); + result = snapshot(&metrics); + assert(result.work_time_total_us == 16700 - 500 - 8000 - 200); + assert(result.vblank_audio_count == 1); + assert(result.vblank_audio_total_us == 500); + assert(result.vblank_backpressure_count == 0); + assert(result.vblank_present_count == 0); + assert(result.frames_late == 1); +} + /* Work percentiles come from the work histogram in 250 us buckets: 18 frames of 8100 us work, one of 12100 and one of 20000 give p50 8250, p95 12250 and p99 20000, while the frame-time p95 is 16750. */ @@ -1050,6 +1072,7 @@ int main(void) test_saturating_counters(); test_wait_and_frame_probes(); test_work_time_subtracts_waits(); + test_audio_drain_leaves_work_only(); test_work_time_percentiles(); test_work_time_clamps_at_zero(); test_waits_reset_each_frame(); From e421190b8f9b7cba385c32d6c34905c3060d3d01 Mon Sep 17 00:00:00 2001 From: redcode9 <33084859+redcode9@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:51:49 +0100 Subject: [PATCH 2/9] Skip the -alloc.txt traces when compare-benchmarks reads a directory Every diagnostic build writes perf-