@@ -87,25 +87,45 @@ type Reader interface {
8787
8888// ReaderStats wraps the statistics of reader.
8989type ReaderStats struct {
90- AccountHit int64
91- AccountMiss int64
92- StorageHit int64
93- StorageMiss int64
90+ // Cache stats
91+ AccountCacheHit int64
92+ AccountCacheMiss int64
93+ StorageCacheHit int64
94+ StorageCacheMiss int64
95+
96+ // Disk stats
97+ AccountDiskHit int64
98+ AccountDiskMiss int64
99+ StorageDiskHit int64
100+ StorageDiskMiss int64
94101}
95102
96103// String implements fmt.Stringer, returning string format statistics.
97104func (s ReaderStats ) String () string {
98105 var (
99- accountRate float64
100- storageRate float64
106+ accountCacheHitRate float64
107+ storageCacheHitRate float64
108+ accountDiskHitRate float64
109+ storageDiskHitRate float64
101110 )
102- if s .AccountHit > 0 {
103- accountRate = float64 (s .AccountHit ) / float64 (s .AccountHit + s .AccountMiss ) * 100
111+ if s .AccountCacheHit > 0 {
112+ accountCacheHitRate = float64 (s .AccountCacheHit ) / float64 (s .AccountCacheHit + s .AccountCacheMiss ) * 100
113+ }
114+ if s .AccountDiskHit > 0 {
115+ accountDiskHitRate = float64 (s .AccountDiskHit ) / float64 (s .AccountDiskHit + s .AccountDiskMiss ) * 100
104116 }
105- if s .StorageHit > 0 {
106- storageRate = float64 (s .StorageHit ) / float64 (s .StorageHit + s .StorageMiss ) * 100
117+ if s .StorageCacheHit > 0 {
118+ storageCacheHitRate = float64 (s .StorageCacheHit ) / float64 (s .StorageCacheHit + s .StorageCacheMiss ) * 100
107119 }
108- 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 )
120+ if s .StorageDiskHit > 0 {
121+ storageDiskHitRate = float64 (s .StorageDiskHit ) / float64 (s .StorageDiskHit + s .StorageDiskMiss ) * 100
122+ }
123+ msg := fmt .Sprintf ("=== Reader statistics ===\n " )
124+ msg += fmt .Sprintf ("account: cache (hit: %d, miss: %d, rate: %.2f)\n " , s .AccountCacheHit , s .AccountCacheMiss , accountCacheHitRate )
125+ msg += fmt .Sprintf ("account: disk (hit: %d, miss: %d, rate: %.2f)\n " , s .AccountDiskHit , s .AccountDiskMiss , accountDiskHitRate )
126+ msg += fmt .Sprintf ("storage: cache (hit: %d, miss: %d, rate: %.2f)\n " , s .StorageCacheHit , s .StorageCacheMiss , storageCacheHitRate )
127+ msg += fmt .Sprintf ("storage: disk (hit: %d, miss: %d, rate: %.2f)\n " , s .StorageDiskHit , s .StorageDiskMiss , storageDiskHitRate )
128+ return msg
109129}
110130
111131// 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
539559
540560type readerWithCacheStats struct {
541561 * readerWithCache
542- accountHit atomic.Int64
543- accountMiss atomic.Int64
544- storageHit atomic.Int64
545- storageMiss atomic.Int64
562+
563+ accountCacheHit atomic.Int64
564+ accountCacheMiss atomic.Int64
565+ storageCacheHit atomic.Int64
566+ storageCacheMiss atomic.Int64
567+
568+ accountDiskHit atomic.Int64
569+ accountDiskMiss atomic.Int64
570+ storageDiskHit atomic.Int64
571+ storageDiskMiss atomic.Int64
546572}
547573
548574// newReaderWithCacheStats constructs the reader with additional statistics tracked.
@@ -562,9 +588,17 @@ func (r *readerWithCacheStats) Account(addr common.Address) (*types.StateAccount
562588 return nil , err
563589 }
564590 if incache {
565- r .accountHit .Add (1 )
591+ r .accountCacheHit .Add (1 )
566592 } else {
567- r .accountMiss .Add (1 )
593+ r .accountCacheMiss .Add (1 )
594+
595+ // If the account was read from the underlying storage, count
596+ // the presence statistics.
597+ if account == nil {
598+ r .accountDiskMiss .Add (1 )
599+ } else {
600+ r .accountDiskHit .Add (1 )
601+ }
568602 }
569603 return account , nil
570604}
@@ -580,19 +614,32 @@ func (r *readerWithCacheStats) Storage(addr common.Address, slot common.Hash) (c
580614 return common.Hash {}, err
581615 }
582616 if incache {
583- r .storageHit .Add (1 )
617+ r .storageCacheHit .Add (1 )
584618 } else {
585- r .storageMiss .Add (1 )
619+ r .storageCacheMiss .Add (1 )
620+
621+ // If the slot was read from the underlying storage, count
622+ // the presence statistics.
623+ if value == (common.Hash {}) {
624+ r .accountDiskMiss .Add (1 )
625+ } else {
626+ r .accountDiskHit .Add (1 )
627+ }
586628 }
587629 return value , nil
588630}
589631
590632// GetStats implements ReaderWithStats, returning the statistics of state reader.
591633func (r * readerWithCacheStats ) GetStats () ReaderStats {
592634 return ReaderStats {
593- AccountHit : r .accountHit .Load (),
594- AccountMiss : r .accountMiss .Load (),
595- StorageHit : r .storageHit .Load (),
596- StorageMiss : r .storageMiss .Load (),
635+ AccountCacheHit : r .accountCacheHit .Load (),
636+ AccountCacheMiss : r .accountCacheMiss .Load (),
637+ StorageCacheHit : r .storageCacheHit .Load (),
638+ StorageCacheMiss : r .storageCacheMiss .Load (),
639+
640+ AccountDiskHit : r .accountDiskHit .Load (),
641+ AccountDiskMiss : r .accountDiskMiss .Load (),
642+ StorageDiskHit : r .storageDiskHit .Load (),
643+ StorageDiskMiss : r .storageDiskMiss .Load (),
597644 }
598645}
0 commit comments