-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug.go
163 lines (138 loc) · 4.8 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
package p2p
import (
"encoding/binary"
"encoding/hex"
"fmt"
"net/http"
"sort"
"strings"
"text/tabwriter"
)
// this file is for debugging only and not included in the factom repo
// DebugMessage is temporary
func (n *Network) DebugMessage() (string, int) {
s := n.controller.peers.Slice()
sort.Slice(s, func(i, j int) bool {
return s[i].Hash < s[j].Hash
})
buf := new(strings.Builder)
fmt.Fprintf(buf, "\nONLINE: (%d/%d/%d)\n", len(s), n.conf.TargetPeers, n.conf.MaxPeers)
tw := tabwriter.NewWriter(buf, 0, 0, 3, ' ', 0)
fmt.Fprintf(tw, "Hash\tMPS Down\tMPS Up\tBps Down\tBps up\tRatio\tDropped\tProt\tType\t\n")
fmt.Fprintf(tw, "----\t--------\t------\t--------\t------\t-----\t-------\t----\t----\t\n")
bps := func(b uint64) string {
if b > 1024 {
return fmt.Sprintf("%.2f KiB/s", float64(b)/1024)
}
return fmt.Sprintf("%d B/s", b)
}
count := len(s)
sums := make([]uint64, 6)
for _, p := range s {
m := p.GetMetrics()
t := "Outgoing"
if p.IsIncoming {
t = "Incoming"
}
sums[0] += uint64(m.MPSDown)
sums[1] += uint64(m.MPSUp)
sums[2] += uint64(m.BPSUp)
sums[3] += uint64(m.BPSUp)
sums[4] += m.Dropped
fmt.Fprintf(tw, "%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%d\t%s\t%s\t\n", p.Hash, m.MPSDown, m.MPSUp, m.BPSDown, m.BPSUp, m.SendFillRatio, m.Dropped, p.prot, t)
}
fmt.Fprintf(tw, "----\t--------\t------\t--------\t------\t-----\t-------\t----\t----\t\n")
fmt.Fprintf(tw, "Sum\t%d\t%d\t%s\t%s\t \t%d\t\t\t\n", sums[0], sums[1], bps(sums[2]), bps(sums[3]), sums[4])
tw.Flush()
fmt.Fprint(buf, "\nBANS:\n")
tw = tabwriter.NewWriter(buf, 0, 0, 3, ' ', 0)
fmt.Fprintf(tw, "Address\tExpires\t\n")
fmt.Fprintf(tw, "-------\t-------\t\n")
n.controller.banMtx.RLock()
for ip, ts := range n.controller.bans {
fmt.Fprintf(tw, "%s\t%s\t\n", ip, ts.Format("2006-01-02 15:04:05"))
}
n.controller.banMtx.RUnlock()
tw.Flush()
return buf.String(), count
}
func (n *Network) DebugHalfviz() string {
halfviz := ""
idFromHash := func(s string) int64 {
bits := strings.Split(s, " ")
if len(bits) != 2 || len(bits[1]) != 8 {
return -1
}
dec, err := hex.DecodeString(bits[1])
if err != nil {
return -2
}
return int64(binary.LittleEndian.Uint32(dec))
}
peers := n.controller.peers.Slice()
for _, p := range peers {
edge := ""
id := idFromHash(p.Hash)
if id < 0 {
return fmt.Sprintf("[halfviz] unable to get id from %s (code %d)", p.Hash, id)
}
if n.conf.NodeID < 4 || id < 4 {
min := n.conf.NodeID
if uint32(id) < min {
min = uint32(id)
}
if min != 0 {
color := []string{"red", "green", "blue"}[min-1]
edge = fmt.Sprintf(" {color:%s, weight=3}", color)
}
}
if p.IsIncoming {
halfviz += fmt.Sprintf("%s -> %s:%s%s\n", p.Endpoint, n.conf.BindIP, n.conf.ListenPort, edge)
} else {
halfviz += fmt.Sprintf("%s:%s -> %s%s\n", n.conf.BindIP, n.conf.ListenPort, p.Endpoint, edge)
}
}
return halfviz
}
// DebugServer is temporary
func DebugServer(n *Network) {
mux := http.NewServeMux()
mux.HandleFunc("/debug", func(rw http.ResponseWriter, req *http.Request) {
a, _ := n.DebugMessage()
rw.Write([]byte(a))
})
mux.HandleFunc("/stats", func(rw http.ResponseWriter, req *http.Request) {
out := ""
out += fmt.Sprintf("Channels\n")
out += fmt.Sprintf("\tToNetwork: %d / %d\n", len(n.toNetwork), cap(n.toNetwork))
out += fmt.Sprintf("\tFromNetwork: %d / %d\n", len(n.fromNetwork), cap(n.fromNetwork))
out += fmt.Sprintf("\tpeerData: %d / %d\n", len(n.controller.peerData), cap(n.controller.peerData))
out += fmt.Sprintf("\nPeers (%d)\n", n.controller.peers.Total())
slice := n.controller.peers.Slice()
sort.Slice(slice, func(i, j int) bool {
return slice[i].connected.Before(slice[j].connected)
})
for _, p := range slice {
out += fmt.Sprintf("\t%s\n", p.Endpoint)
out += fmt.Sprintf("\t\tsend: %d / %d\n", len(p.send), cap(p.send))
m := p.GetMetrics()
out += fmt.Sprintf("\t\tBytesReceived: %d\n", m.BytesReceived)
out += fmt.Sprintf("\t\tBytesSent: %d\n", m.BytesSent)
out += fmt.Sprintf("\t\tMessagesSent: %d\n", m.MessagesSent)
out += fmt.Sprintf("\t\tMessagesReceived: %d\n", m.MessagesReceived)
out += fmt.Sprintf("\t\tMomentConnected: %s\n", m.MomentConnected)
out += fmt.Sprintf("\t\tPeerQuality: %d\n", m.PeerQuality)
out += fmt.Sprintf("\t\tIncoming: %v\n", m.Incoming)
out += fmt.Sprintf("\t\tLastReceive: %s\n", m.LastReceive)
out += fmt.Sprintf("\t\tLastSend: %s\n", m.LastSend)
out += fmt.Sprintf("\t\tMPS Down: %.2f\n", m.MPSDown)
out += fmt.Sprintf("\t\tMPS Up: %.2f\n", m.MPSUp)
out += fmt.Sprintf("\t\tBPS Down: %.2f\n", m.BPSDown)
out += fmt.Sprintf("\t\tBPS Up: %.2f\n", m.BPSUp)
out += fmt.Sprintf("\t\tCapacity: %.2f\n", m.SendFillRatio)
out += fmt.Sprintf("\t\tDropped (send): %d\n", m.Dropped)
}
rw.Write([]byte(out))
})
go http.ListenAndServe("localhost:8070", mux)
}