-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotomem_test.go
286 lines (252 loc) · 8.26 KB
/
protomem_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pprof
import (
"bytes"
"fmt"
"internal/asan"
"internal/profile"
"internal/profilerecord"
"internal/testenv"
"runtime"
"slices"
"strings"
"testing"
)
func TestConvertMemProfile(t *testing.T) {
addr1, addr2, map1, map2 := testPCs(t)
// MemProfileRecord stacks are return PCs, so add one to the
// addresses recorded in the "profile". The proto profile
// locations are call PCs, so conversion will subtract one
// from these and get back to addr1 and addr2.
a1, a2 := uintptr(addr1)+1, uintptr(addr2)+1
rate := int64(512 * 1024)
rec := []profilerecord.MemProfileRecord{
{AllocBytes: 4096, FreeBytes: 1024, AllocObjects: 4, FreeObjects: 1, Stack: []uintptr{a1, a2}},
{AllocBytes: 512 * 1024, FreeBytes: 0, AllocObjects: 1, FreeObjects: 0, Stack: []uintptr{a2 + 1, a2 + 2}},
{AllocBytes: 512 * 1024, FreeBytes: 512 * 1024, AllocObjects: 1, FreeObjects: 1, Stack: []uintptr{a1 + 1, a1 + 2, a2 + 3}},
}
periodType := &profile.ValueType{Type: "space", Unit: "bytes"}
sampleType := []*profile.ValueType{
{Type: "alloc_objects", Unit: "count"},
{Type: "alloc_space", Unit: "bytes"},
{Type: "inuse_objects", Unit: "count"},
{Type: "inuse_space", Unit: "bytes"},
}
samples := []*profile.Sample{
{
Value: []int64{2050, 2099200, 1537, 1574400},
Location: []*profile.Location{
{ID: 1, Mapping: map1, Address: addr1},
{ID: 2, Mapping: map2, Address: addr2},
},
NumLabel: map[string][]int64{"bytes": {1024}},
},
{
Value: []int64{1, 829411, 1, 829411},
Location: []*profile.Location{
{ID: 3, Mapping: map2, Address: addr2 + 1},
{ID: 4, Mapping: map2, Address: addr2 + 2},
},
NumLabel: map[string][]int64{"bytes": {512 * 1024}},
},
{
Value: []int64{1, 829411, 0, 0},
Location: []*profile.Location{
{ID: 5, Mapping: map1, Address: addr1 + 1},
{ID: 6, Mapping: map1, Address: addr1 + 2},
{ID: 7, Mapping: map2, Address: addr2 + 3},
},
NumLabel: map[string][]int64{"bytes": {512 * 1024}},
},
}
for _, tc := range []struct {
name string
defaultSampleType string
}{
{"heap", ""},
{"allocs", "alloc_space"},
} {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
if err := writeHeapProto(&buf, rec, rate, tc.defaultSampleType); err != nil {
t.Fatalf("writing profile: %v", err)
}
p, err := profile.Parse(&buf)
if err != nil {
t.Fatalf("profile.Parse: %v", err)
}
checkProfile(t, p, rate, periodType, sampleType, samples, tc.defaultSampleType)
})
}
}
func genericAllocFunc[T interface{ uint32 | uint64 }](n int) []T {
return make([]T, n)
}
func profileToStrings(p *profile.Profile) []string {
var res []string
for _, s := range p.Sample {
res = append(res, sampleToString(s))
}
return res
}
func sampleToString(s *profile.Sample) string {
var funcs []string
for i := len(s.Location) - 1; i >= 0; i-- {
loc := s.Location[i]
funcs = locationToStrings(loc, funcs)
}
return fmt.Sprintf("%s %v", strings.Join(funcs, ";"), s.Value)
}
func locationToStrings(loc *profile.Location, funcs []string) []string {
for j := range loc.Line {
line := loc.Line[len(loc.Line)-1-j]
funcs = append(funcs, line.Function.Name)
}
return funcs
}
// This is a regression test for https://go.dev/issue/64528.
func TestGenericsHashKeyInPprofBuilder(t *testing.T) {
if asan.Enabled {
t.Skip("extra allocations with -asan throw off the test; see #70079")
}
previousRate := runtime.MemProfileRate
runtime.MemProfileRate = 1
defer func() {
runtime.MemProfileRate = previousRate
}()
for _, sz := range []int{128, 256} {
genericAllocFunc[uint32](sz / 4)
}
for _, sz := range []int{64, 128} {
genericAllocFunc[uint64](sz / 8)
}
runtime.GC()
buf := bytes.NewBuffer(nil)
if err := WriteHeapProfile(buf); err != nil {
t.Fatalf("writing profile: %v", err)
}
p, err := profile.Parse(buf)
if err != nil {
t.Fatalf("profile.Parse: %v", err)
}
actual := profileToStrings(p)
expected := []string{
"testing.tRunner;runtime/pprof.TestGenericsHashKeyInPprofBuilder;runtime/pprof.genericAllocFunc[go.shape.uint32] [1 128 0 0]",
"testing.tRunner;runtime/pprof.TestGenericsHashKeyInPprofBuilder;runtime/pprof.genericAllocFunc[go.shape.uint32] [1 256 0 0]",
"testing.tRunner;runtime/pprof.TestGenericsHashKeyInPprofBuilder;runtime/pprof.genericAllocFunc[go.shape.uint64] [1 64 0 0]",
"testing.tRunner;runtime/pprof.TestGenericsHashKeyInPprofBuilder;runtime/pprof.genericAllocFunc[go.shape.uint64] [1 128 0 0]",
}
for _, l := range expected {
if !slices.Contains(actual, l) {
t.Errorf("profile = %v\nwant = %v", strings.Join(actual, "\n"), l)
}
}
}
type opAlloc struct {
buf [128]byte
}
type opCall struct {
}
var sink []byte
func storeAlloc() {
sink = make([]byte, 16)
}
func nonRecursiveGenericAllocFunction[CurrentOp any, OtherOp any](alloc bool) {
if alloc {
storeAlloc()
} else {
nonRecursiveGenericAllocFunction[OtherOp, CurrentOp](true)
}
}
func TestGenericsInlineLocations(t *testing.T) {
if asan.Enabled {
t.Skip("extra allocations with -asan throw off the test; see #70079")
}
if testenv.OptimizationOff() {
t.Skip("skipping test with optimizations disabled")
}
previousRate := runtime.MemProfileRate
runtime.MemProfileRate = 1
defer func() {
runtime.MemProfileRate = previousRate
sink = nil
}()
nonRecursiveGenericAllocFunction[opAlloc, opCall](true)
nonRecursiveGenericAllocFunction[opCall, opAlloc](false)
runtime.GC()
buf := bytes.NewBuffer(nil)
if err := WriteHeapProfile(buf); err != nil {
t.Fatalf("writing profile: %v", err)
}
p, err := profile.Parse(buf)
if err != nil {
t.Fatalf("profile.Parse: %v", err)
}
const expectedSample = "testing.tRunner;runtime/pprof.TestGenericsInlineLocations;runtime/pprof.nonRecursiveGenericAllocFunction[go.shape.struct {},go.shape.struct { runtime/pprof.buf [128]uint8 }];runtime/pprof.nonRecursiveGenericAllocFunction[go.shape.struct { runtime/pprof.buf [128]uint8 },go.shape.struct {}];runtime/pprof.storeAlloc [1 16 1 16]"
const expectedLocation = "runtime/pprof.nonRecursiveGenericAllocFunction[go.shape.struct {},go.shape.struct { runtime/pprof.buf [128]uint8 }];runtime/pprof.nonRecursiveGenericAllocFunction[go.shape.struct { runtime/pprof.buf [128]uint8 },go.shape.struct {}];runtime/pprof.storeAlloc"
const expectedLocationNewInliner = "runtime/pprof.TestGenericsInlineLocations;" + expectedLocation
var s *profile.Sample
for _, sample := range p.Sample {
if sampleToString(sample) == expectedSample {
s = sample
break
}
}
if s == nil {
t.Fatalf("expected \n%s\ngot\n%s", expectedSample, strings.Join(profileToStrings(p), "\n"))
}
loc := s.Location[0]
actual := strings.Join(locationToStrings(loc, nil), ";")
if expectedLocation != actual && expectedLocationNewInliner != actual {
t.Errorf("expected a location with at least 3 functions\n%s\ngot\n%s\n", expectedLocation, actual)
}
}
func growMap() {
m := make(map[int]int)
for i := range 512 {
m[i] = i
}
}
// Runtime frames are hidden in heap profiles.
// This is a regression test for https://go.dev/issue/71174.
func TestHeapRuntimeFrames(t *testing.T) {
previousRate := runtime.MemProfileRate
runtime.MemProfileRate = 1
defer func() {
runtime.MemProfileRate = previousRate
}()
growMap()
runtime.GC()
buf := bytes.NewBuffer(nil)
if err := WriteHeapProfile(buf); err != nil {
t.Fatalf("writing profile: %v", err)
}
p, err := profile.Parse(buf)
if err != nil {
t.Fatalf("profile.Parse: %v", err)
}
actual := profileToStrings(p)
// We must see growMap at least once.
foundGrowMap := false
for _, l := range actual {
if !strings.Contains(l, "runtime/pprof.growMap") {
continue
}
foundGrowMap = true
// Runtime frames like mapassign and map internals should be hidden.
if strings.Contains(l, "runtime.") {
t.Errorf("Sample got %s, want no runtime frames", l)
}
if strings.Contains(l, "internal/runtime/") {
t.Errorf("Sample got %s, want no runtime frames", l)
}
if strings.Contains(l, "mapassign") { // in case mapassign moves to a package not matching above paths.
t.Errorf("Sample got %s, want no mapassign frames", l)
}
}
if !foundGrowMap {
t.Errorf("Profile got:\n%s\nwant sample in runtime/pprof.growMap", strings.Join(actual, "\n"))
}
}