shared-weight-cluster
shared-weight-cluster is a self-contained, zero-cgo (CGO_ENABLED=0)
multi-process local inference broker demo for macOS that composes seven
github.com/tmc/apple/x/* packages into one end-to-end cluster:
x/xpctensor: shares a single 64 MiB IOSurface-backed FP16 weight slab
across multiple worker subprocesses with physical zero-copy MTLBuffer
aliasing and lease tracking.
x/ossync: dispatches 64-byte job descriptors and collects results over
ossync.Shared mmap SPSC rings (os_sync_wait_on_address) with zero
sockets or pipes on the inference data path.
x/purgeable: manages a volatile KV-cache slab pool (8 × 4 MiB = 32 MiB)
holding AES-256-GCM encrypted tenant KV blocks.
x/mempressure: coordinates memory-pressure shedding (Controller +
Watcher), tightens debug.SetMemoryLimit, calls MarkAllEmpty() on idle
KV slabs, and reports task_info(TASK_VM_INFO).phys_footprint.
x/enclave: creates a Secure Enclave-backed P-256 key (with automatic
ephemeral software SecKey fallback when unentitled), seals/unseals the
per-tenant 32-byte KV-cache DEK (SealDEK/UnsealDEK), verifies hybrid
SealPayload/OpenPayload envelopes, and signs the tenant attestation
verified by every worker before executing jobs.
x/daemon: holds a latency-critical anti-App-Nap activity and IOKit
PreventUserIdleSystemSleep assertion (AcquireLatencyCritical).
x/workgroup: pins each worker's ring-consumer loop onto Apple Silicon
P-cores (UserInteractive QoS inside an os_workgroup).
What it proves
- Zero-copy weight sharing across N worker subprocesses (
os.Args[0] -role=worker):
- Broker verifies
xpctensor.Broker/Client fetch, Tensor.Verify(),
zerocopy.Check, and XPC disconnect lease reclamation.
- Each worker attaches to the 64 MiB
IOSurface via IOSurfaceLookup +
zerocopy.SurfacePool.Wrap, verifies the x/enclave ECDSA-SHA256 tenant
attestation, and proves bidirectional physical aliasing (broker write
observed by all workers; worker write observed directly by broker).
- Low-latency
x/ossync Shared mmap rings:
- Broker dispatches inference jobs over per-worker
mmap rings; workers
compute checksums directly from their zero-copy MTLBuffer mappings and
return results over their response rings.
SIGKILL fault tolerance & zero leaked refs:
- Sending
SIGKILL to worker 0 immediately triggers lease reclamation
(IOSurfaceGetUseCount drops 3 -> 2), surviving workers continue
verifying jobs, and graceful shutdown leaves useCount=0, inUse=false.
- Simulated memory-pressure KV shedding:
watcher.Notify(mempressure.Critical) triggers kvPool.MarkAllEmpty()
(shedding all 32 MiB of idle volatile KV slabs and halving the Go memory
limit 512 MiB -> 256 MiB).
- Note: Darwin's
phys_footprint jetsam ledger already excludes Volatile
purgeable slabs once released, while MarkAllEmpty() immediately discards
their physical pages; the subsequent kvPool.Acquire() confirms
purged=true.
Usage
CGO_ENABLED=0 go vet ./examples/xpc/shared-weight-cluster
CGO_ENABLED=0 go test -v -count=1 -timeout 60s ./examples/xpc/shared-weight-cluster
CGO_ENABLED=0 go run ./examples/xpc/shared-weight-cluster
Flags:
-workers (default 3): number of worker subprocesses (2..4).
-size-mib (default 64): shared weight slab size in MiB.
-jobs (default 24): number of inference jobs dispatched before SIGKILL.
-kv-slabs (default 8): number of 4 MiB purgeable KV-cache slabs.
-role (broker or worker): internal re-exec switch for worker processes.
Measured results
Measured on Apple M4 Max (128 GiB), macOS 26.6.2 (25G83), CGO_ENABLED=0:
shared-weight-cluster (64 MiB shared weights, 3 workers, 32 MiB purgeable KV pool)
=====================================================================================
[1] x/daemon + x/workgroup (Anti-App-Nap + P-core Pinning)
Latency-critical guard: assertion_id=36121 service_status=not-found
Workers P-core pinned: true (UserInteractive, max_parallel_threads=16)
[2] x/enclave (Tenant KV Key Sealing + Attestation)
Secure Enclave probe: available=true hardware_backed=true
DEK seal/unseal + AEAD: dek_ok=true envelope_ok=true sign=4943 µs
All workers attested: true
[3] x/xpctensor + Zero-Copy IOSurface Aliasing (64 MiB)
XPC broker fetch median: 430 µs (aliased=true disconnect_reclaim=true)
Worker attach median: 224 µs (IOSurfaceLookup + MTLBuffer wrap)
Physical aliasing check: all_workers=true reverse_write_seen=true
[4] x/ossync Shared mmap Descriptor Rings (Zero Sockets/Pipes)
Wake latency (med / min): 58 µs / 18 µs
Job RTT (med / min): 261 µs / 53 µs (24/24 jobs verified)
[5] SIGKILL Fault Tolerance & Lease Reclamation
Worker 0 SIGKILL: useCount 3 -> 2 (reclaimed in 2127 µs)
Surviving workers jobs: 8/8 verified after kill
Final IOSurface state: useCount=0 inUse=false
[6] x/purgeable + x/mempressure (Volatile KV-Cache Shedding)
Pressure shed (Critical): freed=32.0 MiB (8 slabs marked empty)
Physical footprint: 76.3 MiB -> 76.9 MiB
Go memory limit: 512 MiB -> 256 MiB
Re-acquire purge detect: purged=true (pool_purged_total=1)