Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 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 ( |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 12 | "flag" |
| 13 | "fmt" |
| 14 | "runtime" |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 15 | "strconv" |
| 16 | ) |
| 17 | |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 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") |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 21 | |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 22 | var b []*byte |
| 23 | var stats = &runtime.MemStats |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 24 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 25 | func OkAmount(size, n uintptr) bool { |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 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 | |
Russ Cox | 839a684 | 2009-01-20 14:40:40 -0800 | [diff] [blame] | 41 | func AllocAndFree(size, count int) { |
Rob Pike | c45d2a7 | 2009-01-09 13:42:46 -0800 | [diff] [blame] | 42 | if *chatty { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 43 | fmt.Printf("size=%d count=%d ...\n", size, count) |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 44 | } |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 45 | n1 := stats.Alloc |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 46 | for i := 0; i < count; i++ { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 47 | b[i] = runtime.Alloc(uintptr(size)) |
| 48 | base, n := runtime.Lookup(b[i]) |
Ian Lance Taylor | 03c40f5 | 2009-01-16 15:03:22 -0800 | [diff] [blame] | 49 | if base != b[i] || !OkAmount(uintptr(size), n) { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 50 | println("lookup failed: got", base, n, "for", b[i]) |
| 51 | panic("fail") |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 52 | } |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 53 | if runtime.MemStats.Sys > 1e9 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 54 | println("too much memory allocated") |
| 55 | panic("fail") |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 58 | n2 := stats.Alloc |
Rob Pike | c45d2a7 | 2009-01-09 13:42:46 -0800 | [diff] [blame] | 59 | if *chatty { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 60 | fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats) |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 61 | } |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 62 | n3 := stats.Alloc |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 63 | for j := 0; j < count; j++ { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 64 | i := j |
Rob Pike | c45d2a7 | 2009-01-09 13:42:46 -0800 | [diff] [blame] | 65 | if *reverse { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 66 | i = count - 1 - j |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 67 | } |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 68 | alloc := uintptr(stats.Alloc) |
| 69 | base, n := runtime.Lookup(b[i]) |
Ian Lance Taylor | 03c40f5 | 2009-01-16 15:03:22 -0800 | [diff] [blame] | 70 | if base != b[i] || !OkAmount(uintptr(size), n) { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 71 | println("lookup failed: got", base, n, "for", b[i]) |
| 72 | panic("fail") |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 73 | } |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 74 | runtime.Free(b[i]) |
| 75 | if stats.Alloc != uint64(alloc-n) { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 76 | println("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n) |
| 77 | panic("fail") |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 78 | } |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 79 | if runtime.MemStats.Sys > 1e9 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 80 | println("too much memory allocated") |
| 81 | panic("fail") |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 82 | } |
| 83 | } |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 84 | n4 := stats.Alloc |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 85 | |
Rob Pike | c45d2a7 | 2009-01-09 13:42:46 -0800 | [diff] [blame] | 86 | if *chatty { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 87 | fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats) |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 88 | } |
| 89 | if n2-n1 != n3-n4 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 90 | println("wrong alloc count: ", n2-n1, n3-n4) |
| 91 | panic("fail") |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | func atoi(s string) int { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 96 | i, _ := strconv.Atoi(s) |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 97 | return i |
| 98 | } |
| 99 | |
| 100 | func main() { |
Russ Cox | 6eb251f | 2010-03-24 09:40:09 -0700 | [diff] [blame] | 101 | runtime.MemProfileRate = 0 // disable profiler |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 102 | flag.Parse() |
| 103 | b = make([]*byte, 10000) |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 104 | if flag.NArg() > 0 { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 105 | AllocAndFree(atoi(flag.Arg(0)), atoi(flag.Arg(1))) |
| 106 | return |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 107 | } |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 108 | maxb := 1 << 22 |
Russ Cox | ebd27d6 | 2009-10-09 11:18:32 -0700 | [diff] [blame] | 109 | if !*longtest { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 110 | maxb = 1 << 19 |
Russ Cox | ebd27d6 | 2009-10-09 11:18:32 -0700 | [diff] [blame] | 111 | } |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 112 | for j := 1; j <= maxb; j <<= 1 { |
| 113 | n := len(b) |
| 114 | max := uintptr(1 << 28) |
Rob Pike | c45d2a7 | 2009-01-09 13:42:46 -0800 | [diff] [blame] | 115 | if !*longtest { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 116 | max = uintptr(maxb) |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 117 | } |
Russ Cox | b014be7 | 2009-06-05 10:59:37 -0700 | [diff] [blame] | 118 | if uintptr(j)*uintptr(n) > max { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 119 | n = int(max / uintptr(j)) |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 120 | } |
| 121 | if n < 10 { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 122 | n = 10 |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 123 | } |
| 124 | for m := 1; m <= n; { |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 125 | AllocAndFree(j, m) |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 126 | if m == n { |
| 127 | break |
| 128 | } |
Russ Cox | 33e396a | 2010-02-03 16:31:34 -0800 | [diff] [blame] | 129 | m = 5 * m / 4 |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 130 | if m < 4 { |
| 131 | m++ |
| 132 | } |
| 133 | if m > n { |
| 134 | m = n |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |