Skip to content

Commit

Permalink
Merge pull request #519 from uprendis/feature/fix-metrics-db-size-per…
Browse files Browse the repository at this point in the history
…formance

Fix db_size metric performance
  • Loading branch information
uprendis committed Oct 14, 2023
2 parents cd87811 + 406dc20 commit 19c1a95
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion cmd/opera/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/console/prompt"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
gmetrics "github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/discover/discfilter"
"github.com/ethereum/go-ethereum/params"
Expand Down Expand Up @@ -299,7 +300,10 @@ func makeNode(ctx *cli.Context, cfg *config, genesisStore *genesisstore.Store) (
if genesisStore != nil {
_ = genesisStore.Close()
}
metrics.SetDataDir(cfg.Node.DataDir)

if gmetrics.Enabled {
metrics.SetDataDir(cfg.Node.DataDir)
}
memorizeDBPreset(cfg)

// substitute default bootnodes if requested
Expand Down
12 changes: 8 additions & 4 deletions cmd/opera/launcher/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func measureDbDir(name, datadir string) {
rescan = (len(datadir) > 0 && datadir != "inmemory")
)
for {
time.Sleep(time.Second)
time.Sleep(10 * time.Second)

if rescan {
size := sizeOfDir(datadir)
size := sizeOfDir(datadir, new(int))
atomic.StoreInt64(&dbSize, size)
}

Expand All @@ -45,8 +45,12 @@ func measureDbDir(name, datadir string) {
}
}

func sizeOfDir(dir string) (size int64) {
func sizeOfDir(dir string, counter *int) (size int64) {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
*counter++
if *counter % 100 == 0 {
time.Sleep(100 * time.Millisecond)
}
if err != nil {
log.Debug("datadir walk", "path", path, "err", err)
return filepath.SkipDir
Expand All @@ -58,7 +62,7 @@ func sizeOfDir(dir string) (size int64) {

dst, err := filepath.EvalSymlinks(path)
if err == nil && dst != path {
size += sizeOfDir(dst)
size += sizeOfDir(dst, counter)
} else {
size += info.Size()
}
Expand Down

0 comments on commit 19c1a95

Please sign in to comment.