diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/malloc1.go | 26 | ||||
-rw-r--r-- | test/mallocrand.go | 86 | ||||
-rw-r--r-- | test/mallocrep.go | 56 |
3 files changed, 168 insertions, 0 deletions
diff --git a/test/malloc1.go b/test/malloc1.go new file mode 100644 index 000000000..fe1a5c0d5 --- /dev/null +++ b/test/malloc1.go @@ -0,0 +1,26 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// trivial malloc test + +package main + +import ( + "flag"; + "fmt"; + "malloc"; +) + +var chatty bool; +var chatty_flag = flag.Bool("v", false, &chatty, "chatty"); + +func main() { + malloc.Free(malloc.Alloc(1)); + if chatty { + fmt.printf("%+v %v\n", *malloc.GetStats(), uint64(0)); + } +} + diff --git a/test/mallocrand.go b/test/mallocrand.go new file mode 100644 index 000000000..63e70f378 --- /dev/null +++ b/test/mallocrand.go @@ -0,0 +1,86 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Random malloc test. + +package main + +import ( + "flag"; + "malloc"; + "rand"; + "unsafe"; +) + +var chatty bool; +var chatty_flag = flag.Bool("v", false, &chatty, "chatty"); + +var footprint uint64; +var allocated uint64; +func bigger() { + if f := malloc.GetStats().sys; footprint < f { + footprint = f; + if chatty { + println("Footprint", footprint, " for ", allocated); + } + if footprint > 1e9 { + panicln("too big"); + } + } +} + +// Prime the data structures by allocating one of +// each block in order. After this, there should be +// little reason to ask for more memory from the OS. +func prime() { + for i := 0; i < 16; i++ { + b := malloc.Alloc(1<<uint(i)); + malloc.Free(b); + } + for i := uint64(0); i < 256; i++ { + b := malloc.Alloc(i<<12); + malloc.Free(b); + } +} + +func memset(b *byte, c byte, n uint64) { + np := uintptr(n); + for i := uintptr(0); i < np; i++ { + *(b.(unsafe.pointer).(uintptr)+i).(unsafe.pointer).(*byte) = c; + } +} + +func main() { + flag.Parse(); +// prime(); + var blocks [1] struct { base *byte; siz uint64; }; + for i := 0; i < 1<<12; i++ { + if i%(1<<10) == 0 && chatty { + println(i); + } + b := rand.rand() % len(blocks); + if blocks[b].base != nil { + // println("Free", blocks[b].siz, blocks[b].base); + malloc.Free(blocks[b].base); + blocks[b].base = nil; + allocated -= blocks[b].siz; + continue + } + siz := uint64(rand.rand() >> (11 + rand.urand32() % 20)); + base := malloc.Alloc(siz); + // ptr := uint64(syscall.BytePtr(base))+uint64(siz/2); + // obj, size, ref, ok := allocator.find(ptr); + // if obj != base || *ref != 0 || !ok { + // panicln("find", siz, obj, ref, ok); + // } + blocks[b].base = base; + blocks[b].siz = siz; + allocated += siz; + // println("Alloc", siz, base); + memset(base, 0xbb, siz); + bigger(); + } +} diff --git a/test/mallocrep.go b/test/mallocrep.go new file mode 100644 index 000000000..2911b4a05 --- /dev/null +++ b/test/mallocrep.go @@ -0,0 +1,56 @@ +// $G $D/$F.go && $L $F.$A && ./$A.out + +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Repeated malloc test. + +package main + +import ( + "flag"; + "malloc" +) + +var chatty bool; +var chatty_flag = flag.Bool("v", false, &chatty, "chatty"); + +var oldsys uint64; +func bigger() { + if st := malloc.GetStats(); oldsys < st.sys { + oldsys = st.sys; + if chatty { + println(st.sys, " system bytes for ", st.alloc, " Go bytes"); + } + if st.sys > 1e9 { + panicln("too big"); + } + } +} + +func main() { + flag.Parse(); + for i := 0; i < 1<<8; i++ { + for j := 1; j <= 1<<22; j<<=1 { + if i == 0 && chatty { + println("First alloc:", j); + } + b := malloc.Alloc(uint64(j)); + malloc.Free(b); + if a := malloc.GetStats().alloc; a != 0 { + panicln("malloc wrong count", a); + } + bigger(); + } + if i%(1<<10) == 0 && chatty { + println(i); + } + if i == 0 { + if chatty { + println("Primed", i); + } + // malloc.frozen = true; + } + } +} |