-
Notifications
You must be signed in to change notification settings - Fork 14
/
debug.go
275 lines (245 loc) · 5.29 KB
/
debug.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package dpos
import (
"fmt"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/qlcchain/go-qlc/common/types"
)
type checkPointPos byte
const (
checkPointReceive checkPointPos = iota
checkPointBlockProcessStart
checkPointBlockProcessEnd
checkPointUncheckProcessStart
checkPointUncheckProcessEnd
checkPointBlockConfirmed
checkPointBlockCheck
checkPointProcessResult
checkPointEnd
checkPointSectionStart
checkPointSectionEnd
checkPointSection2Start
checkPointSection2End
)
const (
rcv = iota
bps
bpe
up1s
up1e
up2s
up2e
up3s
up3e
bcm
blBut
)
const (
bc = iota
pr
ed
ss
se
s2s
s2e
bpBut
)
type PerfType int32
const (
perfTypeClose PerfType = iota
perfTypeBlockLife
perfTypeBlockProcess
perfTypeAll
perfTypeExport
)
type perfInfo struct {
status atomic.Value
blockLife *sync.Map
blockProcess *sync.Map
}
func (pt PerfType) String() string {
switch pt {
case perfTypeClose:
return "close"
case perfTypeBlockLife:
return "blockLife"
case perfTypeBlockProcess:
return "blockProcess"
case perfTypeAll:
return "blockLife and blockProcess"
default:
return "invalid"
}
}
func (dps *DPoS) setPerf(in interface{}, out interface{}) {
op := in.(PerfType)
rsp := out.(map[string]interface{})
rsp["err"] = nil
switch op {
case perfTypeClose:
dps.pf.status.Store(op)
case perfTypeBlockLife, perfTypeBlockProcess, perfTypeAll:
dps.pf.status.Store(op)
dps.pf.blockLife = new(sync.Map)
dps.pf.blockProcess = new(sync.Map)
case perfTypeExport:
if dps.pf.status.Load() == perfTypeClose {
rsp["err"] = fmt.Errorf("performance test is closed")
} else {
err := dps.perfDataExport()
if err != nil {
rsp["err"] = fmt.Errorf("export err(%s)", err)
}
}
default:
rsp["err"] = fmt.Errorf("invalid param")
}
}
func (dps *DPoS) getPerf(in interface{}, out interface{}) {
rsp := out.(map[string]interface{})
rsp["err"] = nil
rsp["status"] = fmt.Sprintf("%s", dps.pf.status.Load())
blNum := 0
bpNum := 0
if dps.pf.blockLife != nil {
dps.pf.blockLife.Range(func(key, value interface{}) bool {
blNum++
return true
})
}
if dps.pf.blockProcess != nil {
dps.pf.blockProcess.Range(func(key, value interface{}) bool {
bpNum++
return true
})
}
rsp["blNum"] = blNum
rsp["bpNum"] = bpNum
}
func (dps *DPoS) perfBlockLifeCheckPointAdd(hash types.Hash, pos checkPointPos) {
if dps.pf.status.Load() == perfTypeClose || dps.pf.status.Load() == perfTypeBlockProcess {
return
}
var bl []time.Time
blv, ok := dps.pf.blockLife.Load(hash)
if !ok {
bl = make([]time.Time, blBut)
dps.pf.blockLife.Store(hash, bl)
} else {
bl = blv.([]time.Time)
}
now := time.Now()
switch pos {
case checkPointReceive:
if bl[rcv].IsZero() {
bl[rcv] = now
}
case checkPointBlockProcessStart:
if bl[bps].IsZero() {
bl[bps] = now
}
case checkPointBlockProcessEnd:
if bl[bpe].IsZero() {
bl[bpe] = now
}
case checkPointUncheckProcessStart:
if bl[up1s].IsZero() {
bl[up1s] = now
} else if bl[up2s].IsZero() {
bl[up2s] = now
} else if bl[up3s].IsZero() {
bl[up3s] = now
}
case checkPointUncheckProcessEnd:
if bl[up1e].IsZero() {
bl[up1e] = now
} else if bl[up2e].IsZero() {
bl[up2e] = now
} else if bl[up3e].IsZero() {
bl[up3e] = now
}
case checkPointBlockConfirmed:
if bl[bcm].IsZero() {
bl[bcm] = now
}
}
}
func (dps *DPoS) perfBlockProcessCheckPointAdd(hash types.Hash, pos checkPointPos) {
if dps.pf.status.Load() == perfTypeClose || dps.pf.status.Load() == perfTypeBlockLife {
return
}
var bp []time.Time
bpv, ok := dps.pf.blockProcess.Load(hash)
if !ok {
bp = make([]time.Time, bpBut)
dps.pf.blockProcess.Store(hash, bp)
} else {
bp = bpv.([]time.Time)
}
now := time.Now()
switch pos {
case checkPointBlockCheck:
if bp[bc].IsZero() {
bp[bc] = now
}
case checkPointProcessResult:
if bp[pr].IsZero() {
bp[pr] = now
}
case checkPointEnd:
if bp[ed].IsZero() {
bp[ed] = now
}
case checkPointSectionStart:
if bp[ss].IsZero() {
bp[ss] = now
}
case checkPointSectionEnd:
if bp[se].IsZero() {
bp[se] = now
}
case checkPointSection2Start:
if bp[s2s].IsZero() {
bp[s2s] = now
}
case checkPointSection2End:
if bp[s2e].IsZero() {
bp[s2e] = now
}
}
}
func (dps *DPoS) perfDataExport() error {
dir, err := os.Getwd()
if err != nil {
return err
}
blf, err := os.Create(filepath.Join(dir, fmt.Sprintf("%s_bl.csv", time.Now().Format("20060102T150405"))))
if err != nil {
return err
}
bpf, err := os.Create(filepath.Join(dir, fmt.Sprintf("%s_bp.csv", time.Now().Format("20060102T150405"))))
if err != nil {
return err
}
_, _ = blf.Write([]byte("hash,all,bpc,up1c,up2c,up3c\n"))
dps.pf.blockLife.Range(func(hash, val interface{}) bool {
bl := val.([]time.Time)
_, _ = blf.WriteString(fmt.Sprintf("%s,%s,%s,%s,%s,%s\n",
hash, bl[bcm].Sub(bl[rcv]), bl[bpe].Sub(bl[bps]), bl[up1e].Sub(bl[up1s]),
bl[up2e].Sub(bl[up2s]), bl[up3e].Sub(bl[up3s])))
return true
})
_, _ = bpf.Write([]byte("hash,all,bcu,pru,su,s2u\n"))
dps.pf.blockProcess.Range(func(hash, val interface{}) bool {
bp := val.([]time.Time)
_, _ = bpf.WriteString(fmt.Sprintf("%s,%s,%s,%s,%s,%s\n",
hash, bp[ed].Sub(bp[bc]), bp[pr].Sub(bp[bc]), bp[ed].Sub(bp[pr]), bp[se].Sub(bp[ss]), bp[s2e].Sub(bp[s2s])))
return true
})
_ = blf.Close()
_ = bpf.Close()
return nil
}