Skip to content

Instantly share code, notes, and snippets.

@awreece
Created April 10, 2012 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save awreece/2348427 to your computer and use it in GitHub Desktop.
Save awreece/2348427 to your computer and use it in GitHub Desktop.
Parallel Malloc Test for Go
// +build ignore
// malloc test for false sharing
package main
import (
"flag"
"runtime"
"sync"
"testing"
)
var (
procs int
allocsize int
)
func init() {
flag.IntVar(&procs, "procs", 1, "Value to set GOMAXPROCS")
flag.IntVar(&allocsize, "size", 16, "Size of block to allocate")
}
func do_thread(niters int, wg *sync.WaitGroup) {
runtime.LockOSThread()
for iter := 0; iter < niters; iter++ {
b := runtime.Alloc(uintptr(allocsize))
// Write a bunch to b.
for i, _ := range b {
b[i] = byte(i)
}
runtime.Free(b)
}
wg.Done()
runtime.UnlockOSThread()
}
func BenchmarkSharing(b *testing.B) {
runtime.GC() // clean up garbage from init
oldprocs := runtime.GOMAXPROCS(procs)
var wg sync.WaitGroup
for i := 0; i < procs; i++ {
wg.Add(1)
go do_thread(b.N / procs, &wg)
}
wg.Wait()
runtime.GOMAXPROCS(oldprocs)
}
$ go test mallocparallel_test.go
# command-line-arguments
./mallocparallel_test.go:28: undefined: runtime.Alloc
./mallocparallel_test.go:33: undefined: runtime.Free
FAIL command-line-arguments [build failed]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment