WIP: fix(nvn): HOS_NVN_WATCH_TEX reads its switch once, keeps its chains under a lock and walks only the stack the bind runs on #1954
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
5 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1954
Loading…
Reference in a new issue
No description provided.
Delete branch "siantm/Longshot:perf/nvn-bind-fields"
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
HOS_NVN_WATCH_TEX=<handle hex>(a debugging switch, off by default) logs who binds one texture handle, as the guestcall chains that reach
BindTexture(s)(watch_texinhost/nvn_native.c, called before every texture bind). Onmainit had these faults, fixed here:compared stayed unread and was read again at every bind. It is now read once under
watch_tex_lockat the firsttexture bind; every bind tests it with one inline atomic load (
watch_tex_want), the rest is a cold function(
watch_tex_chains); a value wider than 32 bits turns it off (it could never match);bind. They are looked up and added under the same lock, the lines logged outside it; after 64, one line says new ones
are no longer logged;
thread's stack, and logged the words there as callers. It now reads only whole 16-byte frames inside the guest stack
the bind's context runs on, that are mapped, going up it: the recording thread's stack (
hos_thread_stack, assvcQueryMemorybounds it), or, for a bind on annn::osfiber, the fiber's own stack (newhos_ctx_stackinhost/hos_thread.c, throughfiber_of_ctx), so a fiber's chains are walked asmainwalked them;The record writer this PR started with (
bind_handleswriting its record field by field, sobind_handlesandRECN/rec()stay as onmain) is taken out because its own benchmark showed no difference (1.83 ns a bind on bothsides), and it may come back with a profile share of the bind path that shows one.
How to see it on
main:python -I -B tests/test_nvn_watch_tex.py(new) fails there in wide, match and race, and inzero and other only because
maincallsgetenvtwice at its one read (see Testing). In the game, with the switchset: stray "callers" from the heap or another stack, a fault on an unmapped frame link, every chain past 64 logged at
every bind, and threads recording at once reading the switch at once.
Found in review: BeanBot's 61942,
61943 and
61948; futondestroyer's
62881 and
62882; dyst's
65797 (the fiber's stack).
Read first:
watch_texandwatch_tex_chainsinhost/nvn_native.c;hos_ctx_stackinhost/hos_thread.c;tests/test_nvn_watch_tex.py.Overlap: #2018 (
fix/nvn-bind-array-span@555b5160) merges with this PR without a textual conflict in either order(
git merge-tree); proposed order #2018 first, after which this PR's count check in the watch only repeats it. Itstest (
tests/test_nvn_bind_span.py) cuts the watch with stubs forhos_self,hos_thread_stackandhos_mem_region;the watch now calls
hos_ctx_stack, so with both in that test needs one stub line more (static void hos_ctx_stack(Ctx *c, uint64_t *a, uint64_t *n) { (void)c; *a = 0, *n = 0; }): on the merge of the two it stops at themissing declaration, and with that line it passes (fb3-1954-r3-light). Whichever lands second adds it. #2019 and #2020
merge with this head without a conflict. Left for a follow-up:
guest_chain, which the other watches use, keepsmain's unbounded walk (one bounded walker for both, dyst's suggestion), andsvcQueryMemorystill bounds a fiber'sstack query by the thread's stack.
Testing
Tested: Windows 11, i7-9700, RTX 4060 (NVIDIA 610.88), llvm-mingw clang 23.1.5; game-free checks only. This has
NOT run in the game on this PC, nor on Linux (Still to test). Rebuilt on
main@9dd41326without the record writer:757053eetest,ed959f67fix (the watch fixes of7c5dc91c,519f5c7e,0c4ecdf9andfe1dd999without the writer),e10e72d1hos_ctx_stack,3704a9b5fiber test,2eb5aef9fiber fix. Each check ran in a hidden console of its own, inone job of the PC's job queue (fb3-1954-r3-light; run on trees identical to these commits, before a commit message was
reworded):
python -I -B tests/test_nvn_watch_tex.pyat the head2eb5aef9, with no--cc(LONGSHOT_MINGW's clang) andagain with
CCset: "PASS: every case (unset, zero, other, wide, match, fiber, race)".757053ee(main's watch): fails in zero and other (the variable read twice at the first bind),wide (read again at every bind), match (the 33rd handle of a count of 33 read, the unmapped page and the allocation
above the stack read, every chain past 64 logged) and race ("the switch was read 16 times"); at
ed959f67(the watchfixed, no fiber case yet): passes; at
3704a9b5(the fiber case on the thread-bounded walk): fails in fiber only("600 < 0 < ..." where "600 < d1 < d2 < ..." was expected: the walk stopped at the fiber's link register).
tests/test_query_memory_free.py(cutshost/hos_thread.c) at the head: passes.tests/test_nvn_bind_span.py(#2018's) on the merge of this head with555b5160: stops at "call to undeclaredfunction 'hos_ctx_stack'"; with the one stub line above: "PASS: every array bind checks its count and its whole
span before it reads the array (104 cases)".
python -I -B tests/run_all.py --strict --changed origin/mainat the head (the checks that read a changed file, 8 atonce, the Vulkan headers through
RX_CPM; build job fb3-mxn-changed): 58 passed, 9 failed, 2 skipped(
test_posix_thread_priority.py,test_tracy_funcs.py: Linux only). The 9 (test_crash_record,test_dev_packs,test_host_guest_writes,test_libc_hooks,test_mods_list,test_profiling_mode,test_slow_frames,test_vcore_escape,test_vcore_mask0) fail onmain@9dd41326too, run by name from amainworktree in thesame job, with the same messages; none fails here alone.
Still to test:
maininterleaved, no switches set, expecting the same picture and frame times within the runs' spread (the watch's default
path is one atomic load before a texture bind) (by hand)
HOS_NVN_WATCH_TEXset to a handle it binds, expecting itsnvn: texture ... bound fromlines, each chain once (by hand)python3 -I -B tests/test_nvn_watch_tex.py, expecting it to pass (by hand)PR against
main, expecting the same frame times asmainand the same picture (the Test Farm: play Castle Town'smarket and Hyrule Field on Linux and the Steam Deck, this PR against main, comparing frame times and screenshots)
main, expecting the same frametimes as
mainand the same picture (by hand or on the farm, not run yet)Measurements
No game measurement yet: the game runs are in Still to test. With the switch off (the default) the change leaves one
relaxed atomic load and a not-taken branch before each
BindTexture(s), wheremaincalledwatch_texand read itsstatic; everything else runs only with the switch set. The before-and-after proof is the game-free fixture, run on
this PC (i7-9700, Windows 11, llvm-mingw clang) in fb3-1954-r3-light:
tests/test_nvn_watch_tex.py9dd41326(test commit757053ee)2eb5aef9getenvcalled once)3704a9b5(thread-bounded walk)Verdict: every case that failed on
mainpasses here (game-free fixture); no game measurement yet.Risks
With the switch off nothing but the inline test runs, as above. With it set: the walk now stops at any frame outside
the stack the bind's context runs on (a chain that left the stack, as through a callback on another stack, ends
there), the first new chain after 64 logs one line and later ones none, a value wider than 32 bits turns the watch off,
and
hos_ctx_stacklooks the context up in the fiber list under its shared lock once per logged bind. The watch'smapping check (
hos_mem_region) is a second guard the fixture does not decide on alone (its stacks are always mapped;the docstring says so). No switch added: a revert is enough. Players notice nothing (no saves,
longshot_options.inior caches change).Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named).main, or Measurements says why there is nothing to measure.third_party/untouched; nothing GPL-incompatible.When it applies
mainand passes here.tests/that reads changed source is updated, not weakened.Assisted-by:/Co-Authored-By:trailers.Ready to merge
main, measured again or a comment says why the numbers still hold.Possibly related, for context:
Recommendation: Request changes. The rewritten texture watch still races across recording threads, and the new test can pass with broken command-memory accounting. The reported benchmark shows no speedup, and the full path still needs an in-game comparison.
The host writes texture, image and sampler bind records directly and moves texture-watch logging off the usual bind path. This removes redundant initialization but adds a specialized record writer whose layout must stay in sync with the reader. No player benefit has been shown.
Does it help: Unproven. The PR reports 21 interleaved Windows microbenchmark rounds on an i7-9700, comparing
8859673ewith 3427c186. The medians were identical at 1.83 ns for one handle and 1.88 ns for four. The harness omitted the real allocator and texture watch. No in-game FPS or frame-time data was supplied.Other systems: The common Vulkan replay path consumes these records on NVIDIA, AMD and Steam Deck, including synchronous and threaded replay. The current payload fields and submit-time deferred source are preserved by inspection. A recording failure can still affect textures, images and samplers on every backend. The diff leaves queue ordering, descriptor registration, renderer APIs, saves, caches, audio and input untouched.
Switches: HOS_NVN_WATCH_TEX remains a hex-handle developer diagnostic, off by default, with its existing gpu/vk/switches.def row. Off is the right default. The change moves the check into the bind path and keeps lazy initialization. No switches were added or removed.
Reviewed:
5aa17e9634against2e1f6c32d3: Read AGENTS.md, CONTRIBUTING.md, docs/dev/standards.md, the Forgejo PR template, the full two-file diff and commit, and all of tests/test_nvn_bind_handles.py. Read the changed parts of host/nvn_native.c and the relevant bind, allocator, thread, guest-memory, submit and replay code. Skimmed the rest of the host file, test-runner and CI setup, and profiling metadata. Read the run results quoted in the PR. There were no comments, earlier reviews, changed generated files or translations. No separate review checklist or sibling Tooling checkout was present.; nothing run (a code review).Checked and fine:
Blocking (to change before merge)
Waiting on the author (a run, a measurement or a decision; no edit does it)
Suggestions (the author may take or leave)
host/nvn_native.c:2176: Check that the whole 16-byte frame is readable in the guest stack. An aligned frame pointer in the reserved window can still point at an unmapped page.host/nvn_native.c:2182: Protect the lookup and insertion together. Concurrent matches can duplicate a log or race for a slot, and an unseen chain logs on every match after the table fills.Testing: Read-only review. The author reports 20,000 passing fixture cases at 7a8eb80c and a changed-file suite with 19 passes, two failures also reported on its base, and one skip. The skip is unnamed. No run was reported at
5aa17e96, and no Linux build, game run or head CI result was supplied.Overlaps and merge order: The PR names a separate ReportCounter change from the same experiment. No sibling number or ordering dependency was visible in the checkout. The supplied base already includes #1946.
Automated code review by BeanBot (gpt-6.2-sol), on the pull request's opening. Nothing was run; a maintainer decides.
@ -2156,2 +2170,4 @@if (want == ~0ull)want = watch_tex_want = getenv("HOS_NVN_WATCH_TEX") ? strtoull(getenv("HOS_NVN_WATCH_TEX"), NULL, 16) : 0;if (!want) return;for (int i = 0; i < n; i++) {Validate the count and full readable handle span before the watch or the recorder reads it. An oversized count can currently make the watch run past the array before the recorder rejects it.
@ -2141,2 +2141,4 @@NV(nvnCommandBufferBindUniformBuffersDeferred) { bind_bufs_deferred(c, 0); }NV(nvnCommandBufferBindStorageBuffersDeferred) { bind_bufs_deferred(c, 1); }/* (longshot) The record written field by field, pad_ too, with no zeroing first (every byte is one of them) and onehandle stored as one: the memset and memcpy calls of a record of variable size were a third of a BindTexture. ThePlease source or remove the one-third figure. The supplied benchmark does not measure the whole BindTexture call.
@ -2156,0 +2163,4 @@static uint64_t watch_tex_want = ~0ull;static __attribute__((noinline, cold)) void watch_tex_chains(Ctx *c, const uint64_t *h, int n);static inline void watch_tex(Ctx *c, const uint64_t *h, int n) {if (__builtin_expect(watch_tex_want != 0, 0)) watch_tex_chains(c, h, n);Two recording threads can race with first-use initialization even when the watch is off. Initialize before recording or use an atomic fast-path read and once-only initialization.
@ -0,0 +14,4 @@import tempfilefrom pathlib import PathRUN_ALL_ARGS = ["--cc", "gcc"]Honor CC and choose an available native compiler by default. Forcing gcc breaks an llvm-mingw-only Windows setup.
@ -0,0 +19,4 @@ROOT = Path(__file__).resolve().parents[1]def block(text, marker):Use tests/srccut.py. This counter treats braces in diagnostic strings and comments as C syntax.
@ -0,0 +62,4 @@static uint8_t blk[2][4096];static uint32_t asked[2];/* (the recording's next bytes: cmd_alloc's, rounded to 8 as it does; the new code's block holds a pattern) */static uint8_t *cmd_alloc(nvn_cmd *o, uint32_t size) { (void)o; asked[0] = (size + 7) & ~7u; memset(blk[0], 0xA5, sizeof blk[0]); return blk[0]; }The stub skips the real allocator's counter writes, and both sides share hw_fix. Test independent expected counter deltas and check that the writer leaves the bytes beyond the record untouched.
@ -0,0 +105,4 @@p.add_argument("--cc", default="gcc")a = p.parse_args()nvn = (ROOT / "host" / "nvn_native.c").read_text(encoding="utf-8")cut = "\n".join(block(nvn, m) for m in ("static void put_hdr(", "static void hw_fix(", "static void bind_handles("))All cases pass if the inline watch condition is reversed. Exercise unset, matching, nonmatching and repeated binds with the child environment set explicitly.
5aa17e9634519f5c7e7dThank you for the review. Pushed, rebased onto current
main@6e1521a7(with #1949): head519f5c7e(was5aa17e96).git range-diffshows the PR's own commit unchanged by the rebase; on top of it,7c5dc91c(the test, first) and519f5c7e(the fix).Blocking points of the review:
:2166): fixed in519f5c7e. The inline test readswatch_tex_wantas an atomic (relaxed) load; the first bind reads the switch once underwatch_tex_lock, re-checking it there, and stores the handle atomically. A value wider than the 32 bits compared (sofffffffffffffffftoo, which stayed at the sentinel) now turns the watch off; before, such a handle could never match. Test: 8 threads binding at once for the first time read the switch once and log once;ffffffffffffffffreads it once over 1000 binds.:2173,:1745): the count part is fixed in519f5c7e: the watch reads nothing when the count is out of 0 to 32 (bind_handlesthen stops the game, as before);bind_handlescopies no handle for a count of 0 and copies one handle with an 8-bytememcpy, so a game array of any alignment is read as before. Test: a count of 33 whose 33rd handle matches logs nothing. The span part is not changed here: the array entry points check only the first byte ofP(4)onmaintoo, and this PR does not touch those entry lines (it changesbind_handlesand the watch). Checking8 * countbytes needs everyBind*sentry line changed, which is its own change; I list it for the owner.:2176): the walk is the same as onmain, but this PR moved it intowatch_tex_chains, so the fix stays in that function:519f5c7ereads a frame only when all 16 bytes are mapped guest memory (hos_mem_region), and stops at a link that does not go up the stack. Test: a frame linking to an unmapped page in the window, a frame running past the mapped stack (neither is read) and a link to itself (the walk ends).:2182): likewise frommain, inside the moved function, so fixed in519f5c7e: the lookup and the insertion happen together underwatch_tex_lock, and the line is logged outside it. Policy for a full table: after 64 chains, one line says new ones are no longer logged, then nothing more. Test: 64 chains, then new and repeated ones: one more line in total.tests:65): fixed in7c5dc91c. The check cuts the realcmd_allocandcmd_reserve_recand stubs the pool below them with malloc'd blocks filled with a pattern. All 20000 binds go into one recording, which grows. Each record is checked against bytes built from the inputs alone (header, fields,pad_0, handles).cmd_usedmust grow by the record's size, andhw_usedandlast_recby the hardware size given (or 16 + 4 per handle), not throughhw_fix. The 64 bytes after the record must keep the pattern. The comparison with the oldbind_handlesstays.tests:17): fixed in7c5dc91c, in the ordertests/test_fpx_sticky.pyuses:--cc, then$CC, then on WindowsLONGSHOT_MINGW's clang (whichrun_all.pysets to the clone's llvm-mingw), thenclang,gccorccon PATH. It fails, not passes, with no compiler.RUN_ALL_ARGS = ["--cc", "gcc"]is gone, so the runner no longer forces gcc.tests:108): fixed in7c5dc91c. The check runs the fixture once per case, withHOS_NVN_WATCH_TEXset in the child's environment: unset,0,beef(a handle no bind names),ffffffffffffffff,1234(new chain, repeated bind, the same chain from a four-handle bind, and the variable changed after the first bind, which is not read again) and 8 threads at once. With the condition reversed the switch would never be read, so the unset case (one read expected) and the matching case (lines expected) would fail. That is read from the code; the reversed condition was not run.tests:22): fixed in7c5dc91c:blockandbetweenfromtests/srccut.py, imported as the other checks do (the check's folder put onsys.path, so it works underpython -I). The brace counter is gone.Inline
:2144(the one-third figure): removed in519f5c7e. The micro-benchmark did not time the wholeBindTexture. The comment now says only what changed (nomemset, no run-time-sizedmemcpyfor one handle).Suggestions: taken in
519f5c7e. The record's size comes fromsizeof(nvn_op_hdr),offsetof(nvn_p_bind_handles, h)and the handle's size, and a_Static_assertholds the 8-byte invariant the writer relies on (no rounding). The frame walk and the dedup suggestions are points 3 and 4.Checked through the PC's job queue (job fb-1954-2, on this head):
tests/test_nvn_bind_handles.pypasses at519f5c7e(every case: records, unset, zero, other, wide, match, race), run with no--ccand again withCCset. At the test commit7c5dc91c(this PR's code before the fix) it fails in zero, other, wide, match and race, and records and unset pass there.tests/run_all.py --strict --changed origin/main: 19 passed, 2 failed, 1 skipped. The 2 failures (test_dev_packs.py,test_host_guest_writes.py) fail onmain@6e1521a7with the same messages. The description's Summary, Tested, Measurements and Risks are updated; nothing has run in the game."Does it help": BeanBot reads the PR's numbers right. The micro-benchmark shows no gain (1.83 ns a bind of one handle and 1.88 ns of four, on
mainand with this change), and nothing has been measured in play. Measurements now says so plainly. The change has no switch;HOS_NVN_WATCH_TEXstays off by default.Waiting on the author, all pending and in Still to test (none has run): interleaved in-game runs against the merge base in the Market and a heavy area (bind-path CPU time, average fps, 1 % lows, median and p99 frame times), dropping the change if the full path shows no gain; the local game run with the watch off (same picture) and on (its lines); the Windows and Linux builds, the full checks and CI on this head; the Tier 1 runs, Windows AMD included.
@Beanstalk This writes the NVN texture, image and sampler bind records field by field (host/nvn_native.c) instead of building each record and copying it, and the test of the off-by-default HOS_NVN_WATCH_TEX debug watch is inline; the bytes written must be the same. It has not run in the game here with this head yet. Please play Castle Town Market and Hyrule Field, a minute each, with a picture in each compared with main's, on Linux with NVIDIA, on Linux with AMD (RADV), on Windows with an Intel GPU and on the Steam Deck, the PR against main. The pictures must match main's (no wrong or missing textures), no crash; frame time p99 equal to main's or better, within the spread.
@siantm only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was for519f5c7, and the PR has changed since (fe1dd99), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.Recommendation: Request changes. Bound the diagnostic frame walk to the recording thread’s guest stack and make the concurrency fixture safe when it observes duplicate logs. The performance gain is still unproven.
The rebase includes the renderer build fix. The revised writer preserves the record layout and submit-time deferred handles. The fixture now runs the real allocator, checks independent counter deltas and record bytes, and checks the trailing bytes. The watch now initializes once under a lock, uses an atomic fast-path read and bounds the deduplication table. The missing full-span check in the bind-array wrappers is inherited. Prior review
Blocking
host/nvn_native.c:2191allows a rising frame link into any mapped guest allocation.hos_mem_regionalso accepts the image, heap, system objects and other threads’ stacks. The watch can read unrelated words and report them as callers. Bound the full 16-byte frame to the current guest stack usinghos_thread_stack, assvcQueryMemorydoes, and keep the mapped-range check. Model a second mapped allocation in the fixture so it catches an out-of-stack link.tests/test_nvn_bind_handles.py:137formats every log into the samelast_logarray. If deduplication regresses, multiple workers can write it concurrently, causing undefined behavior in the case the fixture should report as a clean failure. Count only in the threaded case or protect the fixture’s buffer with its own lock.Does it help? Unproven. The earlier 21-round microbenchmark had the same medians on both sides, 1.83 ns for one handle and 1.88 ns for four. After the source fixes, compare warmed and interleaved runs in the Market and a heavy scene against the updated merge base. Include bind-path CPU time, average FPS, 1% lows, median and p99 frame times. If the comparison stays within the spread, the measured result does not justify the record-writer optimization. The diagnostic fixes can be reviewed on their own merits.
Testing. The author reports all seven focused cases passed on Windows at this head. The selected suite reports 19 passes, two failures reproduced on base and one Linux-only skip. Head CI, native Linux build and gameplay remain open. The author’s farm request is awaiting approval and names Linux NVIDIA, Linux RADV, Windows Intel and Steam Deck. Add AMD Windows to the Tier 1 plan. The separate ReportCounter PR has no ordering dependency. Farm request ReportCounter PR
Reviewed
519f5c7eagainst6e1521a7. Checked the interdiff, all changes to both files and revision-matched Graphify maps. Read the relevant allocator, bind, submit, replay, memory-map and thread-stack source. Nothing from the PR was run.@ -2158,3 +2189,3 @@if ((h[i] & 0xFFFFFFFFull) != want) continue;uint64_t chain[8] = {c->x[30]}, fp = c->x[29];for (int k = 1; k < 8 && fp && fp < 0x8000000000ull && !(fp & 7); k++) chain[k] = G64(fp + 8), fp = G64(fp);for (int k = 1; k < 8 && fp && !(fp & 7) && hos_mem_region(fp, 16); k++) { /* (the whole frame mapped) */A higher frame link can land in another mapped guest allocation. hos_mem_region accepts the heap, image and other threads' stacks too. Keep the mapping check and bound the whole frame to the current guest stack via hos_thread_stack. Add a fixture case with a link into a second mapped allocation.
@ -0,0 +134,4 @@static void hos_log(const char *fmt, ...) {va_list ap;va_start(ap, fmt);vsnprintf(last_log, sizeof last_log, fmt, ap);If deduplication regresses, several workers can write last_log concurrently here. Make the fixture race-free even on failure by counting only in the threaded case or guarding the buffer with a fixture lock.
@Beanstalk This writes the NVN texture, image and sampler bind records field by field (host/nvn_native.c) instead of building each record and copying it, and the test of the off-by-default HOS_NVN_WATCH_TEX debug watch is inline; the bytes written must be the same. It has not run in the game here with this head yet. Please play Castle Town Market and Hyrule Field, a minute each, with a picture in each compared with main's, on Linux with NVIDIA, on Linux with AMD (RADV), on Windows with an Intel GPU and on the Steam Deck, the PR against main. The pictures must match main's (no wrong or missing textures), no crash; frame time p99 equal to main's or better, within the spread.
@siantm only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was forfe1dd99, and the PR has changed since (2eb5aef), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.@futondestroyer thanks for the second look (62883). Both points are fixed at
fe1dd999(test0c4ecdf9first, then the fixfe1dd999; still onmain@6e1521a7, so no rebase was needed this round):fe1dd999now walks a frame only when the whole 16-byte frame lies inside the current guest thread's stack, taken fromhos_thread_stack(hos_self(), ...)assvcQueryMemorydoes, and is also mapped (hos_mem_regionis still checked). A link that leaves the stack ends the walk. With no guest thread, nothing past the link register is walked. The fixture (0c4ecdf9) now maps a second allocation above the thread's stack and has a frame whose link points into it. At0c4ecdf9, with the walk still bounded only by a mapped region, thematchcase fails: it logged106 < c1 < c2 < c3and read the other allocation. Atfe1dd999it logs106 < c1and reads nothing there.last_log): the fixture'shos_lognow formats intolast_logunder its ownSRWLOCK, so lines logged from several workers when dedup regresses can no longer race on the buffer. The race case then fails on its counts.Run through this PC's job queue (fb-1954-3, Windows 11, llvm-mingw clang 23.1.5): at
fe1dd999,tests/test_nvn_bind_handles.pypasses all seven cases, both with no--ccand withCCset.run_all.py --strict --changed origin/maingives 19 passed and 2 failed. The two failures aretest_dev_packs,test_host_guest_writes, and they also fail onmain@6e1521a7. I added Windows with an AMD GPU to Still to test. The description's Testing section is updated.Still pending on my side: the interleaved in-game runs against the merge base (Market and a heavy scene: bind-path CPU time, average fps, 1% lows, median and p99 frame times). If those stay within the spread, I'll say plainly that the record-writer part has no measured gain. Also pending: Linux, Tier 1 and CI at this head.
On BeanBot's point 2, count and span before the read and the inherited span check named in your review: the span part is now its own fix, #2018 (
fix/nvn-bind-array-span, test405eb181, fixcfd24dda, onmain@40bfceb0). EveryBind*sentry point checks the count (0 to 32) and then all 8 x count bytes of the game's array in the guest window before the watch or the record reads it, andBindVertexBufferschecks its 16-byte ranges whole.tests/test_nvn_bind_span.pyruns the real entry points over a guest window followed by no-access pages: onmain13 of its cases read past the window's end and fault before any stop, with the fix all 55 pass (this PC's job queue, fb3-sep-2). It and this PR merge without a conflict in either order; not run in the game yet.Description: Risks names #2018 (the Bind*s whole-array check) and the proposed landing order; no code change.
Recommendation: Wait for gameplay measurements and the remaining WIP checks. The two findings in my previous review are resolved at
fe1dd999. The watch checks that each 16-byte frame is inside the recording thread’s mapped guest stack before reading it. The fixture exercises a link into a second mapped allocation and serializes its log writes when several workers log concurrently. Previous reviewDoes it help? Unproven. The earlier 21-round microbenchmark reported the same medians on both sides: 1.83 ns for one handle and 1.88 ns for four. For the performance change, compare warmed, interleaved runs with the watch off in the Market and a demanding scene. Include bind-path CPU time, average FPS, 1% lows, and median and p99 frame times. If the results overlap, keep the diagnostic fixes independently of the record-writer optimization.
Testing. The author reports seven focused Windows cases passing at this head and the new mapped-allocation case failing before its fix. The selected suite reported 19 passes, two failures reproduced on the base, and one Linux-only skip. Local gameplay with the watch off and on, the Linux build, substantive CI and Tier 1 coverage remain open. The existing farm request is pinned to this pair and can check visual parity, crashes and p99 regressions. AMD Windows and repeated gameplay measurements remain separate. Current farm request
Reviewed
fe1dd999against6e1521a7with the exact revision maps, both changed files and the relevant allocator, replay, memory and thread-stack source. The command layout and submit-time deferred handles are preserved by inspection.HOS_NVN_WATCH_TEXstays off by default. The inherited wrapper span check is tracked separately in the author’s proposed first landing. Bind-array span fix@futondestroyer, on your review: no code change this round; the head stays
fe1dd999. None of the following has run yet:main@6e1521a7in the Market and in Hyrule Field (average fps, 1 % lows, median and p99 frame times). They are queued on this PC behind its pack builds. Bind-path CPU time is asked for with them.If the record writer's comparison overlaps, I will say so plainly in Measurements, and whether to keep the diagnostic fixes apart from it then becomes a question for this PR's scope.
Verdict: Request changes. The
HOS_NVN_WATCH_TEXfixes are right and well tested, and every BeanBot and futondestroyer point is fixed in the code. But the half the title is for, the field-by-field record writer, is a perf change whose own benchmark shows no gain (1.83 ns and 1.88 ns on both sides). The farm run as requested cannot show a gain of a few nanoseconds a bind, and "no measured gain, no merge" (docs/dev/standards.md, Performance and build changes) holds it.For a player nothing changes: the recorded bytes are the same, and the watch is a debugging switch that stays off. What is better is the off-by-default diagnostic: no first-use race, no fault on a bad frame link, no stray caller words, and a bounded dedup table. The record writer adds a second, hand-kept copy of
rec()'s job for one record type, with nothing measured to pay for it.Reviewed:
fe1dd999against6e1521a7(its merge base withmain; no merge commits on the branch):host/nvn_native.c's change read in full, with what it relies on (rec/RECN,cmd_alloc,cmd_reserve_rec,put_hdr,hw_fix,nvn_p_bind_handlesandnvn_op_hdringpu/render/rx_nvn.h, theBind*entry points,hos_thread_stack,hos_self,hos_mem_regionand its lock,query_memory, the fiber code inhost/hos_thread.c,host/posix/windows.h's SRWLOCK).tests/test_nvn_bind_handles.pyread in full. Both of futondestroyer's reviews and BeanBot's were checked against the code. The head merges into currentmain(9dd41326) with no textual conflict (git merge-tree). Nothing run (a code review).Fixed in this review: nothing.
Checked and fine:
RECN+rec()wrote. The header (8) plusstage, first, n, pad_(4 × int32) plussrc(8) makes 32 bytes with no implicit padding, then 8 bytes a handle. That is a multiple of 8, socmd_alloc's rounding changes nothing and the header's size equals the allocation. The_Static_assertpins that.pad_is written, so no byte is left as the pattern.cmd_alloc'sput_hdr(p, 0, size)is overwritten as before.n == 0copies nothing.n == 1is a fixed 8-bytememcpy, still unaligned-safe for a guest array.watch_tex_lock, with a double-check. The fast path is a relaxed atomic load of a scalar, which is enough. A value over 32 bits turns the watch off, as Risks says.switches.def's row (diag, off) still holds, and no new switch is added.hos_mem_region(which takes hos_mem's lock) orhos_log, so there is no lock-order issue.[stk, stk + stk_n - 16]with no overflow (it checksstk_n >= 16first). It stops on a non-rising link, and it walks nothing with no guest thread. This answers futondestroyer's 62881.pad_shows as the 0xA5 pattern. Counter deltas come from the model, not fromhw_fix. Removing thenext <= fpbreak fails case 104. Bounding by any mapped region fails case 106 (shown at0c4ecdf9). Racing first use failsrace, and the fixture'slast_logis now under its own lock (62882). It honours--cc/$CC/LONGSHOT_MINGWand usessrccut.Blocking (to change before merge)
host/nvn_native.c:2146(the record writer): this is aperfchange with no measured gain. The PR's own 21-round micro-benchmark has identical medians on both sides. The requested farm run (a minute a scene, p99 "within the spread") can only show "no regression", never a gain this size. Two ways forward:mainagainst this PR:bind_handles' or theBind*entries' CPU share in a profile of the Market, or a harness that records through the realcmd_alloc. A bump allocator can hide the memset.RECN), retitle asfix(nvn): HOS_NVN_WATCH_TEX ..., and land the watch fixes on their own merits. #2018 already defers its watch points (races, frame walk) to them.Also note #2050 makes
rec()always-inline and dropscmd_alloc's header write. What is left for a specialised writer here is smaller still.Suggestions (the author may take or leave)
host/nvn_native.c:2193: on annn::osfiber,c->x[29]points into the fiber's own stack (game memory,HosFiber.stack/size).hos_thread_stack(hos_self())still gives the thread's stack, so the walk now ends at the LR.mainwalked those frames. If the game records any commands on a fiber, the watch loses its callers there. A smallhos_ctx_stack(Ctx *, &addr, &size)inhos_thread.c(viafiber_of_ctx) would bound the walk to the stack actually in use.svcQueryMemoryandGetCurrentStackInfoshare the same gap.guest_chain(line 1711, used by the other watches) keeps the unbounded walk this PR fixes for the texture watch. One bounded walker used by both would fix them all; that can be a follow-up._Static_assertchecks that the size is a multiple of 8, but not that the struct has no hidden padding. Anoffsetof(nvn_p_bind_handles, src) == 16 && offsetof(nvn_p_bind_handles, h) == 24would pin the "every byte is a field" claim the writer relies on.hos_mem_regionhalf of the walk's condition: the fixture's stack is always mapped, so removing it passes. That's fine as a belt-and-braces check, but the docstring could say so.Testing: the author ran the focused check on Windows (all seven cases pass at
fe1dd999;matchfails at0c4ecdf9as it should), plusrun_all --changed(19 pass, the 2 failures also fail onmain). Not done yet: an in-game run, a Linux build, CI at this head, Tier 1, Windows AMD. The farm request 63024 is for this head and would cover picture parity, crashes and the Linux build. It is not the measurement blocking point 1 needs.Overlaps and merge order:
hos_thread_stack/hos_mem_region. 2018 first, as proposed, is fine.static void cmd_reserve_rec(/static uint8_t *cmd_alloc(tostatic inline ...and addscmd_grow. That breaks this PR'sblock(...)cuts, and 2018's too. Whichever lands second updates the other's test.guest_chainbut not this code. The other siblings (2026, 2025, 2022, 1995, 1994, 1968, 1955) touch other parts of the file.@ -2143,1 +2143,4 @@/* (longshot) The record written field by field, pad_ too, with no zeroing first (every byte is one of them), and onehandle copied as one 8-byte block: no memset, and no memcpy of a size known only at run time for BindTexture. Thebytes recorded are the same (tests/test_nvn_bind_handles.py) */static void bind_handles(nvn_cmd *o, uint32_t op, int stage, int first, int n, const uint64_t *h, uint64_t src,No measured gain for the record writer: the micro-benchmark's medians are identical on both sides, and the farm's p99-within-spread can't show a few ns a bind. Show the targeted number (bind path CPU share, main vs PR) moving outside the spread, or keep RECN and land the watch fixes as fix(nvn).
@ -2159,2 +2192,2 @@uint64_t chain[8] = {c->x[30]}, fp = c->x[29];for (int k = 1; k < 8 && fp && fp < 0x8000000000ull && !(fp & 7); k++) chain[k] = G64(fp + 8), fp = G64(fp);uint64_t chain[8] = {c->x[30]}, fp = c->x[29], stk = 0, stk_n = 0;if (hos_self()) hos_thread_stack(hos_self(), &stk, &stk_n); /* (none: no frame is walked) */On an nn::os fiber, x29 is in the fiber's stack, not hos_thread_stack(hos_self())'s, so the walk now stops at the LR where main walked on. A ctx-aware stack lookup (fiber_of_ctx) would keep those chains.
fe1dd999252eb5aef906WIP: perf(nvn): a handle bind's record is written field by field, and HOS_NVN_WATCH_TEX's test is inlineto WIP: fix(nvn): HOS_NVN_WATCH_TEX reads its switch once, keeps its chains under a lock and walks only the stack the bind runs onPushed
2eb5aef9onmain@9dd41326(fromfe1dd999on6e1521a7) and retitledfix(nvn), as dyst's review 65798 asked: the record writer is out (bind_handles,RECNandrec()as onmain), the HOS_NVN_WATCH_TEX fixes stay (757053eetest,ed959f67fix), and the walk follows annn::osfiber's own stack (e10e72d1hos_ctx_stack,3704a9b5test,2eb5aef9fix). Description rewritten: Summary, Testing on the new base (fb3-1954-r3-light, fb3-mxn-changed), Measurements, Risks, and #2018's order (no textual conflict; its test needs onehos_ctx_stackstub line once both are in).@dyst, on your review, and @futondestroyer, on yours: pushed
2eb5aef9onmain@9dd41326. The PR is retitledfix(nvn): HOS_NVN_WATCH_TEX ....bind_handles,RECNandrec()are as onmain.offsetofasserts you suggested went with it. They pinned the writer's assumption that every byte of the record is a field, whichrec()'s zeroing does not need.mainwithout the writer.757053eeis the test, nowtests/test_nvn_watch_tex.py. It fails onmainin wide, match and race, and in zero and other onmain's twogetenvcalls.ed959f67is the fix. The switch is read once under a lock, the chains are looked up under it, the walk is bounded and mapped, and the count is checked. It comes from7c5dc91c,519f5c7e,0c4ecdf9andfe1dd999.cmd_alloc/cmd_reserve_rec, so the semantic conflict with #2050 you noted is gone too.e10e72d1addshos_ctx_stack(Ctx *, &addr, &size)inhost/hos_thread.c. For a fiber's context it gives the fiber's own stack (throughfiber_of_ctx); otherwise it gives the thread's.3704a9b5is the test:It fails at the thread-bounded walk ("600 < 0 ..." where "600 < d1 < d2 ..." was expected).
2eb5aef9bounds the watch byhos_ctx_stack.svcQueryMemoryandGetCurrentStackInfokeep their gap, and so doesguest_chain's walk. One bounded walker for all of them is left for a follow-up.hos_mem_regionalone.hos_ctx_stackstub line once both PRs are in: on the merge it fails without that line and passes with it. Whichever PR lands second adds the line, as the description says.Tested on
9dd41326:tests/test_nvn_watch_tex.pypasses at the head (all 7 cases). The changed-file checks gave 58 passed and 9 failed; the same 9 fail onmainwith the same messages. Details are in the description.@Beanstalk This fixes the HOS_NVN_WATCH_TEX debug watch (off by default; host/nvn_native.c): it reads its switch once, keeps its chains under a lock and walks only the stack the bind runs on. With the switch unset, as players run, nothing should change. Please take a picture at a fixed point with no input, right after the start save settles, and one main-against-itself picture; then play Castle Town Market (from the market-entrance save) and Hyrule Field, two minutes each, for frame times; the PR against main on the same PC with HOS_NVN_WATCH_TEX unset, and one more run of the PR with HOS_NVN_WATCH_TEX set; on Linux with NVIDIA, Linux with AMD (RADV), Windows with Intel and the Steam Deck. The same picture as main within main's own noise, no crash or hang (with the watch set too); unset, frame time p99 equal to main's, within the run-to-run spread.
@siantm only members of
BigBeanscan start the farm. One of them can approve this request by reacting 👍 to it or to this comment.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.