github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/iavl/nodedb_statistics.go (about)

     1  package iavl
     2  
     3  import (
     4  	"fmt"
     5  	"sync/atomic"
     6  
     7  	"github.com/fibonacci-chain/fbc/libs/system/trace"
     8  )
     9  
    10  type RuntimeState struct {
    11  	dbReadTime    int64
    12  	dbReadCount   int64
    13  	nodeReadCount int64
    14  	dbWriteCount  int64
    15  
    16  	totalPersistedCount int64
    17  	totalPersistedSize  int64
    18  	totalDeletedCount   int64
    19  	totalOrphanCount    int64
    20  
    21  	fromPpnc        int64
    22  	fromTpp         int64
    23  	fromNodeCache   int64
    24  	fromOrphanCache int64
    25  	fromDisk        int64
    26  }
    27  
    28  type retrieveType int
    29  
    30  const (
    31  	unknown retrieveType = iota
    32  	fromPpnc
    33  	fromTpp
    34  	fromNodeCache
    35  	fromOrphanCache
    36  	fromDisk
    37  )
    38  
    39  func newRuntimeState() *RuntimeState {
    40  	r := &RuntimeState{}
    41  	return r
    42  }
    43  
    44  func (s *RuntimeState) onLoadNode(from retrieveType) {
    45  
    46  	switch from {
    47  	case fromPpnc:
    48  		atomic.AddInt64(&s.fromPpnc, 1)
    49  	case fromTpp:
    50  		atomic.AddInt64(&s.fromTpp, 1)
    51  	case fromNodeCache:
    52  		atomic.AddInt64(&s.fromNodeCache, 1)
    53  	case fromOrphanCache:
    54  		atomic.AddInt64(&s.fromOrphanCache, 1)
    55  	case fromDisk:
    56  		atomic.AddInt64(&s.fromDisk, 1)
    57  	}
    58  }
    59  
    60  func (s *RuntimeState) addDBReadTime(ts int64) {
    61  	atomic.AddInt64(&s.dbReadTime, ts)
    62  }
    63  
    64  func (s *RuntimeState) addDBReadCount() {
    65  	atomic.AddInt64(&s.dbReadCount, 1)
    66  }
    67  
    68  func (s *RuntimeState) addDBWriteCount(count int64) {
    69  	atomic.AddInt64(&s.dbWriteCount, count)
    70  }
    71  
    72  func (s *RuntimeState) addNodeReadCount() {
    73  	atomic.AddInt64(&s.nodeReadCount, 1)
    74  }
    75  
    76  func (s *RuntimeState) resetDBReadTime() {
    77  	atomic.StoreInt64(&s.dbReadTime, 0)
    78  }
    79  
    80  func (s *RuntimeState) resetDBReadCount() {
    81  	atomic.StoreInt64(&s.dbReadCount, 0)
    82  }
    83  
    84  func (s *RuntimeState) resetDBWriteCount() {
    85  	atomic.StoreInt64(&s.dbWriteCount, 0)
    86  }
    87  
    88  func (s *RuntimeState) resetNodeReadCount() {
    89  	atomic.StoreInt64(&s.nodeReadCount, 0)
    90  }
    91  
    92  func (s *RuntimeState) getDBReadTime() int {
    93  	return int(atomic.LoadInt64(&s.dbReadTime))
    94  }
    95  
    96  func (s *RuntimeState) getDBReadCount() int {
    97  	return int(atomic.LoadInt64(&s.dbReadCount))
    98  }
    99  
   100  func (s *RuntimeState) getDBWriteCount() int {
   101  	return int(atomic.LoadInt64(&s.dbWriteCount))
   102  }
   103  
   104  func (s *RuntimeState) getNodeReadCount() int {
   105  	return int(atomic.LoadInt64(&s.nodeReadCount))
   106  }
   107  
   108  func (s *RuntimeState) resetCount() {
   109  	s.resetDBReadTime()
   110  	s.resetDBReadCount()
   111  	s.resetDBWriteCount()
   112  	s.resetNodeReadCount()
   113  }
   114  
   115  func (s *RuntimeState) increasePersistedSize(num int) {
   116  	atomic.AddInt64(&s.totalPersistedSize, int64(num))
   117  }
   118  func (s *RuntimeState) increasePersistedCount(num int) {
   119  	atomic.AddInt64(&s.totalPersistedCount, int64(num))
   120  }
   121  func (s *RuntimeState) increasOrphanCount(num int) {
   122  	atomic.AddInt64(&s.totalOrphanCount, int64(num))
   123  }
   124  func (s *RuntimeState) increaseDeletedCount() {
   125  	s.totalDeletedCount++
   126  }
   127  
   128  func inOutputModules(name string) bool {
   129  	v, ok := OutputModules[name]
   130  	return ok && v != 0
   131  }
   132  
   133  // ================================
   134  func (ndb *nodeDB) sprintCacheLog(version int64) (printLog string) {
   135  	if !EnableAsyncCommit {
   136  		return
   137  	}
   138  
   139  	if !inOutputModules(ndb.name) {
   140  		return
   141  	}
   142  
   143  	nodeReadCount := ndb.state.getNodeReadCount()
   144  	cacheReadCount := ndb.state.getNodeReadCount() - ndb.state.getDBReadCount()
   145  	header := fmt.Sprintf("Save Version<%d>: Tree<%s>, ", version, ndb.name)
   146  
   147  	printLog = fmt.Sprintf("getNodeFrom<ppnc=%d, tpp=%d, nodeCache=%d, orphanCache=%d, disk=%d>",
   148  		ndb.state.fromPpnc,
   149  		ndb.state.fromTpp,
   150  		ndb.state.fromNodeCache,
   151  		ndb.state.fromOrphanCache,
   152  		ndb.state.fromDisk)
   153  	printLog += fmt.Sprintf(", ppncCache:%d", len(ndb.prePersistNodeCache))
   154  	printLog += fmt.Sprintf(", nodeCache:%d", ndb.nc.nodeCacheLen())
   155  	printLog += fmt.Sprintf(", orphanCache:%d", ndb.oi.orphanNodeCacheLen())
   156  	printLog += fmt.Sprintf(", totalPpnc:%d", treeMap.totalPpncSize)
   157  	printLog += fmt.Sprintf(", evmPpnc:%d", treeMap.evmPpncSize)
   158  	printLog += fmt.Sprintf(", accPpnc:%d", treeMap.accPpncSize)
   159  	printLog += fmt.Sprintf(", dbRCnt:%d", ndb.state.getDBReadCount())
   160  	printLog += fmt.Sprintf(", dbWCnt:%d", ndb.state.getDBWriteCount())
   161  	printLog += fmt.Sprintf(", nodeRCnt:%d", ndb.state.getNodeReadCount())
   162  
   163  	if nodeReadCount > 0 {
   164  		printLog += fmt.Sprintf(", CHit:%.2f", float64(cacheReadCount)/float64(nodeReadCount)*100)
   165  	} else {
   166  		printLog += ", CHit:0"
   167  	}
   168  	printLog += fmt.Sprintf(", TPersisCnt:%d", atomic.LoadInt64(&ndb.state.totalPersistedCount))
   169  	printLog += fmt.Sprintf(", TPersisSize:%d", atomic.LoadInt64(&ndb.state.totalPersistedSize))
   170  	printLog += fmt.Sprintf(", TDelCnt:%d", atomic.LoadInt64(&ndb.state.totalDeletedCount))
   171  	printLog += fmt.Sprintf(", TOrphanCnt:%d", atomic.LoadInt64(&ndb.state.totalOrphanCount))
   172  
   173  	if ndb.name == "evm" {
   174  		trace.GetElapsedInfo().AddInfo(trace.IavlRuntime, printLog)
   175  	}
   176  
   177  	return header + printLog
   178  }
   179  
   180  func (ndb *nodeDB) getDBReadTime() int {
   181  	return ndb.state.getDBReadTime()
   182  }
   183  
   184  func (ndb *nodeDB) getDBReadCount() int {
   185  	return ndb.state.getDBReadCount()
   186  }
   187  
   188  func (ndb *nodeDB) getDBWriteCount() int {
   189  	return ndb.state.getDBWriteCount()
   190  }
   191  
   192  func (ndb *nodeDB) getNodeReadCount() int {
   193  	return ndb.state.getNodeReadCount()
   194  }
   195  
   196  func (ndb *nodeDB) resetCount() {
   197  	ndb.state.resetCount()
   198  }
   199  
   200  func (ndb *nodeDB) addDBReadTime(ts int64) {
   201  	ndb.state.addDBReadTime(ts)
   202  }
   203  
   204  func (ndb *nodeDB) addDBReadCount() {
   205  	ndb.state.addDBReadCount()
   206  }
   207  
   208  func (ndb *nodeDB) addDBWriteCount(count int64) {
   209  	ndb.state.addDBWriteCount(count)
   210  }
   211  
   212  func (ndb *nodeDB) addNodeReadCount() {
   213  	ndb.state.addNodeReadCount()
   214  }