-
Notifications
You must be signed in to change notification settings - Fork 14
/
profile.go
208 lines (164 loc) · 4.74 KB
/
profile.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
package main
import (
"fmt"
"os"
"time"
"github.com/remeh/diago/pprof"
)
type Profile struct {
Samples
TotalSampling uint64
CaptureDuration time.Duration
// "cpu" or "heap"
Type string
functionsMapByLocation ManyFunctionsMap
locationsMap LocationsMap
stringsMap StringsMap
}
func NewProfile(p *pprof.Profile, mode sampleMode) (*Profile, error) {
// start by building some maps because everything
// is indexed in various maps.
// ----------------------
// strings map
stringsMap := buildStringsTable(p)
// functions map
functionsMap := buildFunctionsMap(p, stringsMap)
// locations map
locationsMap, functionsMapByLocation := buildLocationsMap(p, stringsMap, functionsMap)
// let's now build the profile
// ----------------------
typ := ReadProfileType(p)
if typ != "cpu" && typ != "space" {
return nil, fmt.Errorf("unsupported type: %s", typ)
}
profile := readProfile(p, stringsMap, functionsMapByLocation, locationsMap, mode)
switch typ {
case "cpu":
profile.Type = "cpu"
case "space":
profile.Type = "heap"
}
return profile, nil
}
func ReadProfileType(p *pprof.Profile) string {
return p.StringTable[uint64(p.GetPeriodType().Type)]
}
func readProfile(p *pprof.Profile, stringsMap StringsMap, functionsMapByLocation ManyFunctionsMap,
locationsMap LocationsMap, mode sampleMode) *Profile {
var samples Samples
var idx int
switch {
case mode == ModeDefault:
fallthrough
case ReadProfileType(p) == "cpu" && mode == ModeCpu:
idx = 1
case ReadProfileType(p) == "space" && mode == ModeHeapAlloc:
idx = 1
case ReadProfileType(p) == "space" && mode == ModeHeapInuse:
idx = 3
default:
fmt.Printf("err: incompatible mode and profile type. %s & %s\n", ReadProfileType(p), mode)
os.Exit(-1)
}
for _, pprofSample := range p.Sample {
var sample Sample
// cpu [1] cpu usage
// space [1] heap allocated
// space [3] heap in use
value := pprofSample.GetValue()[idx]
for i := len(pprofSample.LocationId) - 1; i >= 0; i-- {
l := pprofSample.LocationId[i]
sample.Functions = append(sample.Functions, functionsMapByLocation[l]...)
sample.Value = value
}
// compute the self time for the leaf
leaf := sample.Functions[len(sample.Functions)-1]
leaf.Self += value
sample.Functions[len(sample.Functions)-1] = leaf
samples = append(samples, sample)
}
// compute the total sampling time
var totalSum uint64
for _, s := range samples {
totalSum += uint64(s.Value)
}
// compute the percentage for every sample
for i, s := range samples {
s.PercentTotal = float64(s.Value) / (float64(totalSum)) * 100.0
samples[i] = s
}
return &Profile{
Samples: samples,
TotalSampling: totalSum,
CaptureDuration: time.Duration(p.GetDurationNanos()),
functionsMapByLocation: functionsMapByLocation,
locationsMap: locationsMap,
stringsMap: stringsMap,
}
}
func (p *Profile) BuildTree(treeName string, aggregateByFunction bool, searchField string) *FunctionsTree {
// prepare the tree
tree := NewFunctionsTree(treeName)
// fill the tree
for _, s := range p.Samples {
node := tree.root
for _, f := range s.Functions {
if s.Value == 0 {
continue
}
node = node.AddFunction(f, s.Value, f.Self, s.PercentTotal, aggregateByFunction)
}
}
if tree.root != nil {
tree.root.filter(searchField)
}
tree.sort()
return tree
}
func buildLocationsMap(profile *pprof.Profile, stringsMap StringsMap, functionsMap FunctionsMap) (LocationsMap, ManyFunctionsMap) {
rv := make(LocationsMap)
lrv := make(ManyFunctionsMap)
for _, location := range profile.Location {
if location.Line[0] == nil {
continue
}
loc := Location{}
for idx := len(location.Line) - 1; idx >= 0; idx-- {
line := location.Line[idx]
inlined := idx != len(location.Line)-1
f := functionsMap[line.GetFunctionId()]
f.LineNumber = uint64(line.GetLine())
if inlined {
f.Name = fmt.Sprintf("(inlined) %s", f.Name)
}
loc.Functions = append(loc.Functions, f)
// set the line number in functions map if not inlined
if !inlined {
f := functionsMap[line.GetFunctionId()]
f.LineNumber = uint64(line.GetLine())
functionsMap[line.GetFunctionId()] = f
}
fs := lrv[uint64(location.GetId())]
lrv[uint64(location.GetId())] = append(fs, f)
rv[uint64(location.GetId())] = loc
}
}
return rv, lrv
}
func buildFunctionsMap(profile *pprof.Profile, stringsMap StringsMap) FunctionsMap {
rv := make(FunctionsMap)
for _, f := range profile.Function {
rv[f.GetId()] = Function{
Name: stringsMap[uint64(f.GetName())],
File: stringsMap[uint64(f.GetFilename())],
}
}
return rv
}
func buildStringsTable(profile *pprof.Profile) StringsMap {
rv := make(StringsMap)
for i, v := range profile.GetStringTable() {
rv[uint64(i)] = v
}
return rv
}