fix(libc): write each log line in one call #3871

Merged
fdfawef324 merged 1 commit from upstream-pr/3871 into main 2026-10-11 22:12:35 +00:00
fdfawef324 commented 2026-10-11 18:19:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-11T18:19:29Z.


What

Demon's Souls (PPSA01342 v1.005, Windows) stops on a black screen on main a11c8f7f: the window shows Demon's Souls | FPS: 0.00 (2) for good, every BPE job worker spins in its scheduler and nothing is reported. This is the black screen #3710 describes after its Acm change; on this machine (RTX 3060) it happens on plain main. The last stderr lines before it are the shader preparation pool's failures:

[Shaders/ShaderRegistry.cpp:747 operator()] Shader 0x249850f00 could not be prepared after registration: ShaderRecompiler::Recompile: GetImageResource dword 0 is not a valid runtime value; chain: LoadAddressU32@pc=0x00000818(...)
RecompileRequest:
<15-20 KB of base64>

Thread stacks sampled with gdb while it is stuck (three samples, all the same):

Thread 32 (shader preparation pool):
  ntdll!ZwWriteFile <- WriteFile <- ucrtbase!_fflush_nolock <- ucrtbase!fwrite <- ucrtbase!fputc
  <- __pformat_putc <- __pformat_putchars <- __pformat_puts <- __mingw_pformat <- __mingw_fprintf
  <- PrepareRegisteredInBackground lambda
Thread 11 "BPE JobWorkerThread CPU9":
  ntdll!RtlEnterCriticalSection <- __mingw_fprintf <- sceSaveDataDirNameSearch (SAVEDATA_TRACE)
Threads 25-30, 33-36, 71: RtlEnterCriticalSection <- __mingw_fprintf (shader disk cache, sampledBudget, ...)
Main thread: sceKernelWaitEventFlag <- GameCoreLoop::MasterGameLoop (waits for the frame's jobs)

mingw's fprintf (libstdc++ turns on __USE_MINGW_ANSI_STDIO) writes an unbuffered stream through fputc, so every character is a WriteFile, and it holds the stream's lock for the whole line. An APS5_LOG_ERR line carrying a RecompileRequest is tens of thousands of writes; while the game runs, stderr grew by about 500 bytes a second. Every other thread that writes to stderr waits behind it, and the job worker waiting in sceSaveDataDirNameSearch's trace keeps GameCoreLoop from ever finishing a frame. With stderr sent to NUL the game passes the black screen within 30 s.

Driver::reportSkip got the same treatment in 8cf2f6580 (GTA V at 0.1 fps). This does it for the APS5_LOG_* macros: WriteLogLine formats the line into memory (one vsnprintf, a second one only for lines over 1 KiB) and writes it with one fwrite and the existing fflush. The format string is still checked at compile time (gnu::format, with mingw's __MINGW_PRINTF_FORMAT so %zu and %llu stay valid there). The UCRT writes an unbuffered stream's fwrite as one even-length block and the last byte, so a line takes at most two writes on Windows and one on Linux.

With this change Demon's Souls renders and stops at its next error, a separate topic:

[videoout] presentation failed: DB_RENDER_CONTROL depth copy, resummarize or decompress is unsupported (register 0x0 = 0x00000010)

Tested

  • Windows Server 2025, WinLibs GCC 15.2.0, Release, ANYPS5_ENABLE_SPIRV_TOOLS=OFF, RTX 3060.
  • New log_macros test: logs a short line, a 20000-character line and a line without arguments to an unbuffered stream whose writes keep their boundaries (a message-mode named pipe on Windows, a SOCK_SEQPACKET socket pair on Linux) and checks the text and the number of writes. On main it fails with a short line took more than two writes; with this change it passes. Linux not run (no Linux machine here).
  • Full ctest -j10: the same 4 tests fail with and without this change on this machine (guest_kernel_sys needs symlink privileges, guest_filesystem gets Access Denied on its probe file, windows_exceptions, agc_driver_emulated_color_compare needs sRGB sampling the device lacks).
  • Demon's Souls PPSA01342 v1.005: black screen at frame 2 on main; with this change it renders and reaches the error above.
  • Dreaming Sarah PPSA02929: 60 FPS in game with AnyPS5's libc and with the title's own libc, as before.
  • python tools/check_conventions.py --base main passes.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4821264651 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3871). Originally posted by [fdfawef324](https://github.com/fdfawef324) on 2026-10-11T18:19:29Z. --- ### What Demon's Souls (PPSA01342 v1.005, Windows) stops on a black screen on `main` a11c8f7f: the window shows `Demon's Souls | FPS: 0.00 (2)` for good, every BPE job worker spins in its scheduler and nothing is reported. This is the black screen #3710 describes after its Acm change; on this machine (RTX 3060) it happens on plain `main`. The last stderr lines before it are the shader preparation pool's failures: ``` [Shaders/ShaderRegistry.cpp:747 operator()] Shader 0x249850f00 could not be prepared after registration: ShaderRecompiler::Recompile: GetImageResource dword 0 is not a valid runtime value; chain: LoadAddressU32@pc=0x00000818(...) RecompileRequest: <15-20 KB of base64> ``` Thread stacks sampled with gdb while it is stuck (three samples, all the same): ``` Thread 32 (shader preparation pool): ntdll!ZwWriteFile <- WriteFile <- ucrtbase!_fflush_nolock <- ucrtbase!fwrite <- ucrtbase!fputc <- __pformat_putc <- __pformat_putchars <- __pformat_puts <- __mingw_pformat <- __mingw_fprintf <- PrepareRegisteredInBackground lambda Thread 11 "BPE JobWorkerThread CPU9": ntdll!RtlEnterCriticalSection <- __mingw_fprintf <- sceSaveDataDirNameSearch (SAVEDATA_TRACE) Threads 25-30, 33-36, 71: RtlEnterCriticalSection <- __mingw_fprintf (shader disk cache, sampledBudget, ...) Main thread: sceKernelWaitEventFlag <- GameCoreLoop::MasterGameLoop (waits for the frame's jobs) ``` mingw's `fprintf` (libstdc++ turns on `__USE_MINGW_ANSI_STDIO`) writes an unbuffered stream through `fputc`, so every character is a `WriteFile`, and it holds the stream's lock for the whole line. An `APS5_LOG_ERR` line carrying a RecompileRequest is tens of thousands of writes; while the game runs, stderr grew by about 500 bytes a second. Every other thread that writes to stderr waits behind it, and the job worker waiting in `sceSaveDataDirNameSearch`'s trace keeps `GameCoreLoop` from ever finishing a frame. With stderr sent to `NUL` the game passes the black screen within 30 s. `Driver::reportSkip` got the same treatment in 8cf2f6580 (GTA V at 0.1 fps). This does it for the `APS5_LOG_*` macros: `WriteLogLine` formats the line into memory (one `vsnprintf`, a second one only for lines over 1 KiB) and writes it with one `fwrite` and the existing `fflush`. The format string is still checked at compile time (`gnu::format`, with mingw's `__MINGW_PRINTF_FORMAT` so `%zu` and `%llu` stay valid there). The UCRT writes an unbuffered stream's `fwrite` as one even-length block and the last byte, so a line takes at most two writes on Windows and one on Linux. With this change Demon's Souls renders and stops at its next error, a separate topic: ``` [videoout] presentation failed: DB_RENDER_CONTROL depth copy, resummarize or decompress is unsupported (register 0x0 = 0x00000010) ``` ### Tested - Windows Server 2025, WinLibs GCC 15.2.0, Release, `ANYPS5_ENABLE_SPIRV_TOOLS=OFF`, RTX 3060. - New `log_macros` test: logs a short line, a 20000-character line and a line without arguments to an unbuffered stream whose writes keep their boundaries (a message-mode named pipe on Windows, a `SOCK_SEQPACKET` socket pair on Linux) and checks the text and the number of writes. On `main` it fails with `a short line took more than two writes`; with this change it passes. Linux not run (no Linux machine here). - Full `ctest -j10`: the same 4 tests fail with and without this change on this machine (`guest_kernel_sys` needs symlink privileges, `guest_filesystem` gets Access Denied on its probe file, `windows_exceptions`, `agc_driver_emulated_color_compare` needs sRGB sampling the device lacks). - Demon's Souls PPSA01342 v1.005: black screen at frame 2 on `main`; with this change it renders and reaches the error above. - Dreaming Sarah PPSA02929: 60 FPS in game with AnyPS5's libc and with the title's own libc, as before. - `python tools/check_conventions.py --base main` passes. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-11 22:12:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T22:12:32Z.


Checked on Linux: PR merged onto current main builds, log_macros passes and fails with LogMacros.hpp reverted, so the test pins the change (short line, 20000-character line and a line without arguments each stay within two writes, text compared exactly). Return value of the macros is unchanged (fflush), format checking is kept via gnu::format. Windows/mingw behaviour relies on your run. Merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114433222 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3871#issuecomment-6114433222). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T22:12:32Z. --- Checked on Linux: PR merged onto current main builds, `log_macros` passes and fails with `LogMacros.hpp` reverted, so the test pins the change (short line, 20000-character line and a line without arguments each stay within two writes, text compared exactly). Return value of the macros is unchanged (`fflush`), format checking is kept via `gnu::format`. Windows/mingw behaviour relies on your run. Merging once CI is green.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!3871
No description provided.