Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/geth: add dbstats command #21887

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 86 additions & 23 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethdb/leveldb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
Expand Down Expand Up @@ -219,6 +221,34 @@ Use "ethereum dump 0" to dump the genesis block.`,
},
Category: "BLOCKCHAIN COMMANDS",
}
statDbCommand = cli.Command{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpicks, would be nice to have a new file dbcmd.go and put all db related commands there(also make the new category DATABASE COMMANDS.

Action: utils.MigrateFlags(leveldbStats),
Name: "leveldb.stats",
Usage: "Print leveldb statistics",
ArgsUsage: " ",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.CacheFlag,
utils.RopstenFlag,
utils.RinkebyFlag,
utils.GoerliFlag,
},
Category: "BLOCKCHAIN COMMANDS",
}
compactDbCommand = cli.Command{
Action: utils.MigrateFlags(leveldbCompact),
Name: "leveldb.compact",
Usage: "Compact leveldb database",
ArgsUsage: " ",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.CacheFlag,
utils.RopstenFlag,
utils.RinkebyFlag,
utils.GoerliFlag,
},
Category: "BLOCKCHAIN COMMANDS",
}
)

// initGenesis will initialise the given JSON format genesis file and writes it as
Expand Down Expand Up @@ -321,17 +351,7 @@ func importChain(ctx *cli.Context) error {
fmt.Printf("Import done in %v.\n\n", time.Since(start))

// Output pre-compaction stats mostly to see the import trashing
stats, err := db.Stat("leveldb.stats")
if err != nil {
utils.Fatalf("Failed to read database stats: %v", err)
}
fmt.Println(stats)

ioStats, err := db.Stat("leveldb.iostats")
if err != nil {
utils.Fatalf("Failed to read database iostats: %v", err)
}
fmt.Println(ioStats)
showLeveldbStats(db)

// Print the memory statistics used by the importing
mem := new(runtime.MemStats)
Expand All @@ -349,22 +369,12 @@ func importChain(ctx *cli.Context) error {
// Compact the entire database to more accurately measure disk io and print the stats
start = time.Now()
fmt.Println("Compacting entire database...")
if err = db.Compact(nil, nil); err != nil {
if err := db.Compact(nil, nil); err != nil {
utils.Fatalf("Compaction failed: %v", err)
}
fmt.Printf("Compaction done in %v.\n\n", time.Since(start))

stats, err = db.Stat("leveldb.stats")
if err != nil {
utils.Fatalf("Failed to read database stats: %v", err)
}
fmt.Println(stats)

ioStats, err = db.Stat("leveldb.iostats")
if err != nil {
utils.Fatalf("Failed to read database iostats: %v", err)
}
fmt.Println(ioStats)
showLeveldbStats(db)
return importErr
}

Expand Down Expand Up @@ -606,6 +616,59 @@ func inspect(ctx *cli.Context) error {
return rawdb.InspectDatabase(chainDb)
}

func showLeveldbStats(db ethdb.Stater) {
if stats, err := db.Stat("leveldb.stats"); err != nil {
log.Warn("Failed to read database stats", "error", err)
} else {
fmt.Println(stats)
}
if ioStats, err := db.Stat("leveldb.iostats"); err != nil {
log.Warn("Failed to read database iostats", "error", err)
} else {
fmt.Println(ioStats)
}
}

func leveldbStats(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()
path := stack.ResolvePath("chaindata")
db, err := leveldb.New(path, 1024, 1000, "")
if err != nil {
return err
}
showLeveldbStats(db)
err = db.Close()
if err != nil {
log.Info("Close err", "error", err)
}
return nil
}

func leveldbCompact(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()
path := stack.ResolvePath("chaindata")
db, err := leveldb.New(path, 1024, 1000, "")
if err != nil {
return err
}
showLeveldbStats(db)
log.Info("Triggering compaction")
err = db.Compact(nil, nil)
if err != nil {
log.Info("Compact err", "error", err)
}
showLeveldbStats(db)
log.Info("Closing db")
err = db.Close()
if err != nil {
log.Info("Close err", "error", err)
}
log.Info("Exiting")
return err
}

// hashish returns true for strings that look like hashes.
func hashish(x string) bool {
_, err := strconv.Atoi(x)
Expand Down
2 changes: 2 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ func init() {
dumpCommand,
dumpGenesisCommand,
inspectCommand,
statDbCommand,
compactDbCommand,
// See accountcmd.go:
accountCommand,
walletCommand,
Expand Down