fix
This commit is contained in:
75
pkg/pprof/pprof.go
Normal file
75
pkg/pprof/pprof.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package pprof
|
||||
|
||||
import (
|
||||
"jiacrontab/pkg/file"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
"time"
|
||||
|
||||
"github.com/iwannay/log"
|
||||
)
|
||||
|
||||
func ListenPprof() {
|
||||
go listenSignal()
|
||||
}
|
||||
|
||||
func cpuprofile() {
|
||||
path := filepath.Join("pprof", "cpuprofile")
|
||||
log.Debugf("profile save in %s", path)
|
||||
|
||||
f, err := file.CreateFile(path)
|
||||
if err != nil {
|
||||
log.Error("could not create CPU profile: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
if err := pprof.StartCPUProfile(f); err != nil {
|
||||
log.Error("could not start CPU profile: ", err)
|
||||
} else {
|
||||
time.Sleep(time.Minute)
|
||||
}
|
||||
defer pprof.StopCPUProfile()
|
||||
}
|
||||
|
||||
func memprofile() {
|
||||
path := filepath.Join("pprof", "memprofile")
|
||||
log.Debugf("profile save in %s", path)
|
||||
f, err := file.CreateFile(path)
|
||||
if err != nil {
|
||||
log.Error("could not create memory profile: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
runtime.GC() // get up-to-date statistics
|
||||
|
||||
if err := pprof.WriteHeapProfile(f); err != nil {
|
||||
log.Error("could not write memory profile: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func profile() {
|
||||
names := []string{
|
||||
"goroutine",
|
||||
"heap",
|
||||
"allocs",
|
||||
"threadcreate",
|
||||
"block",
|
||||
"mutex",
|
||||
}
|
||||
for _, name := range names {
|
||||
path := filepath.Join("pprof", name)
|
||||
log.Debugf("profile save in %s", path)
|
||||
f, err := file.CreateFile(path)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
pprof.Lookup(name).WriteTo(f, 0)
|
||||
}
|
||||
|
||||
}
|
||||
20
pkg/pprof/pprof_posix.go
Normal file
20
pkg/pprof/pprof_posix.go
Normal file
@@ -0,0 +1,20 @@
|
||||
// +build !windows
|
||||
|
||||
package pprof
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func listenSignal() {
|
||||
signChan := make(chan os.Signal, 1)
|
||||
signal.Notify(signChan, syscall.SIGUSR1)
|
||||
for {
|
||||
<-signChan
|
||||
profile()
|
||||
memprofile()
|
||||
cpuprofile()
|
||||
}
|
||||
}
|
||||
4
pkg/pprof/pprof_windows.go
Normal file
4
pkg/pprof/pprof_windows.go
Normal file
@@ -0,0 +1,4 @@
|
||||
package pprof
|
||||
|
||||
func listenSignal() {
|
||||
}
|
||||
Reference in New Issue
Block a user