github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/runtime/mallocrep.go (about)

     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.
     4  
     5  // Repeated malloc test.
     6  
     7  // +build ignore
     8  
     9  package main
    10  
    11  import (
    12  	"flag"
    13  	"runtime"
    14  )
    15  
    16  var chatty = flag.Bool("v", false, "chatty")
    17  
    18  var oldsys uint64
    19  var memstats runtime.MemStats
    20  
    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")
    28  		}
    29  		if st.Sys > 1e9 {
    30  			println("too big")
    31  			panic("fail")
    32  		}
    33  	}
    34  }
    35  
    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)
    46  			}
    47  			if a := memstats.Alloc - stacks; a != 0 {
    48  				println("no allocations but stats report", a, "bytes allocated")
    49  				panic("fail")
    50  			}
    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")
    59  			}
    60  			bigger()
    61  		}
    62  		if i%(1<<10) == 0 && *chatty {
    63  			println(i)
    64  		}
    65  		if i == 0 {
    66  			if *chatty {
    67  				println("Primed", i)
    68  			}
    69  			//	runtime.frozen = true
    70  		}
    71  	}
    72  }