github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/system/trace/util.go (about)

     1  package trace
     2  
     3  import (
     4  	"github.com/spf13/viper"
     5  	"sync"
     6  )
     7  
     8  const FlagEnableAnalyzer string = "enable-analyzer"
     9  
    10  var (
    11  	openAnalyzer bool
    12  	dynamicConfig IDynamicConfig = MockDynamicConfig{}
    13  	forceAnalyzerTags map[string]struct{}
    14  	status            bool
    15  	once              sync.Once
    16  )
    17  
    18  func EnableAnalyzer(flag bool)  {
    19  	status = flag
    20  }
    21  
    22  func initForceAnalyzerTags() {
    23  	forceAnalyzerTags = map[string]struct{}{
    24  		RunAnte: {},
    25  		Refund:  {},
    26  		RunMsg:  {},
    27  	}
    28  }
    29  
    30  func init() {
    31  	initForceAnalyzerTags()
    32  
    33  	dbOper = newDbRecord()
    34  	for _, v := range STATEDB_READ {
    35  		dbOper.AddOperType(v, READ)
    36  	}
    37  	for _, v := range STATEDB_WRITE {
    38  		dbOper.AddOperType(v, WRITE)
    39  	}
    40  	for _, v := range EVM_OPER {
    41  		dbOper.AddOperType(v, EVMALL)
    42  	}
    43  }
    44  
    45  func OnAppBeginBlockEnter(height int64) {
    46  	analyzer.reset(height)
    47  	if !dynamicConfig.GetEnableAnalyzer() {
    48  		openAnalyzer = false
    49  		return
    50  	}
    51  	openAnalyzer = true
    52  	lastElapsedTime := GetElapsedInfo().GetElapsedTime()
    53  	if singlePprofDumper != nil && lastElapsedTime > singlePprofDumper.triggerAbciElapsed {
    54  		singlePprofDumper.cpuProfile(height)
    55  	}
    56  }
    57  
    58  func skip(oper string) bool {
    59  	if openAnalyzer {
    60  		return false
    61  	}
    62  	_, ok := forceAnalyzerTags[oper]
    63  	return !ok
    64  }
    65  
    66  func OnAppDeliverTxEnter() {
    67  	if analyzer != nil {
    68  		analyzer.onAppDeliverTxEnter()
    69  	}
    70  }
    71  
    72  func OnCommitDone() {
    73  	if analyzer != nil {
    74  		analyzer.onCommitDone()
    75  	}
    76  }
    77  
    78  func StartTxLog(oper string) {
    79  	if !skip(oper) {
    80  		analyzer.startTxLog(oper)
    81  	}
    82  }
    83  
    84  func StopTxLog(oper string) {
    85  	if !skip(oper) {
    86  		analyzer.stopTxLog(oper)
    87  	}
    88  }
    89  
    90  func SetDynamicConfig(c IDynamicConfig) {
    91  	dynamicConfig = c
    92  }
    93  
    94  type IDynamicConfig interface {
    95  	GetEnableAnalyzer() bool
    96  }
    97  
    98  type MockDynamicConfig struct {
    99  }
   100  
   101  func (c MockDynamicConfig) GetEnableAnalyzer() bool {
   102  	return viper.GetBool(FlagEnableAnalyzer)
   103  }