/
topology.go
195 lines (162 loc) · 3.82 KB
/
topology.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
package upfront
import (
"fmt"
"math"
"path/filepath"
"golang.org/x/tools/go/ssa"
)
type GoTopology []*Goro
func (goros GoTopology) LogCycles() {
indexes := make(map[*Goro]int)
lenGoros := len(goros)
dist := make([][]int, lenGoros)
next := make([][]*Goro, lenGoros)
makeLabel := func(fun *ssa.Function) string {
pos := fun.Prog.Fset.Position(fun.Pos())
path := fmt.Sprintf("%s/%s", fun.Package().Pkg.Path(), filepath.Base(pos.Filename))
return path + ":" + fun.Name()
}
for i, goro := range goros {
indexes[goro] = i
}
for i := 0; i < lenGoros; i++ {
dist[i] = make([]int, lenGoros)
next[i] = make([]*Goro, lenGoros)
for j := 0; j < lenGoros; j++ {
dist[i][j] = math.MaxInt32
next[i][j] = nil
}
}
for _, goro := range goros {
hosti := indexes[goro]
for _, spawned := range goro.SpawnedGoroutines {
guesti := indexes[spawned]
dist[hosti][guesti] = 1
next[hosti][guesti] = spawned
}
}
for k := 0; k < lenGoros; k++ {
for i := 0; i < lenGoros; i++ {
for j := 0; j < lenGoros; j++ {
if dist[i][j] > (dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j]
next[i][j] = next[i][k]
}
}
}
}
foundCycle := "none"
for i, goro := range goros {
switch dist[i][i] {
case math.MaxInt32:
case 1:
if foundCycle == "none" {
foundCycle = ""
}
foundCycle += fmt.Sprintf("\n:~ 1 @@ %s", makeLabel(goro.Entry))
default:
if foundCycle == "none" {
foundCycle = ""
}
foundCycle += fmt.Sprintf("\n:~ %d @@ %s", dist[i][i], makeLabel(goro.Entry))
succ := next[i][i]
for goro != succ {
foundCycle += " -▶ " + makeLabel(succ.Entry)
succi := indexes[succ]
succ = next[succi][i]
}
}
}
fmt.Println(foundCycle)
}
// Calculate cycles in the call graph.
func (goros GoTopology) Cycles() GoCycles {
indexes := make(map[*Goro]int)
lenGoros := len(goros)
dist := make([][]int, lenGoros)
next := make([][]*Goro, lenGoros)
for i, goro := range goros {
indexes[goro] = i
}
for i := 0; i < lenGoros; i++ {
dist[i] = make([]int, lenGoros)
next[i] = make([]*Goro, lenGoros)
for j := 0; j < lenGoros; j++ {
dist[i][j] = math.MaxInt32
next[i][j] = nil
}
}
for _, goro := range goros {
hosti := indexes[goro]
for _, seq := range goro.CrossPackageCalls {
guesti := indexes[seq]
dist[hosti][guesti] = 1
next[hosti][guesti] = seq
}
for _, spawned := range goro.SpawnedGoroutines {
guesti := indexes[spawned]
dist[hosti][guesti] = 1
next[hosti][guesti] = spawned
}
}
for k := 0; k < lenGoros; k++ {
for i := 0; i < lenGoros; i++ {
for j := 0; j < lenGoros; j++ {
if dist[i][j] > (dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j]
next[i][j] = next[i][k]
}
}
}
}
cycles := map[*Goro][]*Goro{}
for i, goro := range goros {
switch dist[i][i] {
case math.MaxInt32:
default:
cycles[goro] = []*Goro{goro}
succ := next[i][i]
for goro != succ {
cycles[goro] = append(cycles[goro], succ)
succi := indexes[succ]
succ = next[succi][i]
}
}
}
return cycles
}
func (goros GoTopology) FunToGoro(f *ssa.Function) *Goro {
for _, g := range goros {
if g.Entry == f {
return g
}
}
return nil
}
func (goros GoTopology) HasCycle(g *Goro) bool {
return goros.Cycles().HasCycle(g)
}
func (goros GoTopology) FunHasCycle(f *ssa.Function) bool {
return goros.Cycles().FunHasCycle(f)
}
type GoCycles map[*Goro][]*Goro
func (goros GoCycles) FunToGoro(f *ssa.Function) *Goro {
for g := range goros {
if g.Entry == f {
return g
}
}
return nil
}
func (goro GoCycles) HasCycle(g *Goro) bool {
_, ok := goro[g]
return ok
}
func (goros GoCycles) FunHasCycle(f *ssa.Function) bool {
return goros.HasCycle(goros.FunToGoro(f))
}
type GoCollection interface {
HasCycle(*Goro) bool
FunToGoro(*ssa.Function) *Goro
FunHasCycle(*ssa.Function) bool
}