From d45456665f23c134c4bec3a82a2aa90e740b18d9 Mon Sep 17 00:00:00 2001 From: endercodezz Date: Fri, 6 Nov 2026 16:08:05 +0200 Subject: [PATCH] fix(3ds): stop rewriting report.txt during play On a slow SD card a background rewrite took up to 2.6 s and the game's ISO reads waited behind it: freezes every 5 s, 64-74 % speed, while a run on the same card with 78 ms writes had none (#66). The bottom screen still updates every 5 s, but report.txt is now written at the start, on exit and when the bottom screen is touched. An empty report_live.txt restores the write every 5 s for runs that end without an exit. Verified: CTest 32/32; devkitARM build; Azahar autotest of 1,500 vblanks writes the file only at the start and end (2 written), 6 times with report_live.txt. Not verified: the tap in a real run, a slow card on hardware. --- .agents/skills/n3ds-platform/SKILL.md | 2 +- .claude/skills/n3ds-platform/SKILL.md | 2 +- README.md | 6 ++-- docs/CURRENT_STATE.md | 10 +++++++ platform/3ds/main.cpp | 40 ++++++++++++++++++++------- 5 files changed, 45 insertions(+), 15 deletions(-) diff --git a/.agents/skills/n3ds-platform/SKILL.md b/.agents/skills/n3ds-platform/SKILL.md index 19aaa955..eb9791ad 100644 --- a/.agents/skills/n3ds-platform/SKILL.md +++ b/.agents/skills/n3ds-platform/SKILL.md @@ -80,7 +80,7 @@ osSetSpeedupEnable(true); // Boost CPU to 804 MHz and enable L2 cache `/msys2/usr/bin/bash.exe -lc "export DEVKITPRO=/opt/devkitpro DEVKITARM=/opt/devkitpro/devkitARM; cd && make -C build/3ds -j10"` (first time: `cmake -S platform/3ds -B build/3ds -G 'Unix Makefiles' -DCMAKE_TOOLCHAIN_FILE=/opt/devkitpro/cmake/3DS.cmake -DCMAKE_BUILD_TYPE=Release`, plus `-DPython3_EXECUTABLE=` if msys2 has none). - Code size is the constraint (docs/3DS_PLATFORM.md 8.5). Never add a define that changes `Runtime`'s layout to only some targets; a define that changes compile flags of `p3p_aot` rebuilds all 237 units (~9 min) — put runtime-only defines on `psprecomp_runtime`. -- Test in Azahar (New 3DS mode) with the ISO in its virtual `sdmc/p3p3ds/`. Read `sdmc/p3p3ds/report.txt`: `stage`, heap size/in use, and `failed alloc: from `; resolve the address with `arm-none-eabi-addr2line -f -C -i -e build/3ds/p3p3ds.elf `. The maintainer may be running Azahar too: do not kill an instance you did not start, and do not trust a report without checking its build id and timestamp. Screenshot of the Azahar window: `powershell -NoProfile -ExecutionPolicy Bypass -File .claude/skills/n3ds-platform/scripts/azahar_shot.ps1 -Out .tmp/azahar/shot.png`. A crash photo of the Luma exception screen gives PC and LR: resolve both with addr2line on the ELF of exactly that build (`build ` in the report). +- Test in Azahar (New 3DS mode) with the ISO in its virtual `sdmc/p3p3ds/`. A run that is killed never writes its final report, so put an empty `sdmc/p3p3ds/report_live.txt` there first (report every 5 s), or quit with START+SELECT. Read `sdmc/p3p3ds/report.txt`: `stage`, heap size/in use, and `failed alloc: from `; resolve the address with `arm-none-eabi-addr2line -f -C -i -e build/3ds/p3p3ds.elf `. The maintainer may be running Azahar too: do not kill an instance you did not start, and do not trust a report without checking its build id and timestamp. Screenshot of the Azahar window: `powershell -NoProfile -ExecutionPolicy Bypass -File .claude/skills/n3ds-platform/scripts/azahar_shot.ps1 -Out .tmp/azahar/shot.png`. A crash photo of the Luma exception screen gives PC and LR: resolve both with addr2line on the ELF of exactly that build (`build ` in the report). - Never write `sdmc` or emulator paths of the maintainer's machine into tracked files. ## 3b. P3P3DS Builder release diff --git a/.claude/skills/n3ds-platform/SKILL.md b/.claude/skills/n3ds-platform/SKILL.md index 19aaa955..eb9791ad 100644 --- a/.claude/skills/n3ds-platform/SKILL.md +++ b/.claude/skills/n3ds-platform/SKILL.md @@ -80,7 +80,7 @@ osSetSpeedupEnable(true); // Boost CPU to 804 MHz and enable L2 cache `/msys2/usr/bin/bash.exe -lc "export DEVKITPRO=/opt/devkitpro DEVKITARM=/opt/devkitpro/devkitARM; cd && make -C build/3ds -j10"` (first time: `cmake -S platform/3ds -B build/3ds -G 'Unix Makefiles' -DCMAKE_TOOLCHAIN_FILE=/opt/devkitpro/cmake/3DS.cmake -DCMAKE_BUILD_TYPE=Release`, plus `-DPython3_EXECUTABLE=` if msys2 has none). - Code size is the constraint (docs/3DS_PLATFORM.md 8.5). Never add a define that changes `Runtime`'s layout to only some targets; a define that changes compile flags of `p3p_aot` rebuilds all 237 units (~9 min) — put runtime-only defines on `psprecomp_runtime`. -- Test in Azahar (New 3DS mode) with the ISO in its virtual `sdmc/p3p3ds/`. Read `sdmc/p3p3ds/report.txt`: `stage`, heap size/in use, and `failed alloc: from `; resolve the address with `arm-none-eabi-addr2line -f -C -i -e build/3ds/p3p3ds.elf `. The maintainer may be running Azahar too: do not kill an instance you did not start, and do not trust a report without checking its build id and timestamp. Screenshot of the Azahar window: `powershell -NoProfile -ExecutionPolicy Bypass -File .claude/skills/n3ds-platform/scripts/azahar_shot.ps1 -Out .tmp/azahar/shot.png`. A crash photo of the Luma exception screen gives PC and LR: resolve both with addr2line on the ELF of exactly that build (`build ` in the report). +- Test in Azahar (New 3DS mode) with the ISO in its virtual `sdmc/p3p3ds/`. A run that is killed never writes its final report, so put an empty `sdmc/p3p3ds/report_live.txt` there first (report every 5 s), or quit with START+SELECT. Read `sdmc/p3p3ds/report.txt`: `stage`, heap size/in use, and `failed alloc: from `; resolve the address with `arm-none-eabi-addr2line -f -C -i -e build/3ds/p3p3ds.elf `. The maintainer may be running Azahar too: do not kill an instance you did not start, and do not trust a report without checking its build id and timestamp. Screenshot of the Azahar window: `powershell -NoProfile -ExecutionPolicy Bypass -File .claude/skills/n3ds-platform/scripts/azahar_shot.ps1 -Out .tmp/azahar/shot.png`. A crash photo of the Luma exception screen gives PC and LR: resolve both with addr2line on the ELF of exactly that build (`build ` in the report). - Never write `sdmc` or emulator paths of the maintainer's machine into tracked files. ## 3b. P3P3DS Builder release diff --git a/README.md b/README.md index ba22ea78..1ea64dad 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Every known issue is tracked on [GitHub Issues](https://github.com/endercodezz/P | ATLUS and CRIWARE startup logos were missing ([#5](https://github.com/endercodezz/P3P3DS/issues/5)) | first seconds after start, 0.2.8-0.3.1 | fixed in 0.3.2 by rebinding textures after presenting a frame. The fix was verified in Azahar, and the maintainer reported working logos on console. | | Scene transitions: one or more black frames instead of a fade ([#3](https://github.com/endercodezz/P3P3DS/issues/3)); garbage on hardware until 0.3.0 | cutscene and scene changes | since 0.3.1 black on hardware and in Azahar alike (new render targets are filled black); the missing fade itself is not fixed | | Light fringe around changed facial expressions in dialogue portraits ([#4](https://github.com/endercodezz/P3P3DS/issues/4)) | dialogue | [UNVERIFIED] linear filtering of the overlay sprite samples outside its rectangle | -| 0.3.0 only: on a slow SD card the game stops for seconds every 5 s while it rewrites `report.txt` (one user: 36 % speed, `ui` about 60 %) | everywhere | fixed in 0.3.1, confirmed on hardware by the same user: 97-99 % speed, `ui 0.0`, while one rewrite still takes up to 2.6 s on that card (`sd max 2575 ms`) | +| Up to 0.3.3: on a slow SD card the game freezes every 5 s while `report.txt` is rewritten (0.3.0: the game itself waited, 36 % speed; 0.3.1-0.3.3: the rewrite runs in the background but the game's reads can wait behind it, 64 % speed in one run) | everywhere | the report is no longer written during play unless you touch the bottom screen; not yet confirmed on hardware | | Battle: 16-25 fps, 63-86 % of real time (0.3.0, hardware; was 5-7.5 fps in 0.2.7) ([#16](https://github.com/endercodezz/P3P3DS/issues/16)) | battles | game targets 30 fps; the game's own code is now the largest share (`aot` 63-70 %) | | Dorm: 28-30 fps (0.3.0, hardware) ([#17](https://github.com/endercodezz/P3P3DS/issues/17)) | field | game targets 30 fps | | No sound ([#14](https://github.com/endercodezz/P3P3DS/issues/14)) | everywhere | not implemented on 3DS (the PC runner mixes it) | @@ -53,7 +53,7 @@ On New 3DS (details in [`docs/3DS_PLATFORM.md`](docs/3DS_PLATFORM.md) sections 8 | Saves | save and load at the dorm desk and from LOAD GAME, menu on the bottom screen | | First battle | starts since 0.2.5; transform, skinning and lighting on the GPU since 0.2.6; renders correctly since 0.2.7 (culling fix); slow (see Known issues) | | Memory | heap 71,220 KiB with 60,284 KiB in use (hardware, 2026-10-02; 12 MiB linear heap); about 10.7 MiB free | -| Screens | top: the game; bottom: authorship and debug data (fps, speed, where the time went over the last 10 s, memory), also saved to `sdmc:/p3p3ds/report.txt` | +| Screens | top: the game; bottom: authorship and debug data (fps, speed, where the time went over the last 10 s, memory), also saved to `sdmc:/p3p3ds/report.txt` at the start, on exit and when you touch the bottom screen | Measured on the PC runner (ULUS-10512, details and evidence in [`docs/CURRENT_STATE.md`](docs/CURRENT_STATE.md)): @@ -139,7 +139,7 @@ The builder package (about 2 MB zipped) contains no game data: only the prebuilt The runner creates `p3p3ds/ms0/` (memory stick), `p3p3ds/mods/` (mod chain, see below) and `p3p3ds/report.txt` itself. 2. Start **P3P3DS** from the Homebrew Launcher. -The top screen shows the game, the bottom screen authorship and a live debug report (fps, speed, memory, time split), also written to `sdmc:/p3p3ds/report.txt` every few seconds and on exit — please attach it to bug reports. +The top screen shows the game, the bottom screen authorship and a live debug report (fps, speed, memory, time split), also written to `sdmc:/p3p3ds/report.txt` at the start, on exit (START+SELECT) and whenever you touch the bottom screen — please attach it to bug reports. An empty `sdmc:/p3p3ds/report_live.txt` also writes it every 5 s, for runs that crash before you can quit; on a slow SD card that can make the game freeze for a moment. | 3DS | PSP | |---|---| diff --git a/docs/CURRENT_STATE.md b/docs/CURRENT_STATE.md index f9267069..c6aa3d93 100644 --- a/docs/CURRENT_STATE.md +++ b/docs/CURRENT_STATE.md @@ -2,6 +2,16 @@ This is the current source of truth, newest entries first. Evidence concerns ULUS-10512; each section says whether it was measured on the PC runner, in Azahar or on a real New 3DS. +## No report.txt writes during play on the New 3DS (#71, #66, 2026-11-06) + +- [VERIFIED] User reports on #66 (New 2DS XL, 0.3.2 build 2f91246, same Samsung EVO Plus 64 GB, attached report.txt and profile_seconds.csv): a run with freezes about 5 s apart had `sd max 2656 ms`, `io 36` (last 10 s), speed 64-74 %; a run without freezes had `sd max 78 ms`, `io 0`, speed 96-99 %. +- [INFERRED] The card's write latency varies between sessions, and while a slow background rewrite runs, the game's ISO reads wait behind it. Not measured directly: no per-second CSV exists of a run with freezes. +- `platform/3ds/main.cpp`: the bottom screen still updates every 5 s, but report.txt is written only at the start of the run (build and `status : running`, before the game reads anything), on exit, and when the bottom screen is touched (through the writer thread; the PSP has no touch screen, so the game never sees it). The new row `report : touch to save, N written` replaces the blank row 5 of the report, so the other rows stay where they were. An empty `sdmc:/p3p3ds/report_live.txt` restores the write every 5 s, for runs that end without an exit (a host crash, a killed emulator). +- [VERIFIED] Azahar, unattended autotest of 1,500 vblanks, this branch: without `report_live.txt` report.txt changed only at the start and at the end (`2 written` in the final report: `booting` and the start write); with it, every 5-6 s (`6 written`). Same speed in both (83 %), so Azahar says nothing about the stall itself: its SD writes take 50 ms. +- [UNVERIFIED] The tap itself was not exercised: a click posted to Azahar's windows did not reach the game (`2 written`). The write path it uses is the one `report_live.txt` ran above, and `hidKeysDown` reports the first frame of a touch [VERIFIED] `3ds/libctru/libctru/source/services/hid.c:199,204`. +- [UNVERIFIED] Hardware with a slow card: no periodic freezes and speed near 97-99 % in the same scene. Needs the reporter's confirmation on the next release. A host crash now leaves only the start report unless `report_live.txt` is present. +- `profile_seconds.csv` (opt-in) still writes every second on the game's thread, so it can disturb a slow card itself. + ## Builder 0.3.3 release preparation (2026-11-05) - `platform/pc/builder/builder.rc` FileVersion and ProductVersion 0.3.3 ([#69](https://github.com/endercodezz/P3P3DS/issues/69)). Contents since 0.3.2: Builder update notice (#67), texture packs (texpack converter, PPSSPP-compatible keys, replacement on the PC and in the 3DS renderer), native game preparation (#33), host time audit docs (#50), Zig BinDiff tool. diff --git a/platform/3ds/main.cpp b/platform/3ds/main.cpp index b82cdab4..7f54e0ca 100644 --- a/platform/3ds/main.cpp +++ b/platform/3ds/main.cpp @@ -2,7 +2,10 @@ // // Top screen: the PSP framebuffer (480x272) scaled to 400x240 (nearest). // Bottom screen: authorship and live debug information; the same data is -// written to sdmc:/p3p3ds/report.txt every few seconds and on exit. +// written to sdmc:/p3p3ds/report.txt at the start, on exit and when the bottom +// screen is touched. An empty sdmc:/p3p3ds/report_live.txt also writes it every +// 5 s, for runs that may end without an exit (a crash, a killed emulator): the +// writes can hold up the game's reads on a slow SD card. // // Game data: sdmc:/p3p3ds/*.iso (the user's ULUS-10512 image) mounted as // disc0:, sdmc:/p3p3ds/ms0 as ms0:, sdmc:/p3p3ds/mods as the profile's mod directory. @@ -98,6 +101,8 @@ constexpr const char *kBase = "sdmc:/p3p3ds"; struct Stats { u64 start_ms{}, last_report_ms{}, last_fps_ms{}; + bool report_live{}; // report_live.txt: report.txt every 5 s + std::uint32_t report_writes_shown{}; // `written` count on the bottom screen std::uint64_t frames{}, frames_at_last_fps{}; // Game frames: changes of the displayed PSP framebuffer (the game swaps // buffers once per finished frame); vblanks run at 60/s at full speed. @@ -189,8 +194,9 @@ std::deque g_samples; // sdmc:/p3p3ds/profile_seconds.txt present: one CSV line per second with the // split of that second, to find short slowdowns the 10 s report averages out. FILE *g_seconds_log = nullptr; -// Longest rewrite of report.txt so far (shown as `sd max`). +// Longest rewrite of report.txt so far (shown as `sd max`) and rewrites so far. std::atomic g_report_write_max_ms{}; +std::atomic g_report_writes{}; // 3DS buttons -> PSP buttons by position (PSP: Cross bottom, Circle right, // Square left, Triangle top). @@ -611,7 +617,7 @@ std::string report_text(const Stats &s, const psprecomp::Runtime &rt, p3p3ds::Ke "static recompilation port\n" "by %s\n" "build %s\n" - "\n" + "report : touch to save, %lu written\n" "status : %s\n" "fps : %.1f game frames/s (%.0f vblank/s)\n" "speed : %.0f%% of real time\n" @@ -625,7 +631,7 @@ std::string report_text(const Stats &s, const psprecomp::Runtime &rt, p3p3ds::Ke "stop : %s\n" "%s" "\nSTART+SELECT: quit\n", - kAuthor, P3P3DS_BUILD_ID, s.status.c_str(), s.fps, s.vblank_rate, wall > 0 ? 100.0 * guest / wall : 0.0, guest, wall, + kAuthor, P3P3DS_BUILD_ID, static_cast(g_report_writes.load(std::memory_order_relaxed)), s.status.c_str(), s.fps, s.vblank_rate, wall > 0 ? 100.0 * guest / wall : 0.0, guest, wall, static_cast(s.frames), static_cast(k.threads().vblank_count()), static_cast(s.buttons), static_cast(osGetMemRegionFree(MEMREGION_APPLICATION) / 1024u), @@ -644,12 +650,16 @@ void write_report(const std::string &text) { } const auto ms = static_cast(osGetTime() - start); if (ms > g_report_write_max_ms.load(std::memory_order_relaxed)) g_report_write_max_ms.store(ms, std::memory_order_relaxed); + g_report_writes.fetch_add(1u, std::memory_order_relaxed); } -// The periodic report.txt is written by a thread of its own on core 0 below +// During play report.txt is written by a thread of its own on core 0 below // the game's priority: on a slow SD card one rewrite took about 3 s (user // report, 0.3.0: `ui` 60 % of the time, 36 % speed) and the game stood still -// for it every 5 s. The writer's file-system request blocks only the writer. +// for it every 5 s. The writer's file-system request blocks only the writer, +// but the game's ISO reads still share the card with it (issue #71: freezes +// every 5 s with `sd max 2656 ms`, none with 78 ms on the same card), so it +// writes only on request unless report_live.txt asks for every 5 s. struct ReportWriter { LightLock lock; LightEvent wake; @@ -891,6 +901,7 @@ int main() { std::fclose(f); if (stats.dump_every) std::filesystem::create_directories(std::string(kBase) + "/frames", ec); } + if (FILE *f = std::fopen("sdmc:/p3p3ds/report_live.txt", "r")) { stats.report_live = true; std::fclose(f); } if (FILE *f = std::fopen("sdmc:/p3p3ds/profile_seconds.txt", "r")) { std::fclose(f); g_seconds_log = std::fopen("sdmc:/p3p3ds/profile_seconds.csv", "w"); @@ -966,6 +977,11 @@ int main() { rt.stop("Quit by user (START+SELECT)"); throw psprecomp::FrontierHalt{}; } + if (hidKeysDown() & KEY_TOUCH) { // the PSP has no touch screen: free for the runner + const u64 ui_start = svcGetSystemTick(); + post_report(report_text(stats, rt, kernel)); + stats.ui_ticks += svcGetSystemTick() - ui_start; + } { const double guest_us = static_cast(kernel.threads().now()); const double wall_us = static_cast(svcGetSystemTick() - stats.run_start_ticks) / ticks_per_us; @@ -1009,19 +1025,23 @@ int main() { log_second(g_samples[g_samples.size() - 2], g_samples.back(), rt); stats.ui_ticks += svcGetSystemTick() - log_start; } - // Report every 5 s (bottom screen and SD card): redrawing it each - // second cost 8 % of the time in battle on hardware. - if (now - stats.last_report_ms >= 5000u) { + // Bottom screen every 5 s: redrawing it each second cost 8 % of + // the time in battle on hardware. Sooner after a save by touch, + // to show the new count. + const std::uint32_t writes = g_report_writes.load(std::memory_order_relaxed); + if (now - stats.last_report_ms >= 5000u || writes != stats.report_writes_shown) { const u64 ui_start = svcGetSystemTick(); const std::string text = report_text(stats, rt, kernel); if (!save_menu.active) show(text); - post_report(text); + if (stats.report_live && now - stats.last_report_ms >= 5000u) post_report(text); + stats.report_writes_shown = writes; stats.last_report_ms = now; stats.ui_ticks += svcGetSystemTick() - ui_start; } } }; stage = "run"; + write_report(report_text(stats, rt, kernel)); // a run that crashes still leaves its build and `status : running` start_report_writer(); std::printf("Starting game...\n"); try { rt.run(entry, ~0ull); }