Debian Codegraph @

profile/filter.go

raw symbols SCIP
1 // Copyright 2014 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/
16
17 // Implements methods to filter samples from profiles.
18
19 import "regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/"
20
21 // FilterSamplesByName filters the samples in a profile and only keeps
22 // samples where at least one frame matches focus but none match ignore.
23 // Returns true is the corresponding regexp matched at least one sample.
24 func (p *Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#) FilterSamplesByNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#FilterSamplesByName().(focus, ignore, hide, show *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#) (fm, im, hm, hnm bool) {
25 if focus == nil && ignore == nil && hide == nil && show == nil {
26 fm = true // Missing focus implies a match
27 return
28 }
29 focusOrIgnore := make(map[uint64]bool)
30 hidden := make(map[uint64]bool)
31 for _, l := range p.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Location. {
32 if ignore != nil && l.matchesNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#matchesName().(ignore) {
33 im = true
34 focusOrIgnore[l.IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.] = false
35 } else if focus == nil || l.matchesNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#matchesName().(focus) {
36 fm = true
37 focusOrIgnore[l.IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.] = true
38 }
39
40 if hide != nil && l.matchesNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#matchesName().(hide) {
41 hm = true
42 l.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line. = l.unmatchedLinesscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#unmatchedLines().(hide)
43 if len(l.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.) == 0 {
44 hidden[l.IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.] = true
45 }
46 }
47 if show != nil {
48 l.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line. = l.matchedLinesscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#matchedLines().(show)
49 if len(l.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.) == 0 {
50 hidden[l.IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.] = true
51 } else {
52 hnm = true
53 }
54 }
55 }
56
57 s := make([]*Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#, 0, len(p.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Sample.))
58 for _, sample := range p.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Sample. {
59 if focusedAndNotIgnoredscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/focusedAndNotIgnored().(sample.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Location., focusOrIgnore) {
60 if len(hidden) > 0 {
61 var locs []*Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#
62 for _, loc := range sample.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Location. {
63 if !hidden[loc.IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.] {
64 locs = append(locs, loc)
65 }
66 }
67 if len(locs) == 0 {
68 // Remove sample with no locations (by not adding it to s).
69 continue
70 }
71 sample.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Location. = locs
72 }
73 s = append(s, sample)
74 }
75 }
76 p.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Sample. = s
77
78 return
79 }
80
81 // ShowFrom drops all stack frames above the highest matching frame and returns
82 // whether a match was found. If showFrom is nil it returns false and does not
83 // modify the profile.
84 //
85 // Example: consider a sample with frames [A, B, C, B], where A is the root.
86 // ShowFrom(nil) returns false and has frames [A, B, C, B].
87 // ShowFrom(A) returns true and has frames [A, B, C, B].
88 // ShowFrom(B) returns true and has frames [B, C, B].
89 // ShowFrom(C) returns true and has frames [C, B].
90 // ShowFrom(D) returns false and drops the sample because no frames remain.
91 func (p *Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#) ShowFromscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#ShowFrom().(showFrom *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#) (matched bool) {
92 if showFrom == nil {
93 return false
94 }
95 // showFromLocs stores location IDs that matched ShowFrom.
96 showFromLocs := make(map[uint64]bool)
97 // Apply to locations.
98 for _, loc := range p.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Location. {
99 if filterShowFromLocationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/filterShowFromLocation().(loc, showFrom) {
100 showFromLocs[loc.IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.] = true
101 matched = true
102 }
103 }
104 // For all samples, strip locations after the highest matching one.
105 s := make([]*Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#, 0, len(p.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Sample.))
106 for _, sample := range p.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Sample. {
107 for i := len(sample.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Location.) - 1; i >= 0; i-- {
108 if showFromLocs[sample.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Location.[i].IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.] {
109 sample.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Location. = sample.Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Location.[:i+1]
110 s = append(s, sample)
111 break
112 }
113 }
114 }
115 p.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Sample. = s
116 return matched
117 }
118
119 // filterShowFromLocation tests a showFrom regex against a location, removes
120 // lines after the last match and returns whether a match was found. If the
121 // mapping is matched, then all lines are kept.
122 func filterShowFromLocationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/filterShowFromLocation().(loc *Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#, showFrom *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#) bool {
123 if m := loc.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.; m != nil && showFrom.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.) {
124 return true
125 }
126 if i := loc.lastMatchedLineIndexscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#lastMatchedLineIndex().(showFrom); i >= 0 {
127 loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line. = loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.[:i+1]
128 return true
129 }
130 return false
131 }
132
133 // lastMatchedLineIndex returns the index of the last line that matches a regex,
134 // or -1 if no match is found.
135 func (loc *Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#) lastMatchedLineIndexscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#lastMatchedLineIndex().(re *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#) int {
136 for i := len(loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.) - 1; i >= 0; i-- {
137 if fn := loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.[i].Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Function.; fn != nil {
138 if re.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(fn.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Name.) || re.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(fn.Filenamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Filename.) {
139 return i
140 }
141 }
142 }
143 return -1
144 }
145
146 // FilterTagsByName filters the tags in a profile and only keeps
147 // tags that match show and not hide.
148 func (p *Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#) FilterTagsByNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#FilterTagsByName().(show, hide *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#) (sm, hm bool) {
149 matchRemove := func(name string) bool {
150 matchShow := show == nil || show.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(name)
151 matchHide := hide != nil && hide.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(name)
152
153 if matchShow {
154 sm = true
155 }
156 if matchHide {
157 hm = true
158 }
159 return !matchShow || matchHide
160 }
161 for _, s := range p.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Sample. {
162 for lab := range s.Labelscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Label. {
163 if matchRemove(lab) {
164 delete(s.Labelscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#Label., lab)
165 }
166 }
167 for lab := range s.NumLabelscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#NumLabel. {
168 if matchRemove(lab) {
169 delete(s.NumLabelscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#NumLabel., lab)
170 }
171 }
172 }
173 return
174 }
175
176 // matchesName returns whether the location matches the regular
177 // expression. It checks any available function names, file names, and
178 // mapping object filename.
179 func (loc *Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#) matchesNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#matchesName().(re *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#) bool {
180 for _, ln := range loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line. {
181 if fn := ln.Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Function.; fn != nil {
182 if re.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(fn.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Name.) || re.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(fn.Filenamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Filename.) {
183 return true
184 }
185 }
186 }
187 if m := loc.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.; m != nil && re.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.) {
188 return true
189 }
190 return false
191 }
192
193 // unmatchedLines returns the lines in the location that do not match
194 // the regular expression.
195 func (loc *Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#) unmatchedLinesscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#unmatchedLines().(re *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#) []Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line# {
196 if m := loc.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.; m != nil && re.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.) {
197 return nil
198 }
199 var lines []Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#
200 for _, ln := range loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line. {
201 if fn := ln.Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Function.; fn != nil {
202 if re.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(fn.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Name.) || re.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(fn.Filenamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Filename.) {
203 continue
204 }
205 }
206 lines = append(lines, ln)
207 }
208 return lines
209 }
210
211 // matchedLines returns the lines in the location that match
212 // the regular expression.
213 func (loc *Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#) matchedLinesscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#matchedLines().(re *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#) []Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line# {
214 if m := loc.Mappingscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Mapping.; m != nil && re.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(m.Filescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Mapping#File.) {
215 return loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line.
216 }
217 var lines []Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#
218 for _, ln := range loc.Linescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#Line. {
219 if fn := ln.Functionscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Line#Function.; fn != nil {
220 if !re.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(fn.Namescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Name.) && !re.MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(fn.Filenamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Function#Filename.) {
221 continue
222 }
223 }
224 lines = append(lines, ln)
225 }
226 return lines
227 }
228
229 // focusedAndNotIgnored looks up a slice of ids against a map of
230 // focused/ignored locations. The map only contains locations that are
231 // explicitly focused or ignored. Returns whether there is at least
232 // one focused location but no ignored locations.
233 func focusedAndNotIgnoredscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/focusedAndNotIgnored().(locs []*Locationscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#, m map[uint64]bool) bool {
234 var f bool
235 for _, loc := range locs {
236 if focus, focusOrIgnore := m[loc.IDscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Location#ID.]; focusOrIgnore {
237 if focus {
238 // Found focused location. Must keep searching in case there
239 // is an ignored one as well.
240 f = true
241 } else {
242 // Found ignored location. Can return false right away.
243 return false
244 }
245 }
246 }
247 return f
248 }
249
250 // TagMatch selects tags for filtering
251 type TagMatchscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/TagMatch# func(s *Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#) bool
252
253 // FilterSamplesByTag removes all samples from the profile, except
254 // those that match focus and do not match the ignore regular
255 // expression.
256 func (p *Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#) FilterSamplesByTagscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#FilterSamplesByTag().(focus, ignore TagMatchscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/TagMatch#) (fm, im bool) {
257 samples := make([]*Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Sample#, 0, len(p.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Sample.))
258 for _, s := range p.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Sample. {
259 focused, ignored := true, false
260 if focus != nil {
261 focused = focus(s)
262 }
263 if ignore != nil {
264 ignored = ignore(s)
265 }
266 fm = fm || focused
267 im = im || ignored
268 if focused && !ignored {
269 samples = append(samples, s)
270 }
271 }
272 p.Samplescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#Sample. = samples
273 return
274 }