2017-03-02 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / go / runtime / gc_test.go
blob2a6acf0836e46aa38d692f46d10307bc9124a5fa
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.
5 package runtime_test
7 import (
8 "os"
9 "reflect"
10 "runtime"
11 "runtime/debug"
12 "testing"
13 "time"
14 "unsafe"
17 func TestGcSys(t *testing.T) {
18 if os.Getenv("GOGC") == "off" {
19 t.Skip("skipping test; GOGC=off in environment")
21 got := runTestProg(t, "testprog", "GCSys")
22 want := "OK\n"
23 if got != want {
24 t.Fatalf("expected %q, but got %q", want, got)
28 func TestGcDeepNesting(t *testing.T) {
29 type T [2][2][2][2][2][2][2][2][2][2]*int
30 a := new(T)
32 // Prevent the compiler from applying escape analysis.
33 // This makes sure new(T) is allocated on heap, not on the stack.
34 t.Logf("%p", a)
36 a[0][0][0][0][0][0][0][0][0][0] = new(int)
37 *a[0][0][0][0][0][0][0][0][0][0] = 13
38 runtime.GC()
39 if *a[0][0][0][0][0][0][0][0][0][0] != 13 {
40 t.Fail()
44 func TestGcHashmapIndirection(t *testing.T) {
45 defer debug.SetGCPercent(debug.SetGCPercent(1))
46 runtime.GC()
47 type T struct {
48 a [256]int
50 m := make(map[T]T)
51 for i := 0; i < 2000; i++ {
52 var a T
53 a.a[0] = i
54 m[a] = T{}
58 func TestGcArraySlice(t *testing.T) {
59 type X struct {
60 buf [1]byte
61 nextbuf []byte
62 next *X
64 var head *X
65 for i := 0; i < 10; i++ {
66 p := &X{}
67 p.buf[0] = 42
68 p.next = head
69 if head != nil {
70 p.nextbuf = head.buf[:]
72 head = p
73 runtime.GC()
75 for p := head; p != nil; p = p.next {
76 if p.buf[0] != 42 {
77 t.Fatal("corrupted heap")
82 func TestGcRescan(t *testing.T) {
83 type X struct {
84 c chan error
85 nextx *X
87 type Y struct {
89 nexty *Y
90 p *int
92 var head *Y
93 for i := 0; i < 10; i++ {
94 p := &Y{}
95 p.c = make(chan error)
96 if head != nil {
97 p.nextx = &head.X
99 p.nexty = head
100 p.p = new(int)
101 *p.p = 42
102 head = p
103 runtime.GC()
105 for p := head; p != nil; p = p.nexty {
106 if *p.p != 42 {
107 t.Fatal("corrupted heap")
112 func TestGcLastTime(t *testing.T) {
113 ms := new(runtime.MemStats)
114 t0 := time.Now().UnixNano()
115 runtime.GC()
116 t1 := time.Now().UnixNano()
117 runtime.ReadMemStats(ms)
118 last := int64(ms.LastGC)
119 if t0 > last || last > t1 {
120 t.Fatalf("bad last GC time: got %v, want [%v, %v]", last, t0, t1)
122 pause := ms.PauseNs[(ms.NumGC+255)%256]
123 // Due to timer granularity, pause can actually be 0 on windows
124 // or on virtualized environments.
125 if pause == 0 {
126 t.Logf("last GC pause was 0")
127 } else if pause > 10e9 {
128 t.Logf("bad last GC pause: got %v, want [0, 10e9]", pause)
132 var hugeSink interface{}
134 func TestHugeGCInfo(t *testing.T) {
135 // The test ensures that compiler can chew these huge types even on weakest machines.
136 // The types are not allocated at runtime.
137 if hugeSink != nil {
138 // 400MB on 32 bots, 4TB on 64-bits.
139 const n = (400 << 20) + (unsafe.Sizeof(uintptr(0))-4)<<40
140 hugeSink = new([n]*byte)
141 hugeSink = new([n]uintptr)
142 hugeSink = new(struct {
143 x float64
144 y [n]*byte
145 z []string
147 hugeSink = new(struct {
148 x float64
149 y [n]uintptr
150 z []string
156 func TestPeriodicGC(t *testing.T) {
157 // Make sure we're not in the middle of a GC.
158 runtime.GC()
160 var ms1, ms2 runtime.MemStats
161 runtime.ReadMemStats(&ms1)
163 // Make periodic GC run continuously.
164 orig := *runtime.ForceGCPeriod
165 *runtime.ForceGCPeriod = 0
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)
176 // Test that periodic GC actually happened.
177 runtime.ReadMemStats(&ms2)
178 numGCs = ms2.NumGC - ms1.NumGC
180 *runtime.ForceGCPeriod = orig
182 if numGCs < want {
183 t.Fatalf("no periodic GC: got %v GCs, want >= 2", numGCs)
188 func BenchmarkSetTypePtr(b *testing.B) {
189 benchSetType(b, new(*byte))
192 func BenchmarkSetTypePtr8(b *testing.B) {
193 benchSetType(b, new([8]*byte))
196 func BenchmarkSetTypePtr16(b *testing.B) {
197 benchSetType(b, new([16]*byte))
200 func BenchmarkSetTypePtr32(b *testing.B) {
201 benchSetType(b, new([32]*byte))
204 func BenchmarkSetTypePtr64(b *testing.B) {
205 benchSetType(b, new([64]*byte))
208 func BenchmarkSetTypePtr126(b *testing.B) {
209 benchSetType(b, new([126]*byte))
212 func BenchmarkSetTypePtr128(b *testing.B) {
213 benchSetType(b, new([128]*byte))
216 func BenchmarkSetTypePtrSlice(b *testing.B) {
217 benchSetType(b, make([]*byte, 1<<10))
220 type Node1 struct {
221 Value [1]uintptr
222 Left, Right *byte
225 func BenchmarkSetTypeNode1(b *testing.B) {
226 benchSetType(b, new(Node1))
229 func BenchmarkSetTypeNode1Slice(b *testing.B) {
230 benchSetType(b, make([]Node1, 32))
233 type Node8 struct {
234 Value [8]uintptr
235 Left, Right *byte
238 func BenchmarkSetTypeNode8(b *testing.B) {
239 benchSetType(b, new(Node8))
242 func BenchmarkSetTypeNode8Slice(b *testing.B) {
243 benchSetType(b, make([]Node8, 32))
246 type Node64 struct {
247 Value [64]uintptr
248 Left, Right *byte
251 func BenchmarkSetTypeNode64(b *testing.B) {
252 benchSetType(b, new(Node64))
255 func BenchmarkSetTypeNode64Slice(b *testing.B) {
256 benchSetType(b, make([]Node64, 32))
259 type Node64Dead struct {
260 Left, Right *byte
261 Value [64]uintptr
264 func BenchmarkSetTypeNode64Dead(b *testing.B) {
265 benchSetType(b, new(Node64Dead))
268 func BenchmarkSetTypeNode64DeadSlice(b *testing.B) {
269 benchSetType(b, make([]Node64Dead, 32))
272 type Node124 struct {
273 Value [124]uintptr
274 Left, Right *byte
277 func BenchmarkSetTypeNode124(b *testing.B) {
278 benchSetType(b, new(Node124))
281 func BenchmarkSetTypeNode124Slice(b *testing.B) {
282 benchSetType(b, make([]Node124, 32))
285 type Node126 struct {
286 Value [126]uintptr
287 Left, Right *byte
290 func BenchmarkSetTypeNode126(b *testing.B) {
291 benchSetType(b, new(Node126))
294 func BenchmarkSetTypeNode126Slice(b *testing.B) {
295 benchSetType(b, make([]Node126, 32))
298 type Node128 struct {
299 Value [128]uintptr
300 Left, Right *byte
303 func BenchmarkSetTypeNode128(b *testing.B) {
304 benchSetType(b, new(Node128))
307 func BenchmarkSetTypeNode128Slice(b *testing.B) {
308 benchSetType(b, make([]Node128, 32))
311 type Node130 struct {
312 Value [130]uintptr
313 Left, Right *byte
316 func BenchmarkSetTypeNode130(b *testing.B) {
317 benchSetType(b, new(Node130))
320 func BenchmarkSetTypeNode130Slice(b *testing.B) {
321 benchSetType(b, make([]Node130, 32))
324 type Node1024 struct {
325 Value [1024]uintptr
326 Left, Right *byte
329 func BenchmarkSetTypeNode1024(b *testing.B) {
330 benchSetType(b, new(Node1024))
333 func BenchmarkSetTypeNode1024Slice(b *testing.B) {
334 benchSetType(b, make([]Node1024, 32))
337 func benchSetType(b *testing.B, x interface{}) {
338 v := reflect.ValueOf(x)
339 t := v.Type()
340 switch t.Kind() {
341 case reflect.Ptr:
342 b.SetBytes(int64(t.Elem().Size()))
343 case reflect.Slice:
344 b.SetBytes(int64(t.Elem().Size()) * int64(v.Len()))
346 b.ResetTimer()
347 //runtime.BenchSetType(b.N, x)
350 func BenchmarkAllocation(b *testing.B) {
351 type T struct {
352 x, y *byte
354 ngo := runtime.GOMAXPROCS(0)
355 work := make(chan bool, b.N+ngo)
356 result := make(chan *T)
357 for i := 0; i < b.N; i++ {
358 work <- true
360 for i := 0; i < ngo; i++ {
361 work <- false
363 for i := 0; i < ngo; i++ {
364 go func() {
365 var x *T
366 for <-work {
367 for i := 0; i < 1000; i++ {
368 x = &T{}
371 result <- x
374 for i := 0; i < ngo; i++ {
375 <-result
379 func TestPrintGC(t *testing.T) {
380 if testing.Short() {
381 t.Skip("Skipping in short mode")
383 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2))
384 done := make(chan bool)
385 go func() {
386 for {
387 select {
388 case <-done:
389 return
390 default:
391 runtime.GC()
395 for i := 0; i < 1e4; i++ {
396 func() {
397 defer print("")
400 close(done)
403 func testTypeSwitch(x interface{}) error {
404 switch y := x.(type) {
405 case nil:
406 // ok
407 case error:
408 return y
410 return nil
413 func testAssert(x interface{}) error {
414 if y, ok := x.(error); ok {
415 return y
417 return nil
420 func testAssertVar(x interface{}) error {
421 var y, ok = x.(error)
422 if ok {
423 return y
425 return nil
428 var a bool
430 //go:noinline
431 func testIfaceEqual(x interface{}) {
432 if x == "abc" {
433 a = true
439 func TestPageAccounting(t *testing.T) {
440 // Grow the heap in small increments. This used to drop the
441 // pages-in-use count below zero because of a rounding
442 // mismatch (golang.org/issue/15022).
443 const blockSize = 64 << 10
444 blocks := make([]*[blockSize]byte, (64<<20)/blockSize)
445 for i := range blocks {
446 blocks[i] = new([blockSize]byte)
449 // Check that the running page count matches reality.
450 pagesInUse, counted := runtime.CountPagesInUse()
451 if pagesInUse != counted {
452 t.Fatalf("mheap_.pagesInUse is %d, but direct count is %d", pagesInUse, counted)