-
Notifications
You must be signed in to change notification settings - Fork 608
/
stacktrace_tree.go
311 lines (278 loc) · 6.4 KB
/
stacktrace_tree.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
package symdb
import (
"bufio"
"io"
"unsafe"
"github.com/dgryski/go-groupvarint"
"github.com/grafana/pyroscope/pkg/util/math"
)
const (
defaultStacktraceTreeSize = 0
stacktraceTreeNodeSize = int(unsafe.Sizeof(node{}))
)
type stacktraceTree struct {
nodes []node
}
type node struct {
p int32 // Parent index.
r int32 // Reference the to stack frame data.
// Auxiliary members only needed for insertion.
fc int32 // First child index.
ns int32 // Next sibling index.
}
func newStacktraceTree(size int) *stacktraceTree {
if size < 1 {
size = 1
}
t := stacktraceTree{nodes: make([]node, 1, size)}
t.nodes[0] = node{
p: sentinel,
fc: sentinel,
ns: sentinel,
}
return &t
}
const sentinel = -1
func (t *stacktraceTree) len() uint32 { return uint32(len(t.nodes)) }
func (t *stacktraceTree) insert(refs []uint64) uint32 {
var (
n = &t.nodes[0]
i = n.fc
x int32
)
for j := len(refs) - 1; j >= 0; {
r := int32(refs[j])
if i == sentinel {
ni := int32(len(t.nodes))
n.fc = ni
t.nodes = append(t.nodes, node{
r: r,
p: x,
fc: sentinel,
ns: sentinel,
})
x = ni
n = &t.nodes[ni]
} else {
x = i
n = &t.nodes[i]
}
if n.r == r {
i = n.fc
j--
continue
}
if n.ns < 0 {
n.ns = int32(len(t.nodes))
t.nodes = append(t.nodes, node{
r: r,
p: n.p,
fc: sentinel,
ns: sentinel,
})
}
i = n.ns
}
return uint32(x)
}
func (t *stacktraceTree) resolve(dst []int32, id uint32) []int32 {
dst = dst[:0]
if id >= uint32(len(t.nodes)) {
return dst
}
// Only node members are accessed, in order to avoid
// race condition with insert: r and p are written once,
// when the node is created.
for i := int32(id); i > 0; i = t.nodes[i].p {
dst = append(dst, t.nodes[i].r)
}
return dst
}
const (
maxGroupSize = 17 // 4 * uint32 + control byte
// minGroupSize = 5 // 4 * byte + control byte
)
func (t *stacktraceTree) WriteTo(dst io.Writer) (int64, error) {
e := treeEncoder{
writeSize: 4 << 10,
}
err := e.marshal(t, dst)
return e.written, err
}
type parentPointerTree struct {
nodes []pptNode
}
type pptNode struct {
p int32 // Parent index.
r int32 // Reference the to stack frame data.
}
func newParentPointerTree(size uint32) *parentPointerTree {
return &parentPointerTree{
nodes: make([]pptNode, size),
}
}
func (t *parentPointerTree) resolve(dst []int32, id uint32) []int32 {
if id >= uint32(len(t.nodes)) {
return dst
}
dst = dst[:0]
n := t.nodes[id]
for n.p >= 0 {
dst = append(dst, n.r)
n = t.nodes[n.p]
}
return dst
}
// ReadFrom decodes parent pointer tree from the reader.
// The tree must have enough nodes.
func (t *parentPointerTree) ReadFrom(r io.Reader) (int64, error) {
d := treeDecoder{
bufSize: 4 << 10,
peekSize: 4 << 10,
groupBuffer: 1 << 10,
}
err := d.unmarshal(t, r)
return d.read, err
}
type treeEncoder struct {
writeSize int
written int64
}
func (tc *treeEncoder) marshal(t *stacktraceTree, w io.Writer) (err error) {
// Writes go through a staging buffer.
// Make sure it is allocated on stack.
ws := tc.writeSize
b := make([]byte, ws)
g := make([]uint32, 4)
var n, s int
// For delta zig-zag.
var p, c node
var v int32
for i := 0; i < len(t.nodes); i += 2 {
// First node of the pair.
c = t.nodes[i]
v = c.p - p.p
g[0] = uint32((v << 1) ^ (v >> 31))
g[1] = uint32(c.r)
p = c
if sn := i + 1; sn < len(t.nodes) {
// Second node.
c = t.nodes[sn]
v = c.p - p.p
g[2] = uint32((v << 1) ^ (v >> 31))
g[3] = uint32(c.r)
p = c
} else {
// A stub node is added to complete the group.
g[2] = 0
g[3] = 0
}
groupvarint.Encode4(b[n:], g)
n += groupvarint.BytesUsed[b[n]]
if n+maxGroupSize > ws || i >= len(t.nodes)-2 {
s, err = w.Write(b[:n])
if err != nil {
return err
}
tc.written += int64(s)
n = 0
}
}
return nil
}
type treeDecoder struct {
bufSize int
peekSize int
groupBuffer int // %4 == 0
read int64
}
func (d *treeDecoder) unmarshal(t *parentPointerTree, r io.Reader) error {
buf, ok := r.(*bufio.Reader)
if !ok || buf.Size() < d.peekSize {
buf = bufio.NewReaderSize(r, d.bufSize)
}
g := make([]uint32, d.groupBuffer)
rb := make([]byte, 0, maxGroupSize)
var p, c pptNode // Previous and current nodes.
var np int
var eof bool
for !eof {
// Load the next peekSize bytes.
// Must not exceed Reader's buffer size.
b, err := buf.Peek(d.peekSize)
if err != nil {
if err != io.EOF {
return err
}
eof = true
}
if _, err = buf.Discard(len(b)); err != nil {
return err
}
d.read += int64(len(b))
// Read b into g and decode.
for read := 0; read < len(b); {
// We need to read remaining_nodes * 2 uints or the whole
// group buffer, whichever is smaller.
xn := len(t.nodes) - np // remaining nodes
// Note that g should always be a multiple of 4.
g = g[:math.Min((xn+xn%2)*2, d.groupBuffer)]
var gp int
// Check if there is a remainder. If this is the case,
// decode the group and advance gp.
if len(rb) > 0 {
// It's expected that r contains a single complete group.
m := groupvarint.BytesUsed[rb[0]] - len(rb)
if m >= (len(b) + len(rb)) {
return io.ErrUnexpectedEOF
}
rb = append(rb, b[:m]...)
i, n, rn := decodeU32Groups(g[:4], rb)
if i != 4 || n != len(rb) || rn > 0 {
return io.ErrUnexpectedEOF
}
read += m // Part is read from rb.
rb = rb[:0]
gp += 4
}
// Re-fill g.
gi, n, rn := decodeU32Groups(g[gp:], b[read:])
gp += gi
read += n + rn // Mark remainder bytes as read, we copy them.
if rn > 0 {
// If there is a remainder, it is copied and decoded on
// the next Peek. This should not be possible with eof.
rb = append(rb, b[len(b)-rn:]...)
}
// g is full, or no more data in buf.
for i := 0; i < len(g[:gp])-1; i += 2 {
if np >= len(t.nodes) {
// g may contain an empty node at the end.
return nil
}
v := int32(g[i])
c.p = (v>>1 ^ ((v << 31) >> 31)) + p.p
c.r = int32(g[i+1])
t.nodes[np] = c
np++
p = c
}
}
}
return nil
}
// decodeU32Groups decodes len(dst)/4 groups from src and
// returns: dst offset, bytes read, bytes remaining in src.
func decodeU32Groups(dst []uint32, src []byte) (i, j, rm int) {
var n int
for i < len(dst) && j < len(src) {
n = groupvarint.BytesUsed[src[j]]
if rm = len(src[j:]); rm < n {
return i, j, rm
}
groupvarint.Decode4(dst[i:], src[j:])
i += 4
j += n
}
return i, j, 0
}