Commit 51b6b278 authored by suyanlong's avatar suyanlong

Add runtimePProf function

parent be162b7f
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
"runtime/pprof"
"sync" "sync"
"syscall" "syscall"
"time" "time"
...@@ -18,6 +19,7 @@ import ( ...@@ -18,6 +19,7 @@ import (
"github.com/link33/sidecar/internal/loggers" "github.com/link33/sidecar/internal/loggers"
"github.com/link33/sidecar/internal/repo" "github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/pkg/log" "github.com/link33/sidecar/pkg/log"
"github.com/link33/sidecar/tool"
) )
var startCMD = cli.Command{ var startCMD = cli.Command{
...@@ -63,7 +65,8 @@ func start(ctx *cli.Context) error { ...@@ -63,7 +65,8 @@ func start(ctx *cli.Context) error {
return err return err
} }
runPProf(config.Port.PProf) httpPProf(config.Port.PProf)
runtimePProf()
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
...@@ -96,7 +99,7 @@ func handleShutdown(sidecar internal.Launcher, wg *sync.WaitGroup) { ...@@ -96,7 +99,7 @@ func handleShutdown(sidecar internal.Launcher, wg *sync.WaitGroup) {
}() }()
} }
func runPProf(port int64) { func httpPProf(port int64) {
go func() { go func() {
addr := fmt.Sprintf("localhost:%d", port) addr := fmt.Sprintf("localhost:%d", port)
fmt.Printf("Pprof on localhost:%d\n\n", port) fmt.Printf("Pprof on localhost:%d\n\n", port)
...@@ -108,6 +111,38 @@ func runPProf(port int64) { ...@@ -108,6 +111,38 @@ func runPProf(port int64) {
}() }()
} }
// runtimePProf will record the cpu or memory profiles every 5 second.
func runtimePProf() {
tick := time.NewTicker(time.Second * 5)
rootPath := filepath.Join(repo.DefaultPathRoot, "/pprof/")
exist := tool.Exist(rootPath)
if !exist {
err := os.Mkdir(rootPath, os.ModePerm)
if err != nil {
fmt.Printf("----- runtimePProf start failed, err: %s -----\n", err.Error())
return
}
}
go func() {
for {
select {
case <-tick.C:
pprof.StopCPUProfile()
subCpuPath := fmt.Sprint("cpu-", time.Now().Format("20060102-15:04:05"))
cpuPath := filepath.Join(rootPath, subCpuPath)
cpuFile, _ := os.Create(cpuPath)
_ = pprof.StartCPUProfile(cpuFile)
subMemPath := fmt.Sprint("mem-", time.Now().Format("20060102-15:04:05"))
memPath := filepath.Join(rootPath, subMemPath)
memFile, _ := os.Create(memPath)
_ = pprof.WriteHeapProfile(memFile)
_ = memFile.Close()
}
}
}()
}
func checkPlugin(pluginName string) error { func checkPlugin(pluginName string) error {
// check if plugin exists // check if plugin exists
pluginRoot, err := repo.PluginPath() pluginRoot, err := repo.PluginPath()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment