-
Notifications
You must be signed in to change notification settings - Fork 574
/
resolver_pprof_truncate.go
280 lines (258 loc) · 8.06 KB
/
resolver_pprof_truncate.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
package symdb
import (
"reflect"
"unsafe"
googlev1 "github.com/grafana/pyroscope/api/gen/proto/go/google/v1"
"github.com/grafana/pyroscope/pkg/model"
schemav1 "github.com/grafana/pyroscope/pkg/phlaredb/schemas/v1"
"github.com/grafana/pyroscope/pkg/slices"
)
const (
truncationMark = 1 << 30
truncatedNodeName = "other"
)
type pprofProtoTruncatedSymbols struct {
symbols *Symbols
samples *schemav1.Samples
profile googlev1.Profile
lut []uint32
cur int
maxNodes int64
truncated int
functionTree *model.StacktraceTree
stacktraces []truncatedStacktraceSample
// Two buffers are needed as we handle both function and location
// stacks simultaneously.
functionsBuf []int32
locationsBuf []uint64
// subtree denotes the lowest common ancestor of all the stack traces.
// The subtree path consists of function IDs, the root is at subtree[0].
subtree []int32
fnNames func(locations []int32) ([]int32, bool)
// After truncation many samples will have the same stack trace.
// The map is used to deduplicate them. The key is sample.LocationId
// slice turned into a string: the underlying memory must not change.
sampleMap map[string]*googlev1.Sample
// As an optimisation, we merge all the stack trace samples that were
// fully truncated to a single sample.
fullyTruncated int64
}
type truncatedStacktraceSample struct {
stacktraceID uint32
functionNodeIdx int32
value int64
}
func (r *pprofProtoTruncatedSymbols) init(symbols *Symbols, samples schemav1.Samples) {
r.symbols = symbols
r.samples = &samples
// We optimistically assume that each stacktrace has only
// 2 unique nodes. For pathological cases it may exceed 10.
r.functionTree = model.NewStacktraceTree(samples.Len() * 2)
r.stacktraces = make([]truncatedStacktraceSample, 0, samples.Len())
r.sampleMap = make(map[string]*googlev1.Sample, samples.Len())
if len(r.subtree) > 0 {
r.fnNames = r.locFunctionsFiltered
} else {
r.fnNames = r.locFunctions
}
}
func (r *pprofProtoTruncatedSymbols) InsertStacktrace(stacktraceID uint32, locations []int32) {
value := int64(r.samples.Values[r.cur])
r.cur++
functions, ok := r.fnNames(locations)
if ok {
functionNodeIdx := r.functionTree.Insert(functions, value)
r.stacktraces = append(r.stacktraces, truncatedStacktraceSample{
stacktraceID: stacktraceID,
functionNodeIdx: functionNodeIdx,
value: value,
})
}
}
func (r *pprofProtoTruncatedSymbols) locFunctions(locations []int32) ([]int32, bool) {
r.functionsBuf = r.functionsBuf[:0]
for i := 0; i < len(locations); i++ {
lines := r.symbols.Locations[locations[i]].Line
for j := 0; j < len(lines); j++ {
r.functionsBuf = append(r.functionsBuf, int32(lines[j].FunctionId))
}
}
return r.functionsBuf, true
}
func (r *pprofProtoTruncatedSymbols) locFunctionsFiltered(locations []int32) ([]int32, bool) {
r.functionsBuf = r.functionsBuf[:0]
var pos int
pathLen := len(r.subtree)
// Even if len(locations) < pathLen, we still
// need to inspect locations line by line.
for i := len(locations) - 1; i >= 0; i-- {
lines := r.symbols.Locations[locations[i]].Line
for j := len(lines) - 1; j >= 0; j-- {
f := int32(lines[j].FunctionId)
if pos < pathLen {
if r.subtree[pos] != f {
return nil, false
}
pos++
}
r.functionsBuf = append(r.functionsBuf, f)
}
}
if pos < len(r.subtree) {
return nil, false
}
slices.Reverse(r.functionsBuf)
return r.functionsBuf, true
}
func (r *pprofProtoTruncatedSymbols) buildPprof() *googlev1.Profile {
r.markNodesForTruncation()
for _, n := range r.stacktraces {
r.addSample(n)
}
r.createSamples()
createSampleTypeStub(&r.profile)
copyLocations(&r.profile, r.symbols, r.lut)
copyFunctions(&r.profile, r.symbols, r.lut)
copyMappings(&r.profile, r.symbols, r.lut)
copyStrings(&r.profile, r.symbols, r.lut)
if r.truncated > 0 || r.fullyTruncated > 0 {
createLocationStub(&r.profile)
}
return &r.profile
}
func (r *pprofProtoTruncatedSymbols) markNodesForTruncation() {
minValue := r.functionTree.MinValue(r.maxNodes)
if minValue == 0 {
return
}
for i := range r.functionTree.Nodes {
if r.functionTree.Nodes[i].Total < minValue {
r.functionTree.Nodes[i].Location |= truncationMark
r.truncated++
}
}
}
func (r *pprofProtoTruncatedSymbols) addSample(n truncatedStacktraceSample) {
// Find the original stack trace and remove truncated
// locations based on the truncated functions.
var off int
r.functionsBuf, off = r.buildFunctionsStack(r.functionsBuf, n.functionNodeIdx)
if off < 0 {
// The stack has no functions without the truncation mark.
r.fullyTruncated += n.value
return
}
r.locationsBuf = r.symbols.Stacktraces.LookupLocations(r.locationsBuf, n.stacktraceID)
if off > 0 {
// Some functions were truncated.
r.locationsBuf = truncateLocations(r.locationsBuf, r.functionsBuf, off, r.symbols)
// Otherwise, if the offset is zero, the stack can be taken as is.
}
// Truncation may result in vast duplication of stack traces.
// Even if a particular stack trace is not truncated, we still
// remember it, as there might be another truncated stack trace
// that fully matches it.
// Note that this is safe to take locationsBuf memory for the
// map key lookup as it is not retained.
if s, dup := r.sampleMap[uint64sliceString(r.locationsBuf)]; dup {
s.Value[0] += n.value
return
}
// If this is a new stack trace, copy locations, create
// the sample, and add the stack trace to the map.
locationsCopy := make([]uint64, len(r.locationsBuf))
copy(locationsCopy, r.locationsBuf)
s := &googlev1.Sample{LocationId: locationsCopy, Value: []int64{n.value}}
r.profile.Sample = append(r.profile.Sample, s)
r.sampleMap[uint64sliceString(locationsCopy)] = s
}
func (r *pprofProtoTruncatedSymbols) buildFunctionsStack(funcs []int32, idx int32) ([]int32, int) {
offset := -1
funcs = funcs[:0]
for i := idx; i > 0; i = r.functionTree.Nodes[i].Parent {
n := r.functionTree.Nodes[i]
if offset < 0 && n.Location&truncationMark == 0 {
// Remember the first node to keep.
offset = len(funcs)
}
funcs = append(funcs, n.Location&^truncationMark)
}
return funcs, offset
}
func (r *pprofProtoTruncatedSymbols) createSamples() {
samples := len(r.sampleMap)
r.profile.Sample = make([]*googlev1.Sample, samples, samples+1)
var i int
for _, s := range r.sampleMap {
r.profile.Sample[i] = s
i++
}
if r.fullyTruncated > 0 {
r.createStubSample()
}
}
func truncateLocations(locations []uint64, functions []int32, offset int, symbols *Symbols) []uint64 {
if offset < 1 {
return locations
}
f := len(functions)
l := len(locations)
for ; l > 0 && f >= offset; l-- {
location := symbols.Locations[locations[l-1]]
for j := len(location.Line) - 1; j >= 0; j-- {
f--
}
}
if l > 0 {
locations[0] = truncationMark
return append(locations[:1], locations[l:]...)
}
return locations[l:]
}
func uint64sliceString(u []uint64) string {
var s string
if len(u) != 0 {
hdr := (*reflect.SliceHeader)(unsafe.Pointer(&s))
hdr.Data = uintptr(unsafe.Pointer(&u[0]))
hdr.Len = len(u) * 8
}
return s
}
func (r *pprofProtoTruncatedSymbols) createStubSample() {
r.profile.Sample = append(r.profile.Sample, &googlev1.Sample{
LocationId: []uint64{truncationMark},
Value: []int64{r.fullyTruncated},
})
}
func createLocationStub(profile *googlev1.Profile) {
var stubNodeNameIdx int64
for i, s := range profile.StringTable {
if s == truncatedNodeName {
stubNodeNameIdx = int64(i)
break
}
}
if stubNodeNameIdx == 0 {
stubNodeNameIdx = int64(len(profile.StringTable))
profile.StringTable = append(profile.StringTable, truncatedNodeName)
}
stubFn := &googlev1.Function{
Id: uint64(len(profile.Function) + 1),
Name: stubNodeNameIdx,
SystemName: stubNodeNameIdx,
}
profile.Function = append(profile.Function, stubFn)
stubLoc := &googlev1.Location{
Id: uint64(len(profile.Location) + 1),
Line: []*googlev1.Line{{FunctionId: stubFn.Id}},
MappingId: 1,
}
profile.Location = append(profile.Location, stubLoc)
for _, s := range profile.Sample {
for i, loc := range s.LocationId {
if loc == truncationMark {
s.LocationId[i] = stubLoc.Id
}
}
}
}