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