diff options
| author | Ian Lance Taylor <iant@golang.org> | 2016-04-29 15:20:27 -0700 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2016-05-04 00:08:19 +0000 |
| commit | 84e808043ff40992ea1e25beb58365fd8e4f2591 (patch) | |
| tree | 7bdc538f6719fc3b08409baa020e436a4ab1a64a /src/runtime/testdata/testprogcgo | |
| parent | 4d5ac10f690bbc742a3cbf186ad8f3169a45ee26 (diff) | |
| download | go-84e808043ff40992ea1e25beb58365fd8e4f2591.tar.xz | |
runtime: use cgo traceback for SIGPROF
If we collected a cgo traceback when entering the SIGPROF signal
handler, record it as part of the profiling stack trace.
This serves as the promised test for https://golang.org/cl/21055 .
Change-Id: I5f60cd6cea1d9b7c3932211483a6bfab60ed21d2
Reviewed-on: https://go-review.googlesource.com/22650
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
Diffstat (limited to 'src/runtime/testdata/testprogcgo')
| -rw-r--r-- | src/runtime/testdata/testprogcgo/pprof.go | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/runtime/testdata/testprogcgo/pprof.go b/src/runtime/testdata/testprogcgo/pprof.go new file mode 100644 index 0000000000..04ac4fe92e --- /dev/null +++ b/src/runtime/testdata/testprogcgo/pprof.go @@ -0,0 +1,96 @@ +// Copyright 2016 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 main + +// Run a slow C function saving a CPU profile. + +/* +#include <stdint.h> + +int salt1; +int salt2; + +void cpuHog() { + int foo = salt1; + int i; + + for (i = 0; i < 100000; i++) { + if (foo > 0) { + foo *= foo; + } else { + foo *= foo + 1; + } + } + salt2 = foo; +} + +static int cpuHogCount; + +struct cgoTracebackArg { + uintptr_t context; + uintptr_t* buf; + uintptr_t max; +}; + +// pprofCgoTraceback is passed to runtime.SetCgoTraceback. +// For testing purposes it pretends that all CPU hits in C code are in cpuHog. +void pprofCgoTraceback(void* parg) { + struct cgoTracebackArg* arg = (struct cgoTracebackArg*)(parg); + arg->buf[0] = (uintptr_t)(cpuHog) + 0x10; + arg->buf[1] = 0; + ++cpuHogCount; +} + +// getCpuHogCount fetches the number of times we've seen cpuHog in the +// traceback. +int getCpuHogCount() { + return cpuHogCount; +} +*/ +import "C" + +import ( + "fmt" + "io/ioutil" + "os" + "runtime" + "runtime/pprof" + "time" + "unsafe" +) + +func init() { + register("CgoPprof", CgoPprof) +} + +func CgoPprof() { + runtime.SetCgoTraceback(0, unsafe.Pointer(C.pprofCgoTraceback), nil, nil) + + f, err := ioutil.TempFile("", "prof") + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } + + if err := pprof.StartCPUProfile(f); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } + + t0 := time.Now() + for C.getCpuHogCount() < 2 && time.Since(t0) < time.Second { + C.cpuHog() + } + + pprof.StopCPUProfile() + + name := f.Name() + if err := f.Close(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(2) + } + + fmt.Println(name) +} |
