Skip to content

Commit 4f664cc

Browse files
author
hengyouhai
committed
feat: add pprof endpoint option for profiling
Add --pprof and --pprof-addr flags to enable pprof HTTP endpoint for runtime profiling and debugging. Changes: - Add EnablePprof and PprofAddr to AgentOptions - Add --pprof flag to enable pprof server (default: false) - Add --pprof-addr flag to configure listen address (default: localhost:6060) - Implement startPprofServer() in agent.go Usage: kyanos watch --pprof kyanos watch --pprof --pprof-addr=:9090 The pprof endpoint provides access to: - /debug/pprof/heap - memory profiling - /debug/pprof/profile - CPU profiling - /debug/pprof/goroutine - goroutine dumps
1 parent 6294a28 commit 4f664cc

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

agent/agent.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"kyanos/version"
2020
"os"
2121
"os/exec"
22+
"net/http"
2223
"os/signal"
2324
"runtime"
2425
"strings"
@@ -34,6 +35,7 @@ import (
3435

3536
func SetupAgent(options ac.AgentOptions) {
3637
startGopsServer(options)
38+
startPprofServer(options)
3739

3840
if err := version.UpgradeDetect(); err != nil {
3941
if errors.Is(err, version.ErrBehindLatest) {
@@ -252,3 +254,17 @@ func startGopsServer(opts ac.AgentOptions) {
252254
}
253255
}
254256
}
257+
258+
func startPprofServer(opts ac.AgentOptions) {
259+
if !opts.EnablePprof {
260+
return
261+
}
262+
go func() {
263+
addr := opts.GetPprofAddr()
264+
common.AgentLog.Infof("Starting pprof server on http://%s/debug/pprof/", addr)
265+
common.AgentLog.Info("pprof endpoints: /debug/pprof/heap, /debug/pprof/profile, /debug/pprof/goroutine")
266+
if err := http.ListenAndServe(addr, nil); err != nil {
267+
common.AgentLog.Errorf("Failed to start pprof server: %v", err)
268+
}
269+
}()
270+
}

agent/common/options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ type AgentOptions struct {
5050
ConntrackCloseWaitTimeMills int
5151
MaxAllowStuckTimeMills int
5252
StartGopsServer bool
53+
EnablePprof bool
54+
PprofAddr string
5355

5456
FilterComm string
5557
ProcessExecEventChannel chan *bpf.AgentProcessExecEvent
@@ -74,6 +76,13 @@ type AgentOptions struct {
7476
FirstPacketEventMapPageNum int
7577
}
7678

79+
func (o AgentOptions) GetPprofAddr() string {
80+
if o.PprofAddr == "" {
81+
return "localhost:6060"
82+
}
83+
return o.PprofAddr
84+
}
85+
7786
func (o AgentOptions) FilterByContainer() bool {
7887
return o.ContainerId != "" || o.ContainerName != "" || o.PodName != ""
7988
}

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ func init() {
105105
rootCmd.PersistentFlags().IntVar(&options.MaxAllowStuckTimeMills, "max-allow-stuck-time-mills", 1000, "--max-allow-stuck-time-mills 100")
106106

107107
rootCmd.PersistentFlags().BoolVar(&options.StartGopsServer, "gops", false, "start gops server")
108+
rootCmd.PersistentFlags().BoolVar(&options.EnablePprof, "pprof", false, "enable pprof HTTP endpoint for profiling")
109+
rootCmd.PersistentFlags().StringVar(&options.PprofAddr, "pprof-addr", "localhost:6060", "pprof HTTP server address")
108110

109111
rootCmd.PersistentFlags().MarkHidden("default-log-level")
110112
rootCmd.PersistentFlags().MarkHidden("agent-log-level")

0 commit comments

Comments
 (0)