From: Dmitriy Vyukov Date: Sun, 11 Aug 2013 09:05:51 +0000 (+0400) Subject: runtime/pprof: add block profile test X-Git-Tag: go1.2rc2~666 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2758101b9d336a892538da38a11fe66ede2aa0cb;p=gostls13.git runtime/pprof: add block profile test Fixes #5993. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/12665046 --- diff --git a/src/pkg/runtime/pprof/pprof_test.go b/src/pkg/runtime/pprof/pprof_test.go index 040d77a434..630d3643be 100644 --- a/src/pkg/runtime/pprof/pprof_test.go +++ b/src/pkg/runtime/pprof/pprof_test.go @@ -11,10 +11,13 @@ import ( "bytes" "hash/crc32" "os/exec" + "regexp" "runtime" . "runtime/pprof" "strings" + "sync" "testing" + "time" "unsafe" ) @@ -148,3 +151,55 @@ var badOS = map[string]bool{ "netbsd": true, "openbsd": true, } + +func TestBlockProfile(t *testing.T) { + runtime.SetBlockProfileRate(1) + defer runtime.SetBlockProfileRate(0) + produceChanContention() + produceMutexContention() + var w bytes.Buffer + Lookup("block").WriteTo(&w, 1) + prof := w.String() + + if !strings.HasPrefix(prof, "--- contention:\ncycles/second=") { + t.Fatalf("Bad profile header:\n%v", prof) + } + + reChan := regexp.MustCompile(` +[0-9]+ [0-9]+ @ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ +# 0x[0-9,a-f]+ runtime/pprof_test\.produceChanContention\+0x[0-9,a-f]+ .*/src/pkg/runtime/pprof/pprof_test.go:[0-9]+ +# 0x[0-9,a-f]+ runtime/pprof_test\.TestBlockProfile\+0x[0-9,a-f]+ .*/src/pkg/runtime/pprof/pprof_test.go:[0-9]+ +`) + if !reChan.MatchString(prof) { + t.Fatalf("Bad chan entry, expect:\n%v\ngot:\n%v", reChan, prof) + } + + reMutex := regexp.MustCompile(` +[0-9]+ [0-9]+ @ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ 0x[0-9,a-f]+ +# 0x[0-9,a-f]+ sync\.\(\*Mutex\)\.Lock\+0x[0-9,a-f]+ .*/src/pkg/sync/mutex\.go:[0-9]+ +# 0x[0-9,a-f]+ runtime/pprof_test\.produceMutexContention\+0x[0-9,a-f]+ .*/src/pkg/runtime/pprof/pprof_test.go:[0-9]+ +# 0x[0-9,a-f]+ runtime/pprof_test\.TestBlockProfile\+0x[0-9,a-f]+ .*/src/pkg/runtime/pprof/pprof_test.go:[0-9]+ +`) + if !reMutex.MatchString(prof) { + t.Fatalf("Bad mutex entry, expect:\n%v\ngot:\n%v", reMutex, prof) + } +} + +func produceChanContention() { + c := make(chan bool) + go func() { + time.Sleep(10 * time.Millisecond) + c <- true + }() + <-c +} + +func produceMutexContention() { + var mu sync.Mutex + mu.Lock() + go func() { + time.Sleep(10 * time.Millisecond) + mu.Unlock() + }() + mu.Lock() +}