示例#1
0
func main() {
	var fail bool

	go f()
	check([]int{1, 4, 5, 4})

	a := accum(0)
	b := accum(1)
	go g(a, b)
	check([]int{2, 4, 6, 9})

	go h()
	check([]int{100, 200, 101, 201, 500, 101, 201, 500})

	runtime.UpdateMemStats()
	n0 := runtime.MemStats.Mallocs

	x, y := newfunc(), newfunc()
	if x(1) != 1 || y(2) != 2 {
		println("newfunc returned broken funcs")
		fail = true
	}

	runtime.UpdateMemStats()
	if n0 != runtime.MemStats.Mallocs {
		println("newfunc allocated unexpectedly")
		fail = true
	}

	ff(1)

	if fail {
		panic("fail")
	}
}
示例#2
0
文件: itoa_test.go 项目: vkargov/gcc
func numAllocations(f func()) int {
	runtime.UpdateMemStats()
	n0 := runtime.MemStats.Mallocs
	f()
	runtime.UpdateMemStats()
	return int(runtime.MemStats.Mallocs - n0)
}
示例#3
0
func TestCountDecodeMallocs(t *testing.T) {
	var buf bytes.Buffer
	enc := NewEncoder(&buf)
	bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
	const count = 1000
	for i := 0; i < count; i++ {
		err := enc.Encode(bench)
		if err != nil {
			t.Fatal("encode:", err)
		}
	}
	dec := NewDecoder(&buf)
	runtime.UpdateMemStats()
	mallocs := 0 - runtime.MemStats.Mallocs
	for i := 0; i < count; i++ {
		*bench = Bench{}
		err := dec.Decode(&bench)
		if err != nil {
			t.Fatal("decode:", err)
		}
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	fmt.Printf("mallocs per decode of type Bench: %d\n", mallocs/count)
}
示例#4
0
func AllocAndFree(size, count int) {
	if *chatty {
		fmt.Printf("size=%d count=%d ...\n", size, count)
	}
	runtime.UpdateMemStats()
	n1 := stats.Alloc
	for i := 0; i < count; i++ {
		b[i] = runtime.Alloc(uintptr(size))
		base, n := runtime.Lookup(b[i])
		if base != b[i] || !OkAmount(uintptr(size), n) {
			println("lookup failed: got", base, n, "for", b[i])
			panic("fail")
		}
		runtime.UpdateMemStats()
		if stats.Sys > 1e9 {
			println("too much memory allocated")
			panic("fail")
		}
	}
	runtime.UpdateMemStats()
	n2 := stats.Alloc
	if *chatty {
		fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
	}
	n3 := stats.Alloc
	for j := 0; j < count; j++ {
		i := j
		if *reverse {
			i = count - 1 - j
		}
		alloc := uintptr(stats.Alloc)
		base, n := runtime.Lookup(b[i])
		if base != b[i] || !OkAmount(uintptr(size), n) {
			println("lookup failed: got", base, n, "for", b[i])
			panic("fail")
		}
		runtime.Free(b[i])
		runtime.UpdateMemStats()
		if stats.Alloc != uint64(alloc-n) {
			println("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n)
			panic("fail")
		}
		if runtime.MemStats.Sys > 1e9 {
			println("too much memory allocated")
			panic("fail")
		}
	}
	runtime.UpdateMemStats()
	n4 := stats.Alloc

	if *chatty {
		fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
	}
	if n2-n1 != n3-n4 {
		println("wrong alloc count: ", n2-n1, n3-n4)
		panic("fail")
	}
}
示例#5
0
文件: fmt_test.go 项目: vkargov/gcc
func TestCountMallocs(t *testing.T) {
	for _, mt := range mallocTest {
		const N = 100
		runtime.UpdateMemStats()
		mallocs := 0 - runtime.MemStats.Mallocs
		for i := 0; i < N; i++ {
			mt.fn()
		}
		runtime.UpdateMemStats()
		mallocs += runtime.MemStats.Mallocs
		if mallocs/N > uint64(mt.count) {
			t.Errorf("%s: expected %d mallocs, got %d", mt.desc, mt.count, mallocs/N)
		}
	}
}
示例#6
0
文件: malloc1.go 项目: WXB506/golang
func main() {
	runtime.Free(runtime.Alloc(1))
	runtime.UpdateMemStats()
	if *chatty {
		fmt.Printf("%+v %v\n", runtime.MemStats, uint64(0))
	}
}
示例#7
0
func TestGcSys(t *testing.T) {
	runtime.GC()
	runtime.UpdateMemStats()
	sys := runtime.MemStats.Sys

	for i := 0; i < 1000000; i++ {
		workthegc()
	}

	// Should only be using a few MB.
	runtime.UpdateMemStats()
	if sys > runtime.MemStats.Sys {
		sys = 0
	} else {
		sys = runtime.MemStats.Sys - sys
	}
	t.Logf("used %d extra bytes", sys)
	if sys > 4<<20 {
		t.Fatalf("using too much memory: %d bytes", sys)
	}
}
示例#8
0
func init() {
	c := make(chan int)
	go send(c)
	<-c

	const chunk = 1 << 20
	runtime.UpdateMemStats()
	sys := runtime.MemStats.Sys
	b := make([]byte, chunk)
	for i := range b {
		b[i] = byte(i%10 + '0')
	}
	s := string(b)
	for i := 0; i < 1000; i++ {
		x = []byte(s)
	}
	runtime.UpdateMemStats()
	sys1 := runtime.MemStats.Sys
	if sys1-sys > chunk*50 {
		println("allocated 1000 chunks of", chunk, "and used ", sys1-sys, "memory")
	}
}
示例#9
0
func bigger() {
	runtime.UpdateMemStats()
	if f := runtime.MemStats.Sys; footprint < f {
		footprint = f
		if *chatty {
			println("Footprint", footprint, " for ", allocated)
		}
		if footprint > 1e9 {
			println("too big")
			panic("fail")
		}
	}
}
示例#10
0
文件: mallocrep.go 项目: vkargov/gcc
func bigger() {
	runtime.UpdateMemStats()
	if st := runtime.MemStats; oldsys < st.Sys {
		oldsys = st.Sys
		if *chatty {
			println(st.Sys, " system bytes for ", st.Alloc, " Go bytes")
		}
		if st.Sys > 1e9 {
			println("too big")
			panic("fail")
		}
	}
}
示例#11
0
func TestGcSys(t *testing.T) {
	for i := 0; i < 1000000; i++ {
		workthegc()
	}

	// Should only be using a few MB.
	runtime.UpdateMemStats()
	sys := runtime.MemStats.Sys
	t.Logf("using %d MB", sys>>20)
	if sys > 10e6 {
		t.Fatalf("using too much memory: %d MB", sys>>20)
	}
}
示例#12
0
func gc() {
	runtime.GC()
	runtime.UpdateMemStats()
	pause := runtime.MemStats.PauseTotalNs
	inuse := runtime.MemStats.Alloc
	free := runtime.MemStats.TotalAlloc - inuse
	fmt.Printf("gc pause: %8.3f ms; collect: %8.0f MB; heapsize: %8.0f MB\n",
		float64(pause-lastPauseNs)/1e6,
		float64(free-lastFree)/1048576,
		float64(inuse)/1048576)
	lastPauseNs = pause
	lastFree = free
}
示例#13
0
文件: mallocrep.go 项目: vkargov/gcc
func main() {
	runtime.GC()               // clean up garbage from init
	runtime.UpdateMemStats()   // first call can do some allocations
	runtime.MemProfileRate = 0 // disable profiler
	runtime.MemStats.Alloc = 0 // ignore stacks
	flag.Parse()
	for i := 0; i < 1<<7; i++ {
		for j := 1; j <= 1<<22; j <<= 1 {
			if i == 0 && *chatty {
				println("First alloc:", j)
			}
			if a := runtime.MemStats.Alloc; a != 0 {
				println("no allocations but stats report", a, "bytes allocated")
				panic("fail")
			}
			b := runtime.Alloc(uintptr(j))
			runtime.UpdateMemStats()
			during := runtime.MemStats.Alloc
			runtime.Free(b)
			runtime.UpdateMemStats()
			if a := runtime.MemStats.Alloc; a != 0 {
				println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
				panic("fail")
			}
			bigger()
		}
		if i%(1<<10) == 0 && *chatty {
			println(i)
		}
		if i == 0 {
			if *chatty {
				println("Primed", i)
			}
			//	runtime.frozen = true
		}
	}
}
示例#14
0
func countMallocs(dial func() (*Client, os.Error), t *testing.T) uint64 {
	once.Do(startServer)
	client, err := dial()
	if err != nil {
		t.Fatal("error dialing", err)
	}
	args := &Args{7, 8}
	reply := new(Reply)
	runtime.UpdateMemStats()
	mallocs := 0 - runtime.MemStats.Mallocs
	const count = 100
	for i := 0; i < count; i++ {
		err := client.Call("Arith.Add", args, reply)
		if err != nil {
			t.Errorf("Add: expected no error but got string %q", err.String())
		}
		if reply.C != args.A+args.B {
			t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
		}
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	return mallocs / count
}
示例#15
0
func main() {
	flag.Parse()
	if *n <= 0 {
		fmt.Fprintf(os.Stderr, "invalid number of goroutines")
		os.Exit(1)
	}

	// Limit the number of spare OS threads to just 1
	runtime.GOMAXPROCS(1)

	// Make a copy of MemStats
	runtime.UpdateMemStats()
	m0 := runtime.MemStats

	t0 := time.Nanoseconds()
	for i := 0; i < *n; i++ {
		go f()
	}
	runtime.Gosched()
	t1 := time.Nanoseconds()
	runtime.GC()

	// Make a copy of MemStats
	runtime.UpdateMemStats()
	m1 := runtime.MemStats

	if counter != *n {
		fmt.Fprintf(os.Stderr, "failed to begin execution of all goroutines")
		os.Exit(1)
	}

	fmt.Printf("Number of goroutines: %d\n", *n)
	fmt.Printf("Per goroutine:\n")
	fmt.Printf("  Memory: %.2f bytes\n", float64(m1.Sys-m0.Sys)/float64(*n))
	fmt.Printf("  Time:   %f µs\n", float64(t1-t0)/float64(*n)/1e3)
}
示例#16
0
func main() {
	flag.Parse()
	buildHeap()
	runtime.GOMAXPROCS(*cpus)
	runtime.UpdateMemStats()
	lastPauseNs = runtime.MemStats.PauseTotalNs
	lastFree = runtime.MemStats.TotalAlloc - runtime.MemStats.Alloc
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}
	for i := 0; i < 10; i++ {
		gc()
	}
}
示例#17
0
func TestCountMallocs(t *testing.T) {
	if testing.Short() {
		return
	}
	runtime.UpdateMemStats()
	mallocs := 0 - runtime.MemStats.Mallocs
	for i := 0; i < 100; i++ {
		Sprintf("")
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	Printf("mallocs per Sprintf(\"\"): %d\n", mallocs/100)
	runtime.UpdateMemStats()
	mallocs = 0 - runtime.MemStats.Mallocs
	for i := 0; i < 100; i++ {
		Sprintf("xxx")
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	Printf("mallocs per Sprintf(\"xxx\"): %d\n", mallocs/100)
	runtime.UpdateMemStats()
	mallocs = 0 - runtime.MemStats.Mallocs
	for i := 0; i < 100; i++ {
		Sprintf("%x", i)
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/100)
	runtime.UpdateMemStats()
	mallocs = 0 - runtime.MemStats.Mallocs
	for i := 0; i < 100; i++ {
		Sprintf("%x %x", i, i)
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	Printf("mallocs per Sprintf(\"%%x %%x\"): %d\n", mallocs/100)
}
示例#18
0
文件: gc2.go 项目: WXB506/golang
func main() {
	const N = 10000
	st := runtime.MemStats
	for i := 0; i < N; i++ {
		c := make(chan int, 10)
		_ = c
		if i%100 == 0 {
			for j := 0; j < 4; j++ {
				runtime.GC()
				runtime.Gosched()
				runtime.GC()
				runtime.Gosched()
			}
		}
	}

	runtime.UpdateMemStats()
	obj := runtime.MemStats.HeapObjects - st.HeapObjects
	if obj > N/5 {
		fmt.Println("too many objects left:", obj)
		os.Exit(1)
	}
}
示例#19
0
文件: fmt_test.go 项目: Sunmonds/gcc
func TestCountMallocs(t *testing.T) {
	if testing.Short() {
		return
	}
	runtime.UpdateMemStats()
	mallocs := 0 - runtime.MemStats.Mallocs
	for i := 0; i < 100; i++ {
		Sprintf("")
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	Printf("mallocs per Sprintf(\"\"): %d\n", mallocs/100)
	runtime.UpdateMemStats()
	mallocs = 0 - runtime.MemStats.Mallocs
	for i := 0; i < 100; i++ {
		Sprintf("xxx")
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	Printf("mallocs per Sprintf(\"xxx\"): %d\n", mallocs/100)
	runtime.UpdateMemStats()
	mallocs = 0 - runtime.MemStats.Mallocs
	for i := 0; i < 100; i++ {
		Sprintf("%x", i)
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/100)
	runtime.UpdateMemStats()
	mallocs = 0 - runtime.MemStats.Mallocs
	for i := 0; i < 100; i++ {
		Sprintf("%s", "hello")
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	Printf("mallocs per Sprintf(\"%%s\"): %d\n", mallocs/100)
	runtime.UpdateMemStats()
	mallocs = 0 - runtime.MemStats.Mallocs
	for i := 0; i < 100; i++ {
		Sprintf("%x %x", i, i)
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	Printf("mallocs per Sprintf(\"%%x %%x\"): %d\n", mallocs/100)
	buf := new(bytes.Buffer)
	runtime.UpdateMemStats()
	mallocs = 0 - runtime.MemStats.Mallocs
	for i := 0; i < 100; i++ {
		buf.Reset()
		Fprintf(buf, "%x %x %x", i, i, i)
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	Printf("mallocs per Fprintf(buf, \"%%x %%x %%x\"): %d\n", mallocs/100)
	runtime.UpdateMemStats()
	mallocs = 0 - runtime.MemStats.Mallocs
	for i := 0; i < 100; i++ {
		buf.Reset()
		Fprintf(buf, "%s", "hello")
	}
	runtime.UpdateMemStats()
	mallocs += runtime.MemStats.Mallocs
	Printf("mallocs per Fprintf(buf, \"%%s\"): %d\n", mallocs/100)
}