Skip to content

engine: shrink block cache to 8 KB and evict ways in FIFO order - #111

Open
bluetoe-99 wants to merge 3 commits into
pound-emu:mainfrom
bluetoe-99:feature/engine-block-cache-8kb
Open

bluetoe-99 wants to merge 3 commits into
pound-emu:mainfrom
bluetoe-99:feature/engine-block-cache-8kb

Conversation

@bluetoe-99

@bluetoe-99 bluetoe-99 commented Oct 29, 2026 •

Copy link
Copy Markdown

Fixes #45.

Cache shrink and FIFO eviction. BLOCK_CACHE_SETS goes from 4096 to 128, and static_assert(8192 == BLOCK_CACHE_SETS * sizeof(block_cache_set_t)) pins the footprint at exactly 8 KB. Way eviction changes from a PC-hash ((pc >> 4) & 3, which evicts the same way for every PC sharing those bits) to the 2-bit FIFO counter per set that the issue asks for, packed four sets per byte into a 32 B array and pinned by a second static_assert at 2 bits per set. The lookup hot loop is untouched.

About the size -A acceptance criterion: the block cache lives inside internal_engine_state_t, which is heap-allocated through the engine's allocator, so it has never appeared in the static image: .bss is 8 B in both builds. The footprint is pinned by the two static_asserts instead, and confirmed from DWARF with pahole:

main:     block_cache[4096] /* 512 262144 */   state size: 262656
this PR:  block_cache[128]  /* 512   8192 */   state size: 8768
          block_cache_fifo[32] /* 8704 32 */

Engine-state allocation drops from 262,656 B to 8,768 B (-96.7%).

Cache invalidation. Cached host_code pointers only stay valid while the Tier 1 layout is intact, so the cache is now cleared at every layout reset: the buffer-overflow reset (this closes the // TODO: When the block cache is implemented, it must be cleared here. at that site, and this PR is what makes that path reachable), the clear-cache request, and bal_engine_reset (which memsets the executable buffer and has the same dangling-pointer problem). A 32x smaller cache makes eviction and recompile churn common, so running the code buffer out becomes reachable.

One latent bug fixed (bal_x86_assembler_reset): it memsets rx_buffer, which points at the executable mapping: PROT_READ|PROT_EXEC on Linux (shm dual mapping), a PAGE_EXECUTE_READ view on Windows, and the same pointer as buffer on APPLE. That memset faulted on Linux and Windows and was redundant on APPLE (the RW dual mapping aliases the same physical pages). Running the reset paths in the new tests produced an immediate SIGSEGV at exactly that line on stock main. Removed.

Tests (test_bal_engine_block_cache, the first engine-level tests): five MOVZ X8,#k; RET blocks spaced 512 B apart. (pc >> 2) & 127 discards bit 9, so all five land in one 4-way set and the fifth insert forces a FIFO eviction. Every block must give identical results before and after eviction, through a bal_engine_clear_cache cycle, and with DISABLE_BLOCK_CACHE. ctest 15/15 (was 14), zero warnings (Clang 18, project strict flags).

Cachegrind (the same 500-seed IPC workload piped byte-identically into bal_fuzzer_worker_ballistic, stock vs this PR):

event main this PR
D1 write misses 2,140,906 40,513 (-98.1%)
D1 read misses 163,886 123,008 (-24.9%)
LL data misses 87,667 79,595 (-9.2%)
instructions 152.4 M 25.5 M (-83%)

The D1 write-miss delta is the per-engine-init zeroing of the old 262 KB state: 262,144/64 = 4,096 cache lines x 500 seeds = 2.048 M, which matches the measured 2,140,906 within other state touches.

perf stat (i9-13900K, pinned to a P-core, whole 3,000-seed fuzzer process tree including driver and Unicorn oracle): L1-dcache-load-misses 10,631,787 becomes 10,423,280 (-2.0%), cycles 1,569,667,093 becomes 1,552,376,645 (-1.1%). The engine is a small share of that process tree, so the Cachegrind per-process numbers are the sharper signal.

Fuzz gate (full corpus, 59,088 seeds, identical corpus and order, Debug build): unique-seed verdict sets against the stock-main baseline: 0 new fails, 0 new errors. Main's 149 unique fail seeds become 16; main's 18 error seeds become 6; every remaining fail or error seed is a member of main's set. (Fail-to-pass flips are order-sensitivity of fuzzer worker state across seeds, not claimed fixes.)

@bluetoe-99
bluetoe-99 force-pushed the feature/engine-block-cache-8kb branch 2 times, most recently from 8cbf9ec to 20a12e8 Compare October 29, 2026 19:21
Reduce block cache sets from 4096 to 128 (issue pound-emu#45): 128 sets x 64 B
= 8192 B exact, enforced by a static_assert. Replace the PC-hash way
selection with a 2-bit FIFO counter per set, packed 4 sets per byte
(32 B total). The lookup hot path is untouched.

Because cached host code pointers only alias valid compiled blocks
while the Tier 1 layout is intact, clear the block cache at every
layout reset: the buffer-overflow reset (closing the TODO at that
site), the clear-cache request, and bal_engine_reset. A 32x smaller
cache makes evictions and recompiles common, which makes buffer
exhaustion - and the dangling pointers - reachable.

Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
bal_x86_assembler_reset memsets rx_buffer, which points at the RX
mapping (Linux shm dual mapping, Windows file-mapping view): never
writable, so the reset faulted on any platform that enforces W^X. On
APPLE buffer == rx_buffer, so the memset was a redundant second
zeroing of the same pages.

Writes to the RW alias already cover the executable mapping (same
physical backing), so zeroing assembler->buffer is sufficient. This
path is reached by the Tier 1 buffer-overflow reset and
bal_engine_clear_cache, both newly exercised by the engine
block-cache tests.

Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
Five blocks spaced 512 bytes apart all share one block cache set,
because (pc >> 2) & 127 discards bit 9, so the fifth insert forces a
FIFO way eviction. Assert every block produces identical results
before and after eviction. Cover the clear-cache request path and the
DISABLE_BLOCK_CACHE bypass flag.

Signed-off-by: bluetoe-99 <102478736+bluetoe-99@users.noreply.github.com>
@bluetoe-99
bluetoe-99 force-pushed the feature/engine-block-cache-8kb branch from 20a12e8 to aed331e Compare October 29, 2026 19:46
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.

[ENGINE] Shrink Block Cache to 8KB

1 participant