From eccc69eb8383a0692fcbe44d44befc21c141c2f4 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 05:46:28 -0800 Subject: [PATCH 1/3] storage: a SPACE baseline at the 1 TiB shape and barrier benchmarks at 1 and 16 TiB TestSpacePerChunkAt1TiBShape reports the holder's heap per chunk after attach for 262,144 chunks every one named by HEAD (with CHUNK_SPACE_RED=1 it asserts the 200 B/chunk target, red today). BenchmarkCutBarrierScan and BenchmarkCopyHead time the barrier's idle candidate scan and the flat map copy at 1 and 16 TiB. Co-Authored-By: Claude Opus 5.5 --- .../core/storage/chunk_space_baseline_test.go | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 seaweed-block/core/storage/chunk_space_baseline_test.go diff --git a/seaweed-block/core/storage/chunk_space_baseline_test.go b/seaweed-block/core/storage/chunk_space_baseline_test.go new file mode 100644 index 0000000000..ded5e56123 --- /dev/null +++ b/seaweed-block/core/storage/chunk_space_baseline_test.go @@ -0,0 +1,108 @@ +package storage + +import ( + "crypto/sha256" + "fmt" + "os" + "runtime" + "testing" +) + +// heapNow is the live heap after two GCs. +func heapNow() uint64 { + runtime.GC() + runtime.GC() + var m runtime.MemStats + runtime.ReadMemStats(&m) + return m.HeapAlloc +} + +// fullHead names every chunk of an S-chunk volume, with references shaped +// as the layout remote's (an owner-qualified key, the chunk's SHA-256). +func fullHead(chunks uint64) chunkHead { + h := chunkHead{Gen: 1, Epoch: 1, Holder: "host-a", Chunks: make(map[uint64]chunkRef, chunks)} + for i := uint64(0); i < chunks; i++ { + sum := sha256.Sum256([]byte(fmt.Sprint(i))) + h.Chunks[i] = chunkRef{Key: fmt.Sprintf("@v1-0011223344556677/chunks/%d/%x", i, sum[:8]), SHA256: sum, Encoding: "full"} + } + return h +} + +// The SPACE baseline (V4-B PR 4's red, r2.736/r2.777): the holder's heap per +// chunk for a 1 TiB-shaped volume (262,144 chunks; one-block chunks here, the +// same count), every chunk named by HEAD, measured after GC once the layer is +// attached: the store's and the layer's retained heap, the remote's copy of +// HEAD excluded (it exists before the first reading). It also reports one +// flat map's own cost. With CHUNK_SPACE_RED=1 it asserts the target, at most +// 200 B per chunk, which fails until PR 4 drops the flat map. +func TestSpacePerChunkAt1TiBShape(t *testing.T) { + const chunks = 262144 + f := newFakeChunkRemote() + f.mu.Lock() + f.head = fullHead(chunks) + f.mu.Unlock() + m0 := heapNow() + oneMap := fullHead(chunks) + flat := float64(heapNow()-m0) / chunks + runtime.KeepAlive(oneMap) + oneMap = chunkHead{} + before := heapNow() + s, err := createMappedWALStoreV3ForTest(t.TempDir()+"/mapped", chunks, 4096, v3TestWAL, WModeStrict, 1) + if err != nil { + t.Fatal(err) + } + s.DisableAutoFlushForRecoveryTest() + t.Cleanup(func() { s.Close() }) + cfg := ctConfig(1) + cfg.ChunkBlocks, cfg.Bitmap = 1, nil + layer := mustAttach(t, s, f, cfg) + per := float64(int64(heapNow())-int64(before)) / chunks + runtime.KeepAlive(layer) + t.Logf("SPACE at 1 TiB shape (%d chunks, v1.1 format %v): %.0f B/chunk held after attach; one flat map alone is %.0f B/chunk", + chunks, chunkV11Format, per, flat) + if os.Getenv("CHUNK_SPACE_RED") == "1" && per > 200 { + t.Fatalf("SPACE red: %.0f B/chunk, want at most 200", per) + } +} + +// syntheticScanLayer is a layer over a store of S chunks with no dirt: the +// barrier's candidate scan (captureCandidates) visits every chunk's map +// entry. The store is the scan's inputs only (an empty dirty map and S idle +// map entries), not a file: the scan's own cost at 16 TiB without 16 GiB of +// test volume. +func syntheticScanLayer(chunks int) *chunkLayerImpl { + s := &WALStore{dm: newDirtyMap(64), v3: &walStoreV3State{}} + s.v3.mapEntries = newLiveMapEntries(make([]chunkLSNEntryV3, chunks), mapEntrySnapshot{}) + return &chunkLayerImpl{s: s, chunkBlocks: 1, numChunks: uint64(chunks), absent: map[uint64]bool{}, filledClean: map[uint64]filledMark{}, localBlocks: map[uint64]bool{}} +} + +// The barrier's O(S) work today (V4-B B1/B2): the idle candidate scan and the +// copy of the flat map, at 1 TiB (262,144 chunks) and 16 TiB (4,194,304). +func BenchmarkCutBarrierScan(b *testing.B) { + for _, n := range []int{262144, 4194304} { + b.Run(fmt.Sprintf("chunks=%d", n), func(b *testing.B) { + l := syntheticScanLayer(n) + b.ResetTimer() + for i := 0; i < b.N; i++ { + if c := l.captureCandidates(); len(c) != 0 { + b.Fatalf("an idle scan named %d candidates", len(c)) + } + } + }) + } +} + +func BenchmarkCopyHead(b *testing.B) { + for _, n := range []uint64{262144, 4194304} { + b.Run(fmt.Sprintf("chunks=%d", n), func(b *testing.B) { + h := fullHead(n) + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + if c := copyHead(h); len(c.Chunks) != int(n) { + b.Fatal("short copy") + } + } + }) + } +} From 191a26da506de9da0e6899965bc29eee1ce3b555 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 06:00:04 -0800 Subject: [PATCH 2/3] storage: the SPACE baseline charges the layer for its own canonical keys; its controls The layer's HEAD now comes from a remote whose GetHead allocates its own key strings (as a real remote's decode does), so the keys the layer keeps are counted; the keys are in the layout's canonical form (114 bytes at the 1 TiB chunk count). TestSpaceMeasurementControls shows a shallow copy would be undercounted by its keys, that the per-chunk heap does not depend on a chunk's block count (1, 8 and 32 blocks a chunk agree), and that key length moves it about one byte a key byte. Co-Authored-By: Claude Opus 5.5 --- .../core/storage/chunk_space_baseline_test.go | 144 ++++++++++++++---- 1 file changed, 117 insertions(+), 27 deletions(-) diff --git a/seaweed-block/core/storage/chunk_space_baseline_test.go b/seaweed-block/core/storage/chunk_space_baseline_test.go index ded5e56123..935cdc3615 100644 --- a/seaweed-block/core/storage/chunk_space_baseline_test.go +++ b/seaweed-block/core/storage/chunk_space_baseline_test.go @@ -1,10 +1,12 @@ package storage import ( + "context" "crypto/sha256" "fmt" "os" "runtime" + "strings" "testing" ) @@ -17,49 +19,137 @@ func heapNow() uint64 { return m.HeapAlloc } -// fullHead names every chunk of an S-chunk volume, with references shaped -// as the layout remote's (an owner-qualified key, the chunk's SHA-256). -func fullHead(chunks uint64) chunkHead { +// canonicalKey is a chunk reference's engine key in the layout remote's form +// (chunkstore.LayoutKey): "layout:" + owner + ":" + the owner's 32-hex +// VOLUME id + ":" + the chunk's name (index "-" the 64-hex SHA-256). +func canonicalKey(owner, ownerID string, index uint64, sum [32]byte) string { + return fmt.Sprintf("layout:%s:%s:%d-%x", owner, ownerID, index, sum) +} + +// fullHead names every chunk of an S-chunk volume with canonical references. +func fullHead(chunks uint64, owner string) chunkHead { + const ownerID = "309016fbc5b31531e1e7c44585851c7d" h := chunkHead{Gen: 1, Epoch: 1, Holder: "host-a", Chunks: make(map[uint64]chunkRef, chunks)} for i := uint64(0); i < chunks; i++ { sum := sha256.Sum256([]byte(fmt.Sprint(i))) - h.Chunks[i] = chunkRef{Key: fmt.Sprintf("@v1-0011223344556677/chunks/%d/%x", i, sum[:8]), SHA256: sum, Encoding: "full"} + h.Chunks[i] = chunkRef{Key: canonicalKey(owner, ownerID, i, sum), SHA256: sum, Encoding: "full"} } return h } -// The SPACE baseline (V4-B PR 4's red, r2.736/r2.777): the holder's heap per -// chunk for a 1 TiB-shaped volume (262,144 chunks; one-block chunks here, the -// same count), every chunk named by HEAD, measured after GC once the layer is -// attached: the store's and the layer's retained heap, the remote's copy of -// HEAD excluded (it exists before the first reading). It also reports one -// flat map's own cost. With CHUNK_SPACE_RED=1 it asserts the target, at most -// 200 B per chunk, which fails until PR 4 drops the flat map. -func TestSpacePerChunkAt1TiBShape(t *testing.T) { - const chunks = 262144 +// deepHead copies a HEAD with every key string newly allocated. +func deepHead(h chunkHead) chunkHead { + out := copyHead(h) + for i, r := range out.Chunks { + r.Key = strings.Clone(r.Key) + out.Chunks[i] = r + } + return out +} + +// ownedHeadRemote answers GetHead with a HEAD whose key strings are its own +// (allocated at the call), as a real remote's decode does: a layer that +// keeps them is charged for them, never for the fake's backing. +type ownedHeadRemote struct{ *fakeChunkRemote } + +func (r ownedHeadRemote) GetHead(ctx context.Context) (chunkHead, error) { + h, err := r.fakeChunkRemote.GetHead(ctx) + return deepHead(h), err +} + +func (r ownedHeadRemote) CASHeadDelta(ctx context.Context, e chunkHeadVersion, n chunkHead, c map[uint64]*chunkRef) (uint64, error) { + return casDeltaVia(ctx, r.GetHead, r.CASHead, e, n, c) +} + +// heldPerChunk attaches a layer to a store of chunks x chunkBlocks blocks +// whose HEAD names every chunk (keys of the owner's length) and reports the +// heap the store and layer hold after attach, per chunk, after GC. The +// fake's own HEAD exists before the first reading; the layer's copy has its +// own keys (ownedHeadRemote). +func heldPerChunk(t *testing.T, chunks uint64, chunkBlocks int, owner string) float64 { + t.Helper() f := newFakeChunkRemote() f.mu.Lock() - f.head = fullHead(chunks) + f.head = fullHead(chunks, owner) f.mu.Unlock() - m0 := heapNow() - oneMap := fullHead(chunks) - flat := float64(heapNow()-m0) / chunks - runtime.KeepAlive(oneMap) - oneMap = chunkHead{} before := heapNow() - s, err := createMappedWALStoreV3ForTest(t.TempDir()+"/mapped", chunks, 4096, v3TestWAL, WModeStrict, 1) + s, err := createMappedWALStoreV3ForTest(t.TempDir()+"/mapped", uint32(chunks)*uint32(chunkBlocks), 4096, v3TestWAL, WModeStrict, uint64(chunkBlocks)) if err != nil { t.Fatal(err) } s.DisableAutoFlushForRecoveryTest() - t.Cleanup(func() { s.Close() }) cfg := ctConfig(1) - cfg.ChunkBlocks, cfg.Bitmap = 1, nil - layer := mustAttach(t, s, f, cfg) - per := float64(int64(heapNow())-int64(before)) / chunks + cfg.ChunkBlocks, cfg.Bitmap = uint32(chunkBlocks), nil + layer := mustAttach(t, s, ownedHeadRemote{f}, cfg) + per := float64(int64(heapNow())-int64(before)) / float64(chunks) runtime.KeepAlive(layer) - t.Logf("SPACE at 1 TiB shape (%d chunks, v1.1 format %v): %.0f B/chunk held after attach; one flat map alone is %.0f B/chunk", - chunks, chunkV11Format, per, flat) + runtime.KeepAlive(f) + if err := layer.Close(); err != nil { + t.Fatal(err) + } + if err := s.Close(); err != nil { + t.Fatal(err) + } + return per +} + +// The measurement's own controls. (1) A retained copy is charged with its +// keys only when it owns them: a deep copy of a HEAD costs its keys more +// than a shallow one (copyHead shares them), so a layer charged by this +// method for a shallow copy would be undercounted, which ownedHeadRemote +// prevents. (2) The per-chunk heap depends on the chunk count, not on the +// chunk's block count (1, 8 and 32 blocks a chunk agree within 10%), so a +// store of one-block chunks stands for a 1 TiB volume of 4 MiB chunks with +// the same count. (3) Key length moves it by about one byte a key byte. +func TestSpaceMeasurementControls(t *testing.T) { + const chunks = 16384 + h := fullHead(chunks, "v1") + m0 := heapNow() + shallow := copyHead(h) + sh := heapNow() - m0 + m1 := heapNow() + deep := deepHead(h) + dp := heapNow() - m1 + runtime.KeepAlive(shallow) + runtime.KeepAlive(deep) + keyLen := len(h.Chunks[0].Key) + if float64(dp-sh)/chunks < float64(keyLen)*0.8 { + t.Fatalf("a deep copy costs %d B/chunk over a shallow one, want about the key's %d", (dp-sh)/chunks, keyLen) + } + geo := map[int]float64{} + for _, cb := range []int{1, 8, 32} { + geo[cb] = heldPerChunk(t, chunks, cb, "v1") + } + t.Logf("held per chunk at %d chunks: %.0f B (1 block/chunk), %.0f B (8), %.0f B (32)", chunks, geo[1], geo[8], geo[32]) + for _, cb := range []int{8, 32} { + if d := geo[cb]/geo[1] - 1; d > 0.10 || d < -0.10 { + t.Fatalf("held per chunk depends on the chunk's block count: %.0f B at 1 block, %.0f B at %d", geo[1], geo[cb], cb) + } + } + long := strings.Repeat("v", 101) // a 100-byte longer owner name + longer := heldPerChunk(t, chunks, 1, long) + t.Logf("held per chunk with keys 100 B longer: %.0f B (+%.0f)", longer, longer-geo[1]) + if longer-geo[1] < 60 || longer-geo[1] > 160 { + t.Fatalf("keys 100 B longer moved the held heap by %.0f B/chunk, want about 100", longer-geo[1]) + } +} + +// The SPACE baseline (V4-B PR 4's red, r2.736/r2.777): the holder's heap per +// chunk for a 1 TiB volume's chunk count (262,144; one-block chunks, which +// TestSpaceMeasurementControls shows stand for 4 MiB ones), every chunk +// named by HEAD with canonical keys owned by the layer, after GC once the +// layer is attached. With CHUNK_SPACE_RED=1 it asserts the target, at most +// 200 B per chunk, which fails until PR 4 drops the flat map. +func TestSpacePerChunkAt1TiBShape(t *testing.T) { + const chunks = 262144 + m0 := heapNow() + oneMap := fullHead(chunks, "v1") + flat := float64(heapNow()-m0) / chunks + runtime.KeepAlive(oneMap) + oneMap = chunkHead{} + per := heldPerChunk(t, chunks, 1, "v1") + t.Logf("SPACE at 1 TiB's chunk count (%d chunks, v1.1 format %v, canonical %d-byte keys): %.0f B/chunk held after attach; one flat map with its keys is %.0f B/chunk", + chunks, chunkV11Format, len(canonicalKey("v1", "309016fbc5b31531e1e7c44585851c7d", chunks-1, [32]byte{})), per, flat) if os.Getenv("CHUNK_SPACE_RED") == "1" && per > 200 { t.Fatalf("SPACE red: %.0f B/chunk, want at most 200", per) } @@ -95,7 +185,7 @@ func BenchmarkCutBarrierScan(b *testing.B) { func BenchmarkCopyHead(b *testing.B) { for _, n := range []uint64{262144, 4194304} { b.Run(fmt.Sprintf("chunks=%d", n), func(b *testing.B) { - h := fullHead(n) + h := fullHead(n, "v1") b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { From 366d8061a287e7135110699264fd204dc2a9451e Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 06:06:48 -0800 Subject: [PATCH 3/3] storage: the SPACE controls measure the real 1024 blocks a chunk; cleanup on every path The geometry control now includes 1024 blocks a chunk (a 64 GiB sparse store): the held heap per chunk is the same as at one block (387 B on v1.0, 436-439 on v1.1), because the map's wider coverage entry is the on-disk page format, not the attached heap. The store's cleanup is registered before the attach, so a failed attach still closes it. Co-Authored-By: Claude Opus 5.5 --- .../core/storage/chunk_space_baseline_test.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/seaweed-block/core/storage/chunk_space_baseline_test.go b/seaweed-block/core/storage/chunk_space_baseline_test.go index 935cdc3615..c885a0ccc9 100644 --- a/seaweed-block/core/storage/chunk_space_baseline_test.go +++ b/seaweed-block/core/storage/chunk_space_baseline_test.go @@ -78,6 +78,7 @@ func heldPerChunk(t *testing.T, chunks uint64, chunkBlocks int, owner string) fl t.Fatal(err) } s.DisableAutoFlushForRecoveryTest() + t.Cleanup(func() { s.Close() }) // also on an attach failure: no worker or lock outlives the test cfg := ctConfig(1) cfg.ChunkBlocks, cfg.Bitmap = uint32(chunkBlocks), nil layer := mustAttach(t, s, ownedHeadRemote{f}, cfg) @@ -87,9 +88,6 @@ func heldPerChunk(t *testing.T, chunks uint64, chunkBlocks int, owner string) fl if err := layer.Close(); err != nil { t.Fatal(err) } - if err := s.Close(); err != nil { - t.Fatal(err) - } return per } @@ -98,9 +96,12 @@ func heldPerChunk(t *testing.T, chunks uint64, chunkBlocks int, owner string) fl // than a shallow one (copyHead shares them), so a layer charged by this // method for a shallow copy would be undercounted, which ownedHeadRemote // prevents. (2) The per-chunk heap depends on the chunk count, not on the -// chunk's block count (1, 8 and 32 blocks a chunk agree within 10%), so a -// store of one-block chunks stands for a 1 TiB volume of 4 MiB chunks with -// the same count. (3) Key length moves it by about one byte a key byte. +// chunk's block count (1, 8, 32 and the real 1024 blocks a chunk agree +// within 10%; the 1024 store is a 64 GiB sparse file), so a store of +// one-block chunks stands for a 1 TiB volume of 4 MiB chunks with the same +// count. The map's coverage entry is wider at 1024 blocks (144 B against +// 17), but it is the on-disk page format: the attached heap holds each +// chunk's coverage as a pointer, not the entry. (3) Key length moves it by about one byte a key byte. func TestSpaceMeasurementControls(t *testing.T) { const chunks = 16384 h := fullHead(chunks, "v1") @@ -117,11 +118,11 @@ func TestSpaceMeasurementControls(t *testing.T) { t.Fatalf("a deep copy costs %d B/chunk over a shallow one, want about the key's %d", (dp-sh)/chunks, keyLen) } geo := map[int]float64{} - for _, cb := range []int{1, 8, 32} { + for _, cb := range []int{1, 8, 32, 1024} { geo[cb] = heldPerChunk(t, chunks, cb, "v1") } - t.Logf("held per chunk at %d chunks: %.0f B (1 block/chunk), %.0f B (8), %.0f B (32)", chunks, geo[1], geo[8], geo[32]) - for _, cb := range []int{8, 32} { + t.Logf("held per chunk at %d chunks: %.0f B (1 block/chunk), %.0f B (8), %.0f B (32), %.0f B (1024)", chunks, geo[1], geo[8], geo[32], geo[1024]) + for _, cb := range []int{8, 32, 1024} { if d := geo[cb]/geo[1] - 1; d > 0.10 || d < -0.10 { t.Fatalf("held per chunk depends on the chunk's block count: %.0f B at 1 block, %.0f B at %d", geo[1], geo[cb], cb) }