cmd/geth: print disk space cleared after prune-history

The prune-history command completes without reporting how much disk
space was freed, making it difficult for operators to assess the
impact of the operation.

Measure the chaindata directory size before and after pruning, and
include the cleared space in the completion log message.

Before:
  History pruning completed  tail=15537393 elapsed=883.604ms

After:
  History pruning completed  tail=15537393 cleared=672.00GiB elapsed=883.604ms

Fixes #31916
This commit is contained in:
YQ AltLayer 2026-03-01 13:06:38 +00:00
parent 723aae2b4e
commit 444d7c4197

View file

@ -702,6 +702,19 @@ func hashish(x string) bool {
return err != nil
}
// dirSize returns the total size of all files in a directory tree.
func dirSize(path string) int64 {
var size int64
filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return nil
}
size += info.Size()
return nil
})
return size
}
func pruneHistory(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()
@ -737,13 +750,22 @@ func pruneHistory(ctx *cli.Context) error {
return fmt.Errorf("merge block hash mismatch: got %s, want %s", hash.Hex(), mergeBlockHash)
}
// Measure disk usage before pruning.
sizeBefore := dirSize(stack.ResolvePath("chaindata"))
log.Info("Starting history pruning", "head", currentHeader.Number, "tail", mergeBlock, "tailHash", mergeBlockHash)
start := time.Now()
rawdb.PruneTransactionIndex(chaindb, mergeBlock)
if _, err := chaindb.TruncateTail(mergeBlock); err != nil {
return fmt.Errorf("failed to truncate ancient data: %v", err)
}
log.Info("History pruning completed", "tail", mergeBlock, "elapsed", common.PrettyDuration(time.Since(start)))
// Measure disk usage after pruning and report the cleared space.
sizeAfter := dirSize(stack.ResolvePath("chaindata"))
cleared := common.StorageSize(0)
if sizeBefore > sizeAfter {
cleared = common.StorageSize(sizeBefore - sizeAfter)
}
log.Info("History pruning completed", "tail", mergeBlock, "cleared", cleared, "elapsed", common.PrettyDuration(time.Since(start)))
// TODO(s1na): what if there is a crash between the two prune operations?