From: Raul Silvera Date: Wed, 14 Dec 2016 21:32:18 +0000 (-0800) Subject: cmd/pprof: search for sample types in profile X-Git-Tag: go1.8beta2~6 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7e4ef4ebfc4f24bb030716c81e0f812aa3a36c97;p=gostls13.git cmd/pprof: search for sample types in profile 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 --- diff --git a/src/cmd/pprof/internal/driver/driver.go b/src/cmd/pprof/internal/driver/driver.go index 931985a7f2..0f1ed6eece 100644 --- a/src/cmd/pprof/internal/driver/driver.go +++ b/src/cmd/pprof/internal/driver/driver.go @@ -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 {