1// Copyright 2018 Google LLC 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 15package profiler 16 17import ( 18 "bytes" 19 "context" 20 "fmt" 21 "runtime" 22 "time" 23 24 "github.com/google/pprof/profile" 25) 26 27// heapProfile collects an in-use heap profile. The heap profiles returned by 28// the runtime also include the heap allocation metrics. We zero those out 29// since all allocations since program start are recorded, so these make the 30// profile large and there is a separate alloc profile type which shows 31// allocations from a set duration. 32func heapProfile(prof *bytes.Buffer) error { 33 p, err := goHeapProfile() 34 if err != nil { 35 return err 36 } 37 for _, s := range p.Sample { 38 s.Value[0] = 0 39 s.Value[1] = 0 40 } 41 42 // Merge profile with itself to remove samples with only zero values and 43 // reduce profile size. 44 p, err = profile.Merge([]*profile.Profile{p}) 45 if err != nil { 46 return err 47 } 48 return p.Write(prof) 49} 50 51// deltaAllocProfile collects an allocation profile by gathering a heap profile, 52// sleeping for the specified duration, gathering another heap profile and 53// subtracting the initial one from that. It then drops the in-use metrics from 54// the profile. If requested, it forces the GC before taking each of the heap 55// profiles, which improves the profile accuracy (see docs in 56// https://golang.org/src/runtime/mprof.go on why). 57func deltaAllocProfile(ctx context.Context, duration time.Duration, forceGC bool, prof *bytes.Buffer) error { 58 p1, err := allocProfile(forceGC) 59 if err != nil { 60 return err 61 } 62 63 sleep(ctx, duration) 64 65 p2, err := allocProfile(forceGC) 66 if err != nil { 67 return err 68 } 69 70 p1.Scale(-1) 71 p, err := profile.Merge([]*profile.Profile{p1, p2}) 72 if err != nil { 73 return err 74 } 75 p.DurationNanos = duration.Nanoseconds() 76 return p.Write(prof) 77} 78 79// allocProfile collects a single heap profile, and removes all metrics but 80// allocation metrics. 81func allocProfile(forceGC bool) (*profile.Profile, error) { 82 if forceGC { 83 runtime.GC() 84 } 85 p, err := goHeapProfile() 86 if err != nil { 87 return nil, err 88 } 89 p.SampleType = p.SampleType[:2] 90 for _, s := range p.Sample { 91 s.Value = s.Value[:2] 92 } 93 return p, nil 94} 95 96// goHeapProfile collects a heap profile. It returns an error if the metrics 97// in the collected heap profile do not match the expected metrics. 98func goHeapProfile() (*profile.Profile, error) { 99 var prof bytes.Buffer 100 if err := writeHeapProfile(&prof); err != nil { 101 return nil, fmt.Errorf("failed to write heap profile: %v", err) 102 } 103 p, err := profile.Parse(&prof) 104 if err != nil { 105 return nil, err 106 } 107 if got := len(p.SampleType); got != 4 { 108 return nil, fmt.Errorf("invalid heap profile: got %d sample types, want 4", got) 109 } 110 for i, want := range []string{"alloc_objects", "alloc_space", "inuse_objects", "inuse_space"} { 111 if got := p.SampleType[i].Type; got != want { 112 return nil, fmt.Errorf("invalid heap profile: got %q sample type at index %d, want %q", got, i, want) 113 } 114 } 115 return p, nil 116} 117