Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tagshow/taghide commands to select a subset of tags in a profile #33

Merged
merged 3 commits into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/driver/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,12 @@ var pprofVariables = variables{
"tagignore": &variable{stringKind, "", "", helpText(
"Discard samples with tags in range or matched by regexp",
"Discard samples that do include a node with a tag matching this regexp.")},

"tagshow": &variable{stringKind, "", "", helpText(
"Only consider tags matching this regexp",
"Discard tags that do not match this regexp")},
"taghide": &variable{stringKind, "", "", helpText(
"Skip tags matching this regexp",
"Discard tags that match this regexp")},
// Heap profile options
"divide_by": &variable{floatKind, "1", "", helpText(
"Ratio to divide all samples before visualization",
Expand Down
6 changes: 6 additions & 0 deletions internal/driver/driver_focus.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func applyFocus(prof *profile.Profile, v variables, ui plugin.UI) error {
warnNoMatches(tagfocus == nil || tfm, "TagFocus", ui)
warnNoMatches(tagignore == nil || tim, "TagIgnore", ui)

tagshow, err := compileRegexOption("tagshow", v["tagshow"].value, err)
taghide, err := compileRegexOption("taghide", v["taghide"].value, err)
tns, tnh := prof.FilterTagsByName(tagshow, taghide)
warnNoMatches(tagshow == nil || tns, "TagShow", ui)
warnNoMatches(tagignore == nil || tnh, "TagHide", ui)

if prunefrom != nil {
prof.PruneFrom(prunefrom)
}
Expand Down
30 changes: 30 additions & 0 deletions profile/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,36 @@ func (p *Profile) FilterSamplesByName(focus, ignore, hide, show *regexp.Regexp)
return
}

// FilterTagsByName filters the tags in a profile and only keeps
// tags that match show and not hide.
func (p *Profile) FilterTagsByName(show, hide *regexp.Regexp) (sm, hm bool) {
matchShow := func(name string) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a bit difficult to read code with two matchShow variables with different meanings next to each other.
Maybe in this case, I would choose matchRemove for the function, then return !matchShow || matchHide and change later 2x if matchRemove(lab) { ... } so there are less negations to think in.

matchShow := show == nil || show.MatchString(name)
matchHide := hide != nil && hide.MatchString(name)

if matchShow {
sm = true
}
if matchHide {
hm = true
}
return matchShow && !matchHide
}
for _, s := range p.Sample {
for lab := range s.Label {
if !matchShow(lab) {
delete(s.Label, lab)
}
}
for lab := range s.NumLabel {
if !matchShow(lab) {
delete(s.NumLabel, lab)
}
}
}
return
}

// matchesName returns whether the location matches the regular
// expression. It checks any available function names, file names, and
// mapping object filename.
Expand Down
43 changes: 43 additions & 0 deletions profile/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,49 @@ func TestFilter(t *testing.T) {
}
}

func TestTagFilter(t *testing.T) {
// Perform several forms of tag filtering on the test profile.

type filterTestcase struct {
include, exclude *regexp.Regexp
im, em bool
count int
}

countTags := func (p *Profile) map[string]bool {
tags := make(map[string]bool)

for _, s := range p.Sample {
for l := range s.Label {
tags[l] = true
}
for l := range s.NumLabel {
tags[l] = true
}
}
return tags
}

for tx, tc := range []filterTestcase{
{nil, nil, true, false, 3},
{regexp.MustCompile("notfound"), nil, false, false, 0},
{regexp.MustCompile("key1"), nil, true, false, 1},
{nil, regexp.MustCompile("key[12]"), true, true, 1},
} {
prof := testProfile.Copy()
gim, gem := prof.FilterTagsByName(tc.include, tc.exclude)
if gim != tc.im {
t.Errorf("Filter #%d, got include match=%v, want %v", tx, gim, tc.im)
}
if gem != tc.em {
t.Errorf("Filter #%d, got exclude match=%v, want %v", tx, gem, tc.em)
}
if tags := countTags(prof) ; len(tags) != tc.count {
t.Errorf("Filter #%d, got %d tags[%v], want %d", tx, len(tags), tags, tc.count)
}
}
}

// locationHash constructs a string to use as a hashkey for a sample, based on its locations
func locationHash(s *Sample) string {
var tb string
Expand Down