Daily bump.
[official-gcc.git] / libgo / go / runtime / mallocrep.go
blob03ee71edb427988bfa5c0fd72c6270bdb82628f7
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Repeated malloc test.
7 // +build ignore
9 package main
11 import (
12 "flag"
13 "runtime"
16 var chatty = flag.Bool("v", false, "chatty")
18 var oldsys uint64
19 var memstats runtime.MemStats
21 func bigger() {
22 st := &memstats
23 runtime.ReadMemStats(st)
24 if oldsys < st.Sys {
25 oldsys = st.Sys
26 if *chatty {
27 println(st.Sys, " system bytes for ", st.Alloc, " Go bytes")
29 if st.Sys > 1e9 {
30 println("too big")
31 panic("fail")
36 func main() {
37 runtime.GC() // clean up garbage from init
38 runtime.ReadMemStats(&memstats) // first call can do some allocations
39 runtime.MemProfileRate = 0 // disable profiler
40 stacks := memstats.Alloc // ignore stacks
41 flag.Parse()
42 for i := 0; i < 1<<7; i++ {
43 for j := 1; j <= 1<<22; j <<= 1 {
44 if i == 0 && *chatty {
45 println("First alloc:", j)
47 if a := memstats.Alloc - stacks; a != 0 {
48 println("no allocations but stats report", a, "bytes allocated")
49 panic("fail")
51 b := runtime.Alloc(uintptr(j))
52 runtime.ReadMemStats(&memstats)
53 during := memstats.Alloc - stacks
54 runtime.Free(b)
55 runtime.ReadMemStats(&memstats)
56 if a := memstats.Alloc - stacks; a != 0 {
57 println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
58 panic("fail")
60 bigger()
62 if i%(1<<10) == 0 && *chatty {
63 println(i)
65 if i == 0 {
66 if *chatty {
67 println("Primed", i)
69 // runtime.frozen = true