blob: 9f47e52e2b60a80776ac1070ac1ef5f54e37a89f [file] [log] [blame]
Russ Coxe29ce172008-12-18 15:42:28 -08001// $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// Repeated malloc test.
8
9package main
10
11import (
Russ Cox33e396a2010-02-03 16:31:34 -080012 "flag"
13 "runtime"
Russ Coxe29ce172008-12-18 15:42:28 -080014)
15
Russ Cox33e396a2010-02-03 16:31:34 -080016var chatty = flag.Bool("v", false, "chatty")
Russ Coxe29ce172008-12-18 15:42:28 -080017
Russ Cox33e396a2010-02-03 16:31:34 -080018var oldsys uint64
19
Russ Coxe29ce172008-12-18 15:42:28 -080020func bigger() {
Russ Cox226fb092011-07-22 00:55:01 -040021 runtime.UpdateMemStats()
Russ Cox33e396a2010-02-03 16:31:34 -080022 if st := runtime.MemStats; oldsys < st.Sys {
23 oldsys = st.Sys
Rob Pikec45d2a72009-01-09 13:42:46 -080024 if *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080025 println(st.Sys, " system bytes for ", st.Alloc, " Go bytes")
Russ Coxe29ce172008-12-18 15:42:28 -080026 }
Russ Coxf48cbfd2009-01-16 16:12:14 -080027 if st.Sys > 1e9 {
Rob Pike325cf8e2010-03-24 16:46:53 -070028 println("too big")
29 panic("fail")
Russ Coxe29ce172008-12-18 15:42:28 -080030 }
31 }
32}
33
34func main() {
Russ Cox226fb092011-07-22 00:55:01 -040035 runtime.GC() // clean up garbage from init
Russ Cox6eb251f2010-03-24 09:40:09 -070036 runtime.MemProfileRate = 0 // disable profiler
Russ Cox33e396a2010-02-03 16:31:34 -080037 runtime.MemStats.Alloc = 0 // ignore stacks
Russ Cox6eb251f2010-03-24 09:40:09 -070038 flag.Parse()
Russ Coxebd27d62009-10-09 11:18:32 -070039 for i := 0; i < 1<<7; i++ {
Russ Cox33e396a2010-02-03 16:31:34 -080040 for j := 1; j <= 1<<22; j <<= 1 {
Rob Pikec45d2a72009-01-09 13:42:46 -080041 if i == 0 && *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080042 println("First alloc:", j)
Russ Coxe29ce172008-12-18 15:42:28 -080043 }
Russ Cox33e396a2010-02-03 16:31:34 -080044 if a := runtime.MemStats.Alloc; a != 0 {
Rob Pike325cf8e2010-03-24 16:46:53 -070045 println("no allocations but stats report", a, "bytes allocated")
46 panic("fail")
Russ Coxb014be72009-06-05 10:59:37 -070047 }
Russ Cox33e396a2010-02-03 16:31:34 -080048 b := runtime.Alloc(uintptr(j))
Russ Cox226fb092011-07-22 00:55:01 -040049 runtime.UpdateMemStats()
Russ Cox33e396a2010-02-03 16:31:34 -080050 during := runtime.MemStats.Alloc
51 runtime.Free(b)
Russ Cox226fb092011-07-22 00:55:01 -040052 runtime.UpdateMemStats()
Russ Cox33e396a2010-02-03 16:31:34 -080053 if a := runtime.MemStats.Alloc; a != 0 {
Russ Cox00f9f0c2010-03-30 10:34:57 -070054 println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
55 panic("fail")
Russ Coxe29ce172008-12-18 15:42:28 -080056 }
Russ Cox33e396a2010-02-03 16:31:34 -080057 bigger()
Russ Coxe29ce172008-12-18 15:42:28 -080058 }
Rob Pikec45d2a72009-01-09 13:42:46 -080059 if i%(1<<10) == 0 && *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080060 println(i)
Russ Coxe29ce172008-12-18 15:42:28 -080061 }
62 if i == 0 {
Rob Pikec45d2a72009-01-09 13:42:46 -080063 if *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080064 println("Primed", i)
Russ Coxe29ce172008-12-18 15:42:28 -080065 }
Rob Pike4f61fc92010-09-04 10:36:13 +100066 // runtime.frozen = true
Russ Coxe29ce172008-12-18 15:42:28 -080067 }
68 }
69}