Source file src/runtime/gc_test.go

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

View as plain text