golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/cmd/vendor/github.com/google/pprof/profile/index.go (about) 1 // Copyright 2016 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package profile 16 17 import ( 18 "fmt" 19 "strconv" 20 "strings" 21 ) 22 23 // SampleIndexByName returns the appropriate index for a value of sample index. 24 // If numeric, it returns the number, otherwise it looks up the text in the 25 // profile sample types. 26 func (p *Profile) SampleIndexByName(sampleIndex string) (int, error) { 27 if sampleIndex == "" { 28 if dst := p.DefaultSampleType; dst != "" { 29 for i, t := range sampleTypes(p) { 30 if t == dst { 31 return i, nil 32 } 33 } 34 } 35 // By default select the last sample value 36 return len(p.SampleType) - 1, nil 37 } 38 if i, err := strconv.Atoi(sampleIndex); err == nil { 39 if i < 0 || i >= len(p.SampleType) { 40 return 0, fmt.Errorf("sample_index %s is outside the range [0..%d]", sampleIndex, len(p.SampleType)-1) 41 } 42 return i, nil 43 } 44 45 // Remove the inuse_ prefix to support legacy pprof options 46 // "inuse_space" and "inuse_objects" for profiles containing types 47 // "space" and "objects". 48 noInuse := strings.TrimPrefix(sampleIndex, "inuse_") 49 for i, t := range p.SampleType { 50 if t.Type == sampleIndex || t.Type == noInuse { 51 return i, nil 52 } 53 } 54 55 return 0, fmt.Errorf("sample_index %q must be one of: %v", sampleIndex, sampleTypes(p)) 56 } 57 58 func sampleTypes(p *Profile) []string { 59 types := make([]string, len(p.SampleType)) 60 for i, t := range p.SampleType { 61 types[i] = t.Type 62 } 63 return types 64 }