Skip to content

Commit

Permalink
use pprof::bass=true to mark diff base samples
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanmar511 committed May 9, 2018
1 parent 6921532 commit 14703a0
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 121 deletions.
14 changes: 5 additions & 9 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,6 @@ search for them in a directory pointed to by the environment variable
* **-weblist= _regex_:** Generates a source/assembly combined annotated listing for
functions matching *regex*, and starts a web browser to display it.

Samples in a profile may have tags. These tags have a name and a value; this
value can be either numeric or a string. pprof can select samples from a
profile based on these tags using the `-tagfocus` and `-tagignore` options.

# Fetching profiles

pprof can read profiles from a file or directly from a URL over http. Its native
Expand Down Expand Up @@ -248,13 +244,13 @@ pprof can subtract a profile from another, provided the profiles are of
compatible types, in order to compare them. For that, use the
**-diff_base= _profile_** option, where *profile* is the filename or URL for the
profile to be subtracted. This may result in some report entries having negative
values. Percentages in the report are relative to the total of
values. Percentages in the report are relative to the total of
the diff base.

If the merged profile is output as a protocol buffer, all samples in
the diff base profile will have a label with the key "base" and a value of "1".
If pprof is then used to look at the merged profile, it will behave as if
separate source and base profiles were passed in.
If the merged profile is output as a protocol buffer, all samples in
the diff base profile will have a label with the key "pprof::base" and a value
of "true". If pprof is then used to look at the merged profile, it will behave
as if separate source and base profiles were passed in.

The **-base** option can be used in the place of the **-diff_base** option, and
may be preferrable when comparing two cumulative profiles, for example two
Expand Down
51 changes: 31 additions & 20 deletions internal/driver/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,40 +142,51 @@ func parseFlags(o *plugin.Options) (*source, []string, error) {
Comment: *flagAddComment,
}

var base []string
for _, s := range *flagBase {
if *s != "" {
base = append(base, *s)
}
if err := source.addBaseProfiles(flagBase, flagDiffBase); err != nil {
return nil, nil, err
}

var diffBase []string
for _, s := range *flagDiffBase {
if *s != "" {
diffBase = append(diffBase, *s)
}
normalize := pprofVariables["normalize"].boolValue()
if normalize && len(source.Base) == 0 {
return nil, nil, fmt.Errorf("Must have base profile to normalize by")
}
source.Normalize = normalize

if bu, ok := o.Obj.(*binutils.Binutils); ok {
bu.SetTools(*flagTools)
}
return source, cmd, nil
}

// addBaseProfiles adds the list of base profiles or diff base profiles to
// the source. This function will return an error if both base and diff base
// profiles are specified.
func (source *source) addBaseProfiles(flagBase, flagDiffBase *[]*string) error {
base := parseStringListFlag(flagBase)
diffBase := parseStringListFlag(flagDiffBase)

if len(base) > 0 && len(diffBase) > 0 {
return nil, nil, fmt.Errorf("-base and -diff_base flags cannot both be specified")
return fmt.Errorf("-base and -diff_base flags cannot both be specified")
}

source.Base = base
if len(diffBase) > 0 {
source.Base = diffBase
source.DiffBase = true
}
return nil
}

normalize := pprofVariables["normalize"].boolValue()
if normalize && len(source.Base) == 0 {
return nil, nil, fmt.Errorf("Must have base profile to normalize by")
}
source.Normalize = normalize

if bu, ok := o.Obj.(*binutils.Binutils); ok {
bu.SetTools(*flagTools)
// parseStringListFlag list takes a StringList flag, and outputs an array of non-empty
// strings associated with the flag.
func parseStringListFlag(list *[]*string) []string {
var l []string
for _, s := range *list {
if *s != "" {
l = append(l, *s)
}
}
return source, cmd, nil
return l
}

// installFlags creates command line flags for pprof variables.
Expand Down
2 changes: 1 addition & 1 deletion internal/driver/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func grabSourcesAndBases(sources, bases []profileSource, isDiffBase bool, fetch
defer wg.Done()
pbase, mbase, savebase, countbase, errbase = chunkedGrab(bases, fetch, obj, ui)
if pbase != nil && isDiffBase {
pbase.SetTag("base", []string{"1"})
pbase.SetTag("pprof::base", []string{"true"})
}
}()
wg.Wait()
Expand Down
12 changes: 6 additions & 6 deletions internal/driver/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,27 +398,27 @@ func TestFetchWithBase(t *testing.T) {
},
{
values: []int64{-2700, -608881724},
labels: map[string][]string{"base": {"1"}},
labels: map[string][]string{"pprof::base": {"true"}},
},
{
values: []int64{-100, -23992},
labels: map[string][]string{"base": {"1"}},
labels: map[string][]string{"pprof::base": {"true"}},
},
{
values: []int64{-200, -179943},
labels: map[string][]string{"base": {"1"}},
labels: map[string][]string{"pprof::base": {"true"}},
},
{
values: []int64{-100, -17778444},
labels: map[string][]string{"base": {"1"}},
labels: map[string][]string{"pprof::base": {"true"}},
},
{
values: []int64{-100, -75976},
labels: map[string][]string{"base": {"1"}},
labels: map[string][]string{"pprof::base": {"true"}},
},
{
values: []int64{-300, -63568134},
labels: map[string][]string{"base": {"1"}},
labels: map[string][]string{"pprof::base": {"true"}},
},
},
"",
Expand Down
8 changes: 4 additions & 4 deletions internal/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (rpt *Report) newGraph(nodes graph.NodeSet) *graph.Graph {

// Remove tag marking samples from the base profiles, so it does not appear
// as a nodelet in the graph view.
prof.SetTag("base", []string{})
prof.SetTag("pprof::base", []string{})

formatTag := func(v int64, key string) string {
return measurement.ScaledLabel(v, key, o.OutputUnit)
Expand Down Expand Up @@ -1217,7 +1217,7 @@ func NewDefault(prof *profile.Profile, options Options) *Report {
}

// computeTotal computes the sum of the absolute value of all sample values.
// If any samples have the tag "base" with value "1", then the total
// If any samples have the tag "pprof::base" with value "true", then the total
// will only include samples with that tag.
func computeTotal(prof *profile.Profile, value, meanDiv func(v []int64) int64) int64 {
var div, total, diffDiv, diffTotal int64
Expand All @@ -1231,11 +1231,11 @@ func computeTotal(prof *profile.Profile, value, meanDiv func(v []int64) int64) i
v = -v
}
total += v
if sample.HasTag("base", "1") {
div += d
if sample.HasTag("pprof::base", "true") {
diffTotal += v
diffDiv += d
}
div += d
}
if diffTotal > 0 {
total = diffTotal
Expand Down
8 changes: 4 additions & 4 deletions internal/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func TestComputeTotal(t *testing.T) {
{
Location: []*profile.Location{testL[2], testL[1], testL[0]},
Value: []int64{-10, 3},
Label: map[string][]string{"base": {"1"}},
Label: map[string][]string{"pprof::base": {"true"}},
},
{
Location: []*profile.Location{testL[2], testL[1], testL[0]},
Expand All @@ -339,12 +339,12 @@ func TestComputeTotal(t *testing.T) {
{
Location: []*profile.Location{testL[2], testL[1], testL[0]},
Value: []int64{-9000, 3},
Label: map[string][]string{"base": {"1"}},
Label: map[string][]string{"pprof::base": {"true"}},
},
{
Location: []*profile.Location{testL[2], testL[1], testL[0]},
Value: []int64{-1, 3},
Label: map[string][]string{"base": {"1"}},
Label: map[string][]string{"pprof::base": {"true"}},
},
{
Location: []*profile.Location{testL[4], testL[2], testL[0]},
Expand All @@ -353,7 +353,7 @@ func TestComputeTotal(t *testing.T) {
{
Location: []*profile.Location{testL[2], testL[1], testL[0]},
Value: []int64{100, 3},
Label: map[string][]string{"base": {"1"}},
Label: map[string][]string{"pprof::base": {"true"}},
},
}

Expand Down
2 changes: 1 addition & 1 deletion profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ func numLabelsToString(numLabels map[string][]int64, numUnits map[string][]strin
func (p *Profile) SetTag(key string, value []string) {
for _, sample := range p.Sample {
if sample.Label == nil {
sample.Label = make(map[string][]string)
sample.Label = map[string][]string{}
}
sample.Label[key] = value
}
Expand Down
Loading

0 comments on commit 14703a0

Please sign in to comment.