blob: 03ee71edb427988bfa5c0fd72c6270bdb82628f7 [file] [log] [blame]
Russ Coxe29ce172008-12-18 15:42:28 -08001// 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
Russ Cox03f22892012-02-19 00:11:44 -05007// +build ignore
8
Russ Coxe29ce172008-12-18 15:42:28 -08009package 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
Rémy Oudompheng842c9062012-02-06 19:16:26 +010019var memstats runtime.MemStats
Russ Cox33e396a2010-02-03 16:31:34 -080020
Russ Coxe29ce172008-12-18 15:42:28 -080021func bigger() {
Rémy Oudompheng842c9062012-02-06 19:16:26 +010022 st := &memstats
23 runtime.ReadMemStats(st)
24 if oldsys < st.Sys {
Russ Cox33e396a2010-02-03 16:31:34 -080025 oldsys = st.Sys
Rob Pikec45d2a72009-01-09 13:42:46 -080026 if *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080027 println(st.Sys, " system bytes for ", st.Alloc, " Go bytes")
Russ Coxe29ce172008-12-18 15:42:28 -080028 }
Russ Coxf48cbfd2009-01-16 16:12:14 -080029 if st.Sys > 1e9 {
Rob Pike325cf8e2010-03-24 16:46:53 -070030 println("too big")
31 panic("fail")
Russ Coxe29ce172008-12-18 15:42:28 -080032 }
33 }
34}
35
36func main() {
Rémy Oudompheng842c9062012-02-06 19:16:26 +010037 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
Russ Cox6eb251f2010-03-24 09:40:09 -070041 flag.Parse()
Russ Coxebd27d62009-10-09 11:18:32 -070042 for i := 0; i < 1<<7; i++ {
Russ Cox33e396a2010-02-03 16:31:34 -080043 for j := 1; j <= 1<<22; j <<= 1 {
Rob Pikec45d2a72009-01-09 13:42:46 -080044 if i == 0 && *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080045 println("First alloc:", j)
Russ Coxe29ce172008-12-18 15:42:28 -080046 }
Rémy Oudompheng842c9062012-02-06 19:16:26 +010047 if a := memstats.Alloc - stacks; a != 0 {
Rob Pike325cf8e2010-03-24 16:46:53 -070048 println("no allocations but stats report", a, "bytes allocated")
49 panic("fail")
Russ Coxb014be72009-06-05 10:59:37 -070050 }
Russ Cox33e396a2010-02-03 16:31:34 -080051 b := runtime.Alloc(uintptr(j))
Rémy Oudompheng842c9062012-02-06 19:16:26 +010052 runtime.ReadMemStats(&memstats)
53 during := memstats.Alloc - stacks
Russ Cox33e396a2010-02-03 16:31:34 -080054 runtime.Free(b)
Rémy Oudompheng842c9062012-02-06 19:16:26 +010055 runtime.ReadMemStats(&memstats)
56 if a := memstats.Alloc - stacks; a != 0 {
Russ Cox00f9f0c2010-03-30 10:34:57 -070057 println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
58 panic("fail")
Russ Coxe29ce172008-12-18 15:42:28 -080059 }
Russ Cox33e396a2010-02-03 16:31:34 -080060 bigger()
Russ Coxe29ce172008-12-18 15:42:28 -080061 }
Rob Pikec45d2a72009-01-09 13:42:46 -080062 if i%(1<<10) == 0 && *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080063 println(i)
Russ Coxe29ce172008-12-18 15:42:28 -080064 }
65 if i == 0 {
Rob Pikec45d2a72009-01-09 13:42:46 -080066 if *chatty {
Russ Cox33e396a2010-02-03 16:31:34 -080067 println("Primed", i)
Russ Coxe29ce172008-12-18 15:42:28 -080068 }
Rob Pike4f61fc92010-09-04 10:36:13 +100069 // runtime.frozen = true
Russ Coxe29ce172008-12-18 15:42:28 -080070 }
71 }
72}