OSDN Git Service

1d20a58a420be06a59fe92ddb9f40ad50ae6f515
[pf3gnuchains/gcc-fork.git] / libgo / go / runtime / proc_test.go
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         "runtime"
9         "sync/atomic"
10         "testing"
11 )
12
13 var stop = make(chan bool, 1)
14
15 func perpetuumMobile() {
16         select {
17         case <-stop:
18         default:
19                 go perpetuumMobile()
20         }
21 }
22
23 func TestStopTheWorldDeadlock(t *testing.T) {
24         if testing.Short() {
25                 t.Logf("skipping during short test")
26                 return
27         }
28         maxprocs := runtime.GOMAXPROCS(3)
29         compl := make(chan bool, 2)
30         go func() {
31                 for i := 0; i != 1000; i += 1 {
32                         runtime.GC()
33                 }
34                 compl <- true
35         }()
36         go func() {
37                 for i := 0; i != 1000; i += 1 {
38                         runtime.GOMAXPROCS(3)
39                 }
40                 compl <- true
41         }()
42         go perpetuumMobile()
43         <-compl
44         <-compl
45         stop <- true
46         runtime.GOMAXPROCS(maxprocs)
47 }
48
49 func stackGrowthRecursive(i int) {
50         var pad [128]uint64
51         if i != 0 && pad[0] == 0 {
52                 stackGrowthRecursive(i - 1)
53         }
54 }
55
56 func BenchmarkStackGrowth(b *testing.B) {
57         const CallsPerSched = 1000
58         procs := runtime.GOMAXPROCS(-1)
59         N := int32(b.N / CallsPerSched)
60         c := make(chan bool, procs)
61         for p := 0; p < procs; p++ {
62                 go func() {
63                         for atomic.AddInt32(&N, -1) >= 0 {
64                                 runtime.Gosched()
65                                 for g := 0; g < CallsPerSched; g++ {
66                                         stackGrowthRecursive(10)
67                                 }
68                         }
69                         c <- true
70                 }()
71         }
72         for p := 0; p < procs; p++ {
73                 <-c
74         }
75 }
76
77 /* These benchmarks are meaningless for gccgo.
78
79 func BenchmarkSyscall(b *testing.B) {
80         const CallsPerSched = 1000
81         procs := runtime.GOMAXPROCS(-1)
82         N := int32(b.N / CallsPerSched)
83         c := make(chan bool, procs)
84         for p := 0; p < procs; p++ {
85                 go func() {
86                         for atomic.AddInt32(&N, -1) >= 0 {
87                                 runtime.Gosched()
88                                 for g := 0; g < CallsPerSched; g++ {
89                                         runtime.Entersyscall()
90                                         runtime.Exitsyscall()
91                                 }
92                         }
93                         c <- true
94                 }()
95         }
96         for p := 0; p < procs; p++ {
97                 <-c
98         }
99 }
100
101 func BenchmarkSyscallWork(b *testing.B) {
102         const CallsPerSched = 1000
103         const LocalWork = 100
104         procs := runtime.GOMAXPROCS(-1)
105         N := int32(b.N / CallsPerSched)
106         c := make(chan bool, procs)
107         for p := 0; p < procs; p++ {
108                 go func() {
109                         foo := 42
110                         for atomic.AddInt32(&N, -1) >= 0 {
111                                 runtime.Gosched()
112                                 for g := 0; g < CallsPerSched; g++ {
113                                         runtime.Entersyscall()
114                                         for i := 0; i < LocalWork; i++ {
115                                                 foo *= 2
116                                                 foo /= 2
117                                         }
118                                         runtime.Exitsyscall()
119                                 }
120                         }
121                         c <- foo == 42
122                 }()
123         }
124         for p := 0; p < procs; p++ {
125                 <-c
126         }
127 }
128
129 */