Skip to content

Commit

Permalink
single measureDbDir() process
Browse files Browse the repository at this point in the history
  • Loading branch information
rus-alex committed Feb 15, 2023
1 parent 81e30d1 commit 5bfeed5
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions cmd/opera/launcher/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,40 @@ import (
"os"
"path/filepath"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
)

var (
// TODO: refactor it
dbDir atomic.Value
dbSizeMetric = metrics.NewRegisteredFunctionalGauge("db_size", nil, measureDbDir)
dbSize atomic.Int64
dbSizeMetric = metrics.NewRegisteredFunctionalGauge("db_size", nil, func() int64 {
return dbSize.Load()
})
)

func SetDataDir(datadir string) {
dbDir.Store(datadir)
was := dbDir.Swap(datadir)
if was != nil {
panic("SetDataDir() only once!")
}
go measureDbDir()
}

func measureDbDir() (size int64) {
datadir, ok := dbDir.Load().(string)
if !ok || datadir == "" || datadir == "inmemory" {
return
func measureDbDir() {
for {
time.Sleep(time.Second)

datadir, ok := dbDir.Load().(string)
if !ok || len(datadir) == 0 || datadir == "inmemory" {
dbSize.Store(0)
} else {
size := sizeOfDir(datadir)
dbSize.Store(size)
}
}
return sizeOfDir(datadir)
}

func sizeOfDir(dir string) (size int64) {
Expand Down

0 comments on commit 5bfeed5

Please sign in to comment.