github.com/aloncn/graphics-go@v0.0.1/src/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  	"io"
     9  	"os"
    10  	"reflect"
    11  	"runtime"
    12  	"runtime/debug"
    13  	"testing"
    14  	"time"
    15  	"unsafe"
    16  )
    17  
    18  func TestGcSys(t *testing.T) {
    19  	if os.Getenv("GOGC") == "off" {
    20  		t.Skip("skipping test; GOGC=off in environment")
    21  	}
    22  	got := runTestProg(t, "testprog", "GCSys")
    23  	want := "OK\n"
    24  	if got != want {
    25  		t.Fatalf("expected %q, but got %q", want, got)
    26  	}
    27  }
    28  
    29  func TestGcDeepNesting(t *testing.T) {
    30  	type T [2][2][2][2][2][2][2][2][2][2]*int
    31  	a := new(T)
    32  
    33  	// Prevent the compiler from applying escape analysis.
    34  	// This makes sure new(T) is allocated on heap, not on the stack.
    35  	t.Logf("%p", a)
    36  
    37  	a[0][0][0][0][0][0][0][0][0][0] = new(int)
    38  	*a[0][0][0][0][0][0][0][0][0][0] = 13
    39  	runtime.GC()
    40  	if *a[0][0][0][0][0][0][0][0][0][0] != 13 {
    41  		t.Fail()
    42  	}
    43  }
    44  
    45  func TestGcHashmapIndirection(t *testing.T) {
    46  	defer debug.SetGCPercent(debug.SetGCPercent(1))
    47  	runtime.GC()
    48  	type T struct {
    49  		a [256]int
    50  	}
    51  	m := make(map[T]T)
    52  	for i := 0; i < 2000; i++ {
    53  		var a T
    54  		a.a[0] = i
    55  		m[a] = T{}
    56  	}
    57  }
    58  
    59  func TestGcArraySlice(t *testing.T) {
    60  	type X struct {
    61  		buf     [1]byte
    62  		nextbuf []byte
    63  		next    *X
    64  	}
    65  	var head *X
    66  	for i := 0; i < 10; i++ {
    67  		p := &X{}
    68  		p.buf[0] = 42
    69  		p.next = head
    70  		if head != nil {
    71  			p.nextbuf = head.buf[:]
    72  		}
    73  		head = p
    74  		runtime.GC()
    75  	}
    76  	for p := head; p != nil; p = p.next {
    77  		if p.buf[0] != 42 {
    78  			t.Fatal("corrupted heap")
    79  		}
    80  	}
    81  }
    82  
    83  func TestGcRescan(t *testing.T) {
    84  	type X struct {
    85  		c     chan error
    86  		nextx *X
    87  	}
    88  	type Y struct {
    89  		X
    90  		nexty *Y
    91  		p     *int
    92  	}
    93  	var head *Y
    94  	for i := 0; i < 10; i++ {
    95  		p := &Y{}
    96  		p.c = make(chan error)
    97  		if head != nil {
    98  			p.nextx = &head.X
    99  		}
   100  		p.nexty = head
   101  		p.p = new(int)
   102  		*p.p = 42
   103  		head = p
   104  		runtime.GC()
   105  	}
   106  	for p := head; p != nil; p = p.nexty {
   107  		if *p.p != 42 {
   108  			t.Fatal("corrupted heap")
   109  		}
   110  	}
   111  }
   112  
   113  func TestGcLastTime(t *testing.T) {
   114  	ms := new(runtime.MemStats)
   115  	t0 := time.Now().UnixNano()
   116  	runtime.GC()
   117  	t1 := time.Now().UnixNano()
   118  	runtime.ReadMemStats(ms)
   119  	last := int64(ms.LastGC)
   120  	if t0 > last || last > t1 {
   121  		t.Fatalf("bad last GC time: got %v, want [%v, %v]", last, t0, t1)
   122  	}
   123  	pause := ms.PauseNs[(ms.NumGC+255)%256]
   124  	// Due to timer granularity, pause can actually be 0 on windows
   125  	// or on virtualized environments.
   126  	if pause == 0 {
   127  		t.Logf("last GC pause was 0")
   128  	} else if pause > 10e9 {
   129  		t.Logf("bad last GC pause: got %v, want [0, 10e9]", pause)
   130  	}
   131  }
   132  
   133  var hugeSink interface{}
   134  
   135  func TestHugeGCInfo(t *testing.T) {
   136  	// The test ensures that compiler can chew these huge types even on weakest machines.
   137  	// The types are not allocated at runtime.
   138  	if hugeSink != nil {
   139  		// 400MB on 32 bots, 4TB on 64-bits.
   140  		const n = (400 << 20) + (unsafe.Sizeof(uintptr(0))-4)<<40
   141  		hugeSink = new([n]*byte)
   142  		hugeSink = new([n]uintptr)
   143  		hugeSink = new(struct {
   144  			x float64
   145  			y [n]*byte
   146  			z []string
   147  		})
   148  		hugeSink = new(struct {
   149  			x float64
   150  			y [n]uintptr
   151  			z []string
   152  		})
   153  	}
   154  }
   155  
   156  func TestPeriodicGC(t *testing.T) {
   157  	// Make sure we're not in the middle of a GC.
   158  	runtime.GC()
   159  
   160  	var ms1, ms2 runtime.MemStats
   161  	runtime.ReadMemStats(&ms1)
   162  
   163  	// Make periodic GC run continuously.
   164  	orig := *runtime.ForceGCPeriod
   165  	*runtime.ForceGCPeriod = 0
   166  
   167  	// Let some periodic GCs happen. In a heavily loaded system,
   168  	// it's possible these will be delayed, so this is designed to
   169  	// succeed quickly if things are working, but to give it some
   170  	// slack if things are slow.
   171  	var numGCs uint32
   172  	const want = 2
   173  	for i := 0; i < 20 && numGCs < want; i++ {
   174  		time.Sleep(5 * time.Millisecond)
   175  
   176  		// Test that periodic GC actually happened.
   177  		runtime.ReadMemStats(&ms2)
   178  		numGCs = ms2.NumGC - ms1.NumGC
   179  	}
   180  	*runtime.ForceGCPeriod = orig
   181  
   182  	if numGCs < want {
   183  		t.Fatalf("no periodic GC: got %v GCs, want >= 2", numGCs)
   184  	}
   185  }
   186  
   187  func BenchmarkSetTypePtr(b *testing.B) {
   188  	benchSetType(b, new(*byte))
   189  }
   190  
   191  func BenchmarkSetTypePtr8(b *testing.B) {
   192  	benchSetType(b, new([8]*byte))
   193  }
   194  
   195  func BenchmarkSetTypePtr16(b *testing.B) {
   196  	benchSetType(b, new([16]*byte))
   197  }
   198  
   199  func BenchmarkSetTypePtr32(b *testing.B) {
   200  	benchSetType(b, new([32]*byte))
   201  }
   202  
   203  func BenchmarkSetTypePtr64(b *testing.B) {
   204  	benchSetType(b, new([64]*byte))
   205  }
   206  
   207  func BenchmarkSetTypePtr126(b *testing.B) {
   208  	benchSetType(b, new([126]*byte))
   209  }
   210  
   211  func BenchmarkSetTypePtr128(b *testing.B) {
   212  	benchSetType(b, new([128]*byte))
   213  }
   214  
   215  func BenchmarkSetTypePtrSlice(b *testing.B) {
   216  	benchSetType(b, make([]*byte, 1<<10))
   217  }
   218  
   219  type Node1 struct {
   220  	Value       [1]uintptr
   221  	Left, Right *byte
   222  }
   223  
   224  func BenchmarkSetTypeNode1(b *testing.B) {
   225  	benchSetType(b, new(Node1))
   226  }
   227  
   228  func BenchmarkSetTypeNode1Slice(b *testing.B) {
   229  	benchSetType(b, make([]Node1, 32))
   230  }
   231  
   232  type Node8 struct {
   233  	Value       [8]uintptr
   234  	Left, Right *byte
   235  }
   236  
   237  func BenchmarkSetTypeNode8(b *testing.B) {
   238  	benchSetType(b, new(Node8))
   239  }
   240  
   241  func BenchmarkSetTypeNode8Slice(b *testing.B) {
   242  	benchSetType(b, make([]Node8, 32))
   243  }
   244  
   245  type Node64 struct {
   246  	Value       [64]uintptr
   247  	Left, Right *byte
   248  }
   249  
   250  func BenchmarkSetTypeNode64(b *testing.B) {
   251  	benchSetType(b, new(Node64))
   252  }
   253  
   254  func BenchmarkSetTypeNode64Slice(b *testing.B) {
   255  	benchSetType(b, make([]Node64, 32))
   256  }
   257  
   258  type Node64Dead struct {
   259  	Left, Right *byte
   260  	Value       [64]uintptr
   261  }
   262  
   263  func BenchmarkSetTypeNode64Dead(b *testing.B) {
   264  	benchSetType(b, new(Node64Dead))
   265  }
   266  
   267  func BenchmarkSetTypeNode64DeadSlice(b *testing.B) {
   268  	benchSetType(b, make([]Node64Dead, 32))
   269  }
   270  
   271  type Node124 struct {
   272  	Value       [124]uintptr
   273  	Left, Right *byte
   274  }
   275  
   276  func BenchmarkSetTypeNode124(b *testing.B) {
   277  	benchSetType(b, new(Node124))
   278  }
   279  
   280  func BenchmarkSetTypeNode124Slice(b *testing.B) {
   281  	benchSetType(b, make([]Node124, 32))
   282  }
   283  
   284  type Node126 struct {
   285  	Value       [126]uintptr
   286  	Left, Right *byte
   287  }
   288  
   289  func BenchmarkSetTypeNode126(b *testing.B) {
   290  	benchSetType(b, new(Node126))
   291  }
   292  
   293  func BenchmarkSetTypeNode126Slice(b *testing.B) {
   294  	benchSetType(b, make([]Node126, 32))
   295  }
   296  
   297  type Node128 struct {
   298  	Value       [128]uintptr
   299  	Left, Right *byte
   300  }
   301  
   302  func BenchmarkSetTypeNode128(b *testing.B) {
   303  	benchSetType(b, new(Node128))
   304  }
   305  
   306  func BenchmarkSetTypeNode128Slice(b *testing.B) {
   307  	benchSetType(b, make([]Node128, 32))
   308  }
   309  
   310  type Node130 struct {
   311  	Value       [130]uintptr
   312  	Left, Right *byte
   313  }
   314  
   315  func BenchmarkSetTypeNode130(b *testing.B) {
   316  	benchSetType(b, new(Node130))
   317  }
   318  
   319  func BenchmarkSetTypeNode130Slice(b *testing.B) {
   320  	benchSetType(b, make([]Node130, 32))
   321  }
   322  
   323  type Node1024 struct {
   324  	Value       [1024]uintptr
   325  	Left, Right *byte
   326  }
   327  
   328  func BenchmarkSetTypeNode1024(b *testing.B) {
   329  	benchSetType(b, new(Node1024))
   330  }
   331  
   332  func BenchmarkSetTypeNode1024Slice(b *testing.B) {
   333  	benchSetType(b, make([]Node1024, 32))
   334  }
   335  
   336  func benchSetType(b *testing.B, x interface{}) {
   337  	v := reflect.ValueOf(x)
   338  	t := v.Type()
   339  	switch t.Kind() {
   340  	case reflect.Ptr:
   341  		b.SetBytes(int64(t.Elem().Size()))
   342  	case reflect.Slice:
   343  		b.SetBytes(int64(t.Elem().Size()) * int64(v.Len()))
   344  	}
   345  	b.ResetTimer()
   346  	runtime.BenchSetType(b.N, x)
   347  }
   348  
   349  func BenchmarkAllocation(b *testing.B) {
   350  	type T struct {
   351  		x, y *byte
   352  	}
   353  	ngo := runtime.GOMAXPROCS(0)
   354  	work := make(chan bool, b.N+ngo)
   355  	result := make(chan *T)
   356  	for i := 0; i < b.N; i++ {
   357  		work <- true
   358  	}
   359  	for i := 0; i < ngo; i++ {
   360  		work <- false
   361  	}
   362  	for i := 0; i < ngo; i++ {
   363  		go func() {
   364  			var x *T
   365  			for <-work {
   366  				for i := 0; i < 1000; i++ {
   367  					x = &T{}
   368  				}
   369  			}
   370  			result <- x
   371  		}()
   372  	}
   373  	for i := 0; i < ngo; i++ {
   374  		<-result
   375  	}
   376  }
   377  
   378  func TestPrintGC(t *testing.T) {
   379  	if testing.Short() {
   380  		t.Skip("Skipping in short mode")
   381  	}
   382  	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2))
   383  	done := make(chan bool)
   384  	go func() {
   385  		for {
   386  			select {
   387  			case <-done:
   388  				return
   389  			default:
   390  				runtime.GC()
   391  			}
   392  		}
   393  	}()
   394  	for i := 0; i < 1e4; i++ {
   395  		func() {
   396  			defer print("")
   397  		}()
   398  	}
   399  	close(done)
   400  }
   401  
   402  // The implicit y, ok := x.(error) for the case error
   403  // in testTypeSwitch used to not initialize the result y
   404  // before passing &y to assertE2I2GC.
   405  // Catch this by making assertE2I2 call runtime.GC,
   406  // which will force a stack scan and failure if there are
   407  // bad pointers, and then fill the stack with bad pointers
   408  // and run the type switch.
   409  func TestAssertE2I2Liveness(t *testing.T) {
   410  	// Note that this flag is defined in export_test.go
   411  	// and is not available to ordinary imports of runtime.
   412  	*runtime.TestingAssertE2I2GC = true
   413  	defer func() {
   414  		*runtime.TestingAssertE2I2GC = false
   415  	}()
   416  
   417  	poisonStack()
   418  	testTypeSwitch(io.EOF)
   419  	poisonStack()
   420  	testAssert(io.EOF)
   421  	poisonStack()
   422  	testAssertVar(io.EOF)
   423  }
   424  
   425  func poisonStack() uintptr {
   426  	var x [1000]uintptr
   427  	for i := range x {
   428  		x[i] = 0xff
   429  	}
   430  	return x[123]
   431  }
   432  
   433  func testTypeSwitch(x interface{}) error {
   434  	switch y := x.(type) {
   435  	case nil:
   436  		// ok
   437  	case error:
   438  		return y
   439  	}
   440  	return nil
   441  }
   442  
   443  func testAssert(x interface{}) error {
   444  	if y, ok := x.(error); ok {
   445  		return y
   446  	}
   447  	return nil
   448  }
   449  
   450  func testAssertVar(x interface{}) error {
   451  	var y, ok = x.(error)
   452  	if ok {
   453  		return y
   454  	}
   455  	return nil
   456  }
   457  
   458  func TestAssertE2T2Liveness(t *testing.T) {
   459  	*runtime.TestingAssertE2T2GC = true
   460  	defer func() {
   461  		*runtime.TestingAssertE2T2GC = false
   462  	}()
   463  
   464  	poisonStack()
   465  	testIfaceEqual(io.EOF)
   466  }
   467  
   468  var a bool
   469  
   470  //go:noinline
   471  func testIfaceEqual(x interface{}) {
   472  	if x == "abc" {
   473  		a = true
   474  	}
   475  }
   476  
   477  func TestPageAccounting(t *testing.T) {
   478  	// Grow the heap in small increments. This used to drop the
   479  	// pages-in-use count below zero because of a rounding
   480  	// mismatch (golang.org/issue/15022).
   481  	const blockSize = 64 << 10
   482  	blocks := make([]*[blockSize]byte, (64<<20)/blockSize)
   483  	for i := range blocks {
   484  		blocks[i] = new([blockSize]byte)
   485  	}
   486  
   487  	// Check that the running page count matches reality.
   488  	pagesInUse, counted := runtime.CountPagesInUse()
   489  	if pagesInUse != counted {
   490  		t.Fatalf("mheap_.pagesInUse is %d, but direct count is %d", pagesInUse, counted)
   491  	}
   492  }