| 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 profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 19 | "strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/" |
| 20 | "stringsscip-go gomod github.com/golang/go/src go1.24.0 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 *Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#) SampleIndexByNamescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#SampleIndexByName().(sampleIndex string) (int, error) { |
| 27 | if sampleIndex == "" { |
| 28 | if dst := p.DefaultSampleTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#DefaultSampleType.; dst != "" { |
| 29 | for i, t := range sampleTypesscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/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.SampleTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#SampleType.) - 1, nil |
| 37 | } |
| 38 | if i, err := strconvscip-go gomod github.com/golang/go/src go1.24.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.24.0 strconv/Atoi().(sampleIndex); err == nil { |
| 39 | if i < 0 || i >= len(p.SampleTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#SampleType.) { |
| 40 | return 0, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("sample_index %s is outside the range [0..%d]", sampleIndex, len(p.SampleTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#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 := stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.24.0 strings/TrimPrefix().(sampleIndex, "inuse_") |
| 49 | for i, t := range p.SampleTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#SampleType. { |
| 50 | if t.Typescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/ValueType#Type. == sampleIndex || t.Typescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/ValueType#Type. == noInuse { |
| 51 | return i, nil |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return 0, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("sample_index %q must be one of: %v", sampleIndex, sampleTypesscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/sampleTypes().(p)) |
| 56 | } |
| 57 | |
| 58 | func sampleTypesscip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/sampleTypes().(p *Profilescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#) []string { |
| 59 | types := make([]string, len(p.SampleTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#SampleType.)) |
| 60 | for i, t := range p.SampleTypescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/Profile#SampleType. { |
| 61 | types[i] = t.Typescip-go gomod github.com/google/pprof . `github.com/google/pprof/profile`/ValueType#Type. |
| 62 | } |
| 63 | return types |
| 64 | } |