-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtraceinstrument.go
292 lines (249 loc) · 6.68 KB
/
traceinstrument.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package dilithium
import (
"fmt"
"github.com/openziti-incubator/cf"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"net"
"strings"
"sync"
)
type traceInstrument struct {
config *traceInstrumentConfig
}
type traceInstrumentConfig struct {
Wire bool `cf:"wire"`
Control bool `cf:"control"`
TxPortal bool `cf:"tx_portal"`
RxPortal bool `cf:"rx_portal"`
Error bool `cf:"error"`
}
type traceInstrumentInstance struct {
id string
lock sync.Mutex
i *traceInstrument
}
func NewTraceInstrument(config map[string]interface{}) (Instrument, error) {
i := &traceInstrument{
config: new(traceInstrumentConfig),
}
if err := cf.Bind(i.config, config, cf.DefaultOptions()); err != nil {
return nil, errors.Wrap(err, "unable to load config")
}
logrus.Infof(cf.Dump(i.config, cf.DefaultOptions()))
return i, nil
}
func (self *traceInstrument) NewInstance(id string) InstrumentInstance {
return &traceInstrumentInstance{
id: id,
i: self,
}
}
/*
* connection
*/
func (self *traceInstrumentInstance) Listener(addr *net.UDPAddr) {}
func (self *traceInstrumentInstance) Hello(peer *net.UDPAddr) {
}
func (self *traceInstrumentInstance) Connected(peer *net.UDPAddr) {
}
func (self *traceInstrumentInstance) ConnectionError(peer *net.UDPAddr, err error) {
}
func (self *traceInstrumentInstance) Closed(peer Adapter) {}
/*
* wire
*/
func (self *traceInstrumentInstance) WireMessageTx(wm *WireMessage) {
if self.i.config.Wire {
decode, _ := self.decode(wm)
self.lock.Lock()
fmt.Println(fmt.Sprintf("&& %-24s %-8s #%-8d %s {%s} -> %s", self.id, "TX", wm.Seq, wm.messageType(), wm.Mt.FlagsString(), decode))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) WireMessageRetx(wm *WireMessage) {
if self.i.config.Wire {
decode, _ := self.decode(wm)
self.lock.Lock()
fmt.Println(fmt.Sprintf("&& %-24s %-8s #%-8d %s {%s} -> %s", self.id, "RETX", wm.Seq, wm.messageType(), wm.Mt.FlagsString(), decode))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) WireMessageRx(wm *WireMessage) {
if self.i.config.Wire {
decode, _ := self.decode(wm)
self.lock.Lock()
fmt.Println(fmt.Sprintf("&& %-24s %-8s #%-8d %s {%s} -> %s", self.id, "RX", wm.Seq, wm.messageType(), wm.Mt.FlagsString(), decode))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) UnknownPeer(peer *net.UDPAddr) {
if self.i.config.Error {
self.lock.Lock()
fmt.Println(fmt.Sprintf("&& %-24s UNKNOWN PEER: %s", self.id, peer))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) ReadError(err error) {
if self.i.config.Error {
self.lock.Lock()
fmt.Println(fmt.Sprintf("&& %-24s READ ERROR: %v", self.id, err))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) WriteError(err error) {
if self.i.config.Error {
self.lock.Lock()
fmt.Println(fmt.Sprintf("&& %-24s WRITE ERROR: %v", self.id, err))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) UnexpectedMessageType(mt messageType) {
if self.i.config.Error {
self.lock.Lock()
fmt.Println(fmt.Sprintf("&& %-24s UNEXPECTED MESSAGE TYPE: %s", self.id, mt.String()))
self.lock.Unlock()
}
}
/*
* control
*/
func (self *traceInstrumentInstance) TxAck(*WireMessage) {
if self.i.config.Control {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s TX ACK", self.id))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) RxAck(*WireMessage) {
if self.i.config.Control {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s RX ACK", self.id))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) TxKeepalive(*WireMessage) {
if self.i.config.Control {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s TX KEEPALIVE", self.id))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) RxKeepalive(*WireMessage) {
if self.i.config.Control {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s RX KEEPALIVE", self.id))
self.lock.Unlock()
}
}
/*
* txPortal
*/
func (self *traceInstrumentInstance) TxPortalCapacityChanged(capacity int) {
if self.i.config.TxPortal {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s TX PORTAL CAPACITY: %d", self.id, capacity))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) TxPortalSzChanged(sz int) {
if self.i.config.TxPortal {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s TX PORTAL SZ: %d", self.id, sz))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) TxPortalRxSzChanged(sz int) {
if self.i.config.TxPortal {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s TX PORTAL RX SZ: %d", self.id, sz))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) NewRetxMs(retxMs int) {
if self.i.config.TxPortal {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s RETX MS: %d", self.id, retxMs))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) NewRetxScale(retxScale float64) {
if self.i.config.TxPortal {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s RETX SCALE: %0.2f", self.id, retxScale))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) DuplicateAck(seq int32) {
if self.i.config.TxPortal {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s DUPLICATE ACK: #%d", self.id, seq))
self.lock.Unlock()
}
}
/*
* rxPortal
*/
func (self *traceInstrumentInstance) RxPortalSzChanged(sz int) {
if self.i.config.RxPortal {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s RX PORTAL SZ: %d", self.id, sz))
self.lock.Unlock()
}
}
func (self *traceInstrumentInstance) DuplicateRx(wm *WireMessage) {
if self.i.config.RxPortal {
self.lock.Lock()
fmt.Println(fmt.Sprintf("!! %-24s DUPLICATE RX: #%d", self.id, wm.Seq))
self.lock.Unlock()
}
}
/*
* allocation
*/
func (self *traceInstrumentInstance) Allocate(string) {
}
/*
* instrument lifecycle
*/
func (self *traceInstrumentInstance) Shutdown() {
self.lock.Lock()
fmt.Println(fmt.Sprintf("@@ %-24s SHUTDOWN", self.id))
self.lock.Unlock()
}
func (self *traceInstrumentInstance) decode(wm *WireMessage) (string, error) {
out := ""
switch wm.messageType() {
case HELLO:
h, acks, err := wm.asHello()
if err != nil {
return "", err
}
return fmt.Sprintf("{v:%d} |%s|", h.version, self.decodeAcks(acks)), nil
case ACK:
a, rxPortalSz, _, err := wm.asAck()
if err != nil {
return "", err
}
return fmt.Sprintf("|%s| %%%d", self.decodeAcks(a), rxPortalSz), nil
case DATA:
sz, err := wm.asDataSize()
if err != nil {
return "", err
}
return fmt.Sprintf(":%d", sz), nil
default:
return out, nil
}
}
func (self *traceInstrumentInstance) decodeAcks(acks []Ack) string {
out := ""
for _, ack := range acks {
if ack.Start == ack.End {
out += fmt.Sprintf(" @%d", ack.Start)
} else {
out += fmt.Sprintf(" @%d:%d", ack.Start, ack.End)
}
}
return strings.TrimSpace(out)
}