/
analyzer.go
235 lines (202 loc) · 4.53 KB
/
analyzer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package analyzer
import (
"fmt"
sm "github.com/FiboChain/fbc/libs/tendermint/state"
"github.com/spf13/viper"
"sync"
bam "github.com/FiboChain/fbc/libs/cosmos-sdk/baseapp"
"github.com/FiboChain/fbc/libs/tendermint/trace"
)
const FlagEnableAnalyzer string = "enable-analyzer"
var (
singleAnalys *analyer
openAnalyzer bool
once sync.Once
dynamicConfig IDynamicConfig = MockDynamicConfig{}
)
func SetDynamicConfig(c IDynamicConfig) {
dynamicConfig = c
}
type IDynamicConfig interface {
GetEnableAnalyzer() bool
}
type MockDynamicConfig struct {
}
func (c MockDynamicConfig) GetEnableAnalyzer() bool {
return viper.GetBool(FlagEnableAnalyzer)
}
type analyer struct {
status bool
currentTxIndex int64
blockHeight int64
dbRead int64
dbWrite int64
evmCost int64
txs []*txLog
}
func init() {
dbOper = newDbRecord()
for _, v := range STATEDB_READ {
dbOper.AddOperType(v, READ)
}
for _, v := range STATEDB_WRITE {
dbOper.AddOperType(v, WRITE)
}
for _, v := range EVM_OPER {
dbOper.AddOperType(v, EVMALL)
}
}
func newAnalys(height int64) {
if singleAnalys == nil {
singleAnalys = &analyer{
status: !viper.GetBool(sm.FlagParalleledTx),
blockHeight: height,
}
}
}
func OnAppBeginBlockEnter(height int64) {
if !dynamicConfig.GetEnableAnalyzer() {
return
}
newAnalys(height)
lastElapsedTime := trace.GetElapsedInfo().GetElapsedTime()
if singlePprofDumper != nil && lastElapsedTime > singlePprofDumper.triggerAbciElapsed {
singlePprofDumper.cpuProfile(height)
}
}
func OnAppDeliverTxEnter() {
if singleAnalys != nil {
singleAnalys.onAppDeliverTxEnter()
}
}
func OnCommitDone() {
if singleAnalys != nil {
singleAnalys.onCommitDone()
}
singleAnalys = nil
}
func StartTxLog(oper string) {
if singleAnalys != nil {
singleAnalys.startTxLog(oper)
}
}
func StopTxLog(oper string) {
if singleAnalys != nil {
singleAnalys.stopTxLog(oper)
}
}
func (s *analyer) onAppDeliverTxEnter() {
if s.status {
s.newTxLog()
}
}
func (s *analyer) onCommitDone() {
if s.status {
s.format()
}
singleAnalys = nil
}
func (s *analyer) newTxLog() {
s.currentTxIndex++
s.txs = append(s.txs, newTxLog())
}
func (s *analyer) startTxLog(oper string) {
if s.status {
if s.currentTxIndex > 0 && int64(len(s.txs)) == s.currentTxIndex {
s.txs[s.currentTxIndex-1].StartTxLog(oper)
}
}
}
func (s *analyer) stopTxLog(oper string) {
if s.status {
if s.currentTxIndex > 0 && int64(len(s.txs)) == s.currentTxIndex {
s.txs[s.currentTxIndex-1].StopTxLog(oper)
}
}
}
func (s *analyer) format() {
evmcore, record := s.genRecord()
formatDeliverTx(record)
formatRunAnteDetail(record)
formatEvmHandlerDetail(record)
// evm
trace.GetElapsedInfo().AddInfo(trace.Evm, fmt.Sprintf(EVM_FORMAT, s.dbRead, s.dbWrite, evmcore-s.dbRead-s.dbWrite))
}
func addInfo(name string, keys []string, record map[string]int64) {
var comma, format string
for _, v := range keys {
format += fmt.Sprintf("%s%s<%dms>", comma, v, record[v])
comma = ", "
}
trace.GetElapsedInfo().AddInfo(name, format)
}
func (s *analyer) genRecord() (int64, map[string]int64) {
var evmcore int64
var record = make(map[string]int64)
for _, v := range s.txs {
for oper, operObj := range v.Record {
operType := dbOper.GetOperType(oper)
switch operType {
case READ:
s.dbRead += operObj.TimeCost
case WRITE:
s.dbWrite += operObj.TimeCost
case EVMALL:
evmcore += operObj.TimeCost
default:
if _, ok := record[oper]; !ok {
record[oper] = operObj.TimeCost
} else {
record[oper] += operObj.TimeCost
}
}
}
}
return evmcore, record
}
func formatDeliverTx(record map[string]int64) {
// deliver txs
var deliverTxsKeys = []string{
//----- DeliverTx
//bam.DeliverTx,
//bam.TxDecoder,
//bam.RunTx,
//----- run_tx
//bam.InitCtx,
bam.ValTxMsgs,
bam.RunAnte,
bam.RunMsg,
bam.Refund,
bam.EvmHandler,
}
addInfo(trace.DeliverTxs, deliverTxsKeys, record)
}
func formatEvmHandlerDetail(record map[string]int64) {
// run msg
var evmHandlerKeys = []string{
//bam.ConsumeGas,
//bam.Recover,
//----- handler
//bam.EvmHandler,
//bam.ParseChainID,
//bam.VerifySig,
bam.Txhash,
bam.SaveTx,
bam.TransitionDb,
//bam.Bloomfilter,
//bam.EmitEvents,
//bam.HandlerDefer,
//-----
}
addInfo(trace.EvmHandlerDetail, evmHandlerKeys, record)
}
func formatRunAnteDetail(record map[string]int64) {
// ante
var anteKeys = []string{
bam.CacheTxContext,
bam.AnteChain,
bam.AnteOther,
bam.CacheStoreWrite,
}
addInfo(trace.RunAnteDetail, anteKeys, record)
}