1
2
3
4
5 package pprof
6
7 import (
8 "internal/profilerecord"
9 "io"
10 "math"
11 "runtime"
12 "strings"
13 )
14
15
16 func writeHeapProto(w io.Writer, p []profilerecord.MemProfileRecord, rate int64, defaultSampleType string) error {
17 b := newProfileBuilder(w)
18 b.pbValueType(tagProfile_PeriodType, "space", "bytes")
19 b.pb.int64Opt(tagProfile_Period, rate)
20 b.pbValueType(tagProfile_SampleType, "alloc_objects", "count")
21 b.pbValueType(tagProfile_SampleType, "alloc_space", "bytes")
22 b.pbValueType(tagProfile_SampleType, "inuse_objects", "count")
23 b.pbValueType(tagProfile_SampleType, "inuse_space", "bytes")
24 if defaultSampleType != "" {
25 b.pb.int64Opt(tagProfile_DefaultSampleType, b.stringIndex(defaultSampleType))
26 }
27
28 values := []int64{0, 0, 0, 0}
29 var locs []uint64
30 for _, r := range p {
31 hideRuntime := true
32 for tries := 0; tries < 2; tries++ {
33 stk := r.Stack
34
35
36
37 if hideRuntime {
38 for i, addr := range stk {
39 if f := runtime.FuncForPC(addr); f != nil && (strings.HasPrefix(f.Name(), "runtime.") || strings.HasPrefix(f.Name(), "internal/runtime/")) {
40 continue
41 }
42
43 stk = stk[i:]
44 break
45 }
46 }
47 locs = b.appendLocsForStack(locs[:0], stk)
48 if len(locs) > 0 {
49 break
50 }
51 hideRuntime = false
52 }
53
54 values[0], values[1] = scaleHeapSample(r.AllocObjects, r.ObjectSize, rate)
55 values[2], values[3] = scaleHeapSample(r.InUseObjects(), r.ObjectSize, rate)
56 b.pbSample(values, locs, func() {
57 b.pbLabel(tagSample_Label, "bytes", "", r.ObjectSize)
58 })
59 }
60 return b.build()
61 }
62
63
64
65
66
67
68
69
70
71
72 func scaleHeapSample(count, avgSize, rate int64) (int64, int64) {
73 if count == 0 || avgSize == 0 {
74 return 0, 0
75 }
76
77 if rate <= 1 {
78
79
80 return count, count * avgSize
81 }
82
83 scale := 1 / (1 - math.Exp(-float64(avgSize)/float64(rate)))
84
85 return int64(float64(count) * scale), int64(float64(count*avgSize) * scale)
86 }
87
View as plain text