1
0
mirror of https://github.com/golang/go synced 2024-10-05 06:21:24 -06:00
go/src/pkg/runtime/pprof/pprof_test.go

233 lines
5.6 KiB
Go
Raw Normal View History

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pprof_test
import (
"bytes"
"hash/crc32"
"os/exec"
"regexp"
"runtime"
. "runtime/pprof"
"strings"
"sync"
"testing"
"time"
"unsafe"
)
func TestCPUProfile(t *testing.T) {
buf := make([]byte, 100000)
testCPUProfile(t, []string{"crc32.ChecksumIEEE"}, func() {
// This loop takes about a quarter second on a 2 GHz laptop.
// We only need to get one 100 Hz clock tick, so we've got
// a 25x safety buffer.
for i := 0; i < 1000; i++ {
crc32.ChecksumIEEE(buf)
}
})
}
func TestCPUProfileMultithreaded(t *testing.T) {
buf := make([]byte, 100000)
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2))
testCPUProfile(t, []string{"crc32.ChecksumIEEE", "crc32.Update"}, func() {
c := make(chan int)
go func() {
for i := 0; i < 2000; i++ {
crc32.Update(0, crc32.IEEETable, buf)
}
c <- 1
}()
// This loop takes about a quarter second on a 2 GHz laptop.
// We only need to get one 100 Hz clock tick, so we've got
// a 25x safety buffer.
for i := 0; i < 2000; i++ {
crc32.ChecksumIEEE(buf)
}
})
}
func testCPUProfile(t *testing.T, need []string, f func()) {
runtime/pprof: disable test on darwin Fixes #1641. Actually it side steps the real issue, which is that the setitimer(2) implementation on OS X is not useful for profiling of multi-threaded programs. I filed the below using the Apple Bug Reporter. /* Filed as Apple Bug Report #9177434. This program creates a new pthread that loops, wasting cpu time. In the main pthread, it sleeps on a condition that will never come true. Before doing so it sets up an interval timer using ITIMER_PROF. The handler prints a message saying which thread it is running on. POSIX does not specify which thread should receive the signal, but in order to be useful in a user-mode self-profiler like pprof or gprof http://code.google.com/p/google-perftools http://www.delorie.com/gnu/docs/binutils/gprof_25.html it is important that the thread that receives the signal is the one whose execution caused the timer to expire. Linux and FreeBSD handle this by sending the signal to the process's queue but delivering it to the current thread if possible: http://lxr.linux.no/linux+v2.6.38/kernel/signal.c#L802 807 /* 808 * Now find a thread we can wake up to take the signal off the queue. 809 * 810 * If the main thread wants the signal, it gets first crack. 811 * Probably the least surprising to the average bear. 812 * / http://fxr.watson.org/fxr/source/kern/kern_sig.c?v=FREEBSD8;im=bigexcerpts#L1907 1914 /* 1915 * Check if current thread can handle the signal without 1916 * switching context to another thread. 1917 * / On those operating systems, this program prints: $ ./a.out signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread $ The OS X kernel does not have any such preference. Its get_signalthread does not prefer current_thread(), in contrast to the other two systems, so the signal gets delivered to the first thread in the list that is able to handle it, which ends up being the main thread in this experiment. http://fxr.watson.org/fxr/source/bsd/kern/kern_sig.c?v=xnu-1456.1.26;im=excerpts#L1666 $ ./a.out signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread $ The fix is to make get_signalthread use the same heuristic as Linux and FreeBSD, namely to use current_thread() if possible before scanning the process thread list. */ #include <sys/time.h> #include <sys/signal.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> static void handler(int); static void* looper(void*); static pthread_t pmain, ploop; int main(void) { struct itimerval it; struct sigaction sa; pthread_cond_t cond; pthread_mutex_t mu; memset(&sa, 0, sizeof sa); sa.sa_handler = handler; sa.sa_flags = SA_RESTART; memset(&sa.sa_mask, 0xff, sizeof sa.sa_mask); sigaction(SIGPROF, &sa, 0); pmain = pthread_self(); pthread_create(&ploop, 0, looper, 0); memset(&it, 0, sizeof it); it.it_interval.tv_usec = 10000; it.it_value = it.it_interval; setitimer(ITIMER_PROF, &it, 0); pthread_mutex_init(&mu, 0); pthread_mutex_lock(&mu); pthread_cond_init(&cond, 0); for(;;) pthread_cond_wait(&cond, &mu); return 0; } static void handler(int sig) { static int nsig; pthread_t p; p = pthread_self(); if(p == pmain) printf("signal on sleeping main thread\n"); else if(p == ploop) printf("signal on cpu-chewing looper thread\n"); else printf("signal on %p\n", (void*)p); if(++nsig >= 10) exit(0); } static void* looper(void *v) { for(;;); } R=r CC=golang-dev https://golang.org/cl/4273113
2011-03-25 11:47:07 -06:00
switch runtime.GOOS {
case "darwin":
out, err := exec.Command("uname", "-a").CombinedOutput()
if err != nil {
t.Fatal(err)
}
vers := string(out)
t.Logf("uname -a: %v", vers)
runtime/pprof: disable test on darwin Fixes #1641. Actually it side steps the real issue, which is that the setitimer(2) implementation on OS X is not useful for profiling of multi-threaded programs. I filed the below using the Apple Bug Reporter. /* Filed as Apple Bug Report #9177434. This program creates a new pthread that loops, wasting cpu time. In the main pthread, it sleeps on a condition that will never come true. Before doing so it sets up an interval timer using ITIMER_PROF. The handler prints a message saying which thread it is running on. POSIX does not specify which thread should receive the signal, but in order to be useful in a user-mode self-profiler like pprof or gprof http://code.google.com/p/google-perftools http://www.delorie.com/gnu/docs/binutils/gprof_25.html it is important that the thread that receives the signal is the one whose execution caused the timer to expire. Linux and FreeBSD handle this by sending the signal to the process's queue but delivering it to the current thread if possible: http://lxr.linux.no/linux+v2.6.38/kernel/signal.c#L802 807 /* 808 * Now find a thread we can wake up to take the signal off the queue. 809 * 810 * If the main thread wants the signal, it gets first crack. 811 * Probably the least surprising to the average bear. 812 * / http://fxr.watson.org/fxr/source/kern/kern_sig.c?v=FREEBSD8;im=bigexcerpts#L1907 1914 /* 1915 * Check if current thread can handle the signal without 1916 * switching context to another thread. 1917 * / On those operating systems, this program prints: $ ./a.out signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread signal on cpu-chewing looper thread $ The OS X kernel does not have any such preference. Its get_signalthread does not prefer current_thread(), in contrast to the other two systems, so the signal gets delivered to the first thread in the list that is able to handle it, which ends up being the main thread in this experiment. http://fxr.watson.org/fxr/source/bsd/kern/kern_sig.c?v=xnu-1456.1.26;im=excerpts#L1666 $ ./a.out signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread signal on sleeping main thread $ The fix is to make get_signalthread use the same heuristic as Linux and FreeBSD, namely to use current_thread() if possible before scanning the process thread list. */ #include <sys/time.h> #include <sys/signal.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> static void handler(int); static void* looper(void*); static pthread_t pmain, ploop; int main(void) { struct itimerval it; struct sigaction sa; pthread_cond_t cond; pthread_mutex_t mu; memset(&sa, 0, sizeof sa); sa.sa_handler = handler; sa.sa_flags = SA_RESTART; memset(&sa.sa_mask, 0xff, sizeof sa.sa_mask); sigaction(SIGPROF, &sa, 0); pmain = pthread_self(); pthread_create(&ploop, 0, looper, 0); memset(&it, 0, sizeof it); it.it_interval.tv_usec = 10000; it.it_value = it.it_interval; setitimer(ITIMER_PROF, &it, 0); pthread_mutex_init(&mu, 0); pthread_mutex_lock(&mu); pthread_cond_init(&cond, 0); for(;;) pthread_cond_wait(&cond, &mu); return 0; } static void handler(int sig) { static int nsig; pthread_t p; p = pthread_self(); if(p == pmain) printf("signal on sleeping main thread\n"); else if(p == ploop) printf("signal on cpu-chewing looper thread\n"); else printf("signal on %p\n", (void*)p); if(++nsig >= 10) exit(0); } static void* looper(void *v) { for(;;); } R=r CC=golang-dev https://golang.org/cl/4273113
2011-03-25 11:47:07 -06:00
case "plan9":
// unimplemented
return
}
var prof bytes.Buffer
if err := StartCPUProfile(&prof); err != nil {
t.Fatal(err)
}
f()
StopCPUProfile()
// Convert []byte to []uintptr.
bytes := prof.Bytes()
l := len(bytes) / int(unsafe.Sizeof(uintptr(0)))
val := *(*[]uintptr)(unsafe.Pointer(&bytes))
val = val[:l]
if l < 13 {
t.Logf("profile too short: %#x", val)
if badOS[runtime.GOOS] {
t.Skipf("ignoring failure on %s; see golang.org/issue/6047", runtime.GOOS)
return
}
t.FailNow()
}
hd, val, tl := val[:5], val[5:l-3], val[l-3:]
if hd[0] != 0 || hd[1] != 3 || hd[2] != 0 || hd[3] != 1e6/100 || hd[4] != 0 {
t.Fatalf("unexpected header %#x", hd)
}
if tl[0] != 0 || tl[1] != 1 || tl[2] != 0 {
t.Fatalf("malformed end-of-data marker %#x", tl)
}
// Check that profile is well formed and contains ChecksumIEEE.
have := make([]uintptr, len(need))
for len(val) > 0 {
if len(val) < 2 || val[0] < 1 || val[1] < 1 || uintptr(len(val)) < 2+val[1] {
t.Fatalf("malformed profile. leftover: %#x", val)
}
for _, pc := range val[2 : 2+val[1]] {
f := runtime.FuncForPC(pc)
if f == nil {
continue
}
for i, name := range need {
if strings.Contains(f.Name(), name) {
have[i] += val[0]
}
}
}
val = val[2+val[1]:]
}
var total uintptr
for i, name := range need {
total += have[i]
t.Logf("%s: %d\n", name, have[i])
}
ok := true
if total == 0 {
t.Logf("no CPU profile samples collected")
ok = false
}
min := total / uintptr(len(have)) / 3
for i, name := range need {
if have[i] < min {
t.Logf("%s has %d samples out of %d, want at least %d, ideally %d", name, have[i], total, min, total/uintptr(len(have)))
ok = false
}
}
if !ok {
if badOS[runtime.GOOS] {
t.Skipf("ignoring failure on %s; see golang.org/issue/6047", runtime.GOOS)
return
}
t.FailNow()
}
}
func TestCPUProfileWithFork(t *testing.T) {
// Fork can hang if preempted with signals frequently enough (see issue 5517).
// Ensure that we do not do this.
heap := 1 << 30
if testing.Short() {
heap = 100 << 20
}
// This makes fork slower.
garbage := make([]byte, heap)
// Need to touch the slice, otherwise it won't be paged in.
done := make(chan bool)
go func() {
for i := range garbage {
garbage[i] = 42
}
done <- true
}()
<-done
var prof bytes.Buffer
if err := StartCPUProfile(&prof); err != nil {
t.Fatal(err)
}
defer StopCPUProfile()
for i := 0; i < 10; i++ {
exec.Command("go").CombinedOutput()
}
}
// Operating systems that are expected to fail the tests. See issue 6047.
var badOS = map[string]bool{
"darwin": true,
"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()
}