3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Test heap sampling logic.
21 var a64k *[64 * 1024]byte
23 // This test checks that heap sampling produces reasonable
24 // results. Note that heap sampling uses randomization, so the results
25 // vary for run to run. This test only checks that the resulting
26 // values appear reasonable.
28 const countInterleaved = 10000
29 allocInterleaved(countInterleaved)
30 checkAllocations(getMemProfileRecords(), "main.allocInterleaved", countInterleaved, []int64{256 * 1024, 1024, 256 * 1024, 512, 256 * 1024, 256})
34 checkAllocations(getMemProfileRecords(), "main.alloc", count, []int64{1024, 512, 256})
37 // allocInterleaved stress-tests the heap sampling logic by
38 // interleaving large and small allocations.
39 func allocInterleaved(n int) {
40 for i := 0; i < n; i++ {
41 // Test verification depends on these lines being contiguous.
42 a64k = new([64 * 1024]byte)
44 a64k = new([64 * 1024]byte)
46 a64k = new([64 * 1024]byte)
51 // alloc performs only small allocations for sanity testing.
53 for i := 0; i < n; i++ {
54 // Test verification depends on these lines being contiguous.
61 // checkAllocations validates that the profile records collected for
62 // the named function are consistent with count contiguous allocations
63 // of the specified sizes.
64 func checkAllocations(records []runtime.MemProfileRecord, fname string, count int64, size []int64) {
65 a := allocObjects(records, fname)
68 if firstLine == 0 || firstLine > ln {
73 for i, w := range size {
76 checkValue(fname, ln, "objects", count, s.objects)
77 checkValue(fname, ln, "bytes", count*w, s.bytes)
78 totalcount += s.objects
80 // Check the total number of allocations, to ensure some sampling occurred.
81 if totalwant := count * int64(len(size)); totalcount <= 0 || totalcount > totalwant*1024 {
82 panic(fmt.Sprintf("%s want total count > 0 && <= %d, got %d", fname, totalwant*1024, totalcount))
86 // checkValue checks an unsampled value against a range.
87 func checkValue(fname string, ln int, name string, want, got int64) {
88 if got < 0 || got > 1024*want {
89 panic(fmt.Sprintf("%s:%d want %s >= 0 && <= %d, got %d", fname, ln, name, 1024*want, got))
93 func getMemProfileRecords() []runtime.MemProfileRecord {
94 // Force the runtime to update the object and byte counts.
95 // This can take up to two GC cycles to get a complete
96 // snapshot of the current point in time.
100 // Find out how many records there are (MemProfile(nil, true)),
101 // allocate that many records, and get the data.
102 // There's a race—more records might be added between
103 // the two calls—so allocate a few extra records for safety
104 // and also try again if we're very unlucky.
105 // The loop should only execute one iteration in the common case.
106 var p []runtime.MemProfileRecord
107 n, ok := runtime.MemProfile(nil, true)
109 // Allocate room for a slightly bigger profile,
110 // in case a few more entries have been added
111 // since the call to MemProfile.
112 p = make([]runtime.MemProfileRecord, n+50)
113 n, ok = runtime.MemProfile(p, true)
118 // Profile grew; try again.
123 type allocStat struct {
127 // allocObjects examines the profile records for the named function
128 // and returns the allocation stats aggregated by source line number.
129 func allocObjects(records []runtime.MemProfileRecord, function string) map[int]allocStat {
130 a := make(map[int]allocStat)
131 for _, r := range records {
132 for _, s := range r.Stack0 {
136 if f := runtime.FuncForPC(s); f != nil {
138 _, line := f.FileLine(s)
139 if name == function {
141 allocStat.bytes += r.AllocBytes
142 allocStat.objects += r.AllocObjects
148 for line, stats := range a {
149 objects, bytes := scaleHeapSample(stats.objects, stats.bytes, int64(runtime.MemProfileRate))
150 a[line] = allocStat{bytes, objects}
155 // scaleHeapSample unsamples heap allocations.
156 // Taken from src/cmd/pprof/internal/profile/legacy_profile.go
157 func scaleHeapSample(count, size, rate int64) (int64, int64) {
158 if count == 0 || size == 0 {
163 // if rate==1 all samples were collected so no adjustment is needed.
164 // if rate<1 treat as unknown and skip scaling.
168 avgSize := float64(size) / float64(count)
169 scale := 1 / (1 - math.Exp(-avgSize/float64(rate)))
171 return int64(float64(count) * scale), int64(float64(size) * scale)