README
¶
purgeable-kv-pool
Demonstrates kernel-synchronous volatile KV/prefix cache slabs backed by Darwin
mach_vm_allocate(..., VM_FLAGS_PURGABLE) and mach_vm_purgable_control using
x/purgeable and x/mempressure
with zero cgo (CGO_ENABLED=0).
What it demonstrates
- Purgeable slab pool allocation & population: allocates a configurable pool of fixed-size Mach VM purgeable slabs (default 256 MiB: 64 × 4 MiB) and populates deterministic page-strided prefix blocks with checksums.
- Kernel-synchronous volatile transitions (
NonVolatile -> Volatile -> NonVolatile): releases slabs toVM_PURGABLE_VOLATILE(FIFOorLIFO), re-acquires them asVM_PURGABLE_NONVOLATILE, verifies every intact slab's checksum, and benchmarks per-slab transition latency (µs/slab). VM_PURGABLE_EMPTYandErrPurgedsemantics: explicitly empties a probe slab viaSlab.MarkEmpty(), verifiesSlab.State() == Empty, verifiesSlab.Pin()returnspurgeable.ErrPurgedwhile leaving the slabNonVolatileand zero-filled, and empties the entire idle pool viaPool.MarkAllEmpty()to confirm accurate purged-slab accounting.- Live Darwin VM accounting (
x/mempressure.Snapshot()): tracksPhysFootprint(jetsam ledger) andResidentSizeacross every lifecycle phase. Marking slabsVolatileremoves them fromPhysFootprintimmediately while keeping their physical pages warm inResidentSize;MarkAllEmpty()reclaims the physical pages immediately while preserving virtual address reservations.
Usage
# Default 256 MiB pool (64 x 4 MiB slabs, FIFO)
CGO_ENABLED=0 go run ./examples/kernel/purgeable-kv-pool
# 512 MiB pool with LIFO eviction behavior
CGO_ENABLED=0 go run ./examples/kernel/purgeable-kv-pool -size-mib=512 -behavior=lifo
# Run package and example tests
CGO_ENABLED=0 go test -v -count=1 ./x/purgeable ./examples/kernel/purgeable-kv-pool
Measured Results
Hardware & OS: Apple M4 Max, macOS 26.6.2 (25G83), Go 1.27.0 darwin/arm64, CGO_ENABLED=0.
Summary Table
| Pool Size | Slabs | Behavior | Release (NonVolatile -> Volatile) |
Acquire (Volatile -> NonVolatile) |
Full Round-Trip | Volatile PhysFootprint Delta |
Volatile ResidentSize Delta |
Post-MarkAllEmpty ResidentSize Delta |
|---|---|---|---|---|---|---|---|---|
| 256 MiB | 64 × 4 MiB | fifo |
1.89 µs/slab | 1.57 µs/slab | 3.46 µs/slab | +0.19 MiB (from +256.17 MiB) | +256.09 MiB | +0.56 MiB |
| 512 MiB | 128 × 4 MiB | lifo |
1.57 µs/slab | 1.45 µs/slab | 3.02 µs/slab | +0.30 MiB (from +512.30 MiB) | +512.08 MiB | +1.05 MiB |
256 MiB Default Run (fifo)
purgeable-kv-pool: total=256 MiB (64 slabs x 4 MiB), behavior=fifo, bench-iters=20
[1] Populate deterministic prefix blocks
elapsed: 101.945ms (2511.2 MiB/s)
[2] NonVolatile -> Volatile -> NonVolatile Verification
intact slabs: 64/64 (kernel-purged: 0), checksums ok: true
[3] Transition Latency (20 rounds x 64 slabs)
MarkVolatile (Release): 1.89 µs/slab
MarkNonVolatile (Acquire): 1.57 µs/slab
Full round-trip: 3.46 µs/slab
[4] VM_PURGABLE_EMPTY & ErrPurged Verification
single-slab State()==Empty: true, Pin()==ErrPurged: true, zeroed: true
pool MarkAllEmpty detected purged slabs: 64/64
[5] x/mempressure.Snapshot() Footprint Deltas (vs baseline)
Phase PhysFoot MiB Delta MiB Resident MiB Delta MiB
baseline 4.03 +0.00 5.88 +0.00
populated (NonVolatile) 260.20 +256.17 261.95 +256.08
marked Volatile 4.22 +0.19 261.97 +256.09
re-acquired (NonVolatile) 260.22 +256.19 261.97 +256.09
after MarkAllEmpty 4.64 +0.61 6.44 +0.56
after Close 4.53 +0.50 6.45 +0.58
512 MiB Run (lifo)
purgeable-kv-pool: total=512 MiB (128 slabs x 4 MiB), behavior=lifo, bench-iters=20
[1] Populate deterministic prefix blocks
elapsed: 432.896ms (1182.7 MiB/s)
[2] NonVolatile -> Volatile -> NonVolatile Verification
intact slabs: 128/128 (kernel-purged: 0), checksums ok: true
[3] Transition Latency (20 rounds x 128 slabs)
MarkVolatile (Release): 1.57 µs/slab
MarkNonVolatile (Acquire): 1.45 µs/slab
Full round-trip: 3.02 µs/slab
[4] VM_PURGABLE_EMPTY & ErrPurged Verification
single-slab State()==Empty: true, Pin()==ErrPurged: true, zeroed: true
pool MarkAllEmpty detected purged slabs: 128/128
[5] x/mempressure.Snapshot() Footprint Deltas (vs baseline)
Phase PhysFoot MiB Delta MiB Resident MiB Delta MiB
baseline 3.95 +0.00 5.78 +0.00
populated (NonVolatile) 516.25 +512.30 517.86 +512.08
marked Volatile 4.25 +0.30 517.86 +512.08
re-acquired (NonVolatile) 516.39 +512.44 518.00 +512.22
after MarkAllEmpty 5.20 +1.25 6.83 +1.05
after Close 5.02 +1.06 6.89 +1.11
Documentation
¶
Rendered for darwin/amd64
Overview ¶
Command purgeable-kv-pool demonstrates kernel-synchronous volatile KV/prefix cache slabs backed by Darwin mach_vm_purgable_control via x/purgeable, paired with live process footprint accounting from x/mempressure.
Click to show internal directories.
Click to hide internal directories.