golang线上问题卡住分析与排查

从技术上来看,获取堆栈信息第一步。跟java/c中的jstack/pstack一样。
# 获取goroutine堆栈信息
 kill -SIGABRT <pid> # 会生成堆栈到stderr
 
内置pprof
import _ "net/http/pprof"
go func() {
    log.Println(http.ListenAndServe("localhost:6060", nil))
}()

HTTP 服务已经内置,不用专门启动。
# 获取30秒的CPU profile
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30

# 获取堆内存信息
go tool pprof http://localhost:6060/debug/pprof/heap

# 获取goroutine信息
go tool pprof http://localhost:6060/debug/pprof/goroutine
 

可用的 pprof 端点

默认情况下,pprof 会注册以下路由:

  • /debug/pprof/ - pprof 主页,显示所有可用 profile

  • /debug/pprof/heap - 内存分配采样

  • /debug/pprof/goroutine - 所有 goroutine 的堆栈跟踪

  • /debug/pprof/block - 导致同步原语阻塞的堆栈跟踪

  • /debug/pprof/threadcreate - 导致新 OS 线程创建的堆栈跟踪

  • /debug/pprof/cmdline - 程序的命令行调用

  • /debug/pprof/profile - CPU profile

  • /debug/pprof/symbol - 程序符号表

  • /debug/pprof/trace - 程序执行跟踪

posted @ 2025-04-12 19:49  zhjh256  阅读(41)  评论(0)    收藏  举报