WIP: fix(fs): a create, delete or rename gives the virtual core up on every mount #2529
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!2529
Loading…
Reference in a new issue
No description provided.
Delete branch "unixman/fix/save-vcore-path-calls"
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?
Summary
The game's threads run only while they hold one of six virtual cores. A thread that waits in a host file call should give its core up first, so the threads waiting for that core can run in the meantime. OpenFile, the file-handle calls (SetFileSize, WriteFile, FlushFile, CloseFile) and a staged save's Commit already do this. Five calls did not do it fully: CreateFile, DeleteFile, CreateDirectory and DeleteDirectoryRecursively (all four through
save_path_call), and RenameFile.host_path, and its link checks (save_guard) are host file calls, so those calls ran with the core held.A thread in a host call reaches no checkpoint, so it cannot be asked to give its core up. Neither the request of a better-priority waiter nor the starve rule (
HOS_VCORE_STARVE_US, 1 s) helps while the call blocks. Unless another core the waiter may use comes free, only the 2 s escape (HOS_VCORE_ESCAPE_US,HOS_VCORE_ESCAPE_EQ_US) ends the wait, and the escape then runs the waiter without a core, beside the holder.This change makes
save_path_calland RenameFile give the core up beforehost_pathon every mount, as OpenFile does. They take it back on every way out, with no lock held. A failed call still reads its error before the core is taken back, because that wait sets the last error. A call refused for its mount (read-only or unknown) also gives the core up and takes it back, as OpenFile's refusals do. A thread that held no core takes none.The result: none of these five calls makes a host file call with the core held, and the game gets the same answers as before.
How to see it on
main: not in the game. It comes from reading the code, and the new check shows it: onmain,tests/test_vcore_fs_save.pyfails with 29 checks, each a host file call made with the core held (Measurements). The windowless runs in Testing make two of the five calls through the changed code in the game, but the 5 svcores:lines are too coarse to show one call's wait (Risks).Read first:
save_path_callandHLE(_ZN2nn2fs10RenameFileEPKcS2_)inhost/nn_fs.c, thentests/test_vcore_fs_save.py.Related, each checked from its own commits or diff:
a1c09a3c) addedsave_path_call. It gives the core up only for a staged save, and only afterhost_path. That is the gap this change closes.26c12b0donmain, "no core held while OpenFile builds the button prompts", moved OpenFile's give-up to beforehost_path. This change does the same for the five path calls.d6878186) lets a starved waiter have its core after 1 s instead of escaping at 2 s. It came from #262 (closed): workers that waited the full 2 s for a core while a save loaded. That rule needs the holder to give its core up, and a holder in a host call cannot. Whether this change affects #262's report was not tested.host/nn_fs.cpart of each hit. Several open pull requests changehost/nn_fs.c, but none changes how these calls hold the core. Thehost/nn_fs.cchanges of #2154, #2173, #2180, #2411 and #2434 apply to this branch as they are. Those of #461, #61, #1577, #68 and #1428 already fail to apply onmain.host_path, and gives the core up only while it waits for that lock, not during the host file calls. Itshost/nn_fs.chunks are written against the code from beforesave_path_call. #1428 (WIP) changes Commit, to back the saves up.Testing
Tested: Gentoo Linux, Ryzen 9 9950X3D, Radeon RDNA4 (1002:7550), Mesa RADV 26.2.6, sched_ext scx_lavd. The checks ran on this branch's head alone. The game ran windowless (
HOS_HEADLESS=1), on this desktop while it was in use, so its numbers are rough.The branch is on
mainbe663a08. The game ran before that rebase, in two rounds. The latest round ran this fix at 0918f3c5 againstmain9046a8d7. An earlier round ran this fix at 4bdfb221 againstmain90fae88c, before the fix commit's message was reworded and before the branch was rebased twice.host/nn_fs.cand the new check are byte for byte the same at 4bdfb221 and at this head. The earlier round is kept because only it ran a series on a full save folder. Since9046a8d7,mainhas replaced its builder and moved the game's code (#2286, #2411). Their changes tohost/nn_fs.cread the RomFS throughromfs.hand choose the saves folder (hos_saves_root); they don't touch the five calls this PR changes, and the rebase ontobe663a08changed nothing in this PR's diff ofhost/nn_fs.c. This PC's lab builds the game with the buildermainreplaced, so the game was not run again atbe663a08.main, windowless, latest round (at 0918f3c5, onmain9046a8d7).main9046a8d7, each built as a pack over the samemain90fae88cbase, logginglongshot v3.2.0-17-g0918f3c5 (source 5542d96f)andlongshot v3.2.0-15-g9046a8d7 (source 643c1bab). The two packs' sources differ only by this PR's diff.dev.py queue run. That driver is what this PC's lab runs use: it checks every 2 s that no other game build on this PC is running and stops only the game it started. No pair has run throughdev.py queue run --mode sharedyet; one a side can be repeated that way if a reviewer asks. The same switches on both sides:HOS_HEADLESS=1,HOS_FS_TRACE=1,HOS_OOT_TRACE_SAVES=1,HOS_OOT_CHEATS=1(for the teleport) andHOS_CONTROL(the control bridge, used for status, the save listing and the quit). Uncapped, Autosave on, the rest as Auto picks it.!cheat 112and!cheat 1003set 12:00 and go to the Market. Link walks out of its gate into Hyrule Field and back in, and the game writes a transition autosave each way. Then the bridge'squit, after which every game loggedstop: close work done.create directory slot_0N,create file slot_0N/progress.savandcreate file slot_0N/caption.sav. Those are CreateDirectory and two CreateFile on a staged save, throughsave_path_call: 9 calls a side.fs: save: published 3 files). Each run's save loaded in the next run of its side (slot_03, slot_04, then slot_05), with the game in play in the map the previous run had ended in. All 10 runs logged 0 escapes and noran without a coreline. No wait of 50 ms or more named the thread that saves (gmd::SaveDataMgr) or fell between a save's first file call and itspublishedline. Thevcores:numbers are in Risks.main90fae88c, windowless. The same method. This branch ran as a pack over amain90fae88cbase, logginglongshot v3.2.0-14-g4bdfb221 (source f14d9e21), against that base itself, logginglongshot v3.2.0-11-g90fae88c (source 90deadee).create directory slot_0N,create file slot_0N/progress.sav,create file slot_0N/caption.savandCommit: 3 files, 4 ops. Those are CreateDirectory and two CreateFile on a staged save, throughsave_path_call: 9 calls a side.fs: save: published 3 files). Each run's save loaded in the next run of its side, with the published folder byte for byte as the run before had left it, the game in play and Link where he had stood. All 18 runs logged 0 escapes and noran without a coreline. No wait of 50 ms or more named the thread that saves (gmd::SaveDataMgr) or fell between a save's first file call and itspublishedline. Thevcores:numbers are in Risks.tests/test_vcore_fs_save.py, ran alone throughtests/run_all.py --strict --container --exactin CI's image (Ubuntu 24.04), once with clang++ 18.1.3 and once with g++ 13.3.0. At the test commit 6bd9ca57 (main8aa5ab87plus the test only), both fail the same 29 checks. Every one is a host file call made with the core held, by one of the five calls, on a staged save, a save mounted in place or cache storage. None is on SetFileSize, CloseFile, Commit or the refusals. At the head (4bdfb221, the same files as this head), it passes with both. After the rebase ontomain9046a8d7it ran again with both compilers: the same 29 checks fail at the test commit, then 69e6ef0c, and it passes at the fix, 0918f3c5. Onmainbe663a08the check's stand-in for a file handle needed the RomFS field #2286 added (Handle.rom, closed throughhos_romfs_file_close), and the check now has it. CI's image could not be rebuilt on this PC without a network after #2286 changed its Dockerfile, so atbe663a08the check ran on this PC's own compilers, clang++ 22.1.8 and g++ 16.2.1: the same 29 checks fail at the test commit,ea92a2be, and it passes at this head,537fbfb4. As a test of the test, it also ran against 12 deliberately broken versions of the fix, and it fails on all 12 with both compilers. Among them: a refusal that takes a core back although the thread held none, the give-up afterhost_pathas onmain, and the take-back with the save lock still held.mise run test's command (python -I -B tests/run_all.py, unsharded), on this branch alone and onmain:be663a08, on this PC's own tools (not CI's image, see above; not--strict):main686 passed, 56 failed, 101 skipped; this PR at537fbfb4687 passed, 56 failed, 101 skipped. The same 56 checks fail on both sides; they need tools CI's image has and this PC lacks. The extra pass is the new check.--strict --container):main9046a8d7962 passed, 0 failed, 18 skipped; this PR at 0918f3c5 963 passed, 0 failed, 18 skipped.9046a8d7, two timing-dependent checks failed once, one on each side, and passed when run again alone on both sides: nx2/test_depth_sample_types on this PR and tests/test_crash_dialog onmain. tests/byte_compile_tracked also failed on this PR, because this PC's checkout had a stale worktree path, and passed once that was fixed. The counts above are from a second run of both.main. The 11 other checks that readhost/nn_fs.cpass, except test_fs_path_utf8, which is Windows-only and skips. The 18 skips are 13 Windows-only checks, 2 that need a Vulkan device and 3 that need the game's files.Still to test:
fs: save: publishedlines, and 0 escapes and noran without a coreline in thevcores:lines near them (@Beanstalk on Windows with NVIDIA and AMD: walk out of the Market gate into Hyrule Field and back, then read the log's save and core lines).Measurements
Verdict: the bug is gone: the game-free check fails with 29 checks on
mainand passes with this PR, with clang++ and with g++. For a fix that is the measurement; the in-game cost is in Risks.Setup: Gentoo Linux, Ryzen 9 9950X3D.
python -I -B tests/run_all.py --exact tests/test_vcore_fs_save.pyon this PC's own compilers (clang++ 22.1.8, and g++ 16.2.1 throughCXX=g++), once for each commit and compiler; CI's image could not be rebuilt here without a network (Testing). The before side ran at the test commitea92a2be, which ismainbe663a08with only the test added, so itshost/nn_fs.cis main's. The after side ran at this head,537fbfb4. Before the rebase, the same rows gave the same result in CI's image (clang++ 18.1.3, g++ 13.3.0) at 69e6ef0c and 0918f3c5 (onmain9046a8d7), and earlier at 6bd9ca57 (onmain8aa5ab87) and 4bdfb221.be663a08537fbfb4The 29 failing checks on main (clang++; g++ prints the same lines)
After (clang++; g++ prints the same, in 0.1 s):
Risks
The first points come from reading
host/nn_fs.candvc_acquireinhost/hos_thread.c. The in-game points come from the windowless runs in Testing.On a staged save: still one give-up and one take per call, as on
main. Onlyhost_path's link checks have moved inside that window.On a save mounted in place, cache storage, temporary storage, and a call refused for its mount: one give-up and one take per call that
maindid not have. None of these ran in the game.The cost of a take: when a core the thread may use is free, it takes one at once. This counts in the
vcores:line's takes, not its waits. When another thread has taken the core meanwhile and no other allowed core is free, the caller waits as OpenFile, the handle calls, a staged Commit and MountSaveData already do:HOS_VCORE_SLICE_US), at its next checkpoint.At worst, while better-priority threads keep every allowed core busy without blocking, a create, delete or rename returns up to about 1 s later, and never more than 2 s later. A few milliseconds is the case of an equal-priority holder.
In the game, no cost beyond the spread. Windowless, on a desktop in use, three runs a side, so rough. The walk covers two map loads and two saves. Latest round, this fix at 0918f3c5 against
main9046a8d7, each walk run creating a new slot; medians (range):main9046a8d7ran without a coremain's 125.7 ms publish is one run (both of its saves took about 125 ms), in code this PR does not change.The earlier round, this fix at 4bdfb221 against
main90fae88c; series 1 made no create call on either side:mainmainran without a coremain's ranges on every row: waits of 50 ms or more 5.5 against 5.5, longest wait 101.1 ms against 86.9 ms.main's range, although neither side made a changed call there. Series 2, where the calls ran, goes the other way. Those waits fall in a map load or right around one, none in a save. I read it as run-to-run noise of three runs, not as a cost.The frame trace and the FPS counter's Breakdown count the time in these calls as the thread's blocked time, not its running time.
What a player notices after updating: nothing is expected. Saves,
longshot_options.iniand the caches are unchanged, and the calls write the same files. In the runs, every save published and loaded again on both sides.How to turn it off: there is no switch, so a revert is the way back.
HOS_VCORES=0is no fallback: it turns the virtual cores off altogether, and the game's job queues need them.Left as on
main: GetEntryType, OpenDirectory, IsSaveDataExisting, the cache-storage calls anduser_mountstill make host file calls with the core held.Checklist
Ready for review
main; the title is a Conventional Commit. The branch is onbe663a08, the head ofmainwhen this PR was opened.main9046a8d7, before the rebase ontobe663a08; the runs made CreateDirectory and CreateFile through the changed code.mise run testpasses (any failure that also fails onmainis named). Not yet in CI's container at this head: its image could not be rebuilt here without a network. On this PC's own tools the same 56 checks fail onmainbe663a08and here, for tools this PC lacks; in CI's container at9046a8d7, 0 failed (Testing). CI's run on this PR settles it.main, or Measurements says why there is nothing to measure. For this fix, the check fails onmainand passes here (Measurements); the in-game cost is in Risks.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.tests/that reads changed source is updated, not weakened. None needed a change: the 11 checks that readhost/nn_fs.cpass as they are (test_fs_path_utf8 is Windows-only and skips here).main90fae88cbase. This PC has no llvm-mingw, sohost/nn_fs.cwas compiled on its own for Windows with zig cc, with the same diagnostics as main's, but it has not been built with llvm-mingw. The change adds no path handling.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold. Not yet: CI has not run on this head.🤖 Generated with Claude Code
Tested in game: Gentoo Linux, Ryzen 9 9950X3D, Radeon RDNA4 (1002:7550), Mesa RADV 26.2.6, sched_ext scx_lavd; windowless (
HOS_HEADLESS=1), on a desktop in use.longshot v3.2.0-17-g0918f3c5 (source 5542d96f), againstmain9046a8d7,longshot v3.2.0-15-g9046a8d7 (source 643c1bab), each a pack over the samemain90fae88cbase, so the two differ only by this PR's diff. The branch has since been rebased ontomainbe663a08(the new builder, #2286, and #2411); this PR's diff ofhost/nn_fs.cis unchanged, and the game was not run again there.HOS_FS_TRACE=1: a warm-up a side that only loads, three walk runs a side and a run a side that only loads. The walk, from a save: 12:00, teleport to the Market, walk out of its gate into Hyrule Field and back in (two transition autosaves), quit through the control bridge. Each walk run's first save went into a new slot, so CreateDirectory and two CreateFile ran throughsave_path_call, 9 calls a side (on this PR's side, the changed one).fs: save: published 3 files), and each run's save loaded in the next run of its side, with the game in play. 0 escapes and noran without a coreline in any run, and no wait of 50 ms or more inside a save. Thevcores:numbers overlap on every row (Risks).host/nn_fs.c) againstmain90fae88c, 18 runs, saw the same; it is in Testing.tests/test_vcore_fs_save.pycovers them.Opened by AGit (
refs/for/main) from my clone, without a fork, soforge.py pr createdid not post this comment: I post it by hand.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.