core: add state disk hit rates

This commit is contained in:
Gary Rong 2025-10-16 11:00:41 +08:00
parent 70c0699655
commit edd380a1d1
3 changed files with 86 additions and 39 deletions

View file

@ -2037,16 +2037,16 @@ func (bc *BlockChain) ProcessBlock(parentRoot common.Hash, block *types.Block, s
// Upload the statistics of reader at the end // Upload the statistics of reader at the end
defer func() { defer func() {
pStat := prefetch.GetStats() pStat := prefetch.GetStats()
accountCacheHitPrefetchMeter.Mark(pStat.AccountHit) accountCacheHitPrefetchMeter.Mark(pStat.AccountCacheHit)
accountCacheMissPrefetchMeter.Mark(pStat.AccountMiss) accountCacheMissPrefetchMeter.Mark(pStat.AccountCacheMiss)
storageCacheHitPrefetchMeter.Mark(pStat.StorageHit) storageCacheHitPrefetchMeter.Mark(pStat.StorageCacheHit)
storageCacheMissPrefetchMeter.Mark(pStat.StorageMiss) storageCacheMissPrefetchMeter.Mark(pStat.StorageCacheMiss)
rStat := process.GetStats() rStat := process.GetStats()
accountCacheHitMeter.Mark(rStat.AccountHit) accountCacheHitMeter.Mark(rStat.AccountCacheHit)
accountCacheMissMeter.Mark(rStat.AccountMiss) accountCacheMissMeter.Mark(rStat.AccountCacheMiss)
storageCacheHitMeter.Mark(rStat.StorageHit) storageCacheHitMeter.Mark(rStat.StorageCacheHit)
storageCacheMissMeter.Mark(rStat.StorageMiss) storageCacheMissMeter.Mark(rStat.StorageCacheMiss)
if result != nil { if result != nil {
result.stats.StatePrefetchCacheStats = pStat result.stats.StatePrefetchCacheStats = pStat

View file

@ -86,15 +86,15 @@ func (s *ExecuteStats) reportMetrics() {
chainMgaspsMeter.Update(time.Duration(s.MgasPerSecond)) // TODO(rjl493456442) generalize the ResettingTimer chainMgaspsMeter.Update(time.Duration(s.MgasPerSecond)) // TODO(rjl493456442) generalize the ResettingTimer
// Cache hit rates // Cache hit rates
accountCacheHitPrefetchMeter.Mark(s.StatePrefetchCacheStats.AccountHit) accountCacheHitPrefetchMeter.Mark(s.StatePrefetchCacheStats.AccountCacheHit)
accountCacheMissPrefetchMeter.Mark(s.StatePrefetchCacheStats.AccountMiss) accountCacheMissPrefetchMeter.Mark(s.StatePrefetchCacheStats.AccountCacheMiss)
storageCacheHitPrefetchMeter.Mark(s.StatePrefetchCacheStats.StorageHit) storageCacheHitPrefetchMeter.Mark(s.StatePrefetchCacheStats.StorageCacheHit)
storageCacheMissPrefetchMeter.Mark(s.StatePrefetchCacheStats.StorageMiss) storageCacheMissPrefetchMeter.Mark(s.StatePrefetchCacheStats.StorageCacheMiss)
accountCacheHitMeter.Mark(s.StateReadCacheStats.AccountHit) accountCacheHitMeter.Mark(s.StateReadCacheStats.AccountCacheHit)
accountCacheMissMeter.Mark(s.StateReadCacheStats.AccountMiss) accountCacheMissMeter.Mark(s.StateReadCacheStats.AccountCacheMiss)
storageCacheHitMeter.Mark(s.StateReadCacheStats.StorageHit) storageCacheHitMeter.Mark(s.StateReadCacheStats.StorageCacheHit)
storageCacheMissMeter.Mark(s.StateReadCacheStats.StorageMiss) storageCacheMissMeter.Mark(s.StateReadCacheStats.StorageCacheMiss)
} }
// logSlow prints the detailed execution statistics if the block is regarded as slow. // logSlow prints the detailed execution statistics if the block is regarded as slow.

View file

@ -87,25 +87,45 @@ type Reader interface {
// ReaderStats wraps the statistics of reader. // ReaderStats wraps the statistics of reader.
type ReaderStats struct { type ReaderStats struct {
AccountHit int64 // Cache stats
AccountMiss int64 AccountCacheHit int64
StorageHit int64 AccountCacheMiss int64
StorageMiss int64 StorageCacheHit int64
StorageCacheMiss int64
// Disk stats
AccountDiskHit int64
AccountDiskMiss int64
StorageDiskHit int64
StorageDiskMiss int64
} }
// String implements fmt.Stringer, returning string format statistics. // String implements fmt.Stringer, returning string format statistics.
func (s ReaderStats) String() string { func (s ReaderStats) String() string {
var ( var (
accountRate float64 accountCacheHitRate float64
storageRate float64 storageCacheHitRate float64
accountDiskHitRate float64
storageDiskHitRate float64
) )
if s.AccountHit > 0 { if s.AccountCacheHit > 0 {
accountRate = float64(s.AccountHit) / float64(s.AccountHit+s.AccountMiss) * 100 accountCacheHitRate = float64(s.AccountCacheHit) / float64(s.AccountCacheHit+s.AccountCacheMiss) * 100
} }
if s.StorageHit > 0 { if s.AccountDiskHit > 0 {
storageRate = float64(s.StorageHit) / float64(s.StorageHit+s.StorageMiss) * 100 accountDiskHitRate = float64(s.AccountDiskHit) / float64(s.AccountDiskHit+s.AccountDiskMiss) * 100
} }
return fmt.Sprintf("account (hit: %d, miss: %d, rate: %.2f), storage (hit: %d, miss: %d, rate: %.2f)", s.AccountHit, s.AccountMiss, accountRate, s.StorageHit, s.StorageMiss, storageRate) if s.StorageCacheHit > 0 {
storageCacheHitRate = float64(s.StorageCacheHit) / float64(s.StorageCacheHit+s.StorageCacheMiss) * 100
}
if s.StorageDiskHit > 0 {
storageDiskHitRate = float64(s.StorageDiskHit) / float64(s.StorageDiskHit+s.StorageDiskMiss) * 100
}
msg := fmt.Sprintf("=== Reader statistics ===\n")
msg += fmt.Sprintf("account: cache (hit: %d, miss: %d, rate: %.2f)\n", s.AccountCacheHit, s.AccountCacheMiss, accountCacheHitRate)
msg += fmt.Sprintf("account: disk (hit: %d, miss: %d, rate: %.2f)\n", s.AccountDiskHit, s.AccountDiskMiss, accountDiskHitRate)
msg += fmt.Sprintf("storage: cache (hit: %d, miss: %d, rate: %.2f)\n", s.StorageCacheHit, s.StorageCacheMiss, storageCacheHitRate)
msg += fmt.Sprintf("storage: disk (hit: %d, miss: %d, rate: %.2f)\n", s.StorageDiskHit, s.StorageDiskMiss, storageDiskHitRate)
return msg
} }
// ReaderWithStats wraps the additional method to retrieve the reader statistics from. // ReaderWithStats wraps the additional method to retrieve the reader statistics from.
@ -539,10 +559,16 @@ func (r *readerWithCache) Storage(addr common.Address, slot common.Hash) (common
type readerWithCacheStats struct { type readerWithCacheStats struct {
*readerWithCache *readerWithCache
accountHit atomic.Int64
accountMiss atomic.Int64 accountCacheHit atomic.Int64
storageHit atomic.Int64 accountCacheMiss atomic.Int64
storageMiss atomic.Int64 storageCacheHit atomic.Int64
storageCacheMiss atomic.Int64
accountDiskHit atomic.Int64
accountDiskMiss atomic.Int64
storageDiskHit atomic.Int64
storageDiskMiss atomic.Int64
} }
// newReaderWithCacheStats constructs the reader with additional statistics tracked. // newReaderWithCacheStats constructs the reader with additional statistics tracked.
@ -562,9 +588,17 @@ func (r *readerWithCacheStats) Account(addr common.Address) (*types.StateAccount
return nil, err return nil, err
} }
if incache { if incache {
r.accountHit.Add(1) r.accountCacheHit.Add(1)
} else { } else {
r.accountMiss.Add(1) r.accountCacheMiss.Add(1)
// If the account was read from the underlying storage, count
// the presence statistics.
if account == nil {
r.accountDiskMiss.Add(1)
} else {
r.accountDiskHit.Add(1)
}
} }
return account, nil return account, nil
} }
@ -580,9 +614,17 @@ func (r *readerWithCacheStats) Storage(addr common.Address, slot common.Hash) (c
return common.Hash{}, err return common.Hash{}, err
} }
if incache { if incache {
r.storageHit.Add(1) r.storageCacheHit.Add(1)
} else { } else {
r.storageMiss.Add(1) r.storageCacheMiss.Add(1)
// If the slot was read from the underlying storage, count
// the presence statistics.
if value == (common.Hash{}) {
r.accountDiskMiss.Add(1)
} else {
r.accountDiskHit.Add(1)
}
} }
return value, nil return value, nil
} }
@ -590,9 +632,14 @@ func (r *readerWithCacheStats) Storage(addr common.Address, slot common.Hash) (c
// GetStats implements ReaderWithStats, returning the statistics of state reader. // GetStats implements ReaderWithStats, returning the statistics of state reader.
func (r *readerWithCacheStats) GetStats() ReaderStats { func (r *readerWithCacheStats) GetStats() ReaderStats {
return ReaderStats{ return ReaderStats{
AccountHit: r.accountHit.Load(), AccountCacheHit: r.accountCacheHit.Load(),
AccountMiss: r.accountMiss.Load(), AccountCacheMiss: r.accountCacheMiss.Load(),
StorageHit: r.storageHit.Load(), StorageCacheHit: r.storageCacheHit.Load(),
StorageMiss: r.storageMiss.Load(), StorageCacheMiss: r.storageCacheMiss.Load(),
AccountDiskHit: r.accountDiskHit.Load(),
AccountDiskMiss: r.accountDiskMiss.Load(),
StorageDiskHit: r.storageDiskHit.Load(),
StorageDiskMiss: r.storageDiskMiss.Load(),
} }
} }