core/state: export statistics to metrics

This commit is contained in:
Gary Rong 2025-11-24 21:41:19 +08:00
parent ca91254259
commit 4bfa8d34a7

View file

@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/triedb" "github.com/ethereum/go-ethereum/triedb"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
@ -48,6 +49,21 @@ var (
codeKeySize = int64(len(rawdb.CodePrefix) + common.HashLength) codeKeySize = int64(len(rawdb.CodePrefix) + common.HashLength)
) )
// State size metrics
var (
stateSizeMetadataGaugeInfo = metrics.NewRegisteredGaugeInfo("state/metadata", nil)
stateSizeAccountsCountGauge = metrics.NewRegisteredGauge("state/accounts/count", nil)
stateSizeAccountsBytesGauge = metrics.NewRegisteredGauge("state/accounts/bytes", nil)
stateSizeStoragesCountGauge = metrics.NewRegisteredGauge("state/storages/count", nil)
stateSizeStoragesBytesGauge = metrics.NewRegisteredGauge("state/storages/bytes", nil)
stateSizeAccountTrieNodesCountGauge = metrics.NewRegisteredGauge("state/trienodes/account/count", nil)
stateSizeAccountTrieNodesBytesGauge = metrics.NewRegisteredGauge("state/trienodes/account/bytes", nil)
stateSizeStorageTrieNodesCountGauge = metrics.NewRegisteredGauge("state/trienodes/storage/count", nil)
stateSizeStorageTrieNodesBytesGauge = metrics.NewRegisteredGauge("state/trienodes/storage/bytes", nil)
stateSizeContractsCountGauge = metrics.NewRegisteredGauge("state/contracts/count", nil)
stateSizeContractsBytesGauge = metrics.NewRegisteredGauge("state/contracts/bytes", nil)
)
// SizeStats represents either the current state size statistics or the size // SizeStats represents either the current state size statistics or the size
// differences resulting from a state transition. // differences resulting from a state transition.
type SizeStats struct { type SizeStats struct {
@ -76,6 +92,23 @@ func (s SizeStats) String() string {
) )
} }
func (s SizeStats) publish() {
stateSizeMetadataGaugeInfo.Update(metrics.GaugeInfoValue{
"number": fmt.Sprintf("%d", s.BlockNumber),
"hash": s.StateRoot.Hex(),
})
stateSizeAccountsCountGauge.Update(s.Accounts)
stateSizeAccountsBytesGauge.Update(s.AccountBytes)
stateSizeStoragesCountGauge.Update(s.Storages)
stateSizeStoragesBytesGauge.Update(s.StorageBytes)
stateSizeAccountTrieNodesCountGauge.Update(s.AccountTrienodes)
stateSizeAccountTrieNodesBytesGauge.Update(s.AccountTrienodeBytes)
stateSizeStorageTrieNodesCountGauge.Update(s.StorageTrienodes)
stateSizeStorageTrieNodesBytesGauge.Update(s.StorageTrienodeBytes)
stateSizeContractsCountGauge.Update(s.ContractCodes)
stateSizeContractsBytesGauge.Update(s.ContractCodeBytes)
}
// add applies the given state diffs and produces a new version of the statistics. // add applies the given state diffs and produces a new version of the statistics.
func (s SizeStats) add(diff SizeStats) SizeStats { func (s SizeStats) add(diff SizeStats) SizeStats {
s.StateRoot = diff.StateRoot s.StateRoot = diff.StateRoot
@ -309,6 +342,10 @@ func (t *SizeTracker) run() {
stats[u.root] = stat stats[u.root] = stat
last = u.root last = u.root
// Publish statistics to metric system
stat.publish()
// Evict the stale statistics
heap.Push(&h, stats[u.root]) heap.Push(&h, stats[u.root])
for u.blockNumber-h[0].BlockNumber > statEvictThreshold { for u.blockNumber-h[0].BlockNumber > statEvictThreshold {
delete(stats, h[0].StateRoot) delete(stats, h[0].StateRoot)