| |
| |
| |
|
|
| package pprof |
|
|
| import ( |
| "internal/profilerecord" |
| "io" |
| "math" |
| "runtime" |
| "strings" |
| ) |
|
|
| |
| func writeHeapProto(w io.Writer, p []profilerecord.MemProfileRecord, rate int64, defaultSampleType string) error { |
| b := newProfileBuilder(w) |
| b.pbValueType(tagProfile_PeriodType, "space", "bytes") |
| b.pb.int64Opt(tagProfile_Period, rate) |
| b.pbValueType(tagProfile_SampleType, "alloc_objects", "count") |
| b.pbValueType(tagProfile_SampleType, "alloc_space", "bytes") |
| b.pbValueType(tagProfile_SampleType, "inuse_objects", "count") |
| b.pbValueType(tagProfile_SampleType, "inuse_space", "bytes") |
| if defaultSampleType != "" { |
| b.pb.int64Opt(tagProfile_DefaultSampleType, b.stringIndex(defaultSampleType)) |
| } |
|
|
| values := []int64{0, 0, 0, 0} |
| var locs []uint64 |
| for _, r := range p { |
| hideRuntime := true |
| for tries := 0; tries < 2; tries++ { |
| stk := r.Stack |
| |
| |
| |
| if hideRuntime { |
| for i, addr := range stk { |
| if f := runtime.FuncForPC(addr); f != nil && (strings.HasPrefix(f.Name(), "runtime.") || strings.HasPrefix(f.Name(), "internal/runtime/")) { |
| continue |
| } |
| |
| stk = stk[i:] |
| break |
| } |
| } |
| locs = b.appendLocsForStack(locs[:0], stk) |
| if len(locs) > 0 { |
| break |
| } |
| hideRuntime = false |
| } |
|
|
| values[0], values[1] = scaleHeapSample(r.AllocObjects, r.AllocBytes, rate) |
| values[2], values[3] = scaleHeapSample(r.InUseObjects(), r.InUseBytes(), rate) |
| var blockSize int64 |
| if r.AllocObjects > 0 { |
| blockSize = r.AllocBytes / r.AllocObjects |
| } |
| b.pbSample(values, locs, func() { |
| if blockSize != 0 { |
| b.pbLabel(tagSample_Label, "bytes", "", blockSize) |
| } |
| }) |
| } |
| return b.build() |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func scaleHeapSample(count, size, rate int64) (int64, int64) { |
| if count == 0 || size == 0 { |
| return 0, 0 |
| } |
|
|
| if rate <= 1 { |
| |
| |
| return count, size |
| } |
|
|
| avgSize := float64(size) / float64(count) |
| scale := 1 / (1 - math.Exp(-avgSize/float64(rate))) |
|
|
| return int64(float64(count) * scale), int64(float64(size) * scale) |
| } |
|
|