OSDN Git Service

2012-03-10 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / mallocrep1.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         "fmt"
14         "runtime"
15         "strconv"
16 )
17
18 var chatty = flag.Bool("v", false, "chatty")
19 var reverse = flag.Bool("r", false, "reverse")
20 var longtest = flag.Bool("l", false, "long test")
21
22 var b []*byte
23 var stats = new(runtime.MemStats)
24
25 func OkAmount(size, n uintptr) bool {
26         if n < size {
27                 return false
28         }
29         if size < 16*8 {
30                 if n > size+16 {
31                         return false
32                 }
33         } else {
34                 if n > size*9/8 {
35                         return false
36                 }
37         }
38         return true
39 }
40
41 func AllocAndFree(size, count int) {
42         if *chatty {
43                 fmt.Printf("size=%d count=%d ...\n", size, count)
44         }
45         runtime.ReadMemStats(stats)
46         n1 := stats.Alloc
47         for i := 0; i < count; i++ {
48                 b[i] = runtime.Alloc(uintptr(size))
49                 base, n := runtime.Lookup(b[i])
50                 if base != b[i] || !OkAmount(uintptr(size), n) {
51                         println("lookup failed: got", base, n, "for", b[i])
52                         panic("fail")
53                 }
54                 runtime.ReadMemStats(stats)
55                 if stats.Sys > 1e9 {
56                         println("too much memory allocated")
57                         panic("fail")
58                 }
59         }
60         runtime.ReadMemStats(stats)
61         n2 := stats.Alloc
62         if *chatty {
63                 fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
64         }
65         n3 := stats.Alloc
66         for j := 0; j < count; j++ {
67                 i := j
68                 if *reverse {
69                         i = count - 1 - j
70                 }
71                 alloc := uintptr(stats.Alloc)
72                 base, n := runtime.Lookup(b[i])
73                 if base != b[i] || !OkAmount(uintptr(size), n) {
74                         println("lookup failed: got", base, n, "for", b[i])
75                         panic("fail")
76                 }
77                 runtime.Free(b[i])
78                 runtime.ReadMemStats(stats)
79                 if stats.Alloc != uint64(alloc-n) {
80                         println("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n)
81                         panic("fail")
82                 }
83                 if stats.Sys > 1e9 {
84                         println("too much memory allocated")
85                         panic("fail")
86                 }
87         }
88         runtime.ReadMemStats(stats)
89         n4 := stats.Alloc
90
91         if *chatty {
92                 fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
93         }
94         if n2-n1 != n3-n4 {
95                 println("wrong alloc count: ", n2-n1, n3-n4)
96                 panic("fail")
97         }
98 }
99
100 func atoi(s string) int {
101         i, _ := strconv.Atoi(s)
102         return i
103 }
104
105 func main() {
106         runtime.MemProfileRate = 0 // disable profiler
107         flag.Parse()
108         b = make([]*byte, 10000)
109         if flag.NArg() > 0 {
110                 AllocAndFree(atoi(flag.Arg(0)), atoi(flag.Arg(1)))
111                 return
112         }
113         maxb := 1 << 22
114         if !*longtest {
115                 maxb = 1 << 19
116         }
117         for j := 1; j <= maxb; j <<= 1 {
118                 n := len(b)
119                 max := uintptr(1 << 28)
120                 if !*longtest {
121                         max = uintptr(maxb)
122                 }
123                 if uintptr(j)*uintptr(n) > max {
124                         n = int(max / uintptr(j))
125                 }
126                 if n < 10 {
127                         n = 10
128                 }
129                 for m := 1; m <= n; {
130                         AllocAndFree(j, m)
131                         if m == n {
132                                 break
133                         }
134                         m = 5 * m / 4
135                         if m < 4 {
136                                 m++
137                         }
138                         if m > n {
139                                 m = n
140                         }
141                 }
142         }
143 }