From 1b0289c6c6bf21c1d4c75bef19a68c381a36cbaa Mon Sep 17 00:00:00 2001 From: jsvisa Date: Thu, 28 Aug 2025 06:56:32 +0000 Subject: [PATCH] inspect --workers Signed-off-by: jsvisa --- cmd/geth/dbcmd.go | 12 ++++++++++-- core/rawdb/database.go | 19 ++++++++----------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/cmd/geth/dbcmd.go b/cmd/geth/dbcmd.go index 44a52521f0..adf89188b2 100644 --- a/cmd/geth/dbcmd.go +++ b/cmd/geth/dbcmd.go @@ -22,6 +22,7 @@ import ( "os" "os/signal" "path/filepath" + "runtime" "slices" "strconv" "strings" @@ -89,7 +90,13 @@ Remove blockchain and state databases`, Action: inspect, Name: "inspect", ArgsUsage: " ", - Flags: slices.Concat(utils.NetworkFlags, utils.DatabaseFlags), + Flags: slices.Concat(utils.NetworkFlags, utils.DatabaseFlags, []cli.Flag{ + &cli.IntFlag{ + Name: "workers", + Usage: "Number of parallel workers for database inspection", + Value: runtime.NumCPU(), + }, + }), Usage: "Inspect the storage size for each type of data in the database", Description: `This commands iterates the entire database. If the optional 'prefix' and 'start' arguments are provided, then the iteration is limited to the given subset of data.`, } @@ -329,7 +336,8 @@ func inspect(ctx *cli.Context) error { db := utils.MakeChainDatabase(ctx, stack, true) defer db.Close() - return rawdb.InspectDatabase(db, prefix, start) + workers := ctx.Int("workers") + return rawdb.InspectDatabase(db, prefix, start, workers) } func checkStateContent(ctx *cli.Context) error { diff --git a/core/rawdb/database.go b/core/rawdb/database.go index b2d776a01d..82219d96b9 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -394,7 +394,7 @@ func (s *stat) GetCount() counter { // InspectDatabase traverses the entire database and checks the size // of all different categories of data. -func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error { +func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte, workers int) error { var ( start = time.Now() @@ -538,20 +538,17 @@ func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error { return it.Error() } - // Create error group for parallel processing - var g errgroup.Group + var eg errgroup.Group + eg.SetLimit(workers) + + log.Info("Starting parallel database inspection", "workers", workers) - // Split key space into 256 ranges (0x00 to 0xFF) - log.Info("Starting parallel database inspection", "workers", 256) for i := 0; i < 256; i++ { rangePrefix := []byte{byte(i)} - g.Go(func() error { - return processRange(rangePrefix) - }) + eg.Go(func() error { return processRange(rangePrefix) }) } - // Wait for all workers to complete - if err := g.Wait(); err != nil { + if err := eg.Wait(); err != nil { return err } @@ -613,7 +610,7 @@ func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error { table.AppendBulk(stats) table.Render() - log.Info("Parallel database inspection completed", "total_entries", totalCount.Load(), "elapsed", common.PrettyDuration(time.Since(start))) + log.Info("Database inspection completed", "count", totalCount.Load(), "elapsed", common.PrettyDuration(time.Since(start))) if unaccounted.GetSize() > 0 { log.Error("Database contains unaccounted data", "size", unaccounted.GetSize(), "count", unaccounted.GetCount())