core/state: add snapshot cache stats

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2025-03-31 09:08:22 +00:00
parent 29a87e4426
commit 8615149a4d

View file

@ -23,6 +23,7 @@ import (
"fmt"
"sync"
"github.com/VictoriaMetrics/fastcache"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types"
@ -77,6 +78,14 @@ var (
snapshotBloomStorageFalseHitMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/storage/falsehit", nil)
snapshotBloomStorageMissMeter = metrics.NewRegisteredMeter("state/snapshot/bloom/storage/miss", nil)
snapshotCacheGetGauge = metrics.NewRegisteredGauge("state/snapshot/cache/get", nil)
snapshotCacheSetGauge = metrics.NewRegisteredGauge("state/snapshot/cache/set", nil)
snapshotCacheMissGauge = metrics.NewRegisteredGauge("state/snapshot/cache/miss", nil)
snapshotCacheSizeGauge = metrics.NewRegisteredGauge("state/snapshot/cache/size", nil)
snapshotCacheCapacityGauge = metrics.NewRegisteredGauge("state/snapshot/cache/capacity", nil)
snapshotCacheCollisionGauge = metrics.NewRegisteredGauge("state/snapshot/cache/collision", nil)
snapshotCacheEntriesGauge = metrics.NewRegisteredGauge("state/snapshot/cache/entries", nil)
// ErrSnapshotStale is returned from data accessors if the underlying snapshot
// layer had been invalidated due to the chain progressing forward far enough
// to not maintain the layer's original state.
@ -388,7 +397,17 @@ func (t *Tree) Cap(root common.Hash, layers int) error {
// Run the internal capping and discard all stale layers
t.lock.Lock()
defer t.lock.Unlock()
var stats fastcache.Stats
diff.origin.cache.UpdateStats(&stats)
snapshotCacheGetGauge.Update(int64(stats.GetCalls))
snapshotCacheSetGauge.Update(int64(stats.SetCalls))
snapshotCacheMissGauge.Update(int64(stats.Misses))
snapshotCacheSizeGauge.Update(int64(stats.BytesSize))
snapshotCacheCapacityGauge.Update(int64(stats.MaxBytesSize))
snapshotCacheEntriesGauge.Update(int64(stats.EntriesCount))
snapshotCacheCollisionGauge.Update(int64(stats.Collisions))
// Flattening the bottom-most diff layer requires special casing since there's
// no child to rewire to the grandparent. In that case we can fake a temporary