apfs-weight-clone
Demonstrates Darwin APFS copy-on-write cloning (clonefile(2)), buffer-cache
control (F_NOCACHE, F_RDAHEAD, F_FULLFSYNC), and madvise(2) windowing
using x/apfs with zero cgo (CGO_ENABLED=0).
What it demonstrates
- Zero-copy APFS checkpoint cloning (
apfs.CloneFile): creates a
synthetic multi-MiB weight file in $TMPDIR, flushes it via F_FULLFSYNC,
and benchmarks apfs.CloneFile(..., apfs.WithNoOwnerCopy()) against a full
byte copy (io.Copy + Sync).
- Copy-on-write isolation: mutates pages in the cloned checkpoint via a
writable
apfs.MappedFile and verifies the original weight file's checksum
is completely unchanged while the clone reflects the mutation.
- Controlled unified-memory weight streaming: benchmarks three streaming
paths over the same weight file and verifies identical 64-bit checksums:
- default
os.Open + sequential io.ReadFull
apfs.SetNoCache(fd, true) + apfs.SetReadAhead(fd, false) read
apfs.OpenMapped (NoCache: true, DisableReadAhead: true,
Advice: apfs.Sequential) with sliding-window MadviseRange(WillNeed)
prefetching and post-layer page release (FreeReusable with fallback to
DontNeed on read-only file-backed mappings, per XNU VM semantics).
Usage
# Default 64 MiB checkpoint, 4 MiB streaming window
CGO_ENABLED=0 go run ./examples/kernel/apfs-weight-clone
# Larger 256 MiB checkpoint
CGO_ENABLED=0 go run ./examples/kernel/apfs-weight-clone -size-mib=256
# Run tests
CGO_ENABLED=0 go test -v -count=1 ./x/apfs ./examples/kernel/apfs-weight-clone
Measured Results
Hardware & OS: Apple M4 Max (128 GiB unified memory), macOS 26.6.2 (25G83), Go 1.27.0 darwin/arm64, CGO_ENABLED=0.
Summary Table
| Checkpoint Size |
apfs.CloneFile (CoW) |
io.Copy + Sync |
CoW Speedup |
Default Read |
F_NOCACHE Read |
F_NOCACHE + mmap (madvise) |
| 64 MiB |
447 µs |
59.779 ms (1,070.6 MiB/s) |
133.8x |
8.392 ms (7,626.0 MiB/s) |
8.264 ms (7,744.6 MiB/s) |
8.647 ms (7,401.6 MiB/s) |
| 256 MiB |
405 µs |
219.255 ms (1,167.6 MiB/s) |
541.5x |
33.527 ms (7,635.7 MiB/s) |
30.994 ms (8,259.6 MiB/s) |
28.736 ms (8,908.8 MiB/s) |
64 MiB Run
apfs-weight-clone: size=64 MiB, chunk=4 MiB, dir=/var/folders/kj/qbc32fzd2pv6ngnsysy8vpmw0000gn/T/apfs-weight-clone-3091104293
[1] Synthetic checkpoint written & F_FULLFSYNC'd in 58.871ms (1087.1 MiB/s)
[2] Checkpoint Clone vs Full Byte Copy
apfs.CloneFile (CoW): 447µs
io.Copy + Sync: 59.779ms (1070.6 MiB/s)
CoW speedup: 133.8x
[3] CoW Isolation Verification
mutated clone pages; original checksum intact: true
[4] Weight Streaming Benchmark (checksum=8ece72fa98000000)
default read: 8.392ms ( 7626.0 MiB/s)
F_NOCACHE read: 8.264ms ( 7744.6 MiB/s)
F_NOCACHE + mmap (WillNeed/Reusable): 8.647ms ( 7401.6 MiB/s)
256 MiB Run
apfs-weight-clone: size=256 MiB, chunk=4 MiB, dir=/var/folders/kj/qbc32fzd2pv6ngnsysy8vpmw0000gn/T/apfs-weight-clone-1185433389
[1] Synthetic checkpoint written & F_FULLFSYNC'd in 151.587ms (1688.8 MiB/s)
[2] Checkpoint Clone vs Full Byte Copy
apfs.CloneFile (CoW): 405µs
io.Copy + Sync: 219.255ms (1167.6 MiB/s)
CoW speedup: 541.5x
[3] CoW Isolation Verification
mutated clone pages; original checksum intact: true
[4] Weight Streaming Benchmark (checksum=ba5e69b260000000)
default read: 33.527ms ( 7635.7 MiB/s)
F_NOCACHE read: 30.994ms ( 8259.6 MiB/s)
F_NOCACHE + mmap (WillNeed/Reusable): 28.736ms ( 8908.8 MiB/s)