This commit is contained in:
jiazhizhong
2022-03-10 17:09:03 +08:00
commit 1279635d7f
97 changed files with 10632 additions and 0 deletions

75
pkg/pprof/pprof.go Normal file
View 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
View 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()
}
}

View File

@@ -0,0 +1,4 @@
package pprof
func listenSignal() {
}