* builtins.def (BUILT_IN_SETJMP): Revert latest change.
[official-gcc.git] / libgo / go / runtime / gc_test.go
blobf14e0d5050edac075cc8a449e18bffe8a02eef33
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 "fmt"
9 "os"
10 "reflect"
11 "runtime"
12 "runtime/debug"
13 "testing"
14 "time"
15 "unsafe"
18 func TestGcSys(t *testing.T) {
19 if os.Getenv("GOGC") == "off" {
20 t.Skip("skipping test; GOGC=off in environment")
22 got := runTestProg(t, "testprog", "GCSys")
23 want := "OK\n"
24 if got != want {
25 t.Fatalf("expected %q, but got %q", want, got)
29 func TestGcDeepNesting(t *testing.T) {
30 type T [2][2][2][2][2][2][2][2][2][2]*int
31 a := new(T)
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)
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()
45 func TestGcHashmapIndirection(t *testing.T) {
46 defer debug.SetGCPercent(debug.SetGCPercent(1))
47 runtime.GC()
48 type T struct {
49 a [256]int
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{}
59 func TestGcArraySlice(t *testing.T) {
60 type X struct {
61 buf [1]byte
62 nextbuf []byte
63 next *X
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[:]
73 head = p
74 runtime.GC()
76 for p := head; p != nil; p = p.next {
77 if p.buf[0] != 42 {
78 t.Fatal("corrupted heap")
83 func TestGcRescan(t *testing.T) {
84 type X struct {
85 c chan error
86 nextx *X
88 type Y struct {
90 nexty *Y
91 p *int
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
100 p.nexty = head
101 p.p = new(int)
102 *p.p = 42
103 head = p
104 runtime.GC()
106 for p := head; p != nil; p = p.nexty {
107 if *p.p != 42 {
108 t.Fatal("corrupted heap")
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)
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)
133 var hugeSink interface{}
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
148 hugeSink = new(struct {
149 x float64
150 y [n]uintptr
151 z []string
157 func TestPeriodicGC(t *testing.T) {
158 // Make sure we're not in the middle of a GC.
159 runtime.GC()
161 var ms1, ms2 runtime.MemStats
162 runtime.ReadMemStats(&ms1)
164 // Make periodic GC run continuously.
165 orig := *runtime.ForceGCPeriod
166 *runtime.ForceGCPeriod = 0
168 // Let some periodic GCs happen. In a heavily loaded system,
169 // it's possible these will be delayed, so this is designed to
170 // succeed quickly if things are working, but to give it some
171 // slack if things are slow.
172 var numGCs uint32
173 const want = 2
174 for i := 0; i < 20 && numGCs < want; i++ {
175 time.Sleep(5 * time.Millisecond)
177 // Test that periodic GC actually happened.
178 runtime.ReadMemStats(&ms2)
179 numGCs = ms2.NumGC - ms1.NumGC
181 *runtime.ForceGCPeriod = orig
183 if numGCs < want {
184 t.Fatalf("no periodic GC: got %v GCs, want >= 2", numGCs)
189 func BenchmarkSetTypePtr(b *testing.B) {
190 benchSetType(b, new(*byte))
193 func BenchmarkSetTypePtr8(b *testing.B) {
194 benchSetType(b, new([8]*byte))
197 func BenchmarkSetTypePtr16(b *testing.B) {
198 benchSetType(b, new([16]*byte))
201 func BenchmarkSetTypePtr32(b *testing.B) {
202 benchSetType(b, new([32]*byte))
205 func BenchmarkSetTypePtr64(b *testing.B) {
206 benchSetType(b, new([64]*byte))
209 func BenchmarkSetTypePtr126(b *testing.B) {
210 benchSetType(b, new([126]*byte))
213 func BenchmarkSetTypePtr128(b *testing.B) {
214 benchSetType(b, new([128]*byte))
217 func BenchmarkSetTypePtrSlice(b *testing.B) {
218 benchSetType(b, make([]*byte, 1<<10))
221 type Node1 struct {
222 Value [1]uintptr
223 Left, Right *byte
226 func BenchmarkSetTypeNode1(b *testing.B) {
227 benchSetType(b, new(Node1))
230 func BenchmarkSetTypeNode1Slice(b *testing.B) {
231 benchSetType(b, make([]Node1, 32))
234 type Node8 struct {
235 Value [8]uintptr
236 Left, Right *byte
239 func BenchmarkSetTypeNode8(b *testing.B) {
240 benchSetType(b, new(Node8))
243 func BenchmarkSetTypeNode8Slice(b *testing.B) {
244 benchSetType(b, make([]Node8, 32))
247 type Node64 struct {
248 Value [64]uintptr
249 Left, Right *byte
252 func BenchmarkSetTypeNode64(b *testing.B) {
253 benchSetType(b, new(Node64))
256 func BenchmarkSetTypeNode64Slice(b *testing.B) {
257 benchSetType(b, make([]Node64, 32))
260 type Node64Dead struct {
261 Left, Right *byte
262 Value [64]uintptr
265 func BenchmarkSetTypeNode64Dead(b *testing.B) {
266 benchSetType(b, new(Node64Dead))
269 func BenchmarkSetTypeNode64DeadSlice(b *testing.B) {
270 benchSetType(b, make([]Node64Dead, 32))
273 type Node124 struct {
274 Value [124]uintptr
275 Left, Right *byte
278 func BenchmarkSetTypeNode124(b *testing.B) {
279 benchSetType(b, new(Node124))
282 func BenchmarkSetTypeNode124Slice(b *testing.B) {
283 benchSetType(b, make([]Node124, 32))
286 type Node126 struct {
287 Value [126]uintptr
288 Left, Right *byte
291 func BenchmarkSetTypeNode126(b *testing.B) {
292 benchSetType(b, new(Node126))
295 func BenchmarkSetTypeNode126Slice(b *testing.B) {
296 benchSetType(b, make([]Node126, 32))
299 type Node128 struct {
300 Value [128]uintptr
301 Left, Right *byte
304 func BenchmarkSetTypeNode128(b *testing.B) {
305 benchSetType(b, new(Node128))
308 func BenchmarkSetTypeNode128Slice(b *testing.B) {
309 benchSetType(b, make([]Node128, 32))
312 type Node130 struct {
313 Value [130]uintptr
314 Left, Right *byte
317 func BenchmarkSetTypeNode130(b *testing.B) {
318 benchSetType(b, new(Node130))
321 func BenchmarkSetTypeNode130Slice(b *testing.B) {
322 benchSetType(b, make([]Node130, 32))
325 type Node1024 struct {
326 Value [1024]uintptr
327 Left, Right *byte
330 func BenchmarkSetTypeNode1024(b *testing.B) {
331 benchSetType(b, new(Node1024))
334 func BenchmarkSetTypeNode1024Slice(b *testing.B) {
335 benchSetType(b, make([]Node1024, 32))
338 func benchSetType(b *testing.B, x interface{}) {
339 v := reflect.ValueOf(x)
340 t := v.Type()
341 switch t.Kind() {
342 case reflect.Ptr:
343 b.SetBytes(int64(t.Elem().Size()))
344 case reflect.Slice:
345 b.SetBytes(int64(t.Elem().Size()) * int64(v.Len()))
347 b.ResetTimer()
348 //runtime.BenchSetType(b.N, x)
351 func BenchmarkAllocation(b *testing.B) {
352 type T struct {
353 x, y *byte
355 ngo := runtime.GOMAXPROCS(0)
356 work := make(chan bool, b.N+ngo)
357 result := make(chan *T)
358 for i := 0; i < b.N; i++ {
359 work <- true
361 for i := 0; i < ngo; i++ {
362 work <- false
364 for i := 0; i < ngo; i++ {
365 go func() {
366 var x *T
367 for <-work {
368 for i := 0; i < 1000; i++ {
369 x = &T{}
372 result <- x
375 for i := 0; i < ngo; i++ {
376 <-result
380 func TestPrintGC(t *testing.T) {
381 if testing.Short() {
382 t.Skip("Skipping in short mode")
384 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2))
385 done := make(chan bool)
386 go func() {
387 for {
388 select {
389 case <-done:
390 return
391 default:
392 runtime.GC()
396 for i := 0; i < 1e4; i++ {
397 func() {
398 defer print("")
401 close(done)
404 func testTypeSwitch(x interface{}) error {
405 switch y := x.(type) {
406 case nil:
407 // ok
408 case error:
409 return y
411 return nil
414 func testAssert(x interface{}) error {
415 if y, ok := x.(error); ok {
416 return y
418 return nil
421 func testAssertVar(x interface{}) error {
422 var y, ok = x.(error)
423 if ok {
424 return y
426 return nil
429 var a bool
431 //go:noinline
432 func testIfaceEqual(x interface{}) {
433 if x == "abc" {
434 a = true
438 func TestPageAccounting(t *testing.T) {
439 // Grow the heap in small increments. This used to drop the
440 // pages-in-use count below zero because of a rounding
441 // mismatch (golang.org/issue/15022).
442 const blockSize = 64 << 10
443 blocks := make([]*[blockSize]byte, (64<<20)/blockSize)
444 for i := range blocks {
445 blocks[i] = new([blockSize]byte)
448 // Check that the running page count matches reality.
449 pagesInUse, counted := runtime.CountPagesInUse()
450 if pagesInUse != counted {
451 t.Fatalf("mheap_.pagesInUse is %d, but direct count is %d", pagesInUse, counted)
455 func TestReadMemStats(t *testing.T) {
456 base, slow := runtime.ReadMemStatsSlow()
457 if base != slow {
458 logDiff(t, "MemStats", reflect.ValueOf(base), reflect.ValueOf(slow))
459 t.Fatal("memstats mismatch")
463 func logDiff(t *testing.T, prefix string, got, want reflect.Value) {
464 typ := got.Type()
465 switch typ.Kind() {
466 case reflect.Array, reflect.Slice:
467 if got.Len() != want.Len() {
468 t.Logf("len(%s): got %v, want %v", prefix, got, want)
469 return
471 for i := 0; i < got.Len(); i++ {
472 logDiff(t, fmt.Sprintf("%s[%d]", prefix, i), got.Index(i), want.Index(i))
474 case reflect.Struct:
475 for i := 0; i < typ.NumField(); i++ {
476 gf, wf := got.Field(i), want.Field(i)
477 logDiff(t, prefix+"."+typ.Field(i).Name, gf, wf)
479 case reflect.Map:
480 t.Fatal("not implemented: logDiff for map")
481 default:
482 if got.Interface() != want.Interface() {
483 t.Logf("%s: got %v, want %v", prefix, got, want)
488 func BenchmarkReadMemStats(b *testing.B) {
489 var ms runtime.MemStats
490 const heapSize = 100 << 20
491 x := make([]*[1024]byte, heapSize/1024)
492 for i := range x {
493 x[i] = new([1024]byte)
495 hugeSink = x
497 b.ResetTimer()
498 for i := 0; i < b.N; i++ {
499 runtime.ReadMemStats(&ms)
502 hugeSink = nil