Skip to content

Commit 19c1a95

Browse files
authored
Merge pull request #519 from uprendis/feature/fix-metrics-db-size-performance
Fix db_size metric performance
2 parents cd87811 + 406dc20 commit 19c1a95

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

cmd/opera/launcher/launcher.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/ethereum/go-ethereum/console/prompt"
1515
"github.com/ethereum/go-ethereum/ethclient"
1616
"github.com/ethereum/go-ethereum/log"
17+
gmetrics "github.com/ethereum/go-ethereum/metrics"
1718
"github.com/ethereum/go-ethereum/node"
1819
"github.com/ethereum/go-ethereum/p2p/discover/discfilter"
1920
"github.com/ethereum/go-ethereum/params"
@@ -299,7 +300,10 @@ func makeNode(ctx *cli.Context, cfg *config, genesisStore *genesisstore.Store) (
299300
if genesisStore != nil {
300301
_ = genesisStore.Close()
301302
}
302-
metrics.SetDataDir(cfg.Node.DataDir)
303+
304+
if gmetrics.Enabled {
305+
metrics.SetDataDir(cfg.Node.DataDir)
306+
}
303307
memorizeDBPreset(cfg)
304308

305309
// substitute default bootnodes if requested

cmd/opera/launcher/metrics/metrics.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ func measureDbDir(name, datadir string) {
2626
rescan = (len(datadir) > 0 && datadir != "inmemory")
2727
)
2828
for {
29-
time.Sleep(time.Second)
29+
time.Sleep(10 * time.Second)
3030

3131
if rescan {
32-
size := sizeOfDir(datadir)
32+
size := sizeOfDir(datadir, new(int))
3333
atomic.StoreInt64(&dbSize, size)
3434
}
3535

@@ -45,8 +45,12 @@ func measureDbDir(name, datadir string) {
4545
}
4646
}
4747

48-
func sizeOfDir(dir string) (size int64) {
48+
func sizeOfDir(dir string, counter *int) (size int64) {
4949
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
50+
*counter++
51+
if *counter % 100 == 0 {
52+
time.Sleep(100 * time.Millisecond)
53+
}
5054
if err != nil {
5155
log.Debug("datadir walk", "path", path, "err", err)
5256
return filepath.SkipDir
@@ -58,7 +62,7 @@ func sizeOfDir(dir string) (size int64) {
5862

5963
dst, err := filepath.EvalSymlinks(path)
6064
if err == nil && dst != path {
61-
size += sizeOfDir(dst)
65+
size += sizeOfDir(dst, counter)
6266
} else {
6367
size += info.Size()
6468
}

0 commit comments

Comments
 (0)