-
Notifications
You must be signed in to change notification settings - Fork 0
/
callgraph.go
155 lines (131 loc) · 3.21 KB
/
callgraph.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 stamets
import (
"fmt"
"golang.org/x/exp/slices"
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/ssa"
)
// CallGraphMetrics encodes metrics about call graphs e.g.
// as collected by a PTA/RTA analysis.
type CallGraphMetrics struct {
BaseMetrics[*callgraph.Graph]
Functions int
// Call graph out degree metrics
OutDegreeMax int
OutDegreeP50 int
OutDegreeP90 int
OutDegreeP99 int
OutDegreeMode int
// Call graph in degree metrics
InDegreeMax int
InDegreeP50 int
InDegreeP90 int
InDegreeP99 int
InDegreeMode int
}
func (m CallGraphMetrics) String() string {
return fmt.Sprintf(`
CALL GRAPH METRICS
- Number of functions: %d
Call site out-degree metrics:
- P50: %d
- P90: %d
- P99: %d
- Max: %d
- Most common out-degree: %d
Callee in-degree metrics:
- P50: %d
- P90: %d
- P99: %d
- Max: %d
- Most common in-degree: %d
`,
m.NumberOfFunctions(),
m.OutDegreeP50,
m.OutDegreeP90,
m.OutDegreeP99,
m.OutDegreeMax,
m.OutDegreeMode,
m.InDegreeP50,
m.InDegreeP90,
m.InDegreeP99,
m.InDegreeMax,
m.InDegreeMode,
)
}
// GetCallGraphMetrics constructs metrics from a given call graph.
func GetCallGraphMetrics(cg *callgraph.Graph) CallGraphMetrics {
m := CallGraphMetrics{
BaseMetrics: BaseMetrics[*callgraph.Graph]{
Payload: cg,
},
}
m = m.CallGraphInDegreeMetrics()
m = m.CallGraphOutDegreeMetrics()
m.Functions = len(cg.Nodes)
return m
}
// CallGraphOutDegreeMetrics computes out-degree metrics on the call graph. The out degree
// is computed per-call site. Every function without outgoing calls contributes with a 0
// to the statistics.
func (m CallGraphMetrics) CallGraphOutDegreeMetrics() CallGraphMetrics {
if m.Payload == nil {
return m
}
res := m.Payload
// Out-degree mode
outDegrees := make([]int, 0, len(res.Nodes))
// Maximum out-degree
visitCallgraph(res, func(n *callgraph.Node) {
outs := make(map[ssa.CallInstruction]int)
if len(n.Out) == 0 {
outDegrees = append(outDegrees, 0)
}
for _, e := range n.Out {
outs[e.Site]++
}
for _, count := range outs {
if m.OutDegreeMax < count {
m.OutDegreeMax = count
}
outDegrees = append(outDegrees, count)
}
})
slices.Sort(outDegrees)
m.OutDegreeP50 = p50(outDegrees)
m.OutDegreeP90 = p90(outDegrees)
m.OutDegreeP99 = p99(outDegrees)
m.OutDegreeMode = mode(outDegrees)
return m
}
// CallGraphOutdegreeMetrics computes in-degree metrics on the call graph.
func (m CallGraphMetrics) CallGraphInDegreeMetrics() CallGraphMetrics {
if m.Payload == nil {
return m
}
res := m.Payload
// In-degree mode
cardinality := make(map[int]int)
inDegrees := make([]int, 0, len(m.Payload.Nodes))
visitCallgraph(res, func(n *callgraph.Node) {
cardinality[len(n.In)]++
inDegrees = append(inDegrees, len(n.In))
if m.InDegreeMax < len(n.In) {
m.InDegreeMax = len(n.In)
}
})
slices.Sort(inDegrees)
m.InDegreeP50 = p50(inDegrees)
m.InDegreeP90 = p90(inDegrees)
m.InDegreeP99 = p99(inDegrees)
m.InDegreeMode = mode(inDegrees)
return m
}
// NumberOfFunctions produces the number of functions in the call-graph produced
// by the Points-To analysis.
func (m CallGraphMetrics) NumberOfFunctions() int {
if m.Payload == nil {
return m.Functions
}
return len(m.Payload.Nodes)
}