Repository navigation
Deflake StateObjectLockEarlyUnlock (racing sleeps -> handshake) - #316
Merged
Merged
Conversation
StateObjectFixture.StateObjectLockEarlyUnlock raced two fixed sleeps: t1
unlocked early, slept 50ms and then asserted t2 had run, while t2 slept 10ms
before starting. So it asserted that a loaded machine schedules t2 inside a
40ms window. It does not always: the test failed on macos-latest/Debug on an
imgui packaging PR that touches no state code, and independently ~90 minutes
earlier on fix/material-binding-arg-order, blocking both.
Only the in-thread assertion failed; the identical check after the joins
passed, which is the signature of a scheduling race rather than a defect.
Replace the sleeps with a handshake. t1 signals once the lock is actually
held, so t2's acquisition genuinely tests the early unlock instead of
possibly beating t1 to the lock; t1 then waits on the condition variable for
t2 rather than sleeping and hoping. The property under test is unchanged --
t2 must be able to take the lock while t1 is still inside the
state_lock_scope, which is possible only because t1 released it early. Were
unlock() a no-op, t2 would block until the scope exits and t1's wait would
time out, so the test still fails on the defect it exists to catch. The 10s
timeout bounds only the failure path; a passing run returns as soon as t2
signals.
Verified with the real boost primitives against a model of state_lock_scope
whose unlock() can be made a no-op:
- good path: 3000/3000 passed, 0.079 ms/iter
- under 3x CPU oversubscription (96 spinners on 32 cores, the loaded-runner
condition that broke the original): 3000/3000 passed, 1.738 ms/iter
- defect path (unlock() ignored): still DETECTED in both runs
Also drops ~50ms of unconditional sleeping from every run of this test.
Note: 45 other fixed-duration sleeps remain in this file, and the
StateObjectFixture suite has also been seen segfaulting in
StateObjectMixedInstanceThreading on linux/debug. Neither is touched here.
transfix
added a commit
that referenced
this pull request
Nov 24, 2026
…es (#1474) ## Why In cvcdbg's demo3 (Humvees on the Austin city), Firefox misses a refresh on 5.7–8.3% of frames. The cost is the shadow bake. Since CVC-DBG/cvcdbg#311 the map is re-baked every frame a vehicle moves (a stale map let a Humvee drive into its own shadow), and each bake re-drew every caster from the sun, the city included. The bake did not get cheaper at 2048, so it is the draws, not the map's size. ## What `StridedShadowBaker` now bakes the static casters apart from the moving ones (`SceneGraph::setShadowStaticCache`, on by default). - **Sorting.** A caster that changed (moved, deformed, restyled) within the last 60 checks, or appeared since the last whole bake, is **moving**. The rest are **static**. - **Whole bake.** Draws the static casters into the maps the lookup reads. It keeps aside their colour and depth where the moving casters are about to be drawn (a save-under, in a saved map), then draws the moving casters over them, depth-tested. The maps hold exactly what one draw of every caster would, for the old bake's cost plus small copies. - **Moving-only bake** (only moving casters changed). Puts back the texels it kept aside where the moving casters were, keeps aside those under their new places, and draws them there, with the last whole bake's light cameras. - Footprints are each caster's bounds through the light camera, plus two texels. - Rectangles merge where one rectangle around two covers at most twice what they do (a convoy becomes one or two); past 16 they become one. - A vehicle's shadow is redrawn in the frame it moves, so there is no lag and no stale self-shadow. - **What makes a bake whole**, and re-sorts the casters: - a light changes; - the focus moves 2% of its radius (unchanged); - a static caster changes or leaves; - a moving one leaves the bounds the light cameras were fitted to; - `invalidateShadowBake()`, or a resolution change; - several moving casters have stopped and are most of the moving set (they become static). - **When the cache is not used:** with a casting camera light (VTK moves it with the view), under VTK's compositing z pass, or until both kinds of caster are present. Then the bake is as before, and no second set of maps is made. - **Only 3-D props are casters now.** A HUD's text or image actor never drew into the maps, but a changing one used to force a bake. - **Supporting changes:** - `ShadowCasterFilterPass` gains `Preserve`: the bake's camera pass without its clear, optionally scissored. - The baker's `ReleaseGraphicsResources` frees the static maps too, and makes the next frame bake. - Switching the cache off frees the static maps. - `SceneGraph::shadowBakeStats()` reports the bakes, how many were whole, the static and moving casters at the last bake, and the share of the maps it copied and drew into. **Cost:** while both kinds of caster are present, a saved map and depth buffer per casting light at the shadow resolution: +128 MB at 4096 for one light, +8 MB at 1024. With several casting lights, each also gets a depth buffer of its own. ## Measured **Native, synthetic, demo3's caster mix.** On the GTX 1650, Release, vsync off, 1600×900, map 4096 focused, with the low-memory mapper forced as on WebGL and in demo3's fast draw path. The mix is: - one 980k-triangle static mesh; - 6 movers of 7k triangles and 24 of 290, all moved every frame. Render plus `glFinish` per frame, cache on and off in interleaved blocks, two runs: | | Run 1 | Run 2 | |---|---|---| | cache off | 14.23 ms | 14.33 ms | | cache on | 10.19 ms | 10.18 ms | | shadows off | 4.07 ms | 4.12 ms | **In demo3 and demo3-ari themselves** (native, follow camera, `--capture-live`, 300 frames): | | static casters | moving casters | whole bakes | map share of a moving-only bake | |---|---|---|---|---| | demo3 | 3 (the city, 978k cells) | 30 (6 Humvees + 24 wheels) | 95 of 540 (18%) | 0.11% | | demo3-ari | 4 | 13 | 96 of 1080 (9%) | 0.12% | The remaining whole bakes come from the shadow focus following the chase camera (2% of its radius). Native wall-clock frame times of the demos were too noisy on this shared box to compare (load 30–200, the GPU shared with other sessions). **Browser.** cvcdbg main (#316, JSPI and wasm exceptions) built for wasm-mt against this branch. Cache on and off are the same build: a local, uncommitted `--no-static-cache` flag sets `setShadowStaticCache(false)`. Headless Firefox 158 and Chromium 145 on the GTX 1650, 1600×900, `?prof&flags=follow`, map 4096. Each run is 150 s with the first 40 s dropped, runs interleaved, box load 15–25. | | frames over 20 ms, cache off | frames over 20 ms, cache on | render, off → on | |---|---|---|---| | demo3, Firefox (5 runs each) | 1.3% (0.5–2.9) | 2.3% (1.8–3.1) | 10.8 → 10.3 ms | | demo3-ari, Firefox | 7.4% | 2.7% | 10.7 → 7.6 ms | | demo3, Chromium | 0.0% | 0.0% | 8.2 → 7.8 ms | | demo3, Firefox, camera not following (no save-under yet) | 13.3% | 1.8% | 14.5 → 13.5 ms | Before the save-under (`d2f1ecce9`), a whole bake copied the whole map, and demo3 in Firefox missed 4.7–5.6% against 1.5–2.0%. With the save-under, a whole bake costs no more than before. In demo3 with the follow camera, Firefox still misses about a point more with the cache. Its CPU per frame is lower, so that point most likely comes from the 6% of bakes that are still whole, now drawn in two passes. WebGL calls per frame (`?glcount`, Chromium) fall only 2–6% (demo3 3,160 → 3,086). The 30 moving vehicle parts still make about 1,500 calls per bake, and that is Firefox's remaining shadow cost. ## Tests - **New `cvcgl_shadow_static_cache`.** Two scenes alike in every way, one cached, one not. Every frame both make the same change, and every frame's picture must be the same, byte for byte (on Apple's GL, within 2 levels). - A vehicle drives through a building's shadow for 38 frames: - each frame matches the reference and differs from the frame before; - its shadow is on the ground beside it in the frame it moved to; - the building's shadow is on its roof; - after the first bakes, every bake draws only the vehicle, over 0.12% of the map (0 whole). - These each give the reference picture, and a whole bake where expected: a building moved, hidden and shown; the moving vehicle hidden and shown; a car hidden and shown; the sun turned; a caster added; the focus set, moved and cleared. - Cars that stop become static in one whole bake, once they are most of the moving set. - With the cache off, every bake is whole and nothing is cached. - At interval 2, the bakes stride as before, and stale frames match too. - **The new test fails, as it should, on three broken builds:** - the blit without depth: 14 of 38 frames match; - no redraw of the moving casters (#311's stale map): 1 of 38 frames match; - the copy forgetting where the casters were: the interval-2 frames don't match. - **`cvcgl_shadow_pass_reuse` F.** A moving box adds exactly one texture, framebuffer and renderbuffer. A resolution change re-makes them, and the cache off or shadows off frees them. No VTK errors. - **`cvcgl_shadow_pass_programs` A.** Only the mover switches pass sets now, twice a frame. The static casters stay on the view's. - Locally (Linux, GTX 1650, `CVC_REQUIRE_RENDER=1`), all pass: - `cvcgl_shadow_static_cache`, `cvcgl_shadow_casters`, `cvcgl_shadow_pass_reuse`, `cvcgl_shadow_pass_programs`; - `cvcgl_city_sun_shadows`, `cvcgl_shadow_caster_growth`, `cvcgl_shadow_volume`; - `cvcgl_lod_node`, `cvcgl_texture_rows`, `cvcgl_ariadne_scenes`, `cvcgl_room_view`, `cvcgl_multi_app`. ## Not in this PR - The web demos get this only through a libcvc release and a cvcdbg pin. No release is cut here; the 4.0.6 tag is tracked separately. - With a moving shadow focus, demo3 still makes a whole bake every 2% of the focus radius it travels: 6% of its bakes in the browser. - Firefox's remaining cost is per WebGL call: the moving casters' draws in the bake, at about 50 calls each. Fewer or leaner bake draws for them would be the next step.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The flake
StateObjectFixture.StateObjectLockEarlyUnlockraced two fixed sleeps:t1unlocked early, slept 50 ms, then asserted
t2had run — whilet2slept10 ms before starting. It asserted that a loaded machine schedules
t2inside a 40 ms window.
It doesn't always. This failed
recipe / macos-latest / Debugon#313 — an imgui packaging PR that touches zero state code —
and independently ~90 minutes earlier on
fix/material-binding-arg-order(run 33679273045). It is currently blocking unrelated PRs.
Tell-tale: only the in-thread assertion failed; the identical
EXPECT_TRUEafter the joins passed. The flag did get set, just late — the signature of a
scheduling race, not a defect.
The fix
Replace the sleeps with a handshake:
t1signals once the lock is actually held, sot2's acquisitiongenuinely tests the early unlock rather than possibly beating
t1to thelock (which is what
t2's 10 ms sleep was crudely approximating).t1then waits on a condition variable fort2instead of sleeping andhoping.
The property under test is unchanged:
t2must be able to take the lock whilet1is still inside thestate_lock_scope, which is possible only becauset1released it early. Wereunlock()a no-op,t2would block until thescope exits and
t1's wait would time out — so the test still fails on theexact defect it exists to catch. The 10 s timeout bounds only the failure
path; a passing run returns as soon as
t2signals.Verification
Built against the real boost primitives with a model of
state_lock_scopewhose
unlock()can be made a no-op, so both paths are exercised:unlock()ignored)That last row is the important one: the deflake does not buy stability by
making the test vacuous.
Also drops ~50 ms of unconditional sleeping from every run.
Not addressed here
but rewriting them all in one pass is a much larger and riskier change.
StateObjectFixturesuite has also been seen segfaulting inStateObjectMixedInstanceThreadingon linux/debug (run 33663340579). That isa different and more serious signal, and is untouched by this PR.