From 226a448b5798f594345a3b4166c755fa19a5e057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=A5=96=E5=BB=BA?= Date: Wed, 27 Sep 2023 10:13:33 +0800 Subject: [PATCH] dump cpu/mem profile into file on signal SIGUSR1/SIGUSR2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张祖建 --- cmd/cmdmain.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/cmd/cmdmain.go b/cmd/cmdmain.go index 90ba28cd155e..843cb880168a 100644 --- a/cmd/cmdmain.go +++ b/cmd/cmdmain.go @@ -1,8 +1,15 @@ package main import ( + "fmt" "os" + "os/signal" "path/filepath" + "runtime/pprof" + "syscall" + "time" + + "k8s.io/klog/v2" "github.com/kubeovn/kube-ovn/cmd/controller" "github.com/kubeovn/kube-ovn/cmd/controller_health_check" @@ -24,7 +31,47 @@ const ( CmdOvnLeaderChecker = "kube-ovn-leader-checker" ) +const timeFormat = "2006-01-02_15:04:05" + func main() { + ch1 := make(chan os.Signal, 1) + ch2 := make(chan os.Signal, 1) + signal.Notify(ch1, syscall.SIGUSR1) + signal.Notify(ch2, syscall.SIGUSR2) + go func() { + for { + <-ch1 + name := fmt.Sprintf("cpu-profile-%s.pprof", time.Now().Format(timeFormat)) + f, err := os.Create(filepath.Join(os.TempDir(), name)) + if err != nil { + klog.Errorf("failed to create cpu profile file: %v", err) + return + } + defer f.Close() + if err = pprof.StartCPUProfile(f); err != nil { + klog.Errorf("failed to start cpu profile: %v", err) + return + } + time.Sleep(30 * time.Second) + pprof.StopCPUProfile() + } + }() + go func() { + for { + <-ch2 + name := fmt.Sprintf("mem-profile-%s.pprof", time.Now().Format(timeFormat)) + f, err := os.Create(filepath.Join(os.TempDir(), name)) + if err != nil { + klog.Errorf("failed to create memory profile file: %v", err) + return + } + defer f.Close() + if err = pprof.WriteHeapProfile(f); err != nil { + klog.Errorf("failed to write memory profile file: %v", err) + } + } + }() + cmd := filepath.Base(os.Args[0]) switch cmd { case CmdController: