README
¶
kv-vm-snapshot
Prototype of macOS virtual-memory primitives for LLM KV-cache prefix sharing and
memory-pressure shedding (for KV-cache prefix branching), using zero cgo (purego
bindings from github.com/tmc/apple + Go stdlib only).
What it demonstrates
- Copy-on-write KV-cache slab forks (
x/machvm_remap,copy=true): allocates a page-aligned Mach VM slab outside the Go heap, populates it with deterministic prefix blocks, forksNsibling branches copy-on-write, and benchmarks fork latency against a fullmemcpycontrol. - Resident memory & CoW isolation accounting (
x/mempressure.Snapshot): reportsphys_footprint,resident_size, andmaxrss(viatask_info(TASK_VM_INFO)+getrusage) before fork, after fork, after mutating a configurable fraction of pages in branch 0, and after full-read isolation verification across the base slab and all sibling branches. - Memory-pressure shedding (
x/mempressure.Watcher+x/mempressure.Controller): arms a liveDISPATCH_SOURCE_TYPE_MEMORYPRESSUREwatcher and shedding controller backed by an LRU + refcountedSlabPool. Active decoding branches (Refs > 0) are never evicted; unpinned cached slabs (Refs == 0) are unmapped in LRU order (WARNevicts oldest 50% and tightensdebug.SetMemoryLimitto 75%;CRITICALevicts all unpinned slabs and tightensdebug.SetMemoryLimitto 50%;NORMALrestores the base limit).
Usage
# Default 64 MiB slab, 4 CoW branches, 10% page mutation
CGO_ENABLED=0 go run ./examples/kernel/kv-vm-snapshot
# Include simulated WARN -> CRITICAL -> NORMAL pressure-shedding walkthrough
CGO_ENABLED=0 go run ./examples/kernel/kv-vm-snapshot -simulate-pressure
# 1 GiB slab benchmark
CGO_ENABLED=0 go run ./examples/kernel/kv-vm-snapshot -size-mib=1024 -branches=4 -mutate=0.10
# Keep slabs resident and listen for live OS memory-pressure events
# (test with: sudo memory_pressure -S -l warn)
CGO_ENABLED=0 go run ./examples/kernel/kv-vm-snapshot -listen-pressure
# Run verification tests (CoW isolation, fork speedup vs memcpy, pressure eviction)
CGO_ENABLED=0 go test -v ./examples/kernel/kv-vm-snapshot
Measured Results
Hardware & OS: Apple M4 Max (128 GiB unified memory, 16 KiB pages), macOS 26.6.2 (25G83), Go 1.27.0 darwin/arm64, CGO_ENABLED=0.
Summary Table
| Slab Size | Branches | CoW Fork (mean/branch) | Full memcpy (1 slab) |
Speedup | phys_footprint after N forks |
phys_footprint after 10% write |
|---|---|---|---|---|---|---|
| 64 MiB | 4 | 71.8 µs | 8,811 µs | 122.6x | +0.05 MiB (256 MiB logical) | +6.42 MiB (expected 6.41 MiB) |
| 1024 MiB | 4 | 813.5 µs | 117,016 µs | 143.8x | +0.06 MiB (4 GiB logical) | +102.47 MiB (expected 102.41 MiB) |
64 MiB + -simulate-pressure
kv-vm-snapshot: slab=64 MiB (4096 pages @ 16384 B), branches=4, mutate=10.0%
[1] Base KV prefix slab allocated & populated in 4.851ms
phys_footprint: 4.09 MiB -> 68.14 MiB (+64.05 MiB) | resident: 70.03 MiB | maxrss: 70.03 MiB
[2] CoW fork 4 branches via mach_vm_remap(copy=true)
total: 339 µs | mean/branch: 84.8 µs | first: 319 µs
phys_footprint: 68.14 MiB -> 68.19 MiB (+0.05 MiB across 4 x 64 MiB logical slabs)
[3] Full memcpy control (1 x 64 MiB slab)
memcpy: 14836 µs | CoW fork speedup: 174.9x
phys_footprint: 68.19 MiB -> 132.24 MiB (+64.05 MiB)
[4] Mutated 410/4096 pages (10.0%, 6.41 MiB) in branch 0 in 818µs
phys_footprint: 68.19 MiB -> 74.59 MiB (+6.41 MiB) | after full-read verify: 324.31 MiB (resident: 326.16 MiB)
isolation check: PASS (base prefix intact, 3 sibling branches intact, branch 0 updated)
[5] Simulated memory-pressure shedding (x/mempressure Watcher + Controller + LRU SlabPool)
pool armed: active=1 (64.00 MiB) cached=7 (320.00 MiB) go_mem_limit=512.00 MiB footprint=452.39 MiB
[PRESSURE level=warn] evicted=[fork-branch-1, fork-branch-2, fork-branch-3] freed=192.00 MiB | active_kept=1 cached_left=4 | footprint 452.39 MiB -> 261.27 MiB (-191.13 MiB) | GoMemLimit 512.00 MiB -> 384.00 MiB
[PRESSURE level=critical] evicted=[cached-prefix-1, cached-prefix-2, cached-prefix-3, cached-prefix-0] freed=128.00 MiB | active_kept=1 cached_left=0 | footprint 261.27 MiB -> 133.52 MiB (-127.75 MiB) | GoMemLimit 384.00 MiB -> 256.00 MiB
[PRESSURE level=normal] evicted=[none] freed=0.00 MiB | active_kept=1 cached_left=0 | footprint 133.52 MiB -> 133.52 MiB (+0.00 MiB) | GoMemLimit 256.00 MiB -> 512.00 MiB
active branch integrity after WARN -> CRITICAL -> NORMAL: PASS
1024 MiB Run
kv-vm-snapshot: slab=1024 MiB (65536 pages @ 16384 B), branches=4, mutate=10.0%
[1] Base KV prefix slab allocated & populated in 63.751ms
phys_footprint: 4.13 MiB -> 1028.63 MiB (+1024.50 MiB) | resident: 1030.02 MiB | maxrss: 1030.02 MiB
[2] CoW fork 4 branches via mach_vm_remap(copy=true)
total: 3254 µs | mean/branch: 813.5 µs | first: 3217 µs
phys_footprint: 1028.63 MiB -> 1028.69 MiB (+0.06 MiB across 4 x 1024 MiB logical slabs)
[3] Full memcpy control (1 x 1024 MiB slab)
memcpy: 117016 µs | CoW fork speedup: 143.8x
phys_footprint: 1028.69 MiB -> 2053.19 MiB (+1024.50 MiB)
[4] Mutated 6554/65536 pages (10.0%, 102.41 MiB) in branch 0 in 12.166ms
phys_footprint: 1028.69 MiB -> 1131.16 MiB (+102.47 MiB)
isolation check: PASS (base prefix intact, 3 sibling branches intact, branch 0 updated)
go test -v ./examples/kernel/kv-vm-snapshot
=== RUN TestCoWBranchIsolation
main_test.go:53: CoW fork 4 x 32 MiB: phys_footprint 36.42 MiB -> 36.47 MiB (+0.05 MiB)
main_test.go:72: mutated 256 pages (4.00 MiB): phys_footprint 36.47 MiB -> 40.47 MiB (+4.00 MiB)
--- PASS: TestCoWBranchIsolation (0.01s)
=== RUN TestForkSpeedupVsMemcpy
main_test.go:116: 64 MiB slab: CoW fork=286 µs, memcpy=9652 µs, speedup=33.7x
--- PASS: TestForkSpeedupVsMemcpy (0.02s)
=== RUN TestPressureSheddingAndActivePreservation
main_test.go:178: WARN: evicted=[cached-1 cached-2] freed=32.00 MiB footprint 100.53 MiB -> 69.55 MiB limit=384.00 MiB
main_test.go:204: CRITICAL: evicted=[cached-3 cached-0] freed=32.00 MiB footprint 69.56 MiB -> 37.95 MiB limit=256.00 MiB
--- PASS: TestPressureSheddingAndActivePreservation (0.01s)
PASS
ok github.com/tmc/apple/examples/kernel/kv-vm-snapshot 0.504s
Notes for KV-cache integration
- CoW fork vs. same-task read ledgering:
mach_vm_remap(copy=true)avoids copying physical pages on fork (~70–815 µs for 64 MiB–1 GiB) and isolates writes to only the mutated pages. However, once a same-task sibling mapping is read-faulted into the task's pmap, XNU'sphys_footprintledger charges the per-mapping anonymous pages against the task (visible in theafter full-read verifymetric above). Unmapping idle/cached branches viamach.Unmapimmediately reclaims that ledger credit, which is why pairing CoW branching withDISPATCH_SOURCE_TYPE_MEMORYPRESSUREeviction is essential under jetsam. - No cgo, off-heap slabs: slabs live outside the Go heap (
mach_vm_allocate/mach_vm_remap/mach_vm_deallocate), so unmapping dropsphys_footprintsynchronously without waiting for a Go GC cycle, whiledebug.SetMemoryLimitdebug.FreeOSMemory()simultaneously constrains Go-side metadata growth.
Documentation
¶
Rendered for darwin/amd64
Overview ¶
Command kv-vm-snapshot prototypes OS virtual-memory primitives for LLM KV-cache prefix sharing and memory-pressure shedding on macOS:
- Copy-on-write slab forks via Mach mach_vm_remap (x/mach) vs. full memcpy.
- Resident physical footprint verification via x/mempressure.Snapshot before fork, after fork, and after partial-page mutation.
- Live memory-pressure handling via x/mempressure.Watcher and x/mempressure.Controller backed by an LRU + refcounted KV-cache SlabPool.
Click to show internal directories.
Click to hide internal directories.