2017-03-02 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / go / runtime / proc_test.go
blob813c92912b9bc793d848b2c2d2496236ba6c6571
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 "math"
9 "net"
10 "runtime"
11 "runtime/debug"
12 "strings"
13 "sync"
14 "sync/atomic"
15 "syscall"
16 "testing"
17 "time"
20 var stop = make(chan bool, 1)
22 func perpetuumMobile() {
23 select {
24 case <-stop:
25 default:
26 go perpetuumMobile()
30 func TestStopTheWorldDeadlock(t *testing.T) {
31 if testing.Short() {
32 t.Skip("skipping during short test")
34 maxprocs := runtime.GOMAXPROCS(3)
35 compl := make(chan bool, 2)
36 go func() {
37 for i := 0; i != 1000; i += 1 {
38 runtime.GC()
40 compl <- true
41 }()
42 go func() {
43 for i := 0; i != 1000; i += 1 {
44 runtime.GOMAXPROCS(3)
46 compl <- true
47 }()
48 go perpetuumMobile()
49 <-compl
50 <-compl
51 stop <- true
52 runtime.GOMAXPROCS(maxprocs)
55 func TestYieldProgress(t *testing.T) {
56 testYieldProgress(t, false)
59 func TestYieldLockedProgress(t *testing.T) {
60 testYieldProgress(t, true)
63 func testYieldProgress(t *testing.T, locked bool) {
64 c := make(chan bool)
65 cack := make(chan bool)
66 go func() {
67 if locked {
68 runtime.LockOSThread()
70 for {
71 select {
72 case <-c:
73 cack <- true
74 return
75 default:
76 runtime.Gosched()
79 }()
80 time.Sleep(10 * time.Millisecond)
81 c <- true
82 <-cack
85 func TestYieldLocked(t *testing.T) {
86 const N = 10
87 c := make(chan bool)
88 go func() {
89 runtime.LockOSThread()
90 for i := 0; i < N; i++ {
91 runtime.Gosched()
92 time.Sleep(time.Millisecond)
94 c <- true
95 // runtime.UnlockOSThread() is deliberately omitted
96 }()
97 <-c
100 func TestGoroutineParallelism(t *testing.T) {
101 if runtime.NumCPU() == 1 {
102 // Takes too long, too easy to deadlock, etc.
103 t.Skip("skipping on uniprocessor")
105 P := 4
106 N := 10
107 if testing.Short() {
108 P = 3
109 N = 3
111 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(P))
112 // If runtime triggers a forced GC during this test then it will deadlock,
113 // since the goroutines can't be stopped/preempted.
114 // Disable GC for this test (see issue #10958).
115 defer debug.SetGCPercent(debug.SetGCPercent(-1))
116 for try := 0; try < N; try++ {
117 done := make(chan bool)
118 x := uint32(0)
119 for p := 0; p < P; p++ {
120 // Test that all P goroutines are scheduled at the same time
121 go func(p int) {
122 for i := 0; i < 3; i++ {
123 expected := uint32(P*i + p)
124 for atomic.LoadUint32(&x) != expected {
126 atomic.StoreUint32(&x, expected+1)
128 done <- true
129 }(p)
131 for p := 0; p < P; p++ {
132 <-done
137 // Test that all runnable goroutines are scheduled at the same time.
138 func TestGoroutineParallelism2(t *testing.T) {
139 //testGoroutineParallelism2(t, false, false)
140 testGoroutineParallelism2(t, true, false)
141 testGoroutineParallelism2(t, false, true)
142 testGoroutineParallelism2(t, true, true)
145 func testGoroutineParallelism2(t *testing.T, load, netpoll bool) {
146 if runtime.NumCPU() == 1 {
147 // Takes too long, too easy to deadlock, etc.
148 t.Skip("skipping on uniprocessor")
150 P := 4
151 N := 10
152 if testing.Short() {
153 N = 3
155 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(P))
156 // If runtime triggers a forced GC during this test then it will deadlock,
157 // since the goroutines can't be stopped/preempted.
158 // Disable GC for this test (see issue #10958).
159 defer debug.SetGCPercent(debug.SetGCPercent(-1))
160 for try := 0; try < N; try++ {
161 if load {
162 // Create P goroutines and wait until they all run.
163 // When we run the actual test below, worker threads
164 // running the goroutines will start parking.
165 done := make(chan bool)
166 x := uint32(0)
167 for p := 0; p < P; p++ {
168 go func() {
169 if atomic.AddUint32(&x, 1) == uint32(P) {
170 done <- true
171 return
173 for atomic.LoadUint32(&x) != uint32(P) {
177 <-done
179 if netpoll {
180 // Enable netpoller, affects schedler behavior.
181 laddr := "localhost:0"
182 if runtime.GOOS == "android" {
183 // On some Android devices, there are no records for localhost,
184 // see https://golang.org/issues/14486.
185 // Don't use 127.0.0.1 for every case, it won't work on IPv6-only systems.
186 laddr = "127.0.0.1:0"
188 ln, err := net.Listen("tcp", laddr)
189 if err != nil {
190 defer ln.Close() // yup, defer in a loop
193 done := make(chan bool)
194 x := uint32(0)
195 // Spawn P goroutines in a nested fashion just to differ from TestGoroutineParallelism.
196 for p := 0; p < P/2; p++ {
197 go func(p int) {
198 for p2 := 0; p2 < 2; p2++ {
199 go func(p2 int) {
200 for i := 0; i < 3; i++ {
201 expected := uint32(P*i + p*2 + p2)
202 for atomic.LoadUint32(&x) != expected {
204 atomic.StoreUint32(&x, expected+1)
206 done <- true
207 }(p2)
209 }(p)
211 for p := 0; p < P; p++ {
212 <-done
217 func TestBlockLocked(t *testing.T) {
218 const N = 10
219 c := make(chan bool)
220 go func() {
221 runtime.LockOSThread()
222 for i := 0; i < N; i++ {
223 c <- true
225 runtime.UnlockOSThread()
227 for i := 0; i < N; i++ {
232 func TestTimerFairness(t *testing.T) {
233 done := make(chan bool)
234 c := make(chan bool)
235 for i := 0; i < 2; i++ {
236 go func() {
237 for {
238 select {
239 case c <- true:
240 case <-done:
241 return
247 timer := time.After(20 * time.Millisecond)
248 for {
249 select {
250 case <-c:
251 case <-timer:
252 close(done)
253 return
258 func TestTimerFairness2(t *testing.T) {
259 done := make(chan bool)
260 c := make(chan bool)
261 for i := 0; i < 2; i++ {
262 go func() {
263 timer := time.After(20 * time.Millisecond)
264 var buf [1]byte
265 for {
266 syscall.Read(0, buf[0:0])
267 select {
268 case c <- true:
269 case <-c:
270 case <-timer:
271 done <- true
272 return
277 <-done
278 <-done
281 // The function is used to test preemption at split stack checks.
282 // Declaring a var avoids inlining at the call site.
283 var preempt = func() int {
284 var a [128]int
285 sum := 0
286 for _, v := range a {
287 sum += v
289 return sum
292 func TestPreemption(t *testing.T) {
293 t.Skip("gccgo does not implement preemption")
294 // Test that goroutines are preempted at function calls.
295 N := 5
296 if testing.Short() {
297 N = 2
299 c := make(chan bool)
300 var x uint32
301 for g := 0; g < 2; g++ {
302 go func(g int) {
303 for i := 0; i < N; i++ {
304 for atomic.LoadUint32(&x) != uint32(g) {
305 preempt()
307 atomic.StoreUint32(&x, uint32(1-g))
309 c <- true
310 }(g)
316 func TestPreemptionGC(t *testing.T) {
317 t.Skip("gccgo does not implement preemption")
318 // Test that pending GC preempts running goroutines.
319 P := 5
320 N := 10
321 if testing.Short() {
322 P = 3
323 N = 2
325 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(P + 1))
326 var stop uint32
327 for i := 0; i < P; i++ {
328 go func() {
329 for atomic.LoadUint32(&stop) == 0 {
330 preempt()
334 for i := 0; i < N; i++ {
335 runtime.Gosched()
336 runtime.GC()
338 atomic.StoreUint32(&stop, 1)
341 func TestGCFairness(t *testing.T) {
342 output := runTestProg(t, "testprog", "GCFairness")
343 want := "OK\n"
344 if output != want {
345 t.Fatalf("want %s, got %s\n", want, output)
349 func TestGCFairness2(t *testing.T) {
350 output := runTestProg(t, "testprog", "GCFairness2")
351 want := "OK\n"
352 if output != want {
353 t.Fatalf("want %s, got %s\n", want, output)
357 func TestNumGoroutine(t *testing.T) {
358 output := runTestProg(t, "testprog", "NumGoroutine")
359 want := "1\n"
360 if output != want {
361 t.Fatalf("want %q, got %q", want, output)
364 buf := make([]byte, 1<<20)
366 // Try up to 10 times for a match before giving up.
367 // This is a fundamentally racy check but it's important
368 // to notice if NumGoroutine and Stack are _always_ out of sync.
369 for i := 0; ; i++ {
370 // Give goroutines about to exit a chance to exit.
371 // The NumGoroutine and Stack below need to see
372 // the same state of the world, so anything we can do
373 // to keep it quiet is good.
374 runtime.Gosched()
376 n := runtime.NumGoroutine()
377 buf = buf[:runtime.Stack(buf, true)]
379 nstk := strings.Count(string(buf), "goroutine ")
380 if n == nstk {
381 break
383 if i >= 10 {
384 t.Fatalf("NumGoroutine=%d, but found %d goroutines in stack dump: %s", n, nstk, buf)
389 func TestPingPongHog(t *testing.T) {
390 if testing.Short() {
391 t.Skip("skipping in -short mode")
394 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
395 done := make(chan bool)
396 hogChan, lightChan := make(chan bool), make(chan bool)
397 hogCount, lightCount := 0, 0
399 run := func(limit int, counter *int, wake chan bool) {
400 for {
401 select {
402 case <-done:
403 return
405 case <-wake:
406 for i := 0; i < limit; i++ {
407 *counter++
409 wake <- true
414 // Start two co-scheduled hog goroutines.
415 for i := 0; i < 2; i++ {
416 go run(1e6, &hogCount, hogChan)
419 // Start two co-scheduled light goroutines.
420 for i := 0; i < 2; i++ {
421 go run(1e3, &lightCount, lightChan)
424 // Start goroutine pairs and wait for a few preemption rounds.
425 hogChan <- true
426 lightChan <- true
427 time.Sleep(100 * time.Millisecond)
428 close(done)
429 <-hogChan
430 <-lightChan
432 // Check that hogCount and lightCount are within a factor of
433 // 2, which indicates that both pairs of goroutines handed off
434 // the P within a time-slice to their buddy.
435 if hogCount > lightCount*2 || lightCount > hogCount*2 {
436 t.Fatalf("want hogCount/lightCount in [0.5, 2]; got %d/%d = %g", hogCount, lightCount, float64(hogCount)/float64(lightCount))
440 func BenchmarkPingPongHog(b *testing.B) {
441 if b.N == 0 {
442 return
444 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
446 // Create a CPU hog
447 stop, done := make(chan bool), make(chan bool)
448 go func() {
449 for {
450 select {
451 case <-stop:
452 done <- true
453 return
454 default:
459 // Ping-pong b.N times
460 ping, pong := make(chan bool), make(chan bool)
461 go func() {
462 for j := 0; j < b.N; j++ {
463 pong <- <-ping
465 close(stop)
466 done <- true
468 go func() {
469 for i := 0; i < b.N; i++ {
470 ping <- <-pong
472 done <- true
474 b.ResetTimer()
475 ping <- true // Start ping-pong
476 <-stop
477 b.StopTimer()
478 <-ping // Let last ponger exit
479 <-done // Make sure goroutines exit
480 <-done
481 <-done
484 func stackGrowthRecursive(i int) {
485 var pad [128]uint64
486 if i != 0 && pad[0] == 0 {
487 stackGrowthRecursive(i - 1)
491 func TestPreemptSplitBig(t *testing.T) {
492 if testing.Short() {
493 t.Skip("skipping in -short mode")
495 t.Skip("gccgo does not implement preemption")
496 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2))
497 stop := make(chan int)
498 go big(stop)
499 for i := 0; i < 3; i++ {
500 time.Sleep(10 * time.Microsecond) // let big start running
501 runtime.GC()
503 close(stop)
506 func big(stop chan int) int {
507 n := 0
508 for {
509 // delay so that gc is sure to have asked for a preemption
510 for i := 0; i < 1e9; i++ {
514 // call bigframe, which used to miss the preemption in its prologue.
515 bigframe(stop)
517 // check if we've been asked to stop.
518 select {
519 case <-stop:
520 return n
525 func bigframe(stop chan int) int {
526 // not splitting the stack will overflow.
527 // small will notice that it needs a stack split and will
528 // catch the overflow.
529 var x [8192]byte
530 return small(stop, &x)
533 func small(stop chan int, x *[8192]byte) int {
534 for i := range x {
535 x[i] = byte(i)
537 sum := 0
538 for i := range x {
539 sum += int(x[i])
542 // keep small from being a leaf function, which might
543 // make it not do any stack check at all.
544 nonleaf(stop)
546 return sum
549 func nonleaf(stop chan int) bool {
550 // do something that won't be inlined:
551 select {
552 case <-stop:
553 return true
554 default:
555 return false
559 func TestSchedLocalQueue(t *testing.T) {
560 runtime.RunSchedLocalQueueTest()
563 func TestSchedLocalQueueSteal(t *testing.T) {
564 runtime.RunSchedLocalQueueStealTest()
567 func TestSchedLocalQueueEmpty(t *testing.T) {
568 if runtime.NumCPU() == 1 {
569 // Takes too long and does not trigger the race.
570 t.Skip("skipping on uniprocessor")
572 defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(4))
574 // If runtime triggers a forced GC during this test then it will deadlock,
575 // since the goroutines can't be stopped/preempted during spin wait.
576 defer debug.SetGCPercent(debug.SetGCPercent(-1))
578 iters := int(1e5)
579 if testing.Short() {
580 iters = 1e2
582 runtime.RunSchedLocalQueueEmptyTest(iters)
585 func benchmarkStackGrowth(b *testing.B, rec int) {
586 b.RunParallel(func(pb *testing.PB) {
587 for pb.Next() {
588 stackGrowthRecursive(rec)
593 func BenchmarkStackGrowth(b *testing.B) {
594 benchmarkStackGrowth(b, 10)
597 func BenchmarkStackGrowthDeep(b *testing.B) {
598 benchmarkStackGrowth(b, 1024)
601 func BenchmarkCreateGoroutines(b *testing.B) {
602 benchmarkCreateGoroutines(b, 1)
605 func BenchmarkCreateGoroutinesParallel(b *testing.B) {
606 benchmarkCreateGoroutines(b, runtime.GOMAXPROCS(-1))
609 func benchmarkCreateGoroutines(b *testing.B, procs int) {
610 c := make(chan bool)
611 var f func(n int)
612 f = func(n int) {
613 if n == 0 {
614 c <- true
615 return
617 go f(n - 1)
619 for i := 0; i < procs; i++ {
620 go f(b.N / procs)
622 for i := 0; i < procs; i++ {
627 func BenchmarkCreateGoroutinesCapture(b *testing.B) {
628 b.ReportAllocs()
629 for i := 0; i < b.N; i++ {
630 const N = 4
631 var wg sync.WaitGroup
632 wg.Add(N)
633 for i := 0; i < N; i++ {
634 i := i
635 go func() {
636 if i >= N {
637 b.Logf("bad") // just to capture b
639 wg.Done()
642 wg.Wait()
646 func BenchmarkClosureCall(b *testing.B) {
647 sum := 0
648 off1 := 1
649 for i := 0; i < b.N; i++ {
650 off2 := 2
651 func() {
652 sum += i + off1 + off2
655 _ = sum
658 type Matrix [][]float64
660 func BenchmarkMatmult(b *testing.B) {
661 b.StopTimer()
662 // matmult is O(N**3) but testing expects O(b.N),
663 // so we need to take cube root of b.N
664 n := int(math.Cbrt(float64(b.N))) + 1
665 A := makeMatrix(n)
666 B := makeMatrix(n)
667 C := makeMatrix(n)
668 b.StartTimer()
669 matmult(nil, A, B, C, 0, n, 0, n, 0, n, 8)
672 func makeMatrix(n int) Matrix {
673 m := make(Matrix, n)
674 for i := 0; i < n; i++ {
675 m[i] = make([]float64, n)
676 for j := 0; j < n; j++ {
677 m[i][j] = float64(i*n + j)
680 return m
683 func matmult(done chan<- struct{}, A, B, C Matrix, i0, i1, j0, j1, k0, k1, threshold int) {
684 di := i1 - i0
685 dj := j1 - j0
686 dk := k1 - k0
687 if di >= dj && di >= dk && di >= threshold {
688 // divide in two by y axis
689 mi := i0 + di/2
690 done1 := make(chan struct{}, 1)
691 go matmult(done1, A, B, C, i0, mi, j0, j1, k0, k1, threshold)
692 matmult(nil, A, B, C, mi, i1, j0, j1, k0, k1, threshold)
693 <-done1
694 } else if dj >= dk && dj >= threshold {
695 // divide in two by x axis
696 mj := j0 + dj/2
697 done1 := make(chan struct{}, 1)
698 go matmult(done1, A, B, C, i0, i1, j0, mj, k0, k1, threshold)
699 matmult(nil, A, B, C, i0, i1, mj, j1, k0, k1, threshold)
700 <-done1
701 } else if dk >= threshold {
702 // divide in two by "k" axis
703 // deliberately not parallel because of data races
704 mk := k0 + dk/2
705 matmult(nil, A, B, C, i0, i1, j0, j1, k0, mk, threshold)
706 matmult(nil, A, B, C, i0, i1, j0, j1, mk, k1, threshold)
707 } else {
708 // the matrices are small enough, compute directly
709 for i := i0; i < i1; i++ {
710 for j := j0; j < j1; j++ {
711 for k := k0; k < k1; k++ {
712 C[i][j] += A[i][k] * B[k][j]
717 if done != nil {
718 done <- struct{}{}
723 func TestStealOrder(t *testing.T) {
724 runtime.RunStealOrderTest()