-
Notifications
You must be signed in to change notification settings - Fork 573
/
resolver.go
235 lines (215 loc) · 5.96 KB
/
resolver.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
package symdb
import (
"context"
"runtime"
"sync"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"golang.org/x/sync/errgroup"
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/pprof"
)
// Resolver converts stack trace samples to one of the profile
// formats, such as tree or pprof.
//
// Resolver asynchronously loads symbols for each partition as
// they are added with AddSamples or Partition calls.
//
// A new Resolver must be created for each profile.
type Resolver struct {
ctx context.Context
cancel context.CancelFunc
span opentracing.Span
s SymbolsReader
g *errgroup.Group
c int
m sync.Mutex
p map[uint64]*lazyPartition
}
type ResolverOption func(*Resolver)
// WithMaxConcurrent specifies how many partitions
// can be resolved concurrently.
func WithMaxConcurrent(n int) ResolverOption {
return func(r *Resolver) {
r.c = n
}
}
type lazyPartition struct {
id uint64
reader chan PartitionReader
samples map[uint32]int64
err chan error
done chan struct{}
}
func NewResolver(ctx context.Context, s SymbolsReader) *Resolver {
r := Resolver{
s: s,
c: runtime.GOMAXPROCS(-1),
p: make(map[uint64]*lazyPartition),
}
r.span, r.ctx = opentracing.StartSpanFromContext(ctx, "NewResolver")
r.ctx, r.cancel = context.WithCancel(r.ctx)
r.g, r.ctx = errgroup.WithContext(r.ctx)
return &r
}
func (r *Resolver) Release() {
r.cancel()
// The error is already sent to the caller.
_ = r.g.Wait()
r.span.Finish()
}
// AddSamples adds a collection of stack trace samples to the resolver.
// Samples can be added to different partitions concurrently, but modification
// of the same partition is not thread-safe.
func (r *Resolver) AddSamples(partition uint64, s schemav1.Samples) {
p := r.Partition(partition)
for i, sid := range s.StacktraceIDs {
if sid > 0 {
p[sid] += int64(s.Values[i])
}
}
}
func (r *Resolver) AddSamplesWithSpanSelector(partition uint64, s schemav1.Samples, spanSelector model.SpanSelector) {
p := r.Partition(partition)
for i, sid := range s.StacktraceIDs {
if _, ok := spanSelector[s.Spans[i]]; ok {
p[sid] += int64(s.Values[i])
}
}
}
// Partition returns map of samples corresponding to the partition.
// The function initializes symbols of the partition on the first occurrence.
// The call is thread-safe, but access to the returned map is not.
func (r *Resolver) Partition(partition uint64) map[uint32]int64 {
r.m.Lock()
p, ok := r.p[partition]
if ok {
r.m.Unlock()
return p.samples
}
p = &lazyPartition{
id: partition,
samples: make(map[uint32]int64),
err: make(chan error),
done: make(chan struct{}),
reader: make(chan PartitionReader, 1),
}
r.p[partition] = p
r.m.Unlock()
r.g.Go(func() error {
return r.acquirePartition(p)
})
// r.g.Wait() is only called at Resolver.Release.
return p.samples
}
func (r *Resolver) acquirePartition(p *lazyPartition) error {
pr, err := r.s.Partition(r.ctx, p.id)
if err != nil {
r.span.LogFields(log.String("err", err.Error()))
select {
case <-r.ctx.Done():
return r.ctx.Err()
case p.err <- err:
// Signal the partition receiver
// about the failure, so it won't
// block and return early.
return err
}
}
// We've acquired the partition and must release it
// once resolution finished or canceled.
select {
case p.reader <- pr:
// We transferred ownership to the recipient,
// which is now responsible for releasing the
// partition.
<-p.done
case <-r.ctx.Done():
// We still own the partition and must release
// it on our own. It's guaranteed that p.c receiver
// has no access to the partition.
pr.Release()
return r.ctx.Err()
}
return nil
}
func (r *Resolver) Tree() (*model.Tree, error) {
span, ctx := opentracing.StartSpanFromContext(r.ctx, "Resolver.Tree")
defer span.Finish()
var lock sync.Mutex
tree := new(model.Tree)
err := r.withSymbols(ctx, func(symbols *Symbols, samples schemav1.Samples) error {
resolved, err := symbols.Tree(ctx, samples)
if err != nil {
return err
}
lock.Lock()
tree.Merge(resolved)
lock.Unlock()
return nil
})
return tree, err
}
func (r *Resolver) Pprof(maxNodes int64) (*googlev1.Profile, error) {
span, ctx := opentracing.StartSpanFromContext(r.ctx, "Resolver.Pprof")
defer span.Finish()
var lock sync.Mutex
var p pprof.ProfileMerge
err := r.withSymbols(ctx, func(symbols *Symbols, samples schemav1.Samples) error {
resolved, err := symbols.Pprof(ctx, samples, maxNodes)
if err != nil {
return err
}
lock.Lock()
defer lock.Unlock()
return p.Merge(resolved)
})
return p.Profile(), err
}
func (r *Resolver) withSymbols(ctx context.Context, fn func(*Symbols, schemav1.Samples) error) error {
g, ctx := errgroup.WithContext(ctx)
g.SetLimit(r.c)
for _, p := range r.p {
p := p
g.Go(func() error {
defer close(p.done)
select {
case err := <-p.err:
return err
case <-ctx.Done():
return ctx.Err()
case pr := <-p.reader:
defer pr.Release()
return fn(pr.Symbols(), schemav1.NewSamplesFromMap(p.samples))
}
})
}
return g.Wait()
}
type pprofBuilder interface {
StacktraceInserter
init(*Symbols, schemav1.Samples)
buildPprof() *googlev1.Profile
}
func (r *Symbols) Pprof(ctx context.Context, samples schemav1.Samples, maxNodes int64) (*googlev1.Profile, error) {
var b pprofBuilder = new(pprofProtoSymbols)
if maxNodes > 0 {
b = &pprofProtoTruncatedSymbols{maxNodes: maxNodes}
}
b.init(r, samples)
if err := r.Stacktraces.ResolveStacktraceLocations(ctx, b, samples.StacktraceIDs); err != nil {
return nil, err
}
return b.buildPprof(), nil
}
func (r *Symbols) Tree(ctx context.Context, samples schemav1.Samples) (*model.Tree, error) {
t := treeSymbolsFromPool()
defer t.reset()
t.init(r, samples)
if err := r.Stacktraces.ResolveStacktraceLocations(ctx, t, samples.StacktraceIDs); err != nil {
return nil, err
}
return t.tree, nil
}