]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/pprof: search for sample types in profile
authorRaul Silvera <rsilvera@google.com>
Wed, 14 Dec 2016 21:32:18 +0000 (13:32 -0800)
committerMichael Matloob <matloob@golang.org>
Thu, 15 Dec 2016 14:55:37 +0000 (14:55 +0000)
Search the sample types in the profile being processed to map
sample type options to indices in the profile sample type array.

Previously these were hardcoded, which caused issues when the
sample types for a profile type changed. For instance, this was
triggered by the native generation of profiles in profile.proto
format.

This fixes #18230. A similar mechanism already exists on the upstream
pprof.

Change-Id: I945d8d842a0c2ca14299dabefe83124746ecd7e2
Reviewed-on: https://go-review.googlesource.com/34382
Reviewed-by: Michael Matloob <matloob@golang.org>
src/cmd/pprof/internal/driver/driver.go

index 931985a7f2891b7dd1d0829c36f468e151c67eac..0f1ed6eece053877749e62fd4f10ec828b9f24e2 100644 (file)
@@ -780,14 +780,14 @@ func processFlags(p *profile.Profile, ui plugin.UI, f *flags) error {
 
        var err error
        si, sm := *f.flagSampleIndex, *f.flagMean || *f.flagMeanDelay
-       si, err = sampleIndex(p, &f.flagTotalDelay, si, 1, "delay", "-total_delay", err)
-       si, err = sampleIndex(p, &f.flagMeanDelay, si, 1, "delay", "-mean_delay", err)
-       si, err = sampleIndex(p, &f.flagContentions, si, 0, "contentions", "-contentions", err)
+       si, err = sampleIndex(p, &f.flagTotalDelay, si, "delay", "-total_delay", err)
+       si, err = sampleIndex(p, &f.flagMeanDelay, si, "delay", "-mean_delay", err)
+       si, err = sampleIndex(p, &f.flagContentions, si, "contentions", "-contentions", err)
 
-       si, err = sampleIndex(p, &f.flagInUseSpace, si, 1, "inuse_space", "-inuse_space", err)
-       si, err = sampleIndex(p, &f.flagInUseObjects, si, 0, "inuse_objects", "-inuse_objects", err)
-       si, err = sampleIndex(p, &f.flagAllocSpace, si, 1, "alloc_space", "-alloc_space", err)
-       si, err = sampleIndex(p, &f.flagAllocObjects, si, 0, "alloc_objects", "-alloc_objects", err)
+       si, err = sampleIndex(p, &f.flagInUseSpace, si, "inuse_space", "-inuse_space", err)
+       si, err = sampleIndex(p, &f.flagInUseObjects, si, "inuse_objects", "-inuse_objects", err)
+       si, err = sampleIndex(p, &f.flagAllocSpace, si, "alloc_space", "-alloc_space", err)
+       si, err = sampleIndex(p, &f.flagAllocObjects, si, "alloc_objects", "-alloc_objects", err)
 
        if si == -1 {
                // Use last value if none is requested.
@@ -806,7 +806,6 @@ func processFlags(p *profile.Profile, ui plugin.UI, f *flags) error {
 
 func sampleIndex(p *profile.Profile, flag **bool,
        sampleIndex int,
-       newSampleIndex int,
        sampleType, option string,
        err error) (int, error) {
        if err != nil || !**flag {
@@ -816,11 +815,12 @@ func sampleIndex(p *profile.Profile, flag **bool,
        if sampleIndex != -1 {
                return 0, fmt.Errorf("set at most one sample value selection option")
        }
-       if newSampleIndex >= len(p.SampleType) ||
-               p.SampleType[newSampleIndex].Type != sampleType {
-               return 0, fmt.Errorf("option %s not valid for this profile", option)
+       for index, s := range p.SampleType {
+               if sampleType == s.Type {
+                       return index, nil
+               }
        }
-       return newSampleIndex, nil
+       return 0, fmt.Errorf("option %s not valid for this profile", option)
 }
 
 func countFlags(bs []*bool) int {