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") + } + } + }) + } +}