CPU & MEM pprof output #12

This commit is contained in:
kenshinx 2015-10-14 15:21:21 +08:00
parent c37e8c947b
commit 44a3923878
1 changed files with 35 additions and 0 deletions

35
main.go
View File

@ -4,6 +4,7 @@ import (
"os"
"os/signal"
"runtime"
"runtime/pprof"
"time"
)
@ -26,6 +27,11 @@ func main() {
logger.Info("godns %s start", settings.Version)
if settings.Debug {
go profileCPU()
go profileMEM()
}
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt)
@ -40,6 +46,35 @@ forever:
}
func profileCPU() {
f, err := os.Create("godns.cprof")
if err != nil {
logger.Error("%s", err)
return
}
pprof.StartCPUProfile(f)
time.AfterFunc(6*time.Minute, func() {
pprof.StopCPUProfile()
f.Close()
})
}
func profileMEM() {
f, err := os.Create("godns.mprof")
if err != nil {
logger.Error("%s", err)
return
}
time.AfterFunc(5*time.Minute, func() {
pprof.WriteHeapProfile(f)
f.Close()
})
}
func initLogger() {
logger = NewLogger()