diff options
author | Russ Cox <rsc@golang.org> | 2010-02-03 16:31:34 -0800 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2010-02-03 16:31:34 -0800 |
commit | 3732daa8e7c635ba306dd923a4342650524320df (patch) | |
tree | 31d454f4c7a1d0189ddbd9b1e771c8716655dc72 /test | |
parent | 9c681e46bfdf7a2745d2df35412592e8c13e0fce (diff) | |
download | golang-3732daa8e7c635ba306dd923a4342650524320df.tar.gz |
finalizers; merge package malloc into package runtime
R=r, cw
CC=golang-dev
http://codereview.appspot.com/198085
Diffstat (limited to 'test')
-rw-r--r-- | test/gc.go | 16 | ||||
-rw-r--r-- | test/malloc1.go | 13 | ||||
-rw-r--r-- | test/mallocfin.go | 58 | ||||
-rw-r--r-- | test/mallocrand.go | 82 | ||||
-rw-r--r-- | test/mallocrep.go | 47 | ||||
-rw-r--r-- | test/mallocrep1.go | 92 |
6 files changed, 184 insertions, 124 deletions
diff --git a/test/gc.go b/test/gc.go index 0b1dd6374..864d05c39 100644 --- a/test/gc.go +++ b/test/gc.go @@ -6,21 +6,19 @@ package main -import "malloc" +import "runtime" func mk2() { - b := new([10000]byte); - _ = b; -// println(b, "stored at", &b); + b := new([10000]byte) + _ = b + // println(b, "stored at", &b); } -func mk1() { - mk2(); -} +func mk1() { mk2() } func main() { for i := 0; i < 10; i++ { - mk1(); - malloc.GC(); + mk1() + runtime.GC() } } diff --git a/test/malloc1.go b/test/malloc1.go index 62329fe57..146976467 100644 --- a/test/malloc1.go +++ b/test/malloc1.go @@ -9,17 +9,16 @@ package main import ( - "flag"; - "fmt"; - "malloc"; + "flag" + "fmt" + "runtime" ) -var chatty = flag.Bool("v", false, "chatty"); +var chatty = flag.Bool("v", false, "chatty") func main() { - malloc.Free(malloc.Alloc(1)); + runtime.Free(runtime.Alloc(1)) if *chatty { - fmt.Printf("%+v %v\n", *malloc.GetStats(), uint64(0)); + fmt.Printf("%+v %v\n", runtime.MemStats, uint64(0)) } } - diff --git a/test/mallocfin.go b/test/mallocfin.go new file mode 100644 index 000000000..4c832583e --- /dev/null +++ b/test/mallocfin.go @@ -0,0 +1,58 @@ +// $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 finalizer test + +package main + +import "runtime" + +const N = 250 + +type A struct { + b *B + n int +} + +type B struct { + n int +} + +var i int +var nfinal int +var final [N]int + +func finalA(a *A) { + if final[a.n] != 0 { + panicln("finalA", a.n, final[a.n]) + } + final[a.n] = 1 +} + +func finalB(b *B) { + if final[b.n] != 1 { + panicln("finalB", b.n, final[b.n]) + } + final[b.n] = 2 + nfinal++ +} + +func main() { + runtime.GOMAXPROCS(4) + for i = 0; i < N; i++ { + b := &B{i} + a := &A{b, i} + runtime.SetFinalizer(b, finalB) + runtime.SetFinalizer(a, finalA) + } + for i := 0; i < N; i++ { + runtime.GC() + runtime.Gosched() + } + if nfinal < N*9/10 { + panic("not enough finalizing:", nfinal, "/", N) + } +} diff --git a/test/mallocrand.go b/test/mallocrand.go index c0184699f..8129926da 100644 --- a/test/mallocrand.go +++ b/test/mallocrand.go @@ -9,24 +9,25 @@ package main import ( - "flag"; - "malloc"; - "rand"; - "unsafe"; + "flag" + "rand" + "runtime" + "unsafe" ) -var chatty = flag.Bool("v", false, "chatty"); +var chatty = flag.Bool("v", false, "chatty") + +var footprint uint64 +var allocated uint64 -var footprint uint64; -var allocated uint64; func bigger() { - if f := malloc.GetStats().Sys; footprint < f { - footprint = f; + if f := runtime.MemStats.Sys; footprint < f { + footprint = f if *chatty { - println("Footprint", footprint, " for ", allocated); + println("Footprint", footprint, " for ", allocated) } if footprint > 1e9 { - panicln("too big"); + panicln("too big") } } } @@ -36,50 +37,53 @@ func bigger() { // 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); + b := runtime.Alloc(1 << uint(i)) + runtime.Free(b) } for i := uintptr(0); i < 256; i++ { - b := malloc.Alloc(i<<12); - malloc.Free(b); + b := runtime.Alloc(i << 12) + runtime.Free(b) } } func memset(b *byte, c byte, n uintptr) { - np := uintptr(n); + np := uintptr(n) for i := uintptr(0); i < np; i++ { - *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(b))+i)) = c; + *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(b)) + i)) = c } } func main() { - flag.Parse(); -// prime(); - var blocks [1] struct { base *byte; siz uintptr; }; + flag.Parse() + // prime(); + var blocks [1]struct { + base *byte + siz uintptr + } for i := 0; i < 1<<10; i++ { if i%(1<<10) == 0 && *chatty { - println(i); + println(i) } - b := rand.Int() % len(blocks); + b := rand.Int() % 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 -= uint64(blocks[b].siz); + // println("Free", blocks[b].siz, blocks[b].base); + runtime.Free(blocks[b].base) + blocks[b].base = nil + allocated -= uint64(blocks[b].siz) continue } - siz := uintptr(rand.Int() >> (11 + rand.Uint32() % 20)); - base := malloc.Alloc(siz); - // ptr := uintptr(syscall.BytePtr(base))+uintptr(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 += uint64(siz); - // println("Alloc", siz, base); - memset(base, 0xbb, siz); - bigger(); + siz := uintptr(rand.Int() >> (11 + rand.Uint32()%20)) + base := runtime.Alloc(siz) + // ptr := uintptr(syscall.BytePtr(base))+uintptr(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 += uint64(siz) + // println("Alloc", siz, base); + memset(base, 0xbb, siz) + bigger() } } diff --git a/test/mallocrep.go b/test/mallocrep.go index 5367787e9..5e1314ef5 100644 --- a/test/mallocrep.go +++ b/test/mallocrep.go @@ -9,52 +9,53 @@ package main import ( - "flag"; - "malloc" + "flag" + "runtime" ) -var chatty = flag.Bool("v", false, "chatty"); +var chatty = flag.Bool("v", false, "chatty") + +var oldsys uint64 -var oldsys uint64; func bigger() { - if st := malloc.GetStats(); oldsys < st.Sys { - oldsys = st.Sys; + if st := runtime.MemStats; oldsys < st.Sys { + oldsys = st.Sys if *chatty { - println(st.Sys, " system bytes for ", st.Alloc, " Go bytes"); + println(st.Sys, " system bytes for ", st.Alloc, " Go bytes") } if st.Sys > 1e9 { - panicln("too big"); + panicln("too big") } } } func main() { - flag.Parse(); - malloc.GetStats().Alloc = 0; // ignore stacks + flag.Parse() + runtime.MemStats.Alloc = 0 // ignore stacks for i := 0; i < 1<<7; i++ { - for j := 1; j <= 1<<22; j<<=1 { + for j := 1; j <= 1<<22; j <<= 1 { if i == 0 && *chatty { - println("First alloc:", j); + println("First alloc:", j) } - if a := malloc.GetStats().Alloc; a != 0 { - panicln("no allocations but stats report", a, "bytes allocated"); + if a := runtime.MemStats.Alloc; a != 0 { + panicln("no allocations but stats report", a, "bytes allocated") } - b := malloc.Alloc(uintptr(j)); - during := malloc.GetStats().Alloc; - malloc.Free(b); - if a := malloc.GetStats().Alloc; a != 0 { - panic("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)"); + b := runtime.Alloc(uintptr(j)) + during := runtime.MemStats.Alloc + runtime.Free(b) + if a := runtime.MemStats.Alloc; a != 0 { + panic("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)") } - bigger(); + bigger() } if i%(1<<10) == 0 && *chatty { - println(i); + println(i) } if i == 0 { if *chatty { - println("Primed", i); + println("Primed", i) } - // malloc.frozen = true; + // runtime.frozen = true; } } } diff --git a/test/mallocrep1.go b/test/mallocrep1.go index 7552e99b4..d7c937f11 100644 --- a/test/mallocrep1.go +++ b/test/mallocrep1.go @@ -9,18 +9,18 @@ package main import ( - "flag"; - "fmt"; - "malloc"; + "flag" + "fmt" + "runtime" "strconv" ) -var chatty = flag.Bool("v", false, "chatty"); -var reverse = flag.Bool("r", false, "reverse"); -var longtest = flag.Bool("l", false, "long test"); +var chatty = flag.Bool("v", false, "chatty") +var reverse = flag.Bool("r", false, "reverse") +var longtest = flag.Bool("l", false, "long test") -var b []*byte; -var stats = malloc.GetStats(); +var b []*byte +var stats = &runtime.MemStats func OkAmount(size, n uintptr) bool { if n < size { @@ -40,86 +40,86 @@ func OkAmount(size, n uintptr) bool { func AllocAndFree(size, count int) { if *chatty { - fmt.Printf("size=%d count=%d ...\n", size, count); + fmt.Printf("size=%d count=%d ...\n", size, count) } - n1 := stats.Alloc; + n1 := stats.Alloc for i := 0; i < count; i++ { - b[i] = malloc.Alloc(uintptr(size)); - base, n := malloc.Lookup(b[i]); + b[i] = runtime.Alloc(uintptr(size)) + base, n := runtime.Lookup(b[i]) if base != b[i] || !OkAmount(uintptr(size), n) { - panicln("lookup failed: got", base, n, "for", b[i]); + panicln("lookup failed: got", base, n, "for", b[i]) } - if malloc.GetStats().Sys > 1e9 { - panicln("too much memory allocated"); + if runtime.MemStats.Sys > 1e9 { + panicln("too much memory allocated") } } - n2 := stats.Alloc; + n2 := stats.Alloc if *chatty { - fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats); + fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats) } - n3 := stats.Alloc; + n3 := stats.Alloc for j := 0; j < count; j++ { - i := j; + i := j if *reverse { - i = count - 1 - j; + i = count - 1 - j } - alloc := uintptr(stats.Alloc); - base, n := malloc.Lookup(b[i]); + alloc := uintptr(stats.Alloc) + base, n := runtime.Lookup(b[i]) if base != b[i] || !OkAmount(uintptr(size), n) { - panicln("lookup failed: got", base, n, "for", b[i]); + panicln("lookup failed: got", base, n, "for", b[i]) } - malloc.Free(b[i]); - if stats.Alloc != uint64(alloc - n) { - panicln("free alloc got", stats.Alloc, "expected", alloc - n, "after free of", n); + runtime.Free(b[i]) + if stats.Alloc != uint64(alloc-n) { + panicln("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n) } - if malloc.GetStats().Sys > 1e9 { - panicln("too much memory allocated"); + if runtime.MemStats.Sys > 1e9 { + panicln("too much memory allocated") } } - n4 := stats.Alloc; + n4 := stats.Alloc if *chatty { - fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats); + fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats) } if n2-n1 != n3-n4 { - panicln("wrong alloc count: ", n2-n1, n3-n4); + panicln("wrong alloc count: ", n2-n1, n3-n4) } } func atoi(s string) int { - i, _ := strconv.Atoi(s); + i, _ := strconv.Atoi(s) return i } func main() { - flag.Parse(); - b = make([]*byte, 10000); + flag.Parse() + b = make([]*byte, 10000) if flag.NArg() > 0 { - AllocAndFree(atoi(flag.Arg(0)), atoi(flag.Arg(1))); - return; + AllocAndFree(atoi(flag.Arg(0)), atoi(flag.Arg(1))) + return } - maxb := 1<<22; + maxb := 1 << 22 if !*longtest { - maxb = 1<<19; + maxb = 1 << 19 } - for j := 1; j <= maxb; j<<=1 { - n := len(b); - max := uintptr(1<<28); + for j := 1; j <= maxb; j <<= 1 { + n := len(b) + max := uintptr(1 << 28) if !*longtest { - max = uintptr(maxb); + max = uintptr(maxb) } if uintptr(j)*uintptr(n) > max { - n = int(max / uintptr(j)); + n = int(max / uintptr(j)) } if n < 10 { - n = 10; + n = 10 } for m := 1; m <= n; { - AllocAndFree(j, m); + AllocAndFree(j, m) if m == n { break } - m = 5*m/4; + m = 5 * m / 4 if m < 4 { m++ } |