-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnoinline.go
54 lines (47 loc) · 1.5 KB
/
noinline.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
package main
import (
"fmt"
"github.com/google/pprof/profile"
)
const (
grpcProcessDataFunc = "google.golang.org/grpc/internal/transport.(*loopyWriter).processData"
doNotInlinePrefix = "DO NOT INLINE: "
)
// ApplyNoInlineHack renames problematic functions in the profile to avoid bad
// inlining decisions that can have a large memory impact.
// See https://github.com/golang/go/issues/65532 for details.
//
// TODO: Delete this once it's fixed upstream.
func ApplyNoInlineHack(prof *profile.Profile) error {
if err := renameNoInlineFuncs(prof, []string{grpcProcessDataFunc}); err != nil {
return fmt.Errorf("noinline hack: %w", err)
}
return nil
}
func renameNoInlineFuncs(prof *profile.Profile, noInlineFuncs []string) error {
for _, s := range prof.Sample {
leaf, ok := leafLine(s)
if ok && lineContainsAny(leaf, noInlineFuncs) {
// There might be multiple samples that point to the same function.
// But once we rename the function for the function for the first
// time, it stops matching the noInlineFuncs list. So we don't end
// up adding the prefix multiple times.
leaf.Function.Name = doNotInlinePrefix + leaf.Function.Name
}
}
return nil
}
func leafLine(s *profile.Sample) (profile.Line, bool) {
if len(s.Location) == 0 || len(s.Location[0].Line) == 0 {
return profile.Line{}, false
}
return s.Location[0].Line[0], true
}
func lineContainsAny(leaf profile.Line, funcs []string) bool {
for _, fn := range funcs {
if leaf.Function.Name == fn {
return true
}
}
return false
}