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 a StringList type to be used for commandline flags. #583

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions internal/driver/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func installConfigFlags(flag plugin.FlagSet, cfg *config) func() error {

for _, field := range configFields {
n := field.name
fmt.Println(n)
help := configHelp[n]
var setter func()
switch ptr := cfg.fieldPtr(field).(type) {
Expand Down Expand Up @@ -249,6 +250,15 @@ func installConfigFlags(flag plugin.FlagSet, cfg *config) func() error {
}
}
}
case *[]*string:
f := flag.StringList(n, "", help)
setter = func() {
fmt.Println(ptr)
fmt.Println(f)
*ptr = *f
}
default:
panic(fmt.Sprintf("%v %T", ptr, ptr))
}
setters = append(setters, setter)
}
Expand Down
8 changes: 8 additions & 0 deletions internal/driver/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ var configHelp = map[string]string{
"Use name=value syntax to limit the matching to a specific tag.",
"Numeric tag filter examples: 1kb, 1kb:10kb, memory=32mb:",
"String tag filter examples: foo, foo.*bar, mytag=foo.*bar"),
"tagleaf": helpText(
"Adds a pseudo leaf node for samples with tags matched by regexp",
"Use name=value1,value2 syntax to specify the tag and the values",
"for which nodes would be added. Example: latency=1s,2s,3s."),
"tagroot": helpText(
"Adds a pseudo root node for samples with tags matched by regexp",
"Use name=value1,value2 syntax to specify the tag and the values",
"for which nodes would be added. Example: latency=1s,2s,3s."),
Comment on lines +202 to +209
Copy link
Collaborator

Choose a reason for hiding this comment

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

Couple of questions:

  1. The option / flag is of type []string / multi-value. The examples are of the form "foo=val1,val2" What exactly the multi-value here? Is it the comma-separated multi-values as in example, i.e. "foo=val1" and "val2" as two values which is weird? Or is that there is ability to have multiple occurrences of each of these like "-flag=foo=val1,val2 -flag=bar=val3,val4" and the documentation needs to explain this?

  2. How are numeric tags handled, including with units like memory? Can we reuse the same value format as in tagfocus / taghide etc.? I feel that we are introducing a new way to specify a set of values and I don't see why different tag* flags can't unify on a single way to do that.

"tagshow": helpText(
"Only consider tags matching this regexp",
"Discard tags that do not match this regexp"),
Expand Down
33 changes: 21 additions & 12 deletions internal/driver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ type config struct {
Output string `json:"-"`

// Display options.
CallTree bool `json:"call_tree,omitempty"`
RelativePercentages bool `json:"relative_percentages,omitempty"`
Unit string `json:"unit,omitempty"`
CompactLabels bool `json:"compact_labels,omitempty"`
SourcePath string `json:"-"`
TrimPath string `json:"-"`
IntelSyntax bool `json:"intel_syntax,omitempty"`
Mean bool `json:"mean,omitempty"`
SampleIndex string `json:"-"`
DivideBy float64 `json:"-"`
Normalize bool `json:"normalize,omitempty"`
Sort string `json:"sort,omitempty"`
CallTree bool `json:"call_tree,omitempty"`
RelativePercentages bool `json:"relative_percentages,omitempty"`
Unit string `json:"unit,omitempty"`
CompactLabels bool `json:"compact_labels,omitempty"`
SourcePath string `json:"-"`
TrimPath string `json:"-"`
IntelSyntax bool `json:"intel_syntax,omitempty"`
Mean bool `json:"mean,omitempty"`
SampleIndex string `json:"-"`
DivideBy float64 `json:"-"`
Normalize bool `json:"normalize,omitempty"`
Sort string `json:"sort,omitempty"`
TagLeaf []*string `json:"tagleaf,omitempty"`
TagRoot []*string `json:"tagroot,omitempty"`

// Filtering options
DropNegative bool `json:"drop_negative,omitempty"`
Expand Down Expand Up @@ -145,6 +147,8 @@ func init() {
"show_from": "sf",
"tagfocus": "tf",
"tagignore": "ti",
"tagleaf": "tl",
"tagroot": "tr",
"tagshow": "ts",
"taghide": "th",
"mean": "mean",
Expand Down Expand Up @@ -210,6 +214,8 @@ func (cfg *config) get(f configField) string {
return fmt.Sprint(*ptr)
case *bool:
return fmt.Sprint(*ptr)
case *[]*string:
return fmt.Sprint(*ptr)
}
panic(fmt.Sprintf("unsupported config field type %v", f.field.Type))
}
Expand Down Expand Up @@ -247,6 +253,9 @@ func (cfg *config) set(f configField, value string) error {
return err
}
*ptr = v
case *[]*string:
*ptr = (*ptr)[:0]
*ptr = append(*ptr, &value)
default:
panic(fmt.Sprintf("unsupported config field type %v", f.field.Type))
}
Expand Down
7 changes: 7 additions & 0 deletions internal/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func generateRawReport(p *profile.Profile, cmd []string, cfg config, o *plugin.O
return nil, nil, err
}
}
addPseudoLocs(p, cfg, o.UI)
if err := aggregate(p, cfg); err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -342,3 +343,9 @@ func valueExtractor(ix int) sampleValueFunc {
return v[ix]
}
}

func addPseudoLocs(p *profile.Profile, cfg config, ui plugin.UI) {
tr, tl := p.AddPseudoLocs(cfg.TagRoot, cfg.TagLeaf)
warnNoMatches(len(cfg.TagRoot) == 0 || tr, "TagRoot", ui)
warnNoMatches(len(cfg.TagShow) == 0 || tl, "TagLeaf", ui)
}
27 changes: 26 additions & 1 deletion internal/driver/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package driver

import (
"flag"
"fmt"
"strings"
)

Expand Down Expand Up @@ -44,9 +45,33 @@ func (*GoFlags) String(o, d, c string) *string {
return flag.String(o, d, c)
}

type stringList []*string

func (sl *stringList) Set(v string) error {
*sl = append(*sl, &v)
return nil
}

func (sl *stringList) Get() interface{} {
return sl
}

func (sl *stringList) String() string {
nolanmar511 marked this conversation as resolved.
Show resolved Hide resolved
var cl []string
for _, s := range *sl {
cl = append(cl, *s)
}
return fmt.Sprint(cl)
}

// StringList implements the plugin.FlagSet interface.
func (*GoFlags) StringList(o, d, c string) *[]*string {
return &[]*string{flag.String(o, d, c)}
sl := &stringList{}
if len(d) > 0 {
sl.Set(d)
}
flag.Var(sl, o, c)
return (*[]*string)(sl)
}

// ExtraUsage implements the plugin.FlagSet interface.
Expand Down
65 changes: 65 additions & 0 deletions internal/driver/flags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2020 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package driver

import (
"testing"
)

func TestString(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems a bit different (at least to the best of my knowledge and in the context of golang) to test functions on a private type in golang. Testing the public interface would probably be preferable.

But, I know that there are no tests for flag.go right now; will leave it up to @aalexand to decide what sort of testing is sufficient.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ping to @nilayvaish and @aalexand.

I'm not sure about this testing. It may be better than not having tests, but I'm a bit skeptical about testing only a private type, rather than the publicly available functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

AFAICT, it is not possible to test the public interface without creating a separate process. Would you be OK with the _test.go file creating a new process with flags? Otherwise, I think we need to drop the tests all together.

s1 := "test1"
s2a := "test2a"
s2b := "test2b"
for _, tc := range []struct {
sl stringList
want string
}{
{stringList([]*string{}), "[]"},
{stringList([]*string{&s1}), "[test1]"},
{stringList([]*string{&s2a, &s2b}), "[test2a test2b]"},
} {
got := tc.sl.String()
if got != tc.want {
t.Errorf("want %s, got %s", tc.want, got)
}
}
}

func TestSet(t *testing.T) {
s1 := "test1"
s2a := "test2a"
s2b := "test2b"
for _, tc := range []struct {
in stringList
want []string
add string
}{
{stringList([]*string{}), []string{s1}, s1},
{stringList([]*string{&s2a}), []string{s2a, s2b}, s2b},
} {
ok := tc.in.Set(tc.add)
if ok != nil {
t.Errorf("%v", ok)
}
if len(tc.in) != len(tc.want) {
t.Errorf("want len %d, got len %d", len(tc.want), len(tc.in))
}
for i, s := range tc.in {
if *s != tc.want[i] {
t.Errorf("want %s, got %s", tc.want[i], *s)
}
}
}
}
125 changes: 125 additions & 0 deletions profile/pseudo_locs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2020 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package profile

// Implements methods to add pseudo locations to samples.

import "fmt"

func findUniqueLoc(p *Profile) uint64 {
ids := make(map[uint64]bool)
for _, loc := range p.Location {
ids[loc.ID] = true
}
for id := uint64(1); ; id++ {
if ok := ids[id]; !ok {
return id
}
}
}

func findUniqueFun(p *Profile) uint64 {
ids := make(map[uint64]bool)
for _, fun := range p.Function {
ids[fun.ID] = true
}
for id := uint64(1); ; id++ {
if ok := ids[id]; !ok {
return id
}
}
}

func addStrFun(p *Profile, tag string, sl []string) *Function {
fun := new(Function)
fun.ID = findUniqueFun(p)
fun.Name = fmt.Sprint(tag, sl)
p.Function = append(p.Function, fun)
return fun
}

func addNumFun(p *Profile, tag string, nums []int64, units []string) *Function {
fun := new(Function)
fun.ID = findUniqueFun(p)
for i, n := range nums {
fun.Name = fun.Name + fmt.Sprintf(" %d %s", n, units[i])
}
p.Function = append(p.Function, fun)
return fun
}

func addStringLoc(p *Profile, tag string, sl []string) *Location {
loc := new(Location)
loc.ID = findUniqueLoc(p)
var line Line
line.Function = addStrFun(p, tag, sl)
loc.Line = append(loc.Line, line)
loc.IsFolded = false
p.Location = append(p.Location, loc)
return loc
}

func addNumLoc(p *Profile, tag string, nl []int64, nu []string) *Location {
loc := new(Location)
loc.ID = findUniqueLoc(p)
var line Line
line.Function = addNumFun(p, tag, nl, nu)
loc.Line = append(loc.Line, line)
loc.IsFolded = false
p.Location = append(p.Location, loc)
return loc
}

// AddPseudoLocs adds root and leaf locations for tags in a profile as
// requested in the provided options.
func (p *Profile) AddPseudoLocs(rootTags, leafTags []*string) (bool, bool) {
addedRootLoc := false
addedLeafLoc := false
for _, s := range p.Sample {
rootLocs := make([]*Location, 0)
for _, tag := range rootTags {
l1, ok1 := s.Label[*tag]
l2, ok2 := s.NumLabel[*tag]
l3 := s.NumUnit[*tag]
if ok1 || ok2 {
addedRootLoc = true
var loc *Location
if ok1 {
loc = addStringLoc(p, *tag, l1)
} else {
loc = addNumLoc(p, *tag, l2, l3)
}
rootLocs = append(rootLocs, loc)
}
}
s.Location = append(rootLocs, s.Location...)
for _, tag := range leafTags {
l1, ok1 := s.Label[*tag]
l2, ok2 := s.NumLabel[*tag]
l3 := s.NumUnit[*tag]
if ok1 || ok2 {
addedLeafLoc = true
var loc *Location
if ok1 {
loc = addStringLoc(p, *tag, l1)
} else {
loc = addNumLoc(p, *tag, l2, l3)
}
s.Location = append(s.Location, loc)
}
}
}
return addedRootLoc, addedLeafLoc
}
Loading