github.com/AESNooper/go/src@v0.0.0-20220218095104-b56a4ab1bbbb/runtime/gc_test.go (about)

     1  // Copyright 2011 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  package runtime_test
     6  
     7  import (
     8  	"fmt"
     9  	"math/rand"
    10  	"os"
    11  	"reflect"
    12  	"runtime"
    13  	"runtime/debug"
    14  	"sort"
    15  	"strings"
    16  	"sync"
    17  	"sync/atomic"
    18  	"testing"
    19  	"time"
    20  	"unsafe"
    21  )
    22  
    23  func TestGcSys(t *testing.T) {
    24  	t.Skip("skipping known-flaky test; golang.org/issue/37331")
    25  	if os.Getenv("GOGC") == "off" {
    26  		t.Skip("skipping test; GOGC=off in environment")
    27  	}
    28  	got := runTestProg(t, "testprog", "GCSys")
    29  	want := "OK\n"
    30  	if got != want {
    31  		t.Fatalf("expected %q, but got %q", want, got)
    32  	}
    33  }
    34  
    35  func TestGcDeepNesting(t *testing.T) {
    36  	type T [2][2][2][2][2][2][2][2][2][2]*int
    37  	a := new(T)
    38  
    39  	// Prevent the compiler from applying escape analysis.
    40  	// This makes sure new(T) is allocated on heap, not on the stack.
    41  	t.Logf("%p", a)
    42  
    43  	a[0][0][0][0][0][0][0][0][0][0] = new(int)
    44  	*a[0][0][0][0][0][0][0][0][0][0] = 13
    45  	runtime.GC()
    46  	if *a[0][0][0][0][0][0][0][0][0][0] != 13 {
    47  		t.Fail()
    48  	}
    49  }
    50  
    51  func TestGcMapIndirection(t *testing.T) {
    52  	defer debug.SetGCPercent(debug.SetGCPercent(1))
    53  	runtime.GC()
    54  	type T struct {
    55  		a [256]int
    56  	}
    57  	m := make(map[T]T)
    58  	for i := 0; i < 2000; i++ {
    59  		var a T
    60  		a.a[0] = i
    61  		m[a] = T{}
    62  	}
    63  }
    64  
    65  func TestGcArraySlice(t *testing.T) {
    66  	type X struct {
    67  		buf     [1]byte
    68  		nextbuf []byte
    69  		next    *X
    70  	}
    71  	var head *X
    72  	for i := 0; i < 10; i++ {
    73  		p := &X{}
    74  		p.buf[0] = 42
    75  		p.next = head
    76  		if head != nil {
    77  			p.nextbuf = head.buf[:]
    78  		}
    79  		head = p
    80  		runtime.GC()
    81  	}
    82  	for p := head; p != nil; p = p.next {
    83  		if p.buf[0] != 42 {
    84  			t.Fatal("corrupted heap")
    85  		}
    86  	}
    87  }
    88  
    89  func TestGcRescan(t *testing.T) {
    90  	type X struct {
    91  		c     chan error
    92  		nextx *X
    93  	}
    94  	type Y struct {
    95  		X
    96  		nexty *Y
    97  		p     *int
    98  	}
    99  	var head *Y
   100  	for i := 0; i < 10; i++ {
   101  		p := &Y{}
   102  		p.c = make(chan error)
   103  		if head != nil {
   104  			p.nextx = &head.X
   105  		}
   106  		p.nexty = head
   107  		p.p = new(int)
   108  		*p.p = 42
   109  		head = p
   110  		runtime.GC()
   111  	}
   112  	for p := head; p != nil; p = p.nexty {
   113  		if *p.p != 42 {
   114  			t.Fatal("corrupted heap")
   115  		}
   116  	}
   117  }
   118  
   119  func TestGcLastTime(t *testing.T) {
   120  	ms := new(runtime.MemStats)
   121  	t0 := time.Now().UnixNano()
   122  	runtime.GC()
   123  	t1 := time.Now().UnixNano()
   124  	runtime.ReadMemStats(ms)
   125  	last := int64(ms.LastGC)
   126  	if t0 > last || last > t1 {
   127  		t.Fatalf("bad last GC time: got %v, want [%v, %v]", last, t0, t1)
   128  	}
   129  	pause := ms.PauseNs[(ms.NumGC+255)%256]
   130  	// Due to timer granularity, pause can actually be 0 on windows
   131  	// or on virtualized environments.
   132  	if pause == 0 {
   133  		t.Logf("last GC pause was 0")
   134  	} else if pause > 10e9 {
   135  		t.Logf("bad last GC pause: got %v, want [0, 10e9]", pause)
   136  	}
   137  }
   138  
   139  var hugeSink interface{}
   140  
   141  func TestHugeGCInfo(t *testing.T) {
   142  	// The test ensures that compiler can chew these huge types even on weakest machines.
   143  	// The types are not allocated at runtime.
   144  	if hugeSink != nil {
   145  		// 400MB on 32 bots, 4TB on 64-bits.
   146  		const n = (400 << 20) + (unsafe.Sizeof(uintptr(0))-4)<<40
   147  		hugeSink = new([n]*byte)
   148  		hugeSink = new([n]uintptr)
   149  		hugeSink = new(struct {
   150  			x float64
   151  			y [n]*byte
   152  			z []string
   153  		})
   154  		hugeSink = new(struct {
   155  			x float64
   156  			y [n]uintptr
   157  			z []string
   158  		})
   159  	}
   160  }
   161  
   162  func TestPeriodicGC(t *testing.T) {
   163  	if runtime.GOARCH == "wasm" {
   164  		t.Skip("no sysmon on wasm yet")
   165  	}
   166  
   167  	// Make sure we're not in the middle of a GC.
   168  	runtime.GC()
   169  
   170  	var ms1, ms2 runtime.MemStats
   171  	runtime.ReadMemStats(&ms1)
   172  
   173  	// Make periodic GC run continuously.
   174  	orig := *runtime.ForceGCPeriod
   175  	*runtime.ForceGCPeriod = 0
   176  
   177  	// Let some periodic GCs happen. In a heavily loaded system,
   178  	// it's possible these will be delayed, so this is designed to
   179  	// succeed quickly if things are working, but to give it some
   180  	// slack if things are slow.
   181  	var numGCs uint32
   182  	const want = 2
   183  	for i := 0; i < 200 && numGCs < want; i++ {
   184  		time.Sleep(5 * time.Millisecond)
   185  
   186  		// Test that periodic GC actually happened.
   187  		runtime.ReadMemStats(&ms2)
   188  		numGCs = ms2.NumGC - ms1.NumGC
   189  	}
   190  	*runtime.ForceGCPeriod = orig
   191  
   192  	if numGCs < want {
   193  		t.Fatalf("no periodic GC: got %v GCs, want >= 2", numGCs)
   194  	}
   195  }
   196  
   197  func TestGcZombieReporting(t *testing.T) {
   198  	// This test is somewhat sensitive to how the allocator works.
   199  	// Pointers in zombies slice may cross-span, thus we
   200  	// add invalidptr=0 for avoiding the badPointer check.
   201  	// See issue https://golang.org/issues/49613/
   202  	got := runTestProg(t, "testprog", "GCZombie", "GODEBUG=invalidptr=0")
   203  	want := "found pointer to free object"
   204  	if !strings.Contains(got, want) {
   205  		t.Fatalf("expected %q in output, but got %q", want, got)
   206  	}
   207  }
   208  
   209  func TestGCTestMoveStackOnNextCall(t *testing.T) {
   210  	t.Parallel()
   211  	var onStack int
   212  	// GCTestMoveStackOnNextCall can fail in rare cases if there's
   213  	// a preemption. This won't happen many times in quick
   214  	// succession, so just retry a few times.
   215  	for retry := 0; retry < 5; retry++ {
   216  		runtime.GCTestMoveStackOnNextCall()
   217  		if moveStackCheck(t, &onStack, uintptr(unsafe.Pointer(&onStack))) {
   218  			// Passed.
   219  			return
   220  		}
   221  	}
   222  	t.Fatal("stack did not move")
   223  }
   224  
   225  // This must not be inlined because the point is to force a stack
   226  // growth check and move the stack.
   227  //
   228  //go:noinline
   229  func moveStackCheck(t *testing.T, new *int, old uintptr) bool {
   230  	// new should have been updated by the stack move;
   231  	// old should not have.
   232  
   233  	// Capture new's value before doing anything that could
   234  	// further move the stack.
   235  	new2 := uintptr(unsafe.Pointer(new))
   236  
   237  	t.Logf("old stack pointer %x, new stack pointer %x", old, new2)
   238  	if new2 == old {
   239  		// Check that we didn't screw up the test's escape analysis.
   240  		if cls := runtime.GCTestPointerClass(unsafe.Pointer(new)); cls != "stack" {
   241  			t.Fatalf("test bug: new (%#x) should be a stack pointer, not %s", new2, cls)
   242  		}
   243  		// This was a real failure.
   244  		return false
   245  	}
   246  	return true
   247  }
   248  
   249  func TestGCTestMoveStackRepeatedly(t *testing.T) {
   250  	// Move the stack repeatedly to make sure we're not doubling
   251  	// it each time.
   252  	for i := 0; i < 100; i++ {
   253  		runtime.GCTestMoveStackOnNextCall()
   254  		moveStack1(false)
   255  	}
   256  }
   257  
   258  //go:noinline
   259  func moveStack1(x bool) {
   260  	// Make sure this function doesn't get auto-nosplit.
   261  	if x {
   262  		println("x")
   263  	}
   264  }
   265  
   266  func TestGCTestIsReachable(t *testing.T) {
   267  	var all, half []unsafe.Pointer
   268  	var want uint64
   269  	for i := 0; i < 16; i++ {
   270  		// The tiny allocator muddies things, so we use a
   271  		// scannable type.
   272  		p := unsafe.Pointer(new(*int))
   273  		all = append(all, p)
   274  		if i%2 == 0 {
   275  			half = append(half, p)
   276  			want |= 1 << i
   277  		}
   278  	}
   279  
   280  	got := runtime.GCTestIsReachable(all...)
   281  	if want != got {
   282  		t.Fatalf("did not get expected reachable set; want %b, got %b", want, got)
   283  	}
   284  	runtime.KeepAlive(half)
   285  }
   286  
   287  var pointerClassSink *int
   288  var pointerClassData = 42
   289  
   290  func TestGCTestPointerClass(t *testing.T) {
   291  	t.Parallel()
   292  	check := func(p unsafe.Pointer, want string) {
   293  		t.Helper()
   294  		got := runtime.GCTestPointerClass(p)
   295  		if got != want {
   296  			// Convert the pointer to a uintptr to avoid
   297  			// escaping it.
   298  			t.Errorf("for %#x, want class %s, got %s", uintptr(p), want, got)
   299  		}
   300  	}
   301  	var onStack int
   302  	var notOnStack int
   303  	pointerClassSink = &notOnStack
   304  	check(unsafe.Pointer(&onStack), "stack")
   305  	check(unsafe.Pointer(&notOnStack), "heap")
   306  	check(unsafe.Pointer(&pointerClassSink), "bss")
   307  	check(unsafe.Pointer(&pointerClassData), "data")
   308  	check(nil, "other")
   309  }
   310  
   311  func BenchmarkSetTypePtr(b *testing.B) {
   312  	benchSetType(b, new(*byte))
   313  }
   314  
   315  func BenchmarkSetTypePtr8(b *testing.B) {
   316  	benchSetType(b, new([8]*byte))
   317  }
   318  
   319  func BenchmarkSetTypePtr16(b *testing.B) {
   320  	benchSetType(b, new([16]*byte))
   321  }
   322  
   323  func BenchmarkSetTypePtr32(b *testing.B) {
   324  	benchSetType(b, new([32]*byte))
   325  }
   326  
   327  func BenchmarkSetTypePtr64(b *testing.B) {
   328  	benchSetType(b, new([64]*byte))
   329  }
   330  
   331  func BenchmarkSetTypePtr126(b *testing.B) {
   332  	benchSetType(b, new([126]*byte))
   333  }
   334  
   335  func BenchmarkSetTypePtr128(b *testing.B) {
   336  	benchSetType(b, new([128]*byte))
   337  }
   338  
   339  func BenchmarkSetTypePtrSlice(b *testing.B) {
   340  	benchSetType(b, make([]*byte, 1<<10))
   341  }
   342  
   343  type Node1 struct {
   344  	Value       [1]uintptr
   345  	Left, Right *byte
   346  }
   347  
   348  func BenchmarkSetTypeNode1(b *testing.B) {
   349  	benchSetType(b, new(Node1))
   350  }
   351  
   352  func BenchmarkSetTypeNode1Slice(b *testing.B) {
   353  	benchSetType(b, make([]Node1, 32))
   354  }
   355  
   356  type Node8 struct {
   357  	Value       [8]uintptr
   358  	Left, Right *byte
   359  }
   360  
   361  func BenchmarkSetTypeNode8(b *testing.B) {
   362  	benchSetType(b, new(Node8))
   363  }
   364  
   365  func BenchmarkSetTypeNode8Slice(b *testing.B) {
   366  	benchSetType(b, make([]Node8, 32))
   367  }
   368  
   369  type Node64 struct {
   370  	Value       [64]uintptr
   371  	Left, Right *byte
   372  }
   373  
   374  func BenchmarkSetTypeNode64(b *testing.B) {
   375  	benchSetType(b, new(Node64))
   376  }
   377  
   378  func BenchmarkSetTypeNode64Slice(b *testing.B) {
   379  	benchSetType(b, make([]Node64, 32))
   380  }
   381  
   382  type Node64Dead struct {
   383  	Left, Right *byte
   384  	Value       [64]uintptr
   385  }
   386  
   387  func BenchmarkSetTypeNode64Dead(b *testing.B) {
   388  	benchSetType(b, new(Node64Dead))
   389  }
   390  
   391  func BenchmarkSetTypeNode64DeadSlice(b *testing.B) {
   392  	benchSetType(b, make([]Node64Dead, 32))
   393  }
   394  
   395  type Node124 struct {
   396  	Value       [124]uintptr
   397  	Left, Right *byte
   398  }
   399  
   400  func BenchmarkSetTypeNode124(b *testing.B) {
   401  	benchSetType(b, new(Node124))
   402  }
   403  
   404  func BenchmarkSetTypeNode124Slice(b *testing.B) {
   405  	benchSetType(b, make([]Node124, 32))
   406  }
   407  
   408  type Node126 struct {
   409  	Value       [126]uintptr
   410  	Left, Right *byte
   411  }
   412  
   413  func BenchmarkSetTypeNode126(b *testing.B) {
   414  	benchSetType(b, new(Node126))
   415  }
   416  
   417  func BenchmarkSetTypeNode126Slice(b *testing.B) {
   418  	benchSetType(b, make([]Node126, 32))
   419  }
   420  
   421  type Node128 struct {
   422  	Value       [128]uintptr
   423  	Left, Right *byte
   424  }
   425  
   426  func BenchmarkSetTypeNode128(b *testing.B) {
   427  	benchSetType(b, new(Node128))
   428  }
   429  
   430  func BenchmarkSetTypeNode128Slice(b *testing.B) {
   431  	benchSetType(b, make([]Node128, 32))
   432  }
   433  
   434  type Node130 struct {
   435  	Value       [130]uintptr
   436  	Left, Right *byte
   437  }
   438  
   439  func BenchmarkSetTypeNode130(b *testing.B) {
   440  	benchSetType(b, new(Node130))
   441  }
   442  
   443  func BenchmarkSetTypeNode130Slice(b *testing.B) {
   444  	benchSetType(b, make([]Node130, 32))
   445  }
   446  
   447  type Node1024 struct {
   448  	Value       [1024]uintptr
   449  	Left, Right *byte
   450  }
   451  
   452  func BenchmarkSetTypeNode1024(b *testing.B) {
   453  	benchSetType(b, new(Node1024))
   454  }
   455  
   456  func BenchmarkSetTypeNode1024Slice(b *testing.B) {
   457  	benchSetType(b, make([]Node1024, 32))
   458  }
   459  
   460  func benchSetType(b *testing.B, x interface{}) {
   461  	v := reflect.ValueOf(x)
   462  	t := v.Type()
   463  	switch t.Kind() {
   464  	case reflect.Pointer:
   465  		b.SetBytes(int64(t.Elem().Size()))
   466  	case reflect.Slice:
   467  		b.SetBytes(int64(t.Elem().Size()) * int64(v.Len()))
   468  	}
   469  	b.ResetTimer()
   470  	runtime.BenchSetType(b.N, x)
   471  }
   472  
   473  func BenchmarkAllocation(b *testing.B) {
   474  	type T struct {
   475  		x, y *byte
   476  	}
   477  	ngo := runtime.GOMAXPROCS(0)
   478  	work := make(chan bool, b.N+ngo)
   479  	result := make(chan *T)
   480  	for i := 0; i < b.N; i++ {
   481  		work <- true
   482  	}
   483  	for i := 0; i < ngo; i++ {
   484  		work <- false
   485  	}
   486  	for i := 0; i < ngo; i++ {
   487  		go func() {
   488  			var x *T
   489  			for <-work {
   490  				for i := 0; i < 1000; i++ {
   491  					x = &T{}
   492  				}
   493  			}
   494  			result <- x
   495  		}()
   496  	}
   497  	for i := 0; i < ngo; i++ {
   498  		<-result
   499  	}
   500  }
   501  
   502  func TestPrintGC(t *testing.T) {
   503  	if testing.Short() {
   504  		t.Skip("Skipping in short mode")
   505  	}
   506  	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2))
   507  	done := make(chan bool)
   508  	go func() {
   509  		for {
   510  			select {
   511  			case <-done:
   512  				return
   513  			default:
   514  				runtime.GC()
   515  			}
   516  		}
   517  	}()
   518  	for i := 0; i < 1e4; i++ {
   519  		func() {
   520  			defer print("")
   521  		}()
   522  	}
   523  	close(done)
   524  }
   525  
   526  func testTypeSwitch(x interface{}) error {
   527  	switch y := x.(type) {
   528  	case nil:
   529  		// ok
   530  	case error:
   531  		return y
   532  	}
   533  	return nil
   534  }
   535  
   536  func testAssert(x interface{}) error {
   537  	if y, ok := x.(error); ok {
   538  		return y
   539  	}
   540  	return nil
   541  }
   542  
   543  func testAssertVar(x interface{}) error {
   544  	var y, ok = x.(error)
   545  	if ok {
   546  		return y
   547  	}
   548  	return nil
   549  }
   550  
   551  var a bool
   552  
   553  //go:noinline
   554  func testIfaceEqual(x interface{}) {
   555  	if x == "abc" {
   556  		a = true
   557  	}
   558  }
   559  
   560  func TestPageAccounting(t *testing.T) {
   561  	// Grow the heap in small increments. This used to drop the
   562  	// pages-in-use count below zero because of a rounding
   563  	// mismatch (golang.org/issue/15022).
   564  	const blockSize = 64 << 10
   565  	blocks := make([]*[blockSize]byte, (64<<20)/blockSize)
   566  	for i := range blocks {
   567  		blocks[i] = new([blockSize]byte)
   568  	}
   569  
   570  	// Check that the running page count matches reality.
   571  	pagesInUse, counted := runtime.CountPagesInUse()
   572  	if pagesInUse != counted {
   573  		t.Fatalf("mheap_.pagesInUse is %d, but direct count is %d", pagesInUse, counted)
   574  	}
   575  }
   576  
   577  func TestReadMemStats(t *testing.T) {
   578  	base, slow := runtime.ReadMemStatsSlow()
   579  	if base != slow {
   580  		logDiff(t, "MemStats", reflect.ValueOf(base), reflect.ValueOf(slow))
   581  		t.Fatal("memstats mismatch")
   582  	}
   583  }
   584  
   585  func logDiff(t *testing.T, prefix string, got, want reflect.Value) {
   586  	typ := got.Type()
   587  	switch typ.Kind() {
   588  	case reflect.Array, reflect.Slice:
   589  		if got.Len() != want.Len() {
   590  			t.Logf("len(%s): got %v, want %v", prefix, got, want)
   591  			return
   592  		}
   593  		for i := 0; i < got.Len(); i++ {
   594  			logDiff(t, fmt.Sprintf("%s[%d]", prefix, i), got.Index(i), want.Index(i))
   595  		}
   596  	case reflect.Struct:
   597  		for i := 0; i < typ.NumField(); i++ {
   598  			gf, wf := got.Field(i), want.Field(i)
   599  			logDiff(t, prefix+"."+typ.Field(i).Name, gf, wf)
   600  		}
   601  	case reflect.Map:
   602  		t.Fatal("not implemented: logDiff for map")
   603  	default:
   604  		if got.Interface() != want.Interface() {
   605  			t.Logf("%s: got %v, want %v", prefix, got, want)
   606  		}
   607  	}
   608  }
   609  
   610  func BenchmarkReadMemStats(b *testing.B) {
   611  	var ms runtime.MemStats
   612  	const heapSize = 100 << 20
   613  	x := make([]*[1024]byte, heapSize/1024)
   614  	for i := range x {
   615  		x[i] = new([1024]byte)
   616  	}
   617  	hugeSink = x
   618  
   619  	b.ResetTimer()
   620  	for i := 0; i < b.N; i++ {
   621  		runtime.ReadMemStats(&ms)
   622  	}
   623  
   624  	hugeSink = nil
   625  }
   626  
   627  func applyGCLoad(b *testing.B) func() {
   628  	// We’ll apply load to the runtime with maxProcs-1 goroutines
   629  	// and use one more to actually benchmark. It doesn't make sense
   630  	// to try to run this test with only 1 P (that's what
   631  	// BenchmarkReadMemStats is for).
   632  	maxProcs := runtime.GOMAXPROCS(-1)
   633  	if maxProcs == 1 {
   634  		b.Skip("This benchmark can only be run with GOMAXPROCS > 1")
   635  	}
   636  
   637  	// Code to build a big tree with lots of pointers.
   638  	type node struct {
   639  		children [16]*node
   640  	}
   641  	var buildTree func(depth int) *node
   642  	buildTree = func(depth int) *node {
   643  		tree := new(node)
   644  		if depth != 0 {
   645  			for i := range tree.children {
   646  				tree.children[i] = buildTree(depth - 1)
   647  			}
   648  		}
   649  		return tree
   650  	}
   651  
   652  	// Keep the GC busy by continuously generating large trees.
   653  	done := make(chan struct{})
   654  	var wg sync.WaitGroup
   655  	for i := 0; i < maxProcs-1; i++ {
   656  		wg.Add(1)
   657  		go func() {
   658  			defer wg.Done()
   659  			var hold *node
   660  		loop:
   661  			for {
   662  				hold = buildTree(5)
   663  				select {
   664  				case <-done:
   665  					break loop
   666  				default:
   667  				}
   668  			}
   669  			runtime.KeepAlive(hold)
   670  		}()
   671  	}
   672  	return func() {
   673  		close(done)
   674  		wg.Wait()
   675  	}
   676  }
   677  
   678  func BenchmarkReadMemStatsLatency(b *testing.B) {
   679  	stop := applyGCLoad(b)
   680  
   681  	// Spend this much time measuring latencies.
   682  	latencies := make([]time.Duration, 0, 1024)
   683  
   684  	// Run for timeToBench hitting ReadMemStats continuously
   685  	// and measuring the latency.
   686  	b.ResetTimer()
   687  	var ms runtime.MemStats
   688  	for i := 0; i < b.N; i++ {
   689  		// Sleep for a bit, otherwise we're just going to keep
   690  		// stopping the world and no one will get to do anything.
   691  		time.Sleep(100 * time.Millisecond)
   692  		start := time.Now()
   693  		runtime.ReadMemStats(&ms)
   694  		latencies = append(latencies, time.Now().Sub(start))
   695  	}
   696  	// Make sure to stop the timer before we wait! The load created above
   697  	// is very heavy-weight and not easy to stop, so we could end up
   698  	// confusing the benchmarking framework for small b.N.
   699  	b.StopTimer()
   700  	stop()
   701  
   702  	// Disable the default */op metrics.
   703  	// ns/op doesn't mean anything because it's an average, but we
   704  	// have a sleep in our b.N loop above which skews this significantly.
   705  	b.ReportMetric(0, "ns/op")
   706  	b.ReportMetric(0, "B/op")
   707  	b.ReportMetric(0, "allocs/op")
   708  
   709  	// Sort latencies then report percentiles.
   710  	sort.Slice(latencies, func(i, j int) bool {
   711  		return latencies[i] < latencies[j]
   712  	})
   713  	b.ReportMetric(float64(latencies[len(latencies)*50/100]), "p50-ns")
   714  	b.ReportMetric(float64(latencies[len(latencies)*90/100]), "p90-ns")
   715  	b.ReportMetric(float64(latencies[len(latencies)*99/100]), "p99-ns")
   716  }
   717  
   718  func TestUserForcedGC(t *testing.T) {
   719  	// Test that runtime.GC() triggers a GC even if GOGC=off.
   720  	defer debug.SetGCPercent(debug.SetGCPercent(-1))
   721  
   722  	var ms1, ms2 runtime.MemStats
   723  	runtime.ReadMemStats(&ms1)
   724  	runtime.GC()
   725  	runtime.ReadMemStats(&ms2)
   726  	if ms1.NumGC == ms2.NumGC {
   727  		t.Fatalf("runtime.GC() did not trigger GC")
   728  	}
   729  	if ms1.NumForcedGC == ms2.NumForcedGC {
   730  		t.Fatalf("runtime.GC() was not accounted in NumForcedGC")
   731  	}
   732  }
   733  
   734  func writeBarrierBenchmark(b *testing.B, f func()) {
   735  	runtime.GC()
   736  	var ms runtime.MemStats
   737  	runtime.ReadMemStats(&ms)
   738  	//b.Logf("heap size: %d MB", ms.HeapAlloc>>20)
   739  
   740  	// Keep GC running continuously during the benchmark, which in
   741  	// turn keeps the write barrier on continuously.
   742  	var stop uint32
   743  	done := make(chan bool)
   744  	go func() {
   745  		for atomic.LoadUint32(&stop) == 0 {
   746  			runtime.GC()
   747  		}
   748  		close(done)
   749  	}()
   750  	defer func() {
   751  		atomic.StoreUint32(&stop, 1)
   752  		<-done
   753  	}()
   754  
   755  	b.ResetTimer()
   756  	f()
   757  	b.StopTimer()
   758  }
   759  
   760  func BenchmarkWriteBarrier(b *testing.B) {
   761  	if runtime.GOMAXPROCS(-1) < 2 {
   762  		// We don't want GC to take our time.
   763  		b.Skip("need GOMAXPROCS >= 2")
   764  	}
   765  
   766  	// Construct a large tree both so the GC runs for a while and
   767  	// so we have a data structure to manipulate the pointers of.
   768  	type node struct {
   769  		l, r *node
   770  	}
   771  	var wbRoots []*node
   772  	var mkTree func(level int) *node
   773  	mkTree = func(level int) *node {
   774  		if level == 0 {
   775  			return nil
   776  		}
   777  		n := &node{mkTree(level - 1), mkTree(level - 1)}
   778  		if level == 10 {
   779  			// Seed GC with enough early pointers so it
   780  			// doesn't start termination barriers when it
   781  			// only has the top of the tree.
   782  			wbRoots = append(wbRoots, n)
   783  		}
   784  		return n
   785  	}
   786  	const depth = 22 // 64 MB
   787  	root := mkTree(22)
   788  
   789  	writeBarrierBenchmark(b, func() {
   790  		var stack [depth]*node
   791  		tos := -1
   792  
   793  		// There are two write barriers per iteration, so i+=2.
   794  		for i := 0; i < b.N; i += 2 {
   795  			if tos == -1 {
   796  				stack[0] = root
   797  				tos = 0
   798  			}
   799  
   800  			// Perform one step of reversing the tree.
   801  			n := stack[tos]
   802  			if n.l == nil {
   803  				tos--
   804  			} else {
   805  				n.l, n.r = n.r, n.l
   806  				stack[tos] = n.l
   807  				stack[tos+1] = n.r
   808  				tos++
   809  			}
   810  
   811  			if i%(1<<12) == 0 {
   812  				// Avoid non-preemptible loops (see issue #10958).
   813  				runtime.Gosched()
   814  			}
   815  		}
   816  	})
   817  
   818  	runtime.KeepAlive(wbRoots)
   819  }
   820  
   821  func BenchmarkBulkWriteBarrier(b *testing.B) {
   822  	if runtime.GOMAXPROCS(-1) < 2 {
   823  		// We don't want GC to take our time.
   824  		b.Skip("need GOMAXPROCS >= 2")
   825  	}
   826  
   827  	// Construct a large set of objects we can copy around.
   828  	const heapSize = 64 << 20
   829  	type obj [16]*byte
   830  	ptrs := make([]*obj, heapSize/unsafe.Sizeof(obj{}))
   831  	for i := range ptrs {
   832  		ptrs[i] = new(obj)
   833  	}
   834  
   835  	writeBarrierBenchmark(b, func() {
   836  		const blockSize = 1024
   837  		var pos int
   838  		for i := 0; i < b.N; i += blockSize {
   839  			// Rotate block.
   840  			block := ptrs[pos : pos+blockSize]
   841  			first := block[0]
   842  			copy(block, block[1:])
   843  			block[blockSize-1] = first
   844  
   845  			pos += blockSize
   846  			if pos+blockSize > len(ptrs) {
   847  				pos = 0
   848  			}
   849  
   850  			runtime.Gosched()
   851  		}
   852  	})
   853  
   854  	runtime.KeepAlive(ptrs)
   855  }
   856  
   857  func BenchmarkScanStackNoLocals(b *testing.B) {
   858  	var ready sync.WaitGroup
   859  	teardown := make(chan bool)
   860  	for j := 0; j < 10; j++ {
   861  		ready.Add(1)
   862  		go func() {
   863  			x := 100000
   864  			countpwg(&x, &ready, teardown)
   865  		}()
   866  	}
   867  	ready.Wait()
   868  	b.ResetTimer()
   869  	for i := 0; i < b.N; i++ {
   870  		b.StartTimer()
   871  		runtime.GC()
   872  		runtime.GC()
   873  		b.StopTimer()
   874  	}
   875  	close(teardown)
   876  }
   877  
   878  func BenchmarkMSpanCountAlloc(b *testing.B) {
   879  	// Allocate one dummy mspan for the whole benchmark.
   880  	s := runtime.AllocMSpan()
   881  	defer runtime.FreeMSpan(s)
   882  
   883  	// n is the number of bytes to benchmark against.
   884  	// n must always be a multiple of 8, since gcBits is
   885  	// always rounded up 8 bytes.
   886  	for _, n := range []int{8, 16, 32, 64, 128} {
   887  		b.Run(fmt.Sprintf("bits=%d", n*8), func(b *testing.B) {
   888  			// Initialize a new byte slice with pseduo-random data.
   889  			bits := make([]byte, n)
   890  			rand.Read(bits)
   891  
   892  			b.ResetTimer()
   893  			for i := 0; i < b.N; i++ {
   894  				runtime.MSpanCountAlloc(s, bits)
   895  			}
   896  		})
   897  	}
   898  }
   899  
   900  func countpwg(n *int, ready *sync.WaitGroup, teardown chan bool) {
   901  	if *n == 0 {
   902  		ready.Done()
   903  		<-teardown
   904  		return
   905  	}
   906  	*n--
   907  	countpwg(n, ready, teardown)
   908  }