1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package profile
18
19 import (
20 "fmt"
21 "regexp"
22 "slices"
23 "strings"
24 )
25
26 var (
27 reservedNames = []string{"(anonymous namespace)", "operator()"}
28 bracketRx = func() *regexp.Regexp {
29 var quotedNames []string
30 for _, name := range append(reservedNames, "(") {
31 quotedNames = append(quotedNames, regexp.QuoteMeta(name))
32 }
33 return regexp.MustCompile(strings.Join(quotedNames, "|"))
34 }()
35 )
36
37
38 func simplifyFunc(f string) string {
39
40 funcName := strings.TrimPrefix(f, ".")
41
42
43 for _, ind := range bracketRx.FindAllStringSubmatchIndex(funcName, -1) {
44 foundReserved := slices.Contains(reservedNames, funcName[ind[0]:ind[1]])
45 if !foundReserved {
46 funcName = funcName[:ind[0]]
47 break
48 }
49 }
50 return funcName
51 }
52
53
54
55
56 func (p *Profile) Prune(dropRx, keepRx *regexp.Regexp) {
57 prune := make(map[uint64]bool)
58 pruneBeneath := make(map[uint64]bool)
59
60
61
62
63 pruneCache := map[string]bool{}
64 pruneFromHere := func(s string) bool {
65 if r, ok := pruneCache[s]; ok {
66 return r
67 }
68 funcName := simplifyFunc(s)
69 if dropRx.MatchString(funcName) {
70 if keepRx == nil || !keepRx.MatchString(funcName) {
71 pruneCache[s] = true
72 return true
73 }
74 }
75 pruneCache[s] = false
76 return false
77 }
78
79 for _, loc := range p.Location {
80 var i int
81 for i = len(loc.Line) - 1; i >= 0; i-- {
82 if fn := loc.Line[i].Function; fn != nil && fn.Name != "" {
83 if pruneFromHere(fn.Name) {
84 break
85 }
86 }
87 }
88
89 if i >= 0 {
90
91 pruneBeneath[loc.ID] = true
92
93
94 if i == len(loc.Line)-1 {
95
96 prune[loc.ID] = true
97 } else {
98 loc.Line = loc.Line[i+1:]
99 }
100 }
101 }
102
103
104 for _, sample := range p.Sample {
105
106
107
108 foundUser := false
109 for i := len(sample.Location) - 1; i >= 0; i-- {
110 id := sample.Location[i].ID
111 if !prune[id] && !pruneBeneath[id] {
112 foundUser = true
113 continue
114 }
115 if !foundUser {
116 continue
117 }
118 if prune[id] {
119 sample.Location = sample.Location[i+1:]
120 break
121 }
122 if pruneBeneath[id] {
123 sample.Location = sample.Location[i:]
124 break
125 }
126 }
127 }
128 }
129
130
131
132 func (p *Profile) RemoveUninteresting() error {
133 var keep, drop *regexp.Regexp
134 var err error
135
136 if p.DropFrames != "" {
137 if drop, err = regexp.Compile("^(" + p.DropFrames + ")$"); err != nil {
138 return fmt.Errorf("failed to compile regexp %s: %v", p.DropFrames, err)
139 }
140 if p.KeepFrames != "" {
141 if keep, err = regexp.Compile("^(" + p.KeepFrames + ")$"); err != nil {
142 return fmt.Errorf("failed to compile regexp %s: %v", p.KeepFrames, err)
143 }
144 }
145 p.Prune(drop, keep)
146 }
147 return nil
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162 func (p *Profile) PruneFrom(dropRx *regexp.Regexp) {
163 pruneBeneath := make(map[uint64]bool)
164
165 for _, loc := range p.Location {
166 for i := 0; i < len(loc.Line); i++ {
167 if fn := loc.Line[i].Function; fn != nil && fn.Name != "" {
168 funcName := simplifyFunc(fn.Name)
169 if dropRx.MatchString(funcName) {
170
171 pruneBeneath[loc.ID] = true
172 loc.Line = loc.Line[i:]
173 break
174 }
175 }
176 }
177 }
178
179
180 for _, sample := range p.Sample {
181
182 for i, loc := range sample.Location {
183 if pruneBeneath[loc.ID] {
184 sample.Location = sample.Location[i:]
185 break
186 }
187 }
188 }
189 }
190
View as plain text