github.com/gidoBOSSftw5731/go/src@v0.0.0-20210226122457-d24b0edbf019/runtime/pprof/mprof_test.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !js
     6  // +build !js
     7  
     8  package pprof
     9  
    10  import (
    11  	"bytes"
    12  	"fmt"
    13  	"internal/profile"
    14  	"reflect"
    15  	"regexp"
    16  	"runtime"
    17  	"testing"
    18  	"unsafe"
    19  )
    20  
    21  var memSink interface{}
    22  
    23  func allocateTransient1M() {
    24  	for i := 0; i < 1024; i++ {
    25  		memSink = &struct{ x [1024]byte }{}
    26  	}
    27  }
    28  
    29  //go:noinline
    30  func allocateTransient2M() {
    31  	memSink = make([]byte, 2<<20)
    32  }
    33  
    34  func allocateTransient2MInline() {
    35  	memSink = make([]byte, 2<<20)
    36  }
    37  
    38  type Obj32 struct {
    39  	link *Obj32
    40  	pad  [32 - unsafe.Sizeof(uintptr(0))]byte
    41  }
    42  
    43  var persistentMemSink *Obj32
    44  
    45  func allocatePersistent1K() {
    46  	for i := 0; i < 32; i++ {
    47  		// Can't use slice because that will introduce implicit allocations.
    48  		obj := &Obj32{link: persistentMemSink}
    49  		persistentMemSink = obj
    50  	}
    51  }
    52  
    53  // Allocate transient memory using reflect.Call.
    54  
    55  func allocateReflectTransient() {
    56  	memSink = make([]byte, 2<<20)
    57  }
    58  
    59  func allocateReflect() {
    60  	rv := reflect.ValueOf(allocateReflectTransient)
    61  	rv.Call(nil)
    62  }
    63  
    64  var memoryProfilerRun = 0
    65  
    66  func TestMemoryProfiler(t *testing.T) {
    67  	// Disable sampling, otherwise it's difficult to assert anything.
    68  	oldRate := runtime.MemProfileRate
    69  	runtime.MemProfileRate = 1
    70  	defer func() {
    71  		runtime.MemProfileRate = oldRate
    72  	}()
    73  
    74  	// Allocate a meg to ensure that mcache.nextSample is updated to 1.
    75  	for i := 0; i < 1024; i++ {
    76  		memSink = make([]byte, 1024)
    77  	}
    78  
    79  	// Do the interesting allocations.
    80  	allocateTransient1M()
    81  	allocateTransient2M()
    82  	allocateTransient2MInline()
    83  	allocatePersistent1K()
    84  	allocateReflect()
    85  	memSink = nil
    86  
    87  	runtime.GC() // materialize stats
    88  
    89  	memoryProfilerRun++
    90  
    91  	tests := []struct {
    92  		stk    []string
    93  		legacy string
    94  	}{{
    95  		stk: []string{"runtime/pprof.allocatePersistent1K", "runtime/pprof.TestMemoryProfiler"},
    96  		legacy: fmt.Sprintf(`%v: %v \[%v: %v\] @ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+
    97  #	0x[0-9,a-f]+	runtime/pprof\.allocatePersistent1K\+0x[0-9,a-f]+	.*/runtime/pprof/mprof_test\.go:48
    98  #	0x[0-9,a-f]+	runtime/pprof\.TestMemoryProfiler\+0x[0-9,a-f]+	.*/runtime/pprof/mprof_test\.go:83
    99  `, 32*memoryProfilerRun, 1024*memoryProfilerRun, 32*memoryProfilerRun, 1024*memoryProfilerRun),
   100  	}, {
   101  		stk: []string{"runtime/pprof.allocateTransient1M", "runtime/pprof.TestMemoryProfiler"},
   102  		legacy: fmt.Sprintf(`0: 0 \[%v: %v\] @ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+
   103  #	0x[0-9,a-f]+	runtime/pprof\.allocateTransient1M\+0x[0-9,a-f]+	.*/runtime/pprof/mprof_test.go:25
   104  #	0x[0-9,a-f]+	runtime/pprof\.TestMemoryProfiler\+0x[0-9,a-f]+	.*/runtime/pprof/mprof_test.go:80
   105  `, (1<<10)*memoryProfilerRun, (1<<20)*memoryProfilerRun),
   106  	}, {
   107  		stk: []string{"runtime/pprof.allocateTransient2M", "runtime/pprof.TestMemoryProfiler"},
   108  		legacy: fmt.Sprintf(`0: 0 \[%v: %v\] @ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+
   109  #	0x[0-9,a-f]+	runtime/pprof\.allocateTransient2M\+0x[0-9,a-f]+	.*/runtime/pprof/mprof_test.go:31
   110  #	0x[0-9,a-f]+	runtime/pprof\.TestMemoryProfiler\+0x[0-9,a-f]+	.*/runtime/pprof/mprof_test.go:81
   111  `, memoryProfilerRun, (2<<20)*memoryProfilerRun),
   112  	}, {
   113  		stk: []string{"runtime/pprof.allocateTransient2MInline", "runtime/pprof.TestMemoryProfiler"},
   114  		legacy: fmt.Sprintf(`0: 0 \[%v: %v\] @ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+
   115  #	0x[0-9,a-f]+	runtime/pprof\.allocateTransient2MInline\+0x[0-9,a-f]+	.*/runtime/pprof/mprof_test.go:35
   116  #	0x[0-9,a-f]+	runtime/pprof\.TestMemoryProfiler\+0x[0-9,a-f]+	.*/runtime/pprof/mprof_test.go:82
   117  `, memoryProfilerRun, (2<<20)*memoryProfilerRun),
   118  	}, {
   119  		stk: []string{"runtime/pprof.allocateReflectTransient"},
   120  		legacy: fmt.Sprintf(`0: 0 \[%v: %v\] @( 0x[0-9,a-f]+)+
   121  #	0x[0-9,a-f]+	runtime/pprof\.allocateReflectTransient\+0x[0-9,a-f]+	.*/runtime/pprof/mprof_test.go:56
   122  `, memoryProfilerRun, (2<<20)*memoryProfilerRun),
   123  	}}
   124  
   125  	t.Run("debug=1", func(t *testing.T) {
   126  		var buf bytes.Buffer
   127  		if err := Lookup("heap").WriteTo(&buf, 1); err != nil {
   128  			t.Fatalf("failed to write heap profile: %v", err)
   129  		}
   130  
   131  		for _, test := range tests {
   132  			if !regexp.MustCompile(test.legacy).Match(buf.Bytes()) {
   133  				t.Fatalf("The entry did not match:\n%v\n\nProfile:\n%v\n", test.legacy, buf.String())
   134  			}
   135  		}
   136  	})
   137  
   138  	t.Run("proto", func(t *testing.T) {
   139  		var buf bytes.Buffer
   140  		if err := Lookup("heap").WriteTo(&buf, 0); err != nil {
   141  			t.Fatalf("failed to write heap profile: %v", err)
   142  		}
   143  		p, err := profile.Parse(&buf)
   144  		if err != nil {
   145  			t.Fatalf("failed to parse heap profile: %v", err)
   146  		}
   147  		t.Logf("Profile = %v", p)
   148  
   149  		stks := stacks(p)
   150  		for _, test := range tests {
   151  			if !containsStack(stks, test.stk) {
   152  				t.Fatalf("No matching stack entry for %q\n\nProfile:\n%v\n", test.stk, p)
   153  			}
   154  		}
   155  
   156  		if !containsInlinedCall(TestMemoryProfiler, 4<<10) {
   157  			t.Logf("Can't determine whether allocateTransient2MInline was inlined into TestMemoryProfiler.")
   158  			return
   159  		}
   160  
   161  		// Check the inlined function location is encoded correctly.
   162  		for _, loc := range p.Location {
   163  			inlinedCaller, inlinedCallee := false, false
   164  			for _, line := range loc.Line {
   165  				if line.Function.Name == "runtime/pprof.allocateTransient2MInline" {
   166  					inlinedCallee = true
   167  				}
   168  				if inlinedCallee && line.Function.Name == "runtime/pprof.TestMemoryProfiler" {
   169  					inlinedCaller = true
   170  				}
   171  			}
   172  			if inlinedCallee != inlinedCaller {
   173  				t.Errorf("want allocateTransient2MInline after TestMemoryProfiler in one location, got separate location entries:\n%v", loc)
   174  			}
   175  		}
   176  	})
   177  }