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