OSDN Git Service

libgo: Add back tests of syscall time.
[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 func BenchmarkSyscall(b *testing.B) {
78         const CallsPerSched = 1000
79         procs := runtime.GOMAXPROCS(-1)
80         N := int32(b.N / CallsPerSched)
81         c := make(chan bool, procs)
82         for p := 0; p < procs; p++ {
83                 go func() {
84                         for atomic.AddInt32(&N, -1) >= 0 {
85                                 runtime.Gosched()
86                                 for g := 0; g < CallsPerSched; g++ {
87                                         runtime.Entersyscall()
88                                         runtime.Exitsyscall()
89                                 }
90                         }
91                         c <- true
92                 }()
93         }
94         for p := 0; p < procs; p++ {
95                 <-c
96         }
97 }
98
99 func BenchmarkSyscallWork(b *testing.B) {
100         const CallsPerSched = 1000
101         const LocalWork = 100
102         procs := runtime.GOMAXPROCS(-1)
103         N := int32(b.N / CallsPerSched)
104         c := make(chan bool, procs)
105         for p := 0; p < procs; p++ {
106                 go func() {
107                         foo := 42
108                         for atomic.AddInt32(&N, -1) >= 0 {
109                                 runtime.Gosched()
110                                 for g := 0; g < CallsPerSched; g++ {
111                                         runtime.Entersyscall()
112                                         for i := 0; i < LocalWork; i++ {
113                                                 foo *= 2
114                                                 foo /= 2
115                                         }
116                                         runtime.Exitsyscall()
117                                 }
118                         }
119                         c <- foo == 42
120                 }()
121         }
122         for p := 0; p < procs; p++ {
123                 <-c
124         }
125 }