-
Notifications
You must be signed in to change notification settings - Fork 179
/
trace.go
155 lines (124 loc) · 2.71 KB
/
trace.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
package trace
import (
"fmt"
"time"
"github.com/okex/exchain/libs/tendermint/libs/log"
)
const (
GasUsed = "GasUsed"
Produce = "Produce"
RunTx = "RunTx"
Height = "Height"
Tx = "Tx"
BlockSize = "BlockSize"
Elapsed = "Elapsed"
CommitRound = "CommitRound"
Round = "Round"
Evm = "Evm"
Iavl = "Iavl"
DeliverTxs = "DeliverTxs"
Delta = "Delta"
Abci = "abci"
InvalidTxs = "InvalidTxs"
SaveResp = "saveResp"
Persist = "persist"
SaveState = "saveState"
ApplyBlock = "ApplyBlock"
Consensus = "Consensus"
MempoolCheckTxCnt = "checkTxCnt"
MempoolTxsCnt = "mempoolTxsCnt"
Prerun = "Prerun"
)
type IElapsedTimeInfos interface {
AddInfo(key string, info string)
Dump(logger log.Logger)
SetElapsedTime(elapsedTime int64)
GetElapsedTime() int64
}
func SetInfoObject(e IElapsedTimeInfos) {
if e != nil {
elapsedInfo = e
}
}
var elapsedInfo IElapsedTimeInfos = &EmptyTimeInfo{}
func GetElapsedInfo() IElapsedTimeInfos {
return elapsedInfo
}
type Tracer struct {
name string
startTime int64
lastPin string
lastPinStartTime int64
pins []string
intervals []int64
elapsedTime int64
}
func NewTracer(name string) *Tracer {
t := &Tracer{
startTime: time.Now().UnixNano(),
name: name,
}
return t
}
func (t *Tracer) Pin(format string, args ...interface{}) {
t.pinByFormat(fmt.Sprintf(format, args...))
}
func (t *Tracer) pinByFormat(tag string) {
if len(tag) == 0 {
//panic("invalid tag")
return
}
if len(t.pins) > 100 {
// 100 pins limitation
return
}
now := time.Now().UnixNano()
if len(t.lastPin) > 0 {
t.pins = append(t.pins, t.lastPin)
t.intervals = append(t.intervals, (now-t.lastPinStartTime)/1e6)
}
t.lastPinStartTime = now
t.lastPin = tag
}
func (t *Tracer) Format() string {
if len(t.pins) == 0 {
now := time.Now().UnixNano()
t.elapsedTime = (now - t.startTime) / 1e6
return fmt.Sprintf("%s<%dms>",
t.name,
t.elapsedTime,
)
}
t.Pin("_")
now := time.Now().UnixNano()
t.elapsedTime = (now - t.startTime) / 1e6
info := fmt.Sprintf("%s<%dms>",
t.name,
t.elapsedTime,
)
for i := range t.pins {
info += fmt.Sprintf(", %s<%dms>", t.pins[i], t.intervals[i])
}
return info
}
func (t *Tracer) GetElapsedTime() int64 {
return t.elapsedTime
}
func (t *Tracer) Reset() {
t.startTime = time.Now().UnixNano()
t.lastPin = ""
t.lastPinStartTime = 0
t.pins = nil
t.intervals = nil
}
type EmptyTimeInfo struct {
}
func (e *EmptyTimeInfo) AddInfo(key string, info string) {
}
func (e *EmptyTimeInfo) Dump(logger log.Logger) {
}
func (e *EmptyTimeInfo) SetElapsedTime(elapsedTime int64) {
}
func (e *EmptyTimeInfo) GetElapsedTime() int64 {
return 0
}