kv-vm-snapshot

command
v0.6.20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

  1. Copy-on-write KV-cache slab forks (x/mach vm_remap, copy=true): allocates a page-aligned Mach VM slab outside the Go heap, populates it with deterministic prefix blocks, forks N sibling branches copy-on-write, and benchmarks fork latency against a full memcpy control.
  2. Resident memory & CoW isolation accounting (x/mempressure.Snapshot): reports phys_footprint, resident_size, and maxrss (via task_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.
  3. Memory-pressure shedding (x/mempressure.Watcher + x/mempressure.Controller): arms a live DISPATCH_SOURCE_TYPE_MEMORYPRESSURE watcher and shedding controller backed by an LRU + refcounted SlabPool. Active decoding branches (Refs > 0) are never evicted; unpinned cached slabs (Refs == 0) are unmapped in LRU order (WARN evicts oldest 50% and tightens debug.SetMemoryLimit to 75%; CRITICAL evicts all unpinned slabs and tightens debug.SetMemoryLimit to 50%; NORMAL restores 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's phys_footprint ledger charges the per-mapping anonymous pages against the task (visible in the after full-read verify metric above). Unmapping idle/cached branches via mach.Unmap immediately reclaims that ledger credit, which is why pairing CoW branching with DISPATCH_SOURCE_TYPE_MEMORYPRESSURE eviction 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 drops phys_footprint synchronously without waiting for a Go GC cycle, while debug.SetMemoryLimit
    • debug.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:

  1. Copy-on-write slab forks via Mach mach_vm_remap (x/mach) vs. full memcpy.
  2. Resident physical footprint verification via x/mempressure.Snapshot before fork, after fork, and after partial-page mutation.
  3. Live memory-pressure handling via x/mempressure.Watcher and x/mempressure.Controller backed by an LRU + refcounted KV-cache SlabPool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL