@@ -224,3 +224,103 @@ func testWindowsBuildmodeCSharedASLR(t *testing.T, useASLR bool) {
224
224
t .Error ("IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE flag should not be set" )
225
225
}
226
226
}
227
+
228
+ // TestMemProfileCheck tests that cmd/link sets
229
+ // runtime.disableMemoryProfiling if the runtime.MemProfile
230
+ // symbol is unreachable after deadcode (and not dynlinking).
231
+ // The runtime then uses that to set the default value of
232
+ // runtime.MemProfileRate, which this test checks.
233
+ func TestMemProfileCheck (t * testing.T ) {
234
+ testenv .MustHaveGoBuild (t )
235
+ t .Parallel ()
236
+
237
+ tests := []struct {
238
+ name string
239
+ prog string
240
+ wantOut string
241
+ }{
242
+ {
243
+ "no_memprofile" ,
244
+ `
245
+ package main
246
+ import "runtime"
247
+ func main() {
248
+ println(runtime.MemProfileRate)
249
+ }
250
+ ` ,
251
+ "0" ,
252
+ },
253
+ {
254
+ "with_memprofile" ,
255
+ `
256
+ package main
257
+ import "runtime"
258
+ func main() {
259
+ runtime.MemProfile(nil, false)
260
+ println(runtime.MemProfileRate)
261
+ }
262
+ ` ,
263
+ "524288" ,
264
+ },
265
+ {
266
+ "with_memprofile_indirect" ,
267
+ `
268
+ package main
269
+ import "runtime"
270
+ var f = runtime.MemProfile
271
+ func main() {
272
+ if f == nil {
273
+ panic("no f")
274
+ }
275
+ println(runtime.MemProfileRate)
276
+ }
277
+ ` ,
278
+ "524288" ,
279
+ },
280
+ {
281
+ "with_memprofile_runtime_pprof" ,
282
+ `
283
+ package main
284
+ import "runtime"
285
+ import "runtime/pprof"
286
+ func main() {
287
+ _ = pprof.Profiles()
288
+ println(runtime.MemProfileRate)
289
+ }
290
+ ` ,
291
+ "524288" ,
292
+ },
293
+ {
294
+ "with_memprofile_http_pprof" ,
295
+ `
296
+ package main
297
+ import "runtime"
298
+ import _ "net/http/pprof"
299
+ func main() {
300
+ println(runtime.MemProfileRate)
301
+ }
302
+ ` ,
303
+ "524288" ,
304
+ },
305
+ }
306
+ for _ , tt := range tests {
307
+ tt := tt
308
+ t .Run (tt .name , func (t * testing.T ) {
309
+ t .Parallel ()
310
+ tempDir := t .TempDir ()
311
+ src := filepath .Join (tempDir , "x.go" )
312
+ if err := ioutil .WriteFile (src , []byte (tt .prog ), 0644 ); err != nil {
313
+ t .Fatal (err )
314
+ }
315
+ cmd := exec .Command (testenv .GoToolPath (t ), "run" , src )
316
+ out , err := cmd .CombinedOutput ()
317
+ if err != nil {
318
+ t .Fatal (err )
319
+ }
320
+ got := strings .TrimSpace (string (out ))
321
+ if got != tt .wantOut {
322
+ t .Errorf ("got %q; want %q" , got , tt .wantOut )
323
+ }
324
+ })
325
+ }
326
+ }
0 commit comments