-
Notifications
You must be signed in to change notification settings - Fork 573
/
rewriter.go
398 lines (347 loc) · 10.8 KB
/
rewriter.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package symdb
import (
"context"
"math"
"sort"
schemav1 "github.com/grafana/pyroscope/pkg/phlaredb/schemas/v1"
"github.com/grafana/pyroscope/pkg/slices"
)
type Rewriter struct {
symdb *SymDB
source SymbolsReader
partitions map[uint64]*partitionRewriter
}
func NewRewriter(w *SymDB, r SymbolsReader) *Rewriter {
return &Rewriter{
source: r,
symdb: w,
}
}
func (r *Rewriter) Rewrite(partition uint64, stacktraces []uint32) error {
p, err := r.init(partition)
if err != nil {
return err
}
if err = p.populateUnresolved(stacktraces); err != nil {
return err
}
if p.hasUnresolved() {
return p.appendRewrite(stacktraces)
}
return nil
}
func (r *Rewriter) init(partition uint64) (p *partitionRewriter, err error) {
if r.partitions == nil {
r.partitions = make(map[uint64]*partitionRewriter)
}
if p, err = r.getOrCreatePartition(partition); err != nil {
return nil, err
}
return p, nil
}
func (r *Rewriter) getOrCreatePartition(partition uint64) (_ *partitionRewriter, err error) {
p, ok := r.partitions[partition]
if ok {
p.reset()
return p, nil
}
n := &partitionRewriter{name: partition}
n.dst = r.symdb.PartitionWriter(partition)
// Note that the partition is not released: we want to keep
// it during the whole lifetime of the rewriter.
pr, err := r.source.Partition(context.TODO(), partition)
if err != nil {
return nil, err
}
// We clone locations, functions, and mappings,
// because these object will be modified.
n.src = cloneSymbolsPartially(pr.Symbols())
var stats PartitionStats
pr.WriteStats(&stats)
n.stacktraces = newLookupTable[[]int32](stats.MaxStacktraceID)
n.locations = newLookupTable[*schemav1.InMemoryLocation](stats.LocationsTotal)
n.mappings = newLookupTable[*schemav1.InMemoryMapping](stats.MappingsTotal)
n.functions = newLookupTable[*schemav1.InMemoryFunction](stats.FunctionsTotal)
n.strings = newLookupTable[string](stats.StringsTotal)
r.partitions[partition] = n
return n, nil
}
type partitionRewriter struct {
name uint64
src *Symbols
dst *PartitionWriter
stacktraces *lookupTable[[]int32]
locations *lookupTable[*schemav1.InMemoryLocation]
mappings *lookupTable[*schemav1.InMemoryMapping]
functions *lookupTable[*schemav1.InMemoryFunction]
strings *lookupTable[string]
// FIXME(kolesnikovae): schemav1.Stacktrace should be just a uint32 slice:
// type Stacktrace []uint32
current []*schemav1.Stacktrace
}
func (p *partitionRewriter) reset() {
p.stacktraces.reset()
p.locations.reset()
p.mappings.reset()
p.functions.reset()
p.strings.reset()
p.current = p.current[:0]
}
func (p *partitionRewriter) hasUnresolved() bool {
return len(p.stacktraces.unresolved)+
len(p.locations.unresolved)+
len(p.mappings.unresolved)+
len(p.functions.unresolved)+
len(p.strings.unresolved) > 0
}
func (p *partitionRewriter) populateUnresolved(stacktraceIDs []uint32) error {
// Filter out all stack traces that have been already
// resolved and populate locations lookup table.
if err := p.resolveStacktraces(stacktraceIDs); err != nil {
return err
}
if len(p.locations.unresolved) == 0 {
return nil
}
// Resolve functions and mappings for new locations.
unresolvedLocs := p.locations.iter()
for unresolvedLocs.Next() {
location := p.src.Locations[unresolvedLocs.At()]
location.MappingId = p.mappings.tryLookup(location.MappingId)
for j, line := range location.Line {
location.Line[j].FunctionId = p.functions.tryLookup(line.FunctionId)
}
unresolvedLocs.setValue(location)
}
// Resolve strings.
unresolvedMappings := p.mappings.iter()
for unresolvedMappings.Next() {
mapping := p.src.Mappings[unresolvedMappings.At()]
mapping.BuildId = p.strings.tryLookup(mapping.BuildId)
mapping.Filename = p.strings.tryLookup(mapping.Filename)
unresolvedMappings.setValue(mapping)
}
unresolvedFunctions := p.functions.iter()
for unresolvedFunctions.Next() {
function := p.src.Functions[unresolvedFunctions.At()]
function.Name = p.strings.tryLookup(function.Name)
function.Filename = p.strings.tryLookup(function.Filename)
function.SystemName = p.strings.tryLookup(function.SystemName)
unresolvedFunctions.setValue(function)
}
unresolvedStrings := p.strings.iter()
for unresolvedStrings.Next() {
unresolvedStrings.setValue(p.src.Strings[unresolvedStrings.At()])
}
return nil
}
func (p *partitionRewriter) appendRewrite(stacktraces []uint32) error {
p.dst.AppendStrings(p.strings.buf, p.strings.values)
p.strings.updateResolved()
for _, v := range p.functions.values {
v.Name = p.strings.lookupResolved(v.Name)
v.Filename = p.strings.lookupResolved(v.Filename)
v.SystemName = p.strings.lookupResolved(v.SystemName)
}
p.dst.AppendFunctions(p.functions.buf, p.functions.values)
p.functions.updateResolved()
for _, v := range p.mappings.values {
v.BuildId = p.strings.lookupResolved(v.BuildId)
v.Filename = p.strings.lookupResolved(v.Filename)
}
p.dst.AppendMappings(p.mappings.buf, p.mappings.values)
p.mappings.updateResolved()
for _, v := range p.locations.values {
v.MappingId = p.mappings.lookupResolved(v.MappingId)
for j, line := range v.Line {
v.Line[j].FunctionId = p.functions.lookupResolved(line.FunctionId)
}
}
p.dst.AppendLocations(p.locations.buf, p.locations.values)
p.locations.updateResolved()
for _, v := range p.stacktraces.values {
for j, location := range v {
v[j] = int32(p.locations.lookupResolved(uint32(location)))
}
}
p.dst.AppendStacktraces(p.stacktraces.buf, p.stacktracesFromResolvedValues())
p.stacktraces.updateResolved()
for i, v := range stacktraces {
stacktraces[i] = p.stacktraces.lookupResolved(v)
}
return nil
}
func (p *partitionRewriter) resolveStacktraces(stacktraceIDs []uint32) error {
for i, v := range stacktraceIDs {
stacktraceIDs[i] = p.stacktraces.tryLookup(v)
}
if len(p.stacktraces.unresolved) == 0 {
return nil
}
p.stacktraces.initSorted()
return p.src.Stacktraces.ResolveStacktraceLocations(
context.Background(), p, p.stacktraces.buf)
}
func (p *partitionRewriter) stacktracesFromResolvedValues() []*schemav1.Stacktrace {
p.current = slices.GrowLen(p.current, len(p.stacktraces.values))
for i, v := range p.stacktraces.values {
s := p.current[i]
if s == nil {
s = &schemav1.Stacktrace{LocationIDs: make([]uint64, len(v))}
p.current[i] = s
}
s.LocationIDs = slices.GrowLen(s.LocationIDs, len(v))
for j, m := range v {
s.LocationIDs[j] = uint64(m)
}
}
return p.current
}
func (p *partitionRewriter) InsertStacktrace(stacktrace uint32, locations []int32) {
// Resolve locations for new stack traces.
for j, loc := range locations {
locations[j] = int32(p.locations.tryLookup(uint32(loc)))
}
// stacktrace points to resolved which should
// be a marked pointer to unresolved value.
idx := p.stacktraces.resolved[stacktrace] & markerMask
v := &p.stacktraces.values[idx]
n := slices.GrowLen(*v, len(locations))
copy(n, locations)
// Preserve allocated capacity.
p.stacktraces.values[idx] = n
}
func cloneSymbolsPartially(x *Symbols) *Symbols {
n := Symbols{
Stacktraces: x.Stacktraces,
Locations: make([]*schemav1.InMemoryLocation, len(x.Locations)),
Mappings: make([]*schemav1.InMemoryMapping, len(x.Mappings)),
Functions: make([]*schemav1.InMemoryFunction, len(x.Functions)),
Strings: x.Strings,
}
for i, l := range x.Locations {
n.Locations[i] = l.Clone()
}
for i, m := range x.Mappings {
n.Mappings[i] = m.Clone()
}
for i, f := range x.Functions {
n.Functions[i] = f.Clone()
}
return &n
}
const (
marker = 1 << 31
markerMask = math.MaxUint32 >> 1
)
type lookupTable[T any] struct {
// Index is source ID, and the value is the destination ID.
// If destination ID is not known, the element is index to 'unresolved' (marked).
resolved []uint32
unresolved []uint32 // Points to resolved. Index matches values.
values []T // Values are populated for unresolved items.
buf []uint32 // Sorted unresolved values.
}
func newLookupTable[T any](size int) *lookupTable[T] {
var t lookupTable[T]
t.grow(size)
return &t
}
func (t *lookupTable[T]) grow(size int) {
if cap(t.resolved) < size {
t.resolved = make([]uint32, size)
return
}
t.resolved = t.resolved[:size]
for i := range t.resolved {
t.resolved[i] = 0
}
}
func (t *lookupTable[T]) reset() {
t.unresolved = t.unresolved[:0]
t.values = t.values[:0]
t.buf = t.buf[:0]
}
// tryLookup looks up the value at x in resolved.
// If x is has not been resolved yet, the x is memorized
// for future resolve, and returned values is the marked
// index to unresolved.
func (t *lookupTable[T]) tryLookup(x uint32) uint32 {
// todo(ctovena): this is a hack to make sure we don't have any out of bounds errors
// see https://github.com/grafana/pyroscope/issues/2488
if x >= uint32(len(t.resolved)) {
t.grow(int(x + 1))
}
if v := t.resolved[x]; v != 0 {
if v&marker > 0 {
return v // Already marked for resolve.
}
return v - 1 // Already resolved.
}
u := t.newUnresolved(x) | marker
t.resolved[x] = u
return u
}
func (t *lookupTable[T]) newUnresolved(rid uint32) uint32 {
t.unresolved = append(t.unresolved, rid)
x := len(t.values)
if x < cap(t.values) {
// Try to reuse previously allocated value.
t.values = t.values[:x+1]
} else {
var v T
t.values = append(t.values, v)
}
return uint32(x)
}
func (t *lookupTable[T]) storeResolved(i int, rid uint32) {
// The index is incremented to avoid 0 because it is
// used as sentinel and indicates absence (resolved is
// a sparse slice initialized with the maximal expected
// size). Correspondingly, lookupResolved should
// decrement the index on read.
t.resolved[t.unresolved[i]] = rid + 1
}
func (t *lookupTable[T]) lookupResolved(x uint32) uint32 {
if x&marker > 0 {
return t.resolved[t.unresolved[x&markerMask]] - 1
}
return x // Already resolved.
}
// updateResolved loads indices from buf to resolved.
// It is expected that the order matches values.
func (t *lookupTable[T]) updateResolved() {
for i, rid := range t.unresolved {
t.resolved[rid] = t.buf[i] + 1
}
}
func (t *lookupTable[T]) initSorted() {
// Gather and sort references to unresolved values.
t.buf = slices.GrowLen(t.buf, len(t.unresolved))
copy(t.buf, t.unresolved)
sort.Slice(t.buf, func(i, j int) bool {
return t.buf[i] < t.buf[j]
})
}
func (t *lookupTable[T]) iter() *lookupTableIterator[T] {
t.initSorted()
return &lookupTableIterator[T]{table: t}
}
type lookupTableIterator[T any] struct {
table *lookupTable[T]
cur uint32
}
func (t *lookupTableIterator[T]) Next() bool {
return t.cur < uint32(len(t.table.buf))
}
func (t *lookupTableIterator[T]) At() uint32 {
x := t.table.buf[t.cur]
t.cur++
return x
}
func (t *lookupTableIterator[T]) setValue(v T) {
u := t.table.resolved[t.table.buf[t.cur-1]]
t.table.values[u&markerMask] = v
}
func (t *lookupTableIterator[T]) Close() error { return nil }
func (t *lookupTableIterator[T]) Err() error { return nil }