mirror of
https://github.com/google/gops.git
synced 2024-11-24 08:22:25 +02:00
add experimental pprof support
This commit is contained in:
parent
d53c7a7dab
commit
c3bb8a5007
@ -12,6 +12,7 @@ import (
|
||||
"os"
|
||||
gosignal "os/signal"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
|
||||
"github.com/google/gops/signal"
|
||||
)
|
||||
@ -30,6 +31,7 @@ func Start() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(sock)
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
gosignal.Notify(c, os.Interrupt)
|
||||
@ -98,6 +100,10 @@ func handle(conn net.Conn, msg []byte) error {
|
||||
fmt.Fprintf(conn, "debug-gc: %v\n", s.DebugGC)
|
||||
case signal.Version:
|
||||
fmt.Fprintf(conn, "%v\n", runtime.Version())
|
||||
case signal.HeapProfile:
|
||||
pprof.Lookup("heap").WriteTo(conn, 1)
|
||||
case signal.CPUProfile:
|
||||
panic("not implemented")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
25
cmd.go
25
cmd.go
@ -5,6 +5,10 @@ import (
|
||||
"io/ioutil"
|
||||
"net"
|
||||
|
||||
"os/exec"
|
||||
|
||||
"os"
|
||||
|
||||
"github.com/google/gops/signal"
|
||||
)
|
||||
|
||||
@ -13,6 +17,7 @@ var cmds = map[string](func(pid int) error){
|
||||
"gc": gc,
|
||||
"memstats": memStats,
|
||||
"version": version,
|
||||
"pprof": pprof,
|
||||
}
|
||||
|
||||
func stackTrace(pid int) error {
|
||||
@ -47,6 +52,26 @@ func version(pid int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func pprof(pid int) error {
|
||||
out, err := cmd(pid, signal.HeapProfile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tmpfile, err := ioutil.TempFile("", "heap-profile")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ioutil.WriteFile(tmpfile.Name(), []byte(out), 0); err != nil {
|
||||
return err
|
||||
}
|
||||
cmd := exec.Command("go", "tool", "pprof", tmpfile.Name())
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func cmd(pid int, c byte) (string, error) {
|
||||
sock := fmt.Sprintf("/tmp/gops%d.sock", pid)
|
||||
conn, err := net.Dial("unix", sock)
|
||||
|
@ -17,4 +17,10 @@ const (
|
||||
|
||||
// Version prints the Go version.
|
||||
Version = byte(0x4)
|
||||
|
||||
// HeapProfile starts `go tool pprof`` with the current memory profile.
|
||||
HeapProfile = byte(0x5)
|
||||
|
||||
// CPUProfile starts `go tool pprof` with the current CPU profile
|
||||
CPUProfile = byte(0x6)
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user