-
Notifications
You must be signed in to change notification settings - Fork 228
feat: add pprof endpoint option for profiling #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,8 @@ type AgentOptions struct { | |
| ConntrackCloseWaitTimeMills int | ||
| MaxAllowStuckTimeMills int | ||
| StartGopsServer bool | ||
| EnablePprof bool | ||
| PprofAddr string | ||
|
|
||
| FilterComm string | ||
| ProcessExecEventChannel chan *bpf.AgentProcessExecEvent | ||
|
|
@@ -74,6 +76,13 @@ type AgentOptions struct { | |
| FirstPacketEventMapPageNum int | ||
| } | ||
|
|
||
| func (o AgentOptions) GetPprofAddr() string { | ||
| if o.PprofAddr == "" { | ||
| return "localhost:6060" | ||
| } | ||
| return o.PprofAddr | ||
| } | ||
|
Comment on lines
+79
to
+84
|
||
|
|
||
| func (o AgentOptions) FilterByContainer() bool { | ||
| return o.ContainerId != "" || o.ContainerName != "" || o.PodName != "" | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,50 @@ VSCODE 直接打开项目即可,.vscode/launch.json 添加配置如下: | |
|
|
||
| 注意添加 `--debug-output` 参数。 | ||
|
|
||
| ## 性能分析(Profiling) | ||
|
|
||
| Kyanos 提供了 pprof 端点用于运行时性能分析和调试。你可以使用 `--pprof` 标志启用 pprof HTTP 服务器: | ||
|
|
||
| ```bash | ||
| ./kyanos watch --pprof | ||
| ``` | ||
|
|
||
| 默认情况下,pprof 服务器监听 `localhost:6060`。你可以使用 `--pprof-addr` 标志自定义监听地址: | ||
|
|
||
| ```bash | ||
| ./kyanos watch --pprof --pprof-addr="0.0.0.0:9090" | ||
|
||
| ``` | ||
|
|
||
| 可用的 pprof 端点: | ||
|
|
||
| | 端点 | 说明 | | ||
| |------|------| | ||
| | `/debug/pprof/` | 索引页面,显示所有可用的 profile | | ||
| | `/debug/pprof/heap` | 内存堆 profile | | ||
| | `/debug/pprof/profile` | CPU profile(默认 30 秒) | | ||
| | `/debug/pprof/goroutine` | Goroutine 堆栈信息 | | ||
| | `/debug/pprof/allocs` | 内存分配 profile | | ||
| | `/debug/pprof/block` | 阻塞 profile | | ||
| | `/debug/pprof/mutex` | 锁竞争 profile | | ||
|
Comment on lines
+104
to
+112
|
||
|
|
||
| 使用示例: | ||
|
|
||
| ```bash | ||
| # 采集 CPU profile | ||
| curl -o cpu.pprof http://localhost:6060/debug/pprof/profile?seconds=30 | ||
| go tool pprof cpu.pprof | ||
|
|
||
| # 查看堆内存 profile | ||
| go tool pprof http://localhost:6060/debug/pprof/heap | ||
|
|
||
| # 查看 goroutine 堆栈 | ||
| curl http://localhost:6060/debug/pprof/goroutine?debug=1 | ||
| ``` | ||
|
|
||
| > [!TIP] | ||
| > | ||
| > pprof 端点对于诊断 Kyanos 自身的性能问题、内存泄漏或 goroutine 泄漏非常有用。 | ||
|
|
||
| ## 源码结构 | ||
|
|
||
| ``` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,6 +94,50 @@ Make sure to add the `--debug-output` parameter. | |
| Protocol inference code can be debugged by printing logs using `bpf_printk`, | ||
| refer to: https://nakryiko.com/posts/bpf-tips-printk/. | ||
|
|
||
| ## Profiling | ||
|
|
||
| Kyanos provides pprof endpoints for runtime profiling and debugging. You can enable the pprof HTTP server using the `--pprof` flag: | ||
|
|
||
| ```bash | ||
| ./kyanos watch --pprof | ||
| ``` | ||
|
|
||
| By default, the pprof server listens on `localhost:6060`. You can customize the address using the `--pprof-addr` flag: | ||
|
|
||
| ```bash | ||
| ./kyanos watch --pprof --pprof-addr="0.0.0.0:9090" | ||
|
||
| ``` | ||
|
|
||
| Available pprof endpoints: | ||
|
|
||
| | Endpoint | Description | | ||
| |----------|-------------| | ||
| | `/debug/pprof/` | Index page with all available profiles | | ||
| | `/debug/pprof/heap` | Memory heap profile | | ||
| | `/debug/pprof/profile` | CPU profile (30 seconds by default) | | ||
| | `/debug/pprof/goroutine` | Goroutine dump | | ||
|
Comment on lines
+113
to
+118
|
||
| | `/debug/pprof/allocs` | Allocation profile | | ||
| | `/debug/pprof/block` | Block profile | | ||
| | `/debug/pprof/mutex` | Mutex contention profile | | ||
|
|
||
| Example usage: | ||
|
|
||
| ```bash | ||
| # Capture CPU profile | ||
| curl -o cpu.pprof http://localhost:6060/debug/pprof/profile?seconds=30 | ||
| go tool pprof cpu.pprof | ||
|
|
||
| # View heap profile | ||
| go tool pprof http://localhost:6060/debug/pprof/heap | ||
|
|
||
| # View goroutine dump | ||
| curl http://localhost:6060/debug/pprof/goroutine?debug=1 | ||
| ``` | ||
|
|
||
| > [!TIP] | ||
| > | ||
| > The pprof endpoint is useful for diagnosing performance issues, memory leaks, or goroutine leaks in Kyanos itself. | ||
|
|
||
| ## Source Code Structure | ||
|
|
||
| ``` | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
startPprofServer(options)is called beforeoptions.Ctxis initialized (it’s set later viasignal.NotifyContext). When--pprofis enabled,startPprofServerwill block on<-opts.Ctx.Done()and will panic ifCtxis nil. Move the pprof startup to afteroptions = ValidateAndRepairOptions(options)and afteroptions.Ctxis set, or makestartPprofServertolerate a nil context (e.g., skip shutdown hook / use Background).