perf(agc): format and write steady-state log lines off the queue worker #6371
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6371"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-15T13:27:24Z.
What
Texture creation logs one stdout line per texture and mip (
Texture address=...) throughWriteLogLine, which formats with the MinGW printf and flushes every line on the calling thread, here the AGC queue worker. In Sword of the Sea (PPSA25344) cave gameplay, queue worker 0 spent 10-17 % of its samples in__mingw_pformat/WriteLogLine, and stdout held 575,778Texture address=lines (96 MB) per 1500 s row.The benefit is less CPU time on the queue worker and 96 MB less output. No frame-rate change is claimed.
AsyncLogkeeps a bounded queue and a logger thread.AGC_LOG_DEFERREDcaptures numeric arguments on the caller and formats them on the logger thread.WriteLogLinehands finished lines to a per-moduleLogSinkinlibc/LogMacros.hppthatAgcDriverinstalls. Lines that go through it keep their call order across stdout and stderr and are written with one flush per batch.Flushwaits, without a limit, until every line queued before the call is written: on driver failure, inDriver::stopand at exit.APS5_LOG_SYNC=1restores the synchronous path.abort()andstd::terminate(libc chains aSIGABRThandler in front of the previous one whenAsyncLogregisters, so everyabortsite is covered, on Linux too;raise(SIGABRT)still ends the process), guestabort(), the Windows crash report of a fatal exception, and guest_Exit. The flush polls two counters and takes no lock, so a stuck stream or a thread that died holding the queue lock cannot hang a dying process, and after its first timeout later calls return at once.AsyncLogregisters it withLibcSetEndFlush_nid_no_patch. A title that installs its ownSIGABRTdisposition through libkernel replaces the handler, as it replaces the crash report's.libSceAgcDriverhas about 220 directfprintf(stderr, ...)calls and onefputs(115APS5_LOG_*sites go through the sink). They are written at once on the calling thread, so for those lines stdout and stderr no longer interleave in call order: a direct stderr line can appear before older queued stdout lines.APS5_LOG_SYNC=1keeps one order for everything. Stated indocs/dev/ARCHITECTURE.md.deferred write-back failednotes print three times per 10 s, then are counted into onerepeated N more timesline.[log] 10 sline reports lines, bytes, caller time and logger time (APS5_LOG_STATS=0hides it).docs/dev/ARCHITECTURE.md, section "Log output": the logger thread, the bounded queue, theLogSinkhook,APS5_LOG_SYNCandAPS5_LOG_STATS, the flush points and the stream order.Tested
Windows 11, MinGW-w64 GCC 15.2, NVIDIA RTX 3060 / 5070 lab machines for the title row. Rebased onto
origin/main44df21632.agc_async_log(3.05 s) andagc_async_log_fatal(2.88 s) pass, with the guest exit tests (guest_exit_order,guest_system_exit,guest_quick_exit,guest_immediate_exit,guest_return_from_main,guest_system_relaunch,guest_nested_exit) as a regression check on the_Exitchange: https://github.com/AlxcBTW/AnyPS5/actions/runs/38787857401. Each of the two tests also passes 45 times (three ctest loops of 15 repeats running at once).libc.dll), fork CI:agc_async_log(3.19 s) andagc_async_log_fatal(2.86 s) pass, including the child that ends in an access violation through the crash report: https://github.com/AlxcBTW/AnyPS5/actions/runs/38787867338. Each also passes 30 times (three loops of 10 repeats at once).libsandrelinkertargets compile on the fork CI: https://github.com/AlxcBTW/AnyPS5/actions/runs/38787881151.tools/check_conventions.py --base origin/mainpasses.agc_async_log: order of deferred and written lines, long lines, the repeat limit,Flushwaiting for a stream another thread holds (2.4 s, longer than the 2 s timeout of the earlier head), a bounded flush that gives up and then succeeds, a full queue blocking its caller.agc_async_log_fatal: a child queues 100 lines behind a held stream and ends inabort(), instd::terminate(), inraise(SIGABRT)(Linux) or in an access violation (Windows); every line must arrive in order. A last child never releases the stream and must end after the 2 s bound with an empty file.Flushtimeout of the earlier head, a queue that does not block, no flush in the abort handler, no flush inReportCrash, noSIGABRThandler, a 0 ms limit, an unbounded wait (the blocked child then hangs). These were run in a standalone build of the test on Windows against the realCrashReport.cpp. On Linux (fork CI), removing the re-raise fails theraisechild (https://github.com/AlxcBTW/AnyPS5/actions/runs/38789705108) and removing the flush from the abort handler fails theabortchild (https://github.com/AlxcBTW/AnyPS5/actions/runs/38790140072).__mingw_pformat/WriteLogLine, and a 1500 s row wrote 575,778Texture address=lines (96 MB). After, 131 lines per 10 s and 0.006 % of worker time ([log]counter). Logs:hdcave-s10-20261015-*/stderr.log, attached as a comment. This row ran on the first head of this PR. The commits added since then are the end flush, two counters in place of the writer flag, tests and docs; the row was not repeated.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-15T13:27:57Z.
Evidence from the two cave rows (Sword of the Sea PPSA25344, HD output, console settings, RTX 5070 Laptop, Windows 11), as promised in the description.
Before (main
8856f180c+ lab commits, rowhdcave-s8-notex-20261015-140413): stdout.log 14305 lines, 1.7M, of them 10850Texture address=lines in 1500 s.After (same base + this change, row
hdcave-s10-20261015-150703): stdout.log 14747 lines, 1.8M. The[log]counter in steady gameplay:Worker-0 sampling profile before the change (symbolized, inclusive):
__mingw_fprintf11.5 %,__mingw_pformat11.2 %,WriteLogLine5.7 % of the steady gameplay window. Frame rate in that scene did not change (0.8-1.6 FPS either way; the time moved into WAIT_REG_MEM memory waits), so this PR is about CPU time on the queue worker, not a measured FPS win on this title.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T13:33:20Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppL1core/libs/prx/libc/src/specifics/windows/CrashReport.cppL5core/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppL126core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppL750, L1090Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtdocs/dev/ARCHITECTURE.mdcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Graphics/src/Texture.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cppImported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-15T13:50:09Z.
Independent Windows validation of head 610c7e4a59a3c7ea6bd3f3bd7567a67e5cfd8ae1 (AI-assisted). I compiled the actual AsyncLog.hpp and LogMacros.hpp and ran your existing tests/AsyncLog.cpp standalone with WinLibs GCC 15.2 r7 UCRT, -std=c++20 -O2 -DNDEBUG -static -pthread -Wall -Wextra. The author test passes both asynchronously and with APS5_LOG_SYNC=1, with timestamps unset.
I also reproduced an incomplete Flush: idle.wait_for returns after two seconds even when writing is still true, and the void API gives its caller no indication that the logger is still using the stream. That means Flush at exit does not guarantee queued output has finished; a client that closes its FILE after Flush may also race the logger.
The attached native regression holds a real UCRT FILE lock on another thread for 2400 ms, enqueues one deferred line and calls Flush. On two runs the original returns before the stream is released (2011/2021 ms). It then releases the lock and drains before closing, so the probe does not intentionally access a closed FILE.
A one-line candidate replacing idle.wait_for(lock, 2s, predicate) with idle.wait(lock, predicate) passes the same regression (2416/2399 ms, after release, correct line content). Your existing test also passes with that candidate in both async and sync modes. The patch passes git apply --check against the recorded header.
There is a shutdown-policy tradeoff: this candidate waits for completion, so permanently blocked output can hold shutdown indefinitely. If a two-second shutdown budget is intentional, an explicit timeout result plus managed worker/stream lifetime would be needed instead. I am sharing this as a review candidate, not claiming that unbounded shutdown is the desired policy.
Bundle: source, original/fixed headers, one-line patch, reproduction commands and raw results. No full emulator build, registered CTest run, GPU/title/FPS/performance measurement or Linux/macOS validation; no push to your branch.
async-log-flush-pr6371.zip
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-15T14:20:15Z.
Thanks for the independent validation and the regression. Head 103337a75 takes your candidate:
Flushnow waits until the queue is empty and the writer is idle, without a timeout. On the policy: the synchronous path onmainalready blocks the caller on the same stream lock for as long as the stream is held, so a permanently blocked stream holds shutdown there too; the asynchronous logger should not be the one that silently returns while output is still pending. A bounded wait that reports a timeout would only move the problem to the caller, who has nothing better to do with it than wait or close the stream under the logger.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T14:36:34Z.
Review: CHANGES_REQUESTED
Checked the head merged onto current main: it builds,
agc_async_logpasses, and the only other ctest failures areagc_driver_tessellation(SEGFAULT on main too) andagc_driver_store_over_untracked(passes on rerun, load flake). Points to address:PrintFailureandDriver::stopcallFlushAsyncLog_nid_no_patch(Lifecycle.cpp:42,:124). The fatal paths never drain the queue:CrashReport.cpp(abort/fast-fail/hang dump,:510-548),std::terminateinVulkanDevice.cpp:187,std::abortinGuestBufferMemory.cpp:2820. Those are the lines a crash report needs most. Drain the queue (bounded, since the process is dying) from those paths, or write them through the synchronous path, and add a test that exits throughabort()/terminatewith lines queued.WriteLogLine-based stdout lines go through the sink; directfprintf(stderr, ...)/printfcalls in the driver (~500) are written synchronously, so stdout and stderr no longer interleave in call order. State this in the PR or make the sink cover them.docs/dev/ARCHITECTURE.mdneeds a short section for the new logger thread, the bounded queue (blocks the caller when full), theLogSinkhook andAPS5_LOG_SYNC/APS5_LOG_STATS, under the diagram it changes.tests/AsyncLog.cppdoes not coverFlushwaiting for a held stream (the regression simonbronk posted), nor a full queue blocking the caller. Both are cheap to add.windowsjob is skipped on PR CI), Linux, any title. No FPS change is claimed, so the benefit is lower CPU and 96 MB less output; please keep the PR description to that.Happy to re-review once these are in.
Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-15T19:08:47Z.
@adriadam10 Rebased onto
main44df21632(no conflicts) and added three commits on top of the previous two: tests, the end flush, docs. Point by point:1. Lines queued before a crash
One bounded flush, run from the paths that end the process without the exit handlers.
AsyncLog::FlushFor(limit)waits until the entries queued before the call are written. It polls two atomic counters (queued, finished) and takes no lock, so a thread that died holding the queue lock, or a stream held by another thread, cannot hang it.Flushkeeps its wait without a limit (driver failure,Driver::stop, exit). It now counts the same two values instead ofpending.empty() && !writing, so it waits for the lines queued before the call and not for a moment in which no thread is logging.AsyncLogregistersFlushAtEnd(FlushFor(2 s); after its first timeout later calls return at once) with a new libc seam,LibcSetEndFlush_nid_no_patch(Shutdown.hpp,Process.cpp). libc runs itSIGABRThandler that the first registration chains in front of the previous one.std::abort()andstd::terminate()end there, soVulkanDevice.cpp:187,GuestBufferMemory.cpp:2820, the hang dump'sstd::abort()inCrashReport.cppand the otherabort/terminatesites need no edit of their own, and Linux is covered, whereCrashReport.cppis not built. With the default disposition the handler restores it and raises again, soraise(SIGABRT)still ends the process;ReportCrash(CrashReport.cpp) once the first fatal exception is claimed;abort()and inLibcTerminate_nid_no_patch(guest_Exit,quick_exit).The UCRT invalid-parameter handler does not run it: that handler returns and the process continues, so it is not a dying path.
Test:
agc_async_log_fatalruns the test executable as a child. The child queues 100 lines behind a stream that another thread holds and releases 100 ms later, then ends inabort(), instd::terminate(), inraise(SIGABRT)(Linux) or in an access violation through the crash report (Windows). A process that does not wait loses every line. A last child never releases the stream; it must end after the 2 s bound, with an empty file. Every child has a watchdog. Removing the flush from the abort handler, removing it fromReportCrash, removing the handler, a 0 ms limit and an unbounded wait each fail the test (checked in a standalone build of the test on Windows with the realCrashReport.cpp; on Linux, removing the re-raise fails theraisechild, https://github.com/AlxcBTW/AnyPS5/actions/runs/38789705108, and removing the flush from the handler fails theabortchild, https://github.com/AlxcBTW/AnyPS5/actions/runs/38790140072).Known limits:
SIGABRTdisposition through libkernel'ssignalorsigactionreplaces the host handler (Signals.cpp,Install), as it already replaces the crash report's abort handler. Guestabort()runs the flush itself, but anabort()in host code does not on that path.Signals.cpp:296) and fatal host signals on Linux, where no crash report exists, do not run it.2. Order across streams
Not made to cover them.
libSceAgcDriverhas about 220 directfprintf(stderr, ...)calls and onefputsoutsidetests/andtools/(counted with grep; 115APS5_LOG_*sites go through the sink). Their messages have their own formats, so moving them toAPS5_LOG_ERRwould change them. Stated in the description and indocs/dev/ARCHITECTURE.md: lines through the sink keep call order across stdout and stderr, a directfprintf(stderr, ...)is written at once and can appear before older queued lines,APS5_LOG_SYNC=1keeps one order for everything. TheFATAL:line thatGuestBufferMemory.cpp:2820prints before itsabort()is such a line: it comes before the queued lines that the abort handler then writes.3.
docs/dev/ARCHITECTURE.mdNew section "Log output" after the Graphics section: a four-node diagram, the logger thread and the 16 MiB queue that blocks its caller,
LogSinkandAPS5_LOG_SYNC, the order rule above, the flush points (unbounded and bounded), the[log]line andAPS5_LOG_STATS, and which test covers what.4. Tests (
tests/AsyncLog.cpp)FlushWaitsForHeldStreamholds the stream's lock on another thread (flockfile,_lock_fileon Windows) for 2.4 s, queues a line and requiresFlushto return only after the release. It reimplements the regression simonbronk posted, and it fails on the earlier head with the 2 s timeout.FullQueueBlocksCallerqueues more than twiceAsyncLog::Capacity(now public) from a producer thread behind the held stream. The producer must stall below that and stop making progress, and after the release every line must arrive in order, none dropped. It fails when the wait for free space is removed.BoundedFlushGivesUp:FlushForreturns false while the stream is held and true after the release.agc_async_log_fatal: see point 1.5. Description
Edited to the CPU time of the queue worker and the 96 MB of output, with no frame-rate claim. It also states point 2 and the new tests.
Run where
agc_async_logandagc_async_log_fatalpass, plus the guest exit tests: https://github.com/AlxcBTW/AnyPS5/actions/runs/38787857401. 45 repeats of each test with three loops at once pass.libc.dll), fork CI: both pass, including the access-violation child: https://github.com/AlxcBTW/AnyPS5/actions/runs/38787867338. 30 repeats of each with three loops at once pass.libsandrelinkercompile on the fork CI: https://github.com/AlxcBTW/AnyPS5/actions/runs/38787881151.tools/check_conventions.py --base origin/mainpasses.Not run: the rest of ctest (your run of the previous head names
agc_driver_tessellationand a load flake inagc_driver_store_over_untracked), any title with this head, macOS. The Sword of the Sea row in the description ran on the first head and was not repeated.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T09:16:47Z.
Review: APPROVED
Re-checked the new head merged onto current main: it builds,
agc_async_logandagc_async_log_fatalpass. All points from the last review are addressed: the bounded end flush on abort/terminate/crash report/_Exitwith a test that ends through each path, the stream-order note, the "Log output" section indocs/dev/ARCHITECTURE.md, and tests forFlushon a held stream and a full queue. Windows/MinGW behaviour and the title measurement rest on your fork runs; merging once the full Build is green.