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.
16 func TestGcSys(t
*testing
.T
) {
17 /* gccgo does not have a go command
18 if os.Getenv("GOGC") == "off" {
19 t.Skip("skipping test; GOGC=off in environment")
21 data := struct{ Short bool }{testing.Short()}
22 got := executeTest(t, testGCSysSource, &data)
25 t.Fatalf("expected %q, but got %q", want, got)
30 const testGCSysSource
= `
40 memstats := new(runtime.MemStats)
42 runtime.ReadMemStats(memstats)
45 runtime.MemProfileRate = 0 // disable profiler
51 for i := 0; i < itercount; i++ {
55 // Should only be using a few MB.
56 // We allocated 100 MB or (if not short) 1 GB.
57 runtime.ReadMemStats(memstats)
58 if sys > memstats.Sys {
61 sys = memstats.Sys - sys
64 fmt.Printf("using too much memory: %d bytes\n", sys)
70 func workthegc() []byte {
71 return make([]byte, 1029)
75 func TestGcDeepNesting(t
*testing
.T
) {
76 type T
[2][2][2][2][2][2][2][2][2][2]*int
79 // Prevent the compiler from applying escape analysis.
80 // This makes sure new(T) is allocated on heap, not on the stack.
83 a
[0][0][0][0][0][0][0][0][0][0] = new(int)
84 *a
[0][0][0][0][0][0][0][0][0][0] = 13
86 if *a
[0][0][0][0][0][0][0][0][0][0] != 13 {
91 func TestGcHashmapIndirection(t
*testing
.T
) {
92 defer debug
.SetGCPercent(debug
.SetGCPercent(1))
98 for i
:= 0; i
< 2000; i
++ {
105 func TestGcArraySlice(t
*testing
.T
) {
112 for i
:= 0; i
< 10; i
++ {
117 p
.nextbuf
= head
.buf
[:]
122 for p
:= head
; p
!= nil; p
= p
.next
{
124 t
.Fatal("corrupted heap")
129 func TestGcRescan(t
*testing
.T
) {
140 for i
:= 0; i
< 10; i
++ {
142 p
.c
= make(chan error
)
152 for p
:= head
; p
!= nil; p
= p
.nexty
{
154 t
.Fatal("corrupted heap")
159 func TestGcLastTime(t
*testing
.T
) {
160 ms
:= new(runtime
.MemStats
)
161 t0
:= time
.Now().UnixNano()
163 t1
:= time
.Now().UnixNano()
164 runtime
.ReadMemStats(ms
)
165 last
:= int64(ms
.LastGC
)
166 if t0
> last || last
> t1
{
167 t
.Fatalf("bad last GC time: got %v, want [%v, %v]", last
, t0
, t1
)
169 pause
:= ms
.PauseNs
[(ms
.NumGC
+255)%256
]
170 // Due to timer granularity, pause can actually be 0 on windows
171 // or on virtualized environments.
173 t
.Logf("last GC pause was 0")
174 } else if pause
> 10e9
{
175 t
.Logf("bad last GC pause: got %v, want [0, 10e9]", pause
)
179 var hugeSink
interface{}
181 func TestHugeGCInfo(t
*testing
.T
) {
182 // The test ensures that compiler can chew these huge types even on weakest machines.
183 // The types are not allocated at runtime.
185 // 400MB on 32 bots, 4TB on 64-bits.
186 const n
= (400 << 20) + (unsafe
.Sizeof(uintptr(0))-4)<<40
187 hugeSink
= new([n
]*byte)
188 hugeSink
= new([n
]uintptr)
189 hugeSink
= new(struct {
194 hugeSink
= new(struct {
202 func BenchmarkSetTypeNoPtr1(b
*testing
.B
) {
207 for i
:= 0; i
< b
.N
; i
++ {
212 func BenchmarkSetTypeNoPtr2(b
*testing
.B
) {
217 for i
:= 0; i
< b
.N
; i
++ {
222 func BenchmarkSetTypePtr1(b
*testing
.B
) {
227 for i
:= 0; i
< b
.N
; i
++ {
232 func BenchmarkSetTypePtr2(b
*testing
.B
) {
237 for i
:= 0; i
< b
.N
; i
++ {
243 func BenchmarkAllocation(b
*testing
.B
) {
247 ngo
:= runtime
.GOMAXPROCS(0)
248 work
:= make(chan bool, b
.N
+ngo
)
249 result
:= make(chan *T
)
250 for i
:= 0; i
< b
.N
; i
++ {
253 for i
:= 0; i
< ngo
; i
++ {
256 for i
:= 0; i
< ngo
; i
++ {
260 for i
:= 0; i
< 1000; i
++ {
267 for i
:= 0; i
< ngo
; i
++ {
272 func TestPrintGC(t
*testing
.T
) {
274 t
.Skip("Skipping in short mode")
276 defer runtime
.GOMAXPROCS(runtime
.GOMAXPROCS(2))
277 done
:= make(chan bool)
288 for i
:= 0; i
< 1e4
; i
++ {