OLD | NEW |
1 // $G $D/$F.go && $L $F.$A && ./$A.out | 1 // $G $D/$F.go && $L $F.$A && ./$A.out |
2 | 2 |
3 // Copyright 2009 The Go Authors. All rights reserved. | 3 // Copyright 2009 The Go Authors. All rights reserved. |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file. | 5 // license that can be found in the LICENSE file. |
6 | 6 |
7 // Repeated malloc test. | 7 // Repeated malloc test. |
8 | 8 |
9 package main | 9 package main |
10 | 10 |
11 import ( | 11 import ( |
12 » "flag"; | 12 » "flag" |
13 » "fmt"; | 13 » "fmt" |
14 » "malloc"; | 14 » "runtime" |
15 "strconv" | 15 "strconv" |
16 ) | 16 ) |
17 | 17 |
18 var chatty = flag.Bool("v", false, "chatty"); | 18 var chatty = flag.Bool("v", false, "chatty") |
19 var reverse = flag.Bool("r", false, "reverse"); | 19 var reverse = flag.Bool("r", false, "reverse") |
20 var longtest = flag.Bool("l", false, "long test"); | 20 var longtest = flag.Bool("l", false, "long test") |
21 | 21 |
22 var b []*byte; | 22 var b []*byte |
23 var stats = malloc.GetStats(); | 23 var stats = &runtime.MemStats |
24 | 24 |
25 func OkAmount(size, n uintptr) bool { | 25 func OkAmount(size, n uintptr) bool { |
26 if n < size { | 26 if n < size { |
27 return false | 27 return false |
28 } | 28 } |
29 if size < 16*8 { | 29 if size < 16*8 { |
30 if n > size+16 { | 30 if n > size+16 { |
31 return false | 31 return false |
32 } | 32 } |
33 } else { | 33 } else { |
34 if n > size*9/8 { | 34 if n > size*9/8 { |
35 return false | 35 return false |
36 } | 36 } |
37 } | 37 } |
38 return true | 38 return true |
39 } | 39 } |
40 | 40 |
41 func AllocAndFree(size, count int) { | 41 func AllocAndFree(size, count int) { |
42 if *chatty { | 42 if *chatty { |
43 » » fmt.Printf("size=%d count=%d ...\n", size, count); | 43 » » fmt.Printf("size=%d count=%d ...\n", size, count) |
44 } | 44 } |
45 » n1 := stats.Alloc; | 45 » n1 := stats.Alloc |
46 for i := 0; i < count; i++ { | 46 for i := 0; i < count; i++ { |
47 » » b[i] = malloc.Alloc(uintptr(size)); | 47 » » b[i] = runtime.Alloc(uintptr(size)) |
48 » » base, n := malloc.Lookup(b[i]); | 48 » » base, n := runtime.Lookup(b[i]) |
49 if base != b[i] || !OkAmount(uintptr(size), n) { | 49 if base != b[i] || !OkAmount(uintptr(size), n) { |
50 » » » panicln("lookup failed: got", base, n, "for", b[i]); | 50 » » » panicln("lookup failed: got", base, n, "for", b[i]) |
51 } | 51 } |
52 » » if malloc.GetStats().Sys > 1e9 { | 52 » » if runtime.MemStats.Sys > 1e9 { |
53 » » » panicln("too much memory allocated"); | 53 » » » panicln("too much memory allocated") |
54 } | 54 } |
55 } | 55 } |
56 » n2 := stats.Alloc; | 56 » n2 := stats.Alloc |
57 if *chatty { | 57 if *chatty { |
58 » » fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats); | 58 » » fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats) |
59 } | 59 } |
60 » n3 := stats.Alloc; | 60 » n3 := stats.Alloc |
61 for j := 0; j < count; j++ { | 61 for j := 0; j < count; j++ { |
62 » » i := j; | 62 » » i := j |
63 if *reverse { | 63 if *reverse { |
64 » » » i = count - 1 - j; | 64 » » » i = count - 1 - j |
65 } | 65 } |
66 » » alloc := uintptr(stats.Alloc); | 66 » » alloc := uintptr(stats.Alloc) |
67 » » base, n := malloc.Lookup(b[i]); | 67 » » base, n := runtime.Lookup(b[i]) |
68 if base != b[i] || !OkAmount(uintptr(size), n) { | 68 if base != b[i] || !OkAmount(uintptr(size), n) { |
69 » » » panicln("lookup failed: got", base, n, "for", b[i]); | 69 » » » panicln("lookup failed: got", base, n, "for", b[i]) |
70 } | 70 } |
71 » » malloc.Free(b[i]); | 71 » » runtime.Free(b[i]) |
72 » » if stats.Alloc != uint64(alloc - n) { | 72 » » if stats.Alloc != uint64(alloc-n) { |
73 » » » panicln("free alloc got", stats.Alloc, "expected", alloc
- n, "after free of", n); | 73 » » » panicln("free alloc got", stats.Alloc, "expected", alloc
-n, "after free of", n) |
74 } | 74 } |
75 » » if malloc.GetStats().Sys > 1e9 { | 75 » » if runtime.MemStats.Sys > 1e9 { |
76 » » » panicln("too much memory allocated"); | 76 » » » panicln("too much memory allocated") |
77 } | 77 } |
78 } | 78 } |
79 » n4 := stats.Alloc; | 79 » n4 := stats.Alloc |
80 | 80 |
81 if *chatty { | 81 if *chatty { |
82 » » fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats); | 82 » » fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats) |
83 } | 83 } |
84 if n2-n1 != n3-n4 { | 84 if n2-n1 != n3-n4 { |
85 » » panicln("wrong alloc count: ", n2-n1, n3-n4); | 85 » » panicln("wrong alloc count: ", n2-n1, n3-n4) |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 func atoi(s string) int { | 89 func atoi(s string) int { |
90 » i, _ := strconv.Atoi(s); | 90 » i, _ := strconv.Atoi(s) |
91 return i | 91 return i |
92 } | 92 } |
93 | 93 |
94 func main() { | 94 func main() { |
95 » flag.Parse(); | 95 » flag.Parse() |
96 » b = make([]*byte, 10000); | 96 » b = make([]*byte, 10000) |
97 if flag.NArg() > 0 { | 97 if flag.NArg() > 0 { |
98 » » AllocAndFree(atoi(flag.Arg(0)), atoi(flag.Arg(1))); | 98 » » AllocAndFree(atoi(flag.Arg(0)), atoi(flag.Arg(1))) |
99 » » return; | 99 » » return |
100 } | 100 } |
101 » maxb := 1<<22; | 101 » maxb := 1 << 22 |
102 if !*longtest { | 102 if !*longtest { |
103 » » maxb = 1<<19; | 103 » » maxb = 1 << 19 |
104 } | 104 } |
105 » for j := 1; j <= maxb; j<<=1 { | 105 » for j := 1; j <= maxb; j <<= 1 { |
106 » » n := len(b); | 106 » » n := len(b) |
107 » » max := uintptr(1<<28); | 107 » » max := uintptr(1 << 28) |
108 if !*longtest { | 108 if !*longtest { |
109 » » » max = uintptr(maxb); | 109 » » » max = uintptr(maxb) |
110 } | 110 } |
111 if uintptr(j)*uintptr(n) > max { | 111 if uintptr(j)*uintptr(n) > max { |
112 » » » n = int(max / uintptr(j)); | 112 » » » n = int(max / uintptr(j)) |
113 } | 113 } |
114 if n < 10 { | 114 if n < 10 { |
115 » » » n = 10; | 115 » » » n = 10 |
116 } | 116 } |
117 for m := 1; m <= n; { | 117 for m := 1; m <= n; { |
118 » » » AllocAndFree(j, m); | 118 » » » AllocAndFree(j, m) |
119 if m == n { | 119 if m == n { |
120 break | 120 break |
121 } | 121 } |
122 » » » m = 5*m/4; | 122 » » » m = 5 * m / 4 |
123 if m < 4 { | 123 if m < 4 { |
124 m++ | 124 m++ |
125 } | 125 } |
126 if m > n { | 126 if m > n { |
127 m = n | 127 m = n |
128 } | 128 } |
129 } | 129 } |
130 } | 130 } |
131 } | 131 } |
OLD | NEW |