1
2
3
4
5
6
7 package profile
8
9 import (
10 "fmt"
11 "regexp"
12 )
13
14
15
16
17 func (p *Profile) Prune(dropRx, keepRx *regexp.Regexp) {
18 prune := make(map[uint64]bool)
19 pruneBeneath := make(map[uint64]bool)
20
21 for _, loc := range p.Location {
22 var i int
23 for i = len(loc.Line) - 1; i >= 0; i-- {
24 if fn := loc.Line[i].Function; fn != nil && fn.Name != "" {
25 funcName := fn.Name
26
27 if funcName[0] == '.' {
28 funcName = funcName[1:]
29 }
30 if dropRx.MatchString(funcName) {
31 if keepRx == nil || !keepRx.MatchString(funcName) {
32 break
33 }
34 }
35 }
36 }
37
38 if i >= 0 {
39
40 pruneBeneath[loc.ID] = true
41
42
43 if i == len(loc.Line)-1 {
44
45 prune[loc.ID] = true
46 } else {
47 loc.Line = loc.Line[i+1:]
48 }
49 }
50 }
51
52
53 for _, sample := range p.Sample {
54
55
56
57 foundUser := false
58 for i := len(sample.Location) - 1; i >= 0; i-- {
59 id := sample.Location[i].ID
60 if !prune[id] && !pruneBeneath[id] {
61 foundUser = true
62 continue
63 }
64 if !foundUser {
65 continue
66 }
67 if prune[id] {
68 sample.Location = sample.Location[i+1:]
69 break
70 }
71 if pruneBeneath[id] {
72 sample.Location = sample.Location[i:]
73 break
74 }
75 }
76 }
77 }
78
79
80
81 func (p *Profile) RemoveUninteresting() error {
82 var keep, drop *regexp.Regexp
83 var err error
84
85 if p.DropFrames != "" {
86 if drop, err = regexp.Compile("^(" + p.DropFrames + ")$"); err != nil {
87 return fmt.Errorf("failed to compile regexp %s: %v", p.DropFrames, err)
88 }
89 if p.KeepFrames != "" {
90 if keep, err = regexp.Compile("^(" + p.KeepFrames + ")$"); err != nil {
91 return fmt.Errorf("failed to compile regexp %s: %v", p.KeepFrames, err)
92 }
93 }
94 p.Prune(drop, keep)
95 }
96 return nil
97 }
98
View as plain text