예제 #1
1
func main() {
	cpus := runtime.NumCPU()
	runtime.GOMAXPROCS(cpus)

	count := 100000

	die := make(chan bool)

	var startMemory runtime.MemStats
	runtime.ReadMemStats(&startMemory)

	start := time.Now()
	for i := 0; i < count; i++ {
		go waitAround(die)
	}

	elapsed := time.Since(start)
	var endMemory runtime.MemStats
	runtime.ReadMemStats(&endMemory)

	fmt.Printf("Started %d goroutines on %d CPUs in %f seconds \n",
		count, cpus, elapsed.Seconds())

	fmt.Printf("Memory before %d, memory after %d \n",
		startMemory.Alloc, endMemory.Alloc)

	fmt.Printf("%d goroutines running\n", runtime.NumGoroutine())

	close(die)
}
예제 #2
0
// Check that reading large files doesn't lead to large allocations.
func TestReadLargeMemCheck(t *testing.T) {
	ts := NewTestCase(t)
	defer ts.Cleanup()

	content := RandomData(385 * 1023)
	err := ioutil.WriteFile(ts.origFile, []byte(content), 0644)
	if err != nil {
		t.Fatalf("WriteFile failed: %v", err)
	}

	f, err := os.Open(ts.mountFile)
	if err != nil {
		t.Fatalf("Open failed: %v", err)
	}
	buf := make([]byte, len(content)+1024)
	f.Read(buf)
	if err != nil {
		t.Fatalf("Read failed: %v", err)
	}
	f.Close()
	runtime.GC()
	var before, after runtime.MemStats

	N := 100
	runtime.ReadMemStats(&before)
	for i := 0; i < N; i++ {
		f, _ := os.Open(ts.mountFile)
		f.Read(buf)
		f.Close()
	}
	runtime.ReadMemStats(&after)
	delta := int((after.TotalAlloc - before.TotalAlloc))
	delta = (delta - 40000) / N
	t.Logf("bytes per read loop: %d", delta)
}
예제 #3
0
func (c *Corpus) UpdateIndex() {
	if c.Verbose {
		log.Printf("updating index...")
	}
	start := time.Now()
	throttle := c.IndexThrottle
	if throttle <= 0 {
		throttle = 0.9
	} else if throttle > 1.0 {
		throttle = 1.0
	}
	index := c.NewIndex()
	stop := time.Now()
	c.searchIndex.Set(index)
	if c.Verbose {
		secs := stop.Sub(start).Seconds()
		stats := index.Stats()
		log.Printf("index updated (%gs, %d bytes of source, %d files, %d lines, %d unique words, %d spots)",
			secs, stats.Bytes, stats.Files, stats.Lines, stats.Words, stats.Spots)
	}
	memstats := new(runtime.MemStats)
	runtime.ReadMemStats(memstats)
	if c.Verbose {
		log.Printf("before GC: bytes = %d footprint = %d", memstats.HeapAlloc, memstats.Sys)
	}
	runtime.GC()
	runtime.ReadMemStats(memstats)
	if c.Verbose {
		log.Printf("after  GC: bytes = %d footprint = %d", memstats.HeapAlloc, memstats.Sys)
	}
}
/*
Populates a trie structure with the contents of a text file that
has the provided name
*/
func PopulateTrie(file string) {
	trie = wordTrie.NewTrie()
	// Open the text file
	f, err := os.Open(file)
	//f, err := os.Open(file)
	check(err)

	// Begin scanning the text file line by line, populate the trie
	scanner := bufio.NewScanner(f)

	// Initial available memory read prior to entering the while loop
	runtime.ReadMemStats(&mem)
	currentMemory := mem.Alloc

	// Add entries to the trie as long as there is enough memory
	for scanner.Scan() && currentMemory < memoryLimit {
		runtime.ReadMemStats(&mem)
		currentMemory = mem.TotalAlloc
		trie.AddEntry(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "reading standard input:", err)
	}
	f.Close()

	fmt.Println(trie.GetNumberOfItems()) //TESTING
}
예제 #5
0
파일: gc_test.go 프로젝트: arnold8/go
func TestPeriodicGC(t *testing.T) {
	// Make sure we're not in the middle of a GC.
	runtime.GC()

	var ms1, ms2 runtime.MemStats
	runtime.ReadMemStats(&ms1)

	// Make periodic GC run continuously.
	orig := *runtime.ForceGCPeriod
	*runtime.ForceGCPeriod = 0

	// Let some periodic GCs happen. In a heavily loaded system,
	// it's possible these will be delayed, so this is designed to
	// succeed quickly if things are working, but to give it some
	// slack if things are slow.
	var numGCs uint32
	const want = 2
	for i := 0; i < 20 && numGCs < want; i++ {
		time.Sleep(5 * time.Millisecond)

		// Test that periodic GC actually happened.
		runtime.ReadMemStats(&ms2)
		numGCs = ms2.NumGC - ms1.NumGC
	}
	*runtime.ForceGCPeriod = orig

	if numGCs < want {
		t.Fatalf("no periodic GC: got %v GCs, want >= 2", numGCs)
	}
}
func BenchmarkExtract(b *testing.B) {
	config, err := ioutil.ReadFile("test/haproxy.csv")
	if err != nil {
		b.Fatalf("could not read config file: %v", err.Error())
	}

	h := newHaproxy(config)
	defer h.Close()

	e := NewExporter(h.URL, "", 5*time.Second)

	var before, after runtime.MemStats
	runtime.GC()
	runtime.ReadMemStats(&before)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		ch := make(chan prometheus.Metric)
		go func(ch chan prometheus.Metric) {
			for _ = range ch {
			}
		}(ch)

		e.Collect(ch)
		close(ch)
	}

	runtime.GC()
	runtime.ReadMemStats(&after)

	b.Logf("%d bytes used after %d runs", after.Alloc-before.Alloc, b.N)
}
예제 #7
0
파일: adminz.go 프로젝트: theevocater/fsgo
func (a *Adminz) gcHandler(w http.ResponseWriter, r *http.Request) {
	var mem runtime.MemStats

	mb := uint64(1024 * 1024)
	runtime.ReadMemStats(&mem)
	fmt.Fprintln(w, "Before")
	fmt.Fprintln(w, "\tAlloc\t", mem.Alloc/mb)
	fmt.Fprintln(w, "\tTotalAlloc:\t", mem.TotalAlloc/mb)
	fmt.Fprintln(w, "\tHeapAlloc:\t", mem.HeapAlloc/mb)
	fmt.Fprintln(w, "\tHeapSys:\t", mem.HeapSys/mb)
	fmt.Fprintln(w, "\tSys:\t", mem.Sys/mb)

	a.Lock()
	was := a.doPause()
	runtime.GC()
	if was {
		a.doResume()
	}
	a.Unlock()

	runtime.ReadMemStats(&mem)
	fmt.Fprintln(w, "After")
	fmt.Fprintln(w, "\tAlloc\t", mem.Alloc/mb)
	fmt.Fprintln(w, "\tTotalAlloc:\t", mem.TotalAlloc/mb)
	fmt.Fprintln(w, "\tHeapAlloc:\t", mem.HeapAlloc/mb)
	fmt.Fprintln(w, "\tHeapSys:\t", mem.HeapSys/mb)
	fmt.Fprintln(w, "\tSys:\t", mem.Sys/mb)

	w.Write([]byte("OK"))
}
예제 #8
0
func TestClean(t *testing.T) {
	tests := cleantests
	if runtime.GOOS == "windows" {
		for i := range tests {
			tests[i].result = filepath.FromSlash(tests[i].result)
		}
		tests = append(tests, wincleantests...)
	}
	for _, test := range tests {
		if s := filepath.Clean(test.path); s != test.result {
			t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
		}
		if s := filepath.Clean(test.result); s != test.result {
			t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
		}
	}

	var ms runtime.MemStats
	runtime.ReadMemStats(&ms)
	allocs := -ms.Mallocs
	const rounds = 100
	for i := 0; i < rounds; i++ {
		for _, test := range tests {
			filepath.Clean(test.result)
		}
	}
	runtime.ReadMemStats(&ms)
	allocs += ms.Mallocs
	if allocs >= rounds {
		t.Errorf("Clean cleaned paths: %d allocations per test round, want zero", allocs/rounds)
	}
}
예제 #9
0
func writeHeader(writer io.Writer) {
	fmt.Fprintf(writer, "Start time: %s<br>\n", startTime.Format(timeFormat))
	uptime := time.Since(startTime) + time.Millisecond*50
	uptime = (uptime / time.Millisecond / 100) * time.Millisecond * 100
	fmt.Fprintf(writer, "Uptime: %s<br>\n", uptime)
	var rusage syscall.Rusage
	syscall.Getrusage(syscall.RUSAGE_SELF, &rusage)
	userCpuTime := time.Duration(rusage.Utime.Sec)*time.Second +
		time.Duration(rusage.Utime.Usec)*time.Microsecond
	sysCpuTime := time.Duration(rusage.Stime.Sec)*time.Second +
		time.Duration(rusage.Stime.Usec)*time.Microsecond
	cpuTime := rusage.Utime.Sec + rusage.Stime.Sec
	fmt.Fprintf(writer, "CPU Time: %.1f%% (User: %s Sys: %s)<br>\n",
		float64(cpuTime*100)/float64(uptime.Seconds()), userCpuTime, sysCpuTime)
	var memStatsBeforeGC, memStatsAfterGC runtime.MemStats
	runtime.ReadMemStats(&memStatsBeforeGC)
	runtime.GC()
	runtime.ReadMemStats(&memStatsAfterGC)
	fmt.Fprintf(writer, "Allocated memory: %s (%s after GC)<br>\n",
		format.FormatBytes(memStatsBeforeGC.Alloc),
		format.FormatBytes(memStatsAfterGC.Alloc))
	fmt.Fprintf(writer, "System memory: %s (%s after GC)<br>\n",
		format.FormatBytes(memStatsBeforeGC.Sys),
		format.FormatBytes(memStatsAfterGC.Sys))
	fmt.Fprintln(writer, "Raw <a href=\"metrics\">metrics</a>")
}
예제 #10
0
func main() {
	cpus := runtime.NumCPU()
	runtime.GOMAXPROCS(cpus)
	count := 1000 * 100

	var startMemory runtime.MemStats
	runtime.ReadMemStats(&startMemory)

	start := time.Now()
	fin := make(chan bool)
	for i := 0; i < count; i++ {
		go func() {
			<-fin
		}()
	}
	elapsed := time.Since(start)

	var endMemory runtime.MemStats
	runtime.ReadMemStats(&endMemory)
	close(fin)

	fmt.Printf(`
goroutine:  %d
cpu:        %d
time:        %f
memory all: %f MB
memory:      %f byte/goroutine
`,
		count, cpus, elapsed.Seconds(),
		float64(endMemory.Alloc-startMemory.Alloc)/float64(1024*1024),
		float64(endMemory.Alloc-startMemory.Alloc)/float64(count))
}
예제 #11
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)
	memstats := new(runtime.MemStats)
	runtime.ReadMemStats(memstats)
	mallocs := 0 - memstats.Mallocs
	for i := 0; i < count; i++ {
		*bench = Bench{}
		err := dec.Decode(&bench)
		if err != nil {
			t.Fatal("decode:", err)
		}
	}
	runtime.ReadMemStats(memstats)
	mallocs += memstats.Mallocs
	fmt.Printf("mallocs per decode of type Bench: %d\n", mallocs/count)
}
예제 #12
0
func TestChunkReaderAllocs(t *testing.T) {
	// temporarily set GOMAXPROCS to 1 as we are testing memory allocations
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
	var buf bytes.Buffer
	w := newChunkedWriter(&buf)
	a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
	w.Write(a)
	w.Write(b)
	w.Write(c)
	w.Close()

	r := newChunkedReader(&buf)
	readBuf := make([]byte, len(a)+len(b)+len(c)+1)

	var ms runtime.MemStats
	runtime.ReadMemStats(&ms)
	m0 := ms.Mallocs

	n, err := io.ReadFull(r, readBuf)

	runtime.ReadMemStats(&ms)
	mallocs := ms.Mallocs - m0
	if mallocs > 1 {
		t.Errorf("%d mallocs; want <= 1", mallocs)
	}

	if n != len(readBuf)-1 {
		t.Errorf("read %d bytes; want %d", n, len(readBuf)-1)
	}
	if err != io.ErrUnexpectedEOF {
		t.Errorf("read error = %v; want ErrUnexpectedEOF", err)
	}
}
func BenchmarkExtract(b *testing.B) {
	config, err := ioutil.ReadFile("test/recursor_stats.json")
	if err != nil {
		b.Fatalf("could not read config file: %v", err.Error())
	}

	h := newPowerDNS(config)
	defer h.Close()

	hostURL, _ := url.Parse(h.URL)

	e := NewExporter("12345", "recursor", hostURL)

	var before, after runtime.MemStats
	runtime.GC()
	runtime.ReadMemStats(&before)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		ch := make(chan prometheus.Metric)
		go func(ch chan prometheus.Metric) {
			for _ = range ch {
			}
		}(ch)

		e.Collect(ch)
		close(ch)
	}

	runtime.GC()
	runtime.ReadMemStats(&after)

	b.Logf("%d bytes used after %d runs", after.Alloc-before.Alloc, b.N)
}
예제 #14
0
// Cache memory leak for get
func testCacheGetMemoryLeak() {
	pc.OverrideLeaseSeconds(1)
	defer pc.OverrideLeaseSeconds(0)

	var memstats runtime.MemStats
	var initAlloc, midAlloc, finalAlloc uint64
	longValue := strings.Repeat("this sentence is 30 char long\n", 30)

	// Run garbage collection and get memory stats
	runtime.GC()
	runtime.ReadMemStats(&memstats)
	initAlloc = memstats.Alloc

	// Cache a lot of data
	for i := 0; i < 10000; i++ {
		key := fmt.Sprintf("keymemleakget:%d", i)
		pc.Reset()
		forceCacheGet(key, longValue)
		if pc.GetLeaseRequestCount() == 0 {
			LOGE.Println("FAIL: not requesting leases")
			failCount++
			return
		}
		pc.Reset()
		v, err := ls.Get(key)
		if checkError(err, false) {
			return
		}
		if v != longValue {
			LOGE.Println("FAIL: got wrong value")
			failCount++
			return
		}
		if pc.GetRpcCount() > 0 {
			LOGE.Println("FAIL: not caching data")
			failCount++
			return
		}
	}

	runtime.GC()
	runtime.ReadMemStats(&memstats)
	midAlloc = memstats.Alloc

	// Wait for data to expire and someone to cleanup
	time.Sleep(20 * time.Second)

	// Run garbage collection and get memory stats
	runtime.GC()
	runtime.ReadMemStats(&memstats)
	finalAlloc = memstats.Alloc

	if finalAlloc < initAlloc || (finalAlloc-initAlloc) < 5000000 {
		fmt.Fprintln(output, "PASS")
		passCount++
	} else {
		LOGE.Printf("FAIL: not cleaning cache - memory leak - init %d mid %d final %d\n", initAlloc, midAlloc, finalAlloc)
		failCount++
	}
}
예제 #15
0
func BenchmarkWatch(b *testing.B) {
	b.StopTimer()
	s := newStore()
	kvs, _ := generateNRandomKV(b.N, 128)
	b.StartTimer()

	memStats := new(runtime.MemStats)
	runtime.GC()
	runtime.ReadMemStats(memStats)

	for i := 0; i < b.N; i++ {
		w, _ := s.Watch(kvs[i][0], false, false, 0)

		e := newEvent("set", kvs[i][0], uint64(i+1), uint64(i+1))
		s.WatcherHub.notify(e)
		<-w.EventChan
		s.CurrentIndex++
	}

	s.WatcherHub.EventHistory = nil
	afterMemStats := new(runtime.MemStats)
	runtime.GC()
	runtime.ReadMemStats(afterMemStats)
	fmt.Printf("\nBefore Alloc: %v; After Alloc: %v\n",
		memStats.Alloc/1000, afterMemStats.Alloc/1000)
}
예제 #16
0
파일: matrix.go 프로젝트: nfleet/via
// Computes a matrix hash. This should be launched in a goroutine, not in the main thread.
func (v *Via) ComputeMatrix(nodes []int, country string, speedProfile int) (map[string][]int, error) {
	var memStats runtime.MemStats
	runtime.ReadMemStats(&memStats)
	v.Debug.Printf("entering ComputeMatrix for hash, memory used: %d mb.", memStats.Alloc/1e6)
	empty := map[string][]int{}
	t0 := time.Now()

	matrixData := struct {
		Sources []int `json:"sources"`
	}{nodes}
	jsonData, err := json.Marshal(matrixData)
	if err != nil {
		panic("nodes to json error:" + err.Error())
	}

	v.Debug.Println("got country", string(country), "with profile", speedProfile)

	res := ch.Calc_dm(string(jsonData), string(country), int(speedProfile), v.DataDir)

	var matrix map[string][]int
	if err := json.NewDecoder(strings.NewReader(res)).Decode(&matrix); err != nil {
		v.Debug.Println("failed to parse CH results")
		return empty, fmt.Errorf("Failed to parse CH results: %s", err.Error())
	}

	t1 := time.Since(t0)
	v.Debug.Println("calculated matrix in", t1)

	runtime.GC()
	runtime.ReadMemStats(&memStats)
	v.Debug.Printf("Computation completed with memory usage still at %d mb.\n", memStats.Alloc/1e6)

	return matrix, nil
}
예제 #17
0
파일: select2.go 프로젝트: 2thetop/go
func main() {
	runtime.MemProfileRate = 0

	c := make(chan int)
	dummy := make(chan int)

	// warm up
	go sender(c, 100000)
	receiver(c, dummy, 100000)
	runtime.GC()
	memstats := new(runtime.MemStats)
	runtime.ReadMemStats(memstats)
	alloc := memstats.Alloc

	// second time shouldn't increase footprint by much
	go sender(c, 100000)
	receiver(c, dummy, 100000)
	runtime.GC()
	runtime.ReadMemStats(memstats)

	// Be careful to avoid wraparound.
	if memstats.Alloc > alloc && memstats.Alloc-alloc > 1.1e5 {
		println("BUG: too much memory for 100,000 selects:", memstats.Alloc-alloc)
	}
}
예제 #18
0
파일: gc_test.go 프로젝트: rgmabs19357/gcc
func TestGcSys(t *testing.T) {
	memstats := new(runtime.MemStats)
	runtime.GC()
	runtime.ReadMemStats(memstats)
	sys := memstats.Sys

	itercount := 1000000
	if testing.Short() {
		itercount = 100000
	}
	for i := 0; i < itercount; i++ {
		workthegc()
	}

	// Should only be using a few MB.
	runtime.ReadMemStats(memstats)
	if sys > memstats.Sys {
		sys = 0
	} else {
		sys = memstats.Sys - sys
	}
	t.Logf("used %d extra bytes", sys)
	if sys > 4<<20 {
		t.Fatalf("using too much memory: %d bytes", sys)
	}
}
예제 #19
0
func countMallocs(dial func() (*Client, 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)
	memstats := new(runtime.MemStats)
	runtime.ReadMemStats(memstats)
	mallocs := 0 - 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.Error())
		}
		if reply.C != args.A+args.B {
			t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
		}
	}
	runtime.ReadMemStats(memstats)
	mallocs += memstats.Mallocs
	return mallocs / count
}
예제 #20
0
func BenchmarkFingerprinting(b *testing.B) {
	b.StopTimer()
	fps := []*Fingerprint{
		{
			Hash: 0,
			FirstCharacterOfFirstLabelName: "a",
			LabelMatterLength:              2,
			LastCharacterOfLastLabelValue:  "z",
		},
		{
			Hash: 0,
			FirstCharacterOfFirstLabelName: "a",
			LabelMatterLength:              2,
			LastCharacterOfLastLabelValue:  "z",
		},
	}
	for i := 0; i < 10; i++ {
		fps[0].Less(fps[1])
	}
	b.Logf("N: %v", b.N)
	b.StartTimer()

	var pre runtime.MemStats
	runtime.ReadMemStats(&pre)

	for i := 0; i < b.N; i++ {
		fps[0].Less(fps[1])
	}

	var post runtime.MemStats
	runtime.ReadMemStats(&post)

	b.Logf("allocs: %d items: ", post.TotalAlloc-pre.TotalAlloc)
}
예제 #21
0
파일: gc_test.go 프로젝트: stevenxiao215/go
func TestGcSys(t *testing.T) {
	memstats := new(runtime.MemStats)
	runtime.GC()
	runtime.ReadMemStats(memstats)
	sys := memstats.Sys

	runtime.MemProfileRate = 0 // disable profiler

	itercount := 1000000
	if testing.Short() {
		itercount = 100000
	}
	for i := 0; i < itercount; i++ {
		workthegc()
	}

	// Should only be using a few MB.
	// We allocated 100 MB or (if not short) 1 GB.
	runtime.ReadMemStats(memstats)
	if sys > memstats.Sys {
		sys = 0
	} else {
		sys = memstats.Sys - sys
	}
	t.Logf("used %d extra bytes", sys)
	if sys > 16<<20 {
		t.Fatalf("using too much memory: %d bytes", sys)
	}
}
예제 #22
0
파일: path_test.go 프로젝트: timnau/golang
func TestClean(t *testing.T) {
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
	for _, test := range cleantests {
		if s := Clean(test.path); s != test.result {
			t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
		}
		if s := Clean(test.result); s != test.result {
			t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
		}
	}

	var ms runtime.MemStats
	runtime.ReadMemStats(&ms)
	allocs := -ms.Mallocs
	const rounds = 100
	for i := 0; i < rounds; i++ {
		for _, test := range cleantests {
			Clean(test.result)
		}
	}
	runtime.ReadMemStats(&ms)
	allocs += ms.Mallocs
	if allocs >= rounds {
		t.Errorf("Clean cleaned paths: %d allocations per test round, want zero", allocs/rounds)
	}
}
예제 #23
0
파일: init1.go 프로젝트: sjn1978/go-fuzz
func init() {
	c := make(chan int)
	go send(c)
	<-c

	const N = 1000
	const MB = 1 << 20
	b := make([]byte, MB)
	for i := range b {
		b[i] = byte(i%10 + '0')
	}
	s := string(b)

	memstats := new(runtime.MemStats)
	runtime.ReadMemStats(memstats)
	sys, numGC := memstats.Sys, memstats.NumGC

	// Generate 1,000 MB of garbage, only retaining 1 MB total.
	for i := 0; i < N; i++ {
		x = []byte(s)
	}

	// Verify that the garbage collector ran by seeing if we
	// allocated fewer than N*MB bytes from the system.
	runtime.ReadMemStats(memstats)
	sys1, numGC1 := memstats.Sys, memstats.NumGC
	if sys1-sys >= N*MB || numGC1 == numGC {
		println("allocated 1000 chunks of", MB, "and used ", sys1-sys, "memory")
		println("numGC went", numGC, "to", numGC)
		panic("init1")
	}
}
예제 #24
0
파일: allocs.go 프로젝트: RajibTheKing/gcc
// AllocsPerRun returns the average number of allocations during calls to f.
// Although the return value has type float64, it will always be an integral value.
//
// To compute the number of allocations, the function will first be run once as
// a warm-up.  The average number of allocations over the specified number of
// runs will then be measured and returned.
//
// AllocsPerRun sets GOMAXPROCS to 1 during its measurement and will restore
// it before returning.
func AllocsPerRun(runs int, f func()) (avg float64) {
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))

	// Warm up the function
	f()

	// Measure the starting statistics
	var memstats runtime.MemStats
	runtime.ReadMemStats(&memstats)
	mallocs := 0 - memstats.Mallocs

	// Run the function the specified number of times
	for i := 0; i < runs; i++ {
		f()
	}

	// Read the final statistics
	runtime.ReadMemStats(&memstats)
	mallocs += memstats.Mallocs

	// Average the mallocs over the runs (not counting the warm-up).
	// We are forced to return a float64 because the API is silly, but do
	// the division as integers so we can ask if AllocsPerRun()==1
	// instead of AllocsPerRun()<2.
	return float64(mallocs / uint64(runs))
}
예제 #25
0
파일: store.go 프로젝트: julochrobak/comp
func BuildStore(files string) (Store, error) {
	var err error
	store := Store{make(map[string]Type), make(map[string]Value)}
	if files != "" {
		for _, fileName := range strings.Split(files, ",") {
			var file *os.File
			if fileName[0] == '@' {
				fileName = fmt.Sprintf("in.%v", fileName[1:])
				file = os.Stdin
			} else {
				f, e := os.Open(fileName)
				if e != nil {
					err = e
					continue
				}
				defer f.Close()
				file = f
			}

			if e := store.Add(fileName, file); e != nil {
				err = e
				continue
			}
		}
	}

	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	log.Printf("garbage collecting (heap ~%vMB)", m.Alloc/1024/1024)
	runtime.GC()
	runtime.ReadMemStats(&m)
	log.Printf("done (heap ~%vMB)", m.Alloc/1024/1024)

	return store, err
}
예제 #26
0
func TestingBOnePass(b *testing.B, threads int, filelist, mountPoint string) error {
	runtime.GC()
	var before, after runtime.MemStats
	runtime.ReadMemStats(&before)

	// We shell out to an external program, so the time spent by
	// the part stat'ing doesn't interfere with the time spent by
	// the FUSE server.
	cmd := exec.Command("./bulkstat.bin",
		fmt.Sprintf("-cpu=%d", threads),
		fmt.Sprintf("-prefix=%s", mountPoint),
		fmt.Sprintf("-N=%d", b.N),
		fmt.Sprintf("-quiet=%v", !testutil.VerboseTest()),
		filelist)

	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	b.StartTimer()
	err := cmd.Run()
	b.StopTimer()
	runtime.ReadMemStats(&after)
	if err != nil {
		return err
	}

	if testutil.VerboseTest() {
		fmt.Printf("GC count %d, total GC time: %d ns/file\n",
			after.NumGC-before.NumGC, (after.PauseTotalNs-before.PauseTotalNs)/uint64(b.N))
	}
	return nil
}
예제 #27
0
파일: buffer_test.go 프로젝트: 2thetop/go
func TestGrow(t *testing.T) {
	x := []byte{'x'}
	y := []byte{'y'}
	tmp := make([]byte, 72)
	for _, startLen := range []int{0, 100, 1000, 10000, 100000} {
		xBytes := Repeat(x, startLen)
		for _, growLen := range []int{0, 100, 1000, 10000, 100000} {
			buf := NewBuffer(xBytes)
			// If we read, this affects buf.off, which is good to test.
			readBytes, _ := buf.Read(tmp)
			buf.Grow(growLen)
			yBytes := Repeat(y, growLen)
			// Check no allocation occurs in write, as long as we're single-threaded.
			var m1, m2 runtime.MemStats
			runtime.ReadMemStats(&m1)
			buf.Write(yBytes)
			runtime.ReadMemStats(&m2)
			if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs {
				t.Errorf("allocation occurred during write")
			}
			// Check that buffer has correct data.
			if !Equal(buf.Bytes()[0:startLen-readBytes], xBytes[readBytes:]) {
				t.Errorf("bad initial data at %d %d", startLen, growLen)
			}
			if !Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) {
				t.Errorf("bad written data at %d %d", startLen, growLen)
			}
		}
	}
}
예제 #28
0
파일: gc2.go 프로젝트: RajibTheKing/gcc
func main() {
	const N = 10000
	st := new(runtime.MemStats)
	memstats := new(runtime.MemStats)
	runtime.ReadMemStats(st)
	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.ReadMemStats(memstats)
	obj := memstats.HeapObjects - st.HeapObjects
	if obj > N/5 {
		fmt.Println("too many objects left:", obj)
		os.Exit(1)
	}
}
예제 #29
0
파일: gc.go 프로젝트: RajibTheKing/gcc
func GCSys() {
	runtime.GOMAXPROCS(1)
	memstats := new(runtime.MemStats)
	runtime.GC()
	runtime.ReadMemStats(memstats)
	sys := memstats.Sys

	runtime.MemProfileRate = 0 // disable profiler

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

	// Should only be using a few MB.
	// We allocated 100 MB or (if not short) 1 GB.
	runtime.ReadMemStats(memstats)
	if sys > memstats.Sys {
		sys = 0
	} else {
		sys = memstats.Sys - sys
	}
	if sys > 16<<20 {
		fmt.Printf("using too much memory: %d bytes\n", sys)
		return
	}
	fmt.Printf("OK\n")
}
예제 #30
0
파일: path_test.go 프로젝트: mm120/gcc
func TestClean(t *testing.T) {
	for _, test := range cleantests {
		if s := Clean(test.path); s != test.result {
			t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
		}
		if s := Clean(test.result); s != test.result {
			t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
		}
	}

	var ms runtime.MemStats
	runtime.ReadMemStats(&ms)
	allocs := -ms.Mallocs
	const rounds = 100
	for i := 0; i < rounds; i++ {
		for _, test := range cleantests {
			Clean(test.result)
		}
	}
	runtime.ReadMemStats(&ms)
	allocs += ms.Mallocs
	/* Fails with gccgo, which has no escape analysis.
	if allocs >= rounds {
		t.Errorf("Clean cleaned paths: %d allocations per test round, want zero", allocs/rounds)
	}
	*/
}