Merge pull request #67 from chihaya/pprof-handler
Add pprof debug HTTP endpoint
This commit is contained in:
commit
9a0d86610c
2 changed files with 54 additions and 18 deletions
20
chihaya.go
20
chihaya.go
|
@ -9,7 +9,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/pprof"
|
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
@ -27,13 +26,11 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
maxProcs int
|
maxProcs int
|
||||||
profile string
|
|
||||||
configPath string
|
configPath string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.IntVar(&maxProcs, "maxprocs", runtime.NumCPU(), "maximum parallel threads")
|
flag.IntVar(&maxProcs, "maxprocs", runtime.NumCPU(), "maximum parallel threads")
|
||||||
flag.StringVar(&profile, "profile", "", "if non-empty, path to write profiling data")
|
|
||||||
flag.StringVar(&configPath, "config", "", "path to the configuration file")
|
flag.StringVar(&configPath, "config", "", "path to the configuration file")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,21 +44,8 @@ func Boot() {
|
||||||
runtime.GOMAXPROCS(maxProcs)
|
runtime.GOMAXPROCS(maxProcs)
|
||||||
glog.V(1).Info("Set max threads to ", maxProcs)
|
glog.V(1).Info("Set max threads to ", maxProcs)
|
||||||
|
|
||||||
if profile != "" {
|
debugBoot()
|
||||||
f, err := os.Create(profile)
|
defer debugShutdown()
|
||||||
if err != nil {
|
|
||||||
glog.Fatalf("Failed to create profile file: %s\n", err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
pprof.StartCPUProfile(f)
|
|
||||||
glog.Info("Started profiling")
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
pprof.StopCPUProfile()
|
|
||||||
glog.Info("Stopped profiling")
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg, err := config.Open(configPath)
|
cfg, err := config.Open(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
52
debug.go
Normal file
52
debug.go
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
package chihaya
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"runtime/pprof"
|
||||||
|
|
||||||
|
_ "net/http/pprof"
|
||||||
|
|
||||||
|
"github.com/golang/glog"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
profile string
|
||||||
|
debugAddr string
|
||||||
|
profileFile *os.File
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.StringVar(&profile, "profile", "", "if non-empty, path to write CPU profiling data")
|
||||||
|
flag.StringVar(&debugAddr, "debug", "", "if non-empty, address to serve debug data")
|
||||||
|
}
|
||||||
|
|
||||||
|
func debugBoot() {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if debugAddr != "" {
|
||||||
|
go func() {
|
||||||
|
glog.Info("Starting debug HTTP on ", debugAddr)
|
||||||
|
glog.Fatal(http.ListenAndServe(debugAddr, nil))
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
if profile != "" {
|
||||||
|
profileFile, err = os.Create(profile)
|
||||||
|
if err != nil {
|
||||||
|
glog.Fatalf("Failed to create profile file: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pprof.StartCPUProfile(profileFile)
|
||||||
|
glog.Info("Started profiling")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func debugShutdown() {
|
||||||
|
if profileFile != nil {
|
||||||
|
profileFile.Close()
|
||||||
|
pprof.StopCPUProfile()
|
||||||
|
glog.Info("Stopped profiling")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue