-
Notifications
You must be signed in to change notification settings - Fork 0
/
rprof.go
351 lines (293 loc) · 8.52 KB
/
rprof.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
package rprof
import (
"errors"
"io"
"runtime"
"sync"
"time"
proto "go.opentelemetry.io/proto/otlp/profiles/v1experimental"
)
var (
// profiler is the default profiler used by the package-level functions.
profiler = NewProfiler()
)
// Start starts the default profiler.
func Start() error {
return profiler.Start()
}
// Stop stops the default profiler and returns the profile.
func Stop() (*proto.Profile, error) {
return profiler.Stop()
}
// Reader returns a new io.Reader that will be profiled if the profiler is on.
func Reader(r io.Reader) io.Reader {
return profiler.Reader(r)
}
// ReadCloser returns a new io.ReadCloser that will be profiled if the profiler is on.
func ReadCloser(r io.ReadCloser) io.ReadCloser {
return profiler.ReadCloser(r)
}
// ReaderAt returns a new io.ReaderAt that will be profiled if the profiler is on.
func ReaderAt(r io.ReaderAt) io.ReaderAt {
return profiler.ReaderAt(r)
}
// sampleKey is the key used to group a unique sample. If the same stack and
// size bucket are seen multiple times then the values are aggregated.
type sampleKey struct {
locations [128]uintptr
sizeBucketPower uint8
numLocations uint8
}
// Rprof is a profiler that records the number of reads and the number of bytes
// read since the last call to Start.
type Rprof struct {
mu sync.Mutex
samples map[sampleKey][2]int64
startTime int64
}
// Start starts the profiler. If the profiler is already started then it returns an error.
func (p *Rprof) Start() error {
p.mu.Lock()
defer p.mu.Unlock()
if p.startTime != 0 {
return errors.New("profiler already started")
}
p.startTime = time.Now().UnixNano()
p.samples = map[sampleKey][2]int64{}
return nil
}
// profileBuilder is a helper to build a profile.
type profileBuilder struct {
p *proto.Profile
}
// newProfileBuilder returns a new profileBuilder with the given timestamp and duration.
func newProfileBuilder(timestampNanos, durationNanos int64) *profileBuilder {
b := &profileBuilder{
p: &proto.Profile{
// StringTable is initialized with values we know are going to be there.
StringTable: []string{
"",
"reads",
"count",
"read",
"bytes",
},
DurationNanos: durationNanos,
TimeNanos: timestampNanos,
Period: 1,
PeriodType: &proto.ValueType{
Type: 1, // "reads" in the string table
Unit: 2, // "count" in the string table
},
SampleType: []*proto.ValueType{{
Type: 1, // "reads" in the string table
Unit: 2, // "count" in the string table
}, {
Type: 3, // "read" in the string table
Unit: 4, // "bytes" in the string table
}},
},
}
// populate the mappings right away
b.readMapping()
return b
}
// addString adds a string to the string table and returns the index.
func (b *profileBuilder) addString(s string) int64 {
b.p.StringTable = append(b.p.StringTable, s)
return int64(len(b.p.StringTable)) - 1
}
// addMapping is called from the respective platform-specific implementations
// to add a mapping to the profile.
func (b *profileBuilder) addMapping(lo, hi, offset uint64, file, buildID string) {
b.addMappingEntry(lo, hi, offset, file, buildID, false)
}
// addMappingEntry adds a mapping to the profile. If it's a fake mapping then a
// mapping that covers the entire address space is added.
func (b *profileBuilder) addMappingEntry(lo, hi, offset uint64, file, buildID string, fake bool) {
nextIdx := uint64(len(b.p.Mapping)) + 1
if fake {
b.p.Mapping = append(b.p.Mapping, &proto.Mapping{
Id: nextIdx,
MemoryStart: 0,
MemoryLimit: 1 << 63,
FileOffset: 0,
Filename: 0,
BuildId: 0,
})
return
}
b.p.Mapping = append(b.p.Mapping, &proto.Mapping{
Id: nextIdx,
MemoryStart: lo,
MemoryLimit: hi,
FileOffset: offset,
Filename: b.addString(file),
BuildId: b.addString(buildID),
})
}
// build populates the samples and locations in the profile.
func (b *profileBuilder) build(samples map[sampleKey][2]int64) *proto.Profile {
b.p.Sample = make([]*proto.Sample, 0, len(samples))
locIdx := map[uintptr]uint64{}
locs := make([]uint64, 0, 128)
for sampleKey, sampleValue := range samples {
locs = locs[:0]
for _, loc := range sampleKey.locations[:sampleKey.numLocations] {
idx, ok := locIdx[loc]
if !ok {
idx = uint64(len(locIdx)) + 1
locIdx[loc] = idx
var mappingId uint64
addr := uint64(loc)
for i := uint64(0); i < uint64(len(b.p.Mapping)); i++ {
if b.p.Mapping[i].MemoryStart <= addr && addr < b.p.Mapping[i].MemoryLimit {
mappingId = i + 1 // IDs are 1-indexed
break
}
}
b.p.Location = append(b.p.Location, &proto.Location{
Id: idx,
MappingIndex: mappingId,
Address: addr,
})
}
locs = append(locs, idx)
}
b.p.Sample = append(b.p.Sample, &proto.Sample{
// Copy the locations since we're reusing the slice.
LocationIndex: copyLocs(locs),
Value: sampleValue[:],
Label: []*proto.Label{{
Key: 4, // "bytes"
Num: 1 << sampleKey.sizeBucketPower,
}},
})
}
// We do this to signify to the consumer that addresses no longer need to be adjusted.
// https://github.com/google/pprof/blob/813a5fbdbec8a66f7a5aedb876e1b2c3ee0f99ac/internal/elfexec/elfexec.go#L218-L223
for _, m := range b.p.Mapping {
m.MemoryStart = 0
m.MemoryLimit = 1 << 63
m.FileOffset = 0
}
return b.p
}
// copyLocs copies the locations to a new slice.
func copyLocs(locs []uint64) []uint64 {
res := make([]uint64, len(locs))
copy(res, locs)
return res
}
// Stop stops the profiler and returns the profile. If the profiler is not
// started then it returns an error.
func (p *Rprof) Stop() (*proto.Profile, error) {
p.mu.Lock()
if p.startTime == 0 {
p.mu.Unlock()
return nil, errors.New("profiler not started")
}
ts := p.startTime
samples := p.samples
p.startTime = 0
p.mu.Unlock()
duration := time.Now().UnixNano() - ts
b := newProfileBuilder(ts, duration)
return b.build(samples), nil
}
func (p *Rprof) recordSample(size int) {
sizeBucketPower := nextPowerOfTwo(size)
p.mu.Lock()
defer p.mu.Unlock()
if p.startTime == 0 {
// profiler not started
return
}
locations := [128]uintptr{}
numRead := runtime.Callers(3, locations[:])
k := sampleKey{
locations: locations,
numLocations: uint8(numRead),
sizeBucketPower: sizeBucketPower,
}
sample := p.samples[k]
// first sample is the number of reads
sample[0]++
// second sample is the number of bytes read
sample[1] += int64(size)
p.samples[k] = sample
}
// nextPowerOfTwo returns the next power of two that is greater or equal to the input. It returns the power, not the value to be able to return a uint8.
func nextPowerOfTwo(input int) uint8 {
for i := 0; i < 63; i++ {
if 1<<i >= input {
return uint8(i)
}
}
return 63
}
// NewProfiler returns a new profiler.
func NewProfiler() *Rprof {
return &Rprof{}
}
// RprofReader is an io.Reader that will profile the reads if the profiler is on.
type RprofReader struct {
p *Rprof
r io.Reader
}
// Reader returns a new io.Reader that will be profiled if the profiler is on.
func (p *Rprof) Reader(r io.Reader) io.Reader {
return &RprofReader{
p: p,
r: r,
}
}
// Read reads from the underlying reader and records the sample in the profiler.
// Implements io.Reader.
func (r *RprofReader) Read(buf []byte) (int, error) {
n, err := r.r.Read(buf)
r.p.recordSample(n)
return n, err
}
// RprofReadCloser is an io.ReadCloser that will profile the reads if the profiler is on.
type RprofReadCloser struct {
p *Rprof
r io.ReadCloser
}
// ReadCloser returns a new io.ReadCloser that will be profiled if the profiler is on.
func (p *Rprof) ReadCloser(r io.ReadCloser) io.ReadCloser {
return &RprofReadCloser{
p: p,
r: r,
}
}
// Read reads from the underlying reader and records the sample in the profiler.
// Implements io.Reader.
func (r *RprofReadCloser) Read(buf []byte) (int, error) {
n, err := r.r.Read(buf)
r.p.recordSample(n)
return n, err
}
// Close closes the underlying reader.
// Implements io.Closer.
func (r *RprofReadCloser) Close() error {
return r.r.Close()
}
// RprofReaderAt is an io.ReaderAt that will profile the reads if the profiler is on.
type RprofReaderAt struct {
p *Rprof
r io.ReaderAt
}
// ReaderAt returns a new io.ReaderAt that will be profiled if the profiler is on.
func (p *Rprof) ReaderAt(r io.ReaderAt) io.ReaderAt {
return &RprofReaderAt{
p: p,
r: r,
}
}
// ReadAt reads from the underlying reader and records the sample in the profiler.
func (r *RprofReaderAt) ReadAt(buf []byte, off int64) (int, error) {
n, err := r.r.ReadAt(buf, off)
r.p.recordSample(n)
return n, err
}