OSDN Git Service

cffcd1638fcecface8e3002b76fc6b978a73d5bc
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / mallocrep.go
1 // $G $D/$F.go && $L $F.$A && ./$A.out
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 // Repeated malloc test.
8
9 package main
10
11 import (
12         "flag"
13         "runtime"
14 )
15
16 var chatty = flag.Bool("v", false, "chatty")
17
18 var oldsys uint64
19
20 func bigger() {
21         runtime.UpdateMemStats()
22         if st := runtime.MemStats; oldsys < st.Sys {
23                 oldsys = st.Sys
24                 if *chatty {
25                         println(st.Sys, " system bytes for ", st.Alloc, " Go bytes")
26                 }
27                 if st.Sys > 1e9 {
28                         println("too big")
29                         panic("fail")
30                 }
31         }
32 }
33
34 func main() {
35         runtime.GC()               // clean up garbage from init
36         runtime.UpdateMemStats()   // first call can do some allocations
37         runtime.MemProfileRate = 0 // disable profiler
38         runtime.MemStats.Alloc = 0 // ignore stacks
39         flag.Parse()
40         for i := 0; i < 1<<7; i++ {
41                 for j := 1; j <= 1<<22; j <<= 1 {
42                         if i == 0 && *chatty {
43                                 println("First alloc:", j)
44                         }
45                         if a := runtime.MemStats.Alloc; a != 0 {
46                                 println("no allocations but stats report", a, "bytes allocated")
47                                 panic("fail")
48                         }
49                         b := runtime.Alloc(uintptr(j))
50                         runtime.UpdateMemStats()
51                         during := runtime.MemStats.Alloc
52                         runtime.Free(b)
53                         runtime.UpdateMemStats()
54                         if a := runtime.MemStats.Alloc; a != 0 {
55                                 println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
56                                 panic("fail")
57                         }
58                         bigger()
59                 }
60                 if i%(1<<10) == 0 && *chatty {
61                         println(i)
62                 }
63                 if i == 0 {
64                         if *chatty {
65                                 println("Primed", i)
66                         }
67                         //      runtime.frozen = true
68                 }
69         }
70 }