Created
April 10, 2012 05:03
-
-
Save awreece/2348427 to your computer and use it in GitHub Desktop.
Parallel Malloc Test for Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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