Skip to content

Speed up Typhon.Engine unit tests (~189s → ~50s) + fix a page-cache seqlock stall - #550

Merged
nockawa merged 3 commits into
mainfrom
perf/ut-exec-time
Jul 20, 2026
Merged

nockawa merged 3 commits into
mainfrom
perf/ut-exec-time

Conversation

@nockawa

@nockawa nockawa commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Summary

Cuts Typhon.Engine unit-test wall-clock from ~189s to ~50s (~3.8×), and fixes a real page-cache seqlock stall found along the way plus a false-positive in the regression test that guards it.

Changes

Page-cache seqlock stall fix (775ef9a)

Reused cache slots kept a stale odd ModificationCounter, so a checkpoint copying such a page spin-waited its 100ms skip timeout on a phantom writer. Fix: reset the counter to even on slot reuse (TryAcquire), plus a guard in CopyPageWithSeqlock that skips a stale-odd non-Exclusive page immediately instead of spinning.

Reproduced and verified by measurement (min-cache pressure schema, timing engine close):

  • reverted (pre-fix): 2/12 closes spiked to ~325ms (≈ 3 × the 100ms spin)
  • with the fix: max 11ms, no stall

Test-execution speedups

  • Parallelization: fixture-level parallelism enabled — removed 79 stale [NonParallelizable] markers (unblocked by PR Enforce StorageMode is fixed per (component, revision); fixes parallel-fixture data-loss #547's StorageMode-per-(name,revision) fix), plus per-fixture temp-dir I/O isolation and an 8 MiB test cache default.
  • fsync-skip under TestMode: same-process tests read back through the OS page cache, so RandomAccess.FlushToDisk is dead weight; crash-durability fixtures leave TestMode off and keep real fsync.
  • Heavy-fixture trims: DifferentialRecoveryOracle LargeDrain 4000→600 (7.4s→0.3s), ChunkBasedSegmentBitmapL3, HighResolutionTimer Drift, ParallelFence tick counts — no coverage loss.

Seqlock invariant-check scope fix

The regression check CountQuiescentPagesWithOddSeqlock inspected every resident page, including the CK-05 meta pair (logical pages 0–1), which does not participate in the seqlock (different header format, IsExternallyPersisted, never copied by CopyPageWithSeqlock). It read the meta generation as an "odd counter" and failed ~10% of runs. Scoped the check to seqlock pages; added a coherent under-lock diagnostic collector for failure detail. Engine unchanged.

Verification

  • Full suite: 5/5 runs clean — 4030 passed / 0 failed / 38 skipped.
  • Seqlock invariant check: 0/30 isolated after the scope fix.
  • Stall fix: reproduced pre-fix, absent post-fix (see above).

🤖 Generated with Claude Code

nockawa added 2 commits July 20, 2026 16:19
…ing the ModificationCounter, so the checkpoint spin-waited its 100ms skip timeout on a phantom writer (~650ms per engine close). Reset on reuse + guard CopyPageWithSeqlock against a stale odd counter on an unlatched page. Adds a regression test.

Parallelize the suite: strip [NonParallelizable] from fixtures whose only reason was the archetype-registry race (fixed upstream by #547). Isolate the 7 Durability fixtures' DBs into per-fixture temp dirs to remove parallel file-lock contention. Raise the default test page-cache 2→8 MiB. Keep NonParallelizable on the timing-sensitive Runtime fixtures.
@github-actions

Copy link
Copy Markdown
Contributor

📝 Doc-accuracy review (advisory · scoped · Layer 2)

Reviewed:

  • doc/key-concepts/page-cache.md
  • doc/key-concepts/storage-mode.md
  • doc/key-concepts/bulk-load.md
  • doc/key-concepts/cluster-storage.md
  • doc/in-depth-overview/02-storage.md
  • doc/guide/using-with-ai-coding-agents.md
  • doc/feature-set/Storage/page-cache.md
  • doc/feature-set/Storage/page-integrity.md
  • doc/feature-set/Storage/epoch-dirty-tracking.md
  • doc/feature-set/Storage/file-locking-lifecycle.md
  • (Remaining feature-set catalog entries skimmed; no gross contradictions found.)

Findings:

  1. doc/key-concepts/page-cache.md:13 ↔ test/Typhon.Engine.Tests/TestBase.cs:375

    "Defaults are intentionally small (a 2 MB cache) so development exercises the cache machinery"

    The PR changes the default test-fixture cache from PagedMMF.MinimumCacheSize (2 MiB) to 8 * 1024 * 1024 (8 MiB). The sentence is now wrong.

    Suggested fix: "Defaults use an 8 MiB cache; fixtures that deliberately exercise eviction set an explicit [Property("CacheSize", ...)]."

  2. doc/in-depth-overview/02-storage.md:110 ↔ test/Typhon.Engine.Tests/TestBase.cs:375

    "Unit tests opt into it via the internal TestMode flag, which keeps the cache under constant pressure (eviction, backpressure, and the dirty-counter machinery all exercise heavily, so bugs in those paths surface early)"

    The PR comment at TestBase.cs:373 explicitly says the 2 MiB floor "forces constant eviction churn that is unrepresentative and slow." The default is now 8 MiB; constant cache pressure is no longer the general test profile.

    Suggested fix: Replace the parenthetical with: "(fixtures that deliberately exercise eviction opt in via [Property("CacheSize", ...)] rather than running at the minimum by default)".

  3. doc/in-depth-overview/02-storage.md:372 ↔ src/Typhon.Engine/Storage/internals/PagedMMF.cs:1731

    "There's a 100 ms warning threshold; if the counter has been odd that long, the page is skipped this cycle (a writer is hung or simply in flight)."

    The PR adds a second skip path in CopyPageWithSeqlock: an odd counter on a page whose PageState is not Exclusive is skipped immediately (stale counter — no writer to wait for), logged via LogStaleSeqlockCounterSkip, and does not wait for 100 ms. The doc implies 100 ms timeout is the only skip condition.

    Suggested fix: "There are two skip conditions: an odd counter on a page not in Exclusive state is a stale counter and is skipped immediately; an odd counter held by a real exclusive writer for longer than 100 ms also causes a skip (the writer is hung or in backpressure)."

Advisory & scoped to the pages the change touches — recall for cross-cutting or unmapped drift is covered by the weekly Layer 3 audit.

@github-actions

Copy link
Copy Markdown
Contributor

📝 Doc-accuracy review (advisory · scoped · Layer 2)

Reviewed:

  • doc/in-depth-overview/02-storage.md (deep scrutiny — edited in this PR)
  • doc/key-concepts/page-cache.md (deep scrutiny — edited in this PR)
  • doc/guide/using-with-ai-coding-agents.md (deep scrutiny)
  • doc/key-concepts/storage-mode.md
  • doc/key-concepts/cluster-storage.md
  • doc/key-concepts/bulk-load.md
  • doc/feature-set/Storage/README.md
  • doc/feature-set/Storage/page-cache.md
  • doc/feature-set/Storage/page-integrity.md
  • doc/feature-set/Storage/epoch-dirty-tracking.md

Skipped (low drift risk, no cache-size / seqlock / TestMode content expected): file-locking-lifecycle.md, page-allocation-occupancy.md, page-compression-future.md, segment-chunk-allocation.md, storage-introspection.md, string-table.md, vsbs.md, pluggable-storage-backend/ (×3)


Findings:

  1. Config table says 2 MB default; narrative (updated by this PR) says 256 MiB

    • doc/in-depth-overview/02-storage.md:423 — Default column for DatabaseCacheSize reads 256 × 8192 = 2 MB. That arithmetic equals 2 MiB, which is MinimumCacheSize (MinimumMemPageCount × 8 KiB), not the production default. The PR updated the narrative directly above this table to explicitly state the production default is 256 MiB and the test default is 8 MiB, making the table value inconsistent.
    • Code: src/Typhon.Engine/Storage/internals/PagedMMF.cs:60 — DefaultDatabaseCacheSize = 256UL * 1024 * 1024 (256 MiB); src/Typhon.Engine/Storage/public/PagedMMFOptions.cs:56 — property initialised to DefaultCacheSizeBytes.
    • Suggested fix: Change the Default cell from `256 × 8192 = 2 MB` to `256 MiB` (and separately note 2 MiB as the hard minimum in the description cell if desired).
  2. Seqlock description says "retries if odd"; PR adds an immediate-skip path for stale counters

    • doc/feature-set/Storage/page-integrity.md:18 — "the checkpoint's snapshot copy retries if the counter changed or was odd during the copy". This PR introduces a second skip condition (CopyPageWithSeqlock: odd counter on a page whose PageState ≠ Exclusive → skip immediately via LogStaleSeqlockCounterSkip) that is neither a retry nor a 100 ms timeout skip. A reader of this doc will not know the snapshot path can return false without any spin.
    • Code: src/Typhon.Engine/Storage/internals/PagedMMF.cs:1724 — new stale-counter guard: if (_memPagesInfo[memPageIndex].PageState != PageState.Exclusive) { LogStaleSeqlockCounterSkip(…); return false; }.
    • Suggested fix: Extend the seqlock sentence to note two skip conditions: "… retries if the counter changed; if the counter is odd on a page that is not exclusively latched (stale counter), the copy is skipped immediately; if a real writer has held the latch for more than 100 ms, the copy is also skipped rather than spinning indefinitely."

Advisory & scoped to the pages the change touches — recall for cross-cutting or unmapped drift is covered by the weekly Layer 3 audit.

@nockawa
nockawa merged commit 7b17bd6 into main Jul 20, 2026
9 checks passed
@nockawa
nockawa deleted the perf/ut-exec-time branch July 20, 2026 22:33
nockawa added a commit that referenced this pull request Jul 20, 2026
#549 [no-doc] commit token to skip the doc-accuracy review

Mirror of [no-ut]: a case-insensitive [no-doc] token in the PR head commit message skips the advisory Layer-2 doc-accuracy review, saving a billed Claude turn + a redundant comment when a commit cannot affect doc accuracy. Reads pull_request.head.sha (fallback github.sha) so the synthetic merge commit doesn't mask it; gates both the Claude review step and the map-gap nudge. Independent of [no-ut] (no [no-ci] umbrella). Documents both tokens together in CONTRIB.md.

CI: rebalance engine test shards from post-speedup durations

shard.py already bin-packs by class time, but shards.json was balanced against a pre-#550 trx; after the uneven ~4.5x speedup WalIntegrationTests (~35s, one indivisible fixture) became the long pole, leaving the run's max shard ~65s. Regenerated shards.json from the #550 c6id durations so the max shard drops to ~35s (WAL is the floor; the rest balance to ~32s). shard.py plan now accepts multiple --trx files and unions them, so the plan regenerates directly from a gate run's per-shard trx.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant