Russ Cox | e29ce17 | 2008-12-18 15:42:28 -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 | // Random malloc test. |
| 8 | |
| 9 | package main |
| 10 | |
| 11 | import ( |
| 12 | "flag"; |
| 13 | "malloc"; |
| 14 | "rand"; |
| 15 | "unsafe"; |
| 16 | ) |
| 17 | |
Rob Pike | c45d2a7 | 2009-01-09 13:42:46 -0800 | [diff] [blame] | 18 | var chatty = flag.Bool("v", false, "chatty"); |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 19 | |
| 20 | var footprint uint64; |
| 21 | var allocated uint64; |
| 22 | func bigger() { |
Russ Cox | f48cbfd | 2009-01-16 16:12:14 -0800 | [diff] [blame] | 23 | if f := malloc.GetStats().Sys; footprint < f { |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 24 | footprint = f; |
Rob Pike | c45d2a7 | 2009-01-09 13:42:46 -0800 | [diff] [blame] | 25 | if *chatty { |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 26 | println("Footprint", footprint, " for ", allocated); |
| 27 | } |
| 28 | if footprint > 1e9 { |
| 29 | panicln("too big"); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | // Prime the data structures by allocating one of |
| 35 | // each block in order. After this, there should be |
| 36 | // little reason to ask for more memory from the OS. |
| 37 | func prime() { |
| 38 | for i := 0; i < 16; i++ { |
| 39 | b := malloc.Alloc(1<<uint(i)); |
| 40 | malloc.Free(b); |
| 41 | } |
| 42 | for i := uint64(0); i < 256; i++ { |
| 43 | b := malloc.Alloc(i<<12); |
| 44 | malloc.Free(b); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func memset(b *byte, c byte, n uint64) { |
| 49 | np := uintptr(n); |
| 50 | for i := uintptr(0); i < np; i++ { |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 51 | *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(b))+i)) = c; |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | func main() { |
| 56 | flag.Parse(); |
| 57 | // prime(); |
| 58 | var blocks [1] struct { base *byte; siz uint64; }; |
| 59 | for i := 0; i < 1<<12; i++ { |
Rob Pike | c45d2a7 | 2009-01-09 13:42:46 -0800 | [diff] [blame] | 60 | if i%(1<<10) == 0 && *chatty { |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 61 | println(i); |
| 62 | } |
Russ Cox | f48cbfd | 2009-01-16 16:12:14 -0800 | [diff] [blame] | 63 | b := rand.Int() % len(blocks); |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 64 | if blocks[b].base != nil { |
| 65 | // println("Free", blocks[b].siz, blocks[b].base); |
| 66 | malloc.Free(blocks[b].base); |
| 67 | blocks[b].base = nil; |
| 68 | allocated -= blocks[b].siz; |
| 69 | continue |
| 70 | } |
Russ Cox | f48cbfd | 2009-01-16 16:12:14 -0800 | [diff] [blame] | 71 | siz := uint64(rand.Int() >> (11 + rand.Uint32() % 20)); |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 72 | base := malloc.Alloc(siz); |
| 73 | // ptr := uint64(syscall.BytePtr(base))+uint64(siz/2); |
| 74 | // obj, size, ref, ok := allocator.find(ptr); |
| 75 | // if obj != base || *ref != 0 || !ok { |
| 76 | // panicln("find", siz, obj, ref, ok); |
| 77 | // } |
| 78 | blocks[b].base = base; |
| 79 | blocks[b].siz = siz; |
| 80 | allocated += siz; |
| 81 | // println("Alloc", siz, base); |
| 82 | memset(base, 0xbb, siz); |
| 83 | bigger(); |
| 84 | } |
| 85 | } |