1. pprof相关
1.1. pprof数据采集
1.1.1. dump方式
- 项目中添加代码,以dump文件方式采集数据,并生成待分析文件
// 创建分析文件
file, err := os.Create("./cpu.prof")
if err != nil {
fmt.Printf("创建采集文件失败, err:%v\n", err)
return
}
// 进行cpu数据的获取
pprof.StartCPUProfile(file)
defer pprof.StopCPUProfile()
1.1.2. http方式
- 项目的import中添加代码,以http方式提供采集到的数据
// import中添加
_ "net/http/pprof"
// 代码中添加http服务
http.ListenAndServe("0.0.0.0:6061", nil)
1.2. pprof数据分析
1.2.1. 解析文件方式
go tool pprof -svg {具体profFile路径}
// 例如
go tool pprof -svg cpu.prof
- 提供http服务方式<font color='red'>(建议使用第二种)</font>
go tool pprof -http=172.21.51.109:60610 cpu.prof
go tool pprof -http=0.0.0.0:6066 http://172.21.51.109:60610/debug/pprof/heap
1.2.2. http接口分析
http://{ip}:{port}/debug/pprof
go tool pprof http://172.21.51.109:60610/debug/pprof/{type} // type 为界面查看原始数据中的各种数据类型
2. 火焰图(针对http方式数据分析)
2.1. 安装
go get github.com/uber/go-torch
git clone https://github.com/brendangregg/FlameGraph.git
cp FlameGraph/flamegraph.pl /usr/bin
2.2. 运行
// 分析cpu
go-torch -u http://172.21.51.109:60610 --seconds 60 -f cpu.svg
// 分析mem
go-torch http://172.21.51.109:60610/debug/pprof/heap --colors mem -f mem.svg
3. 内存问题排查
- 每隔一段时间使用heap获取profile,比如间隔1分钟执行以下命令两次,可以得到两个profile文件
go tool pprof http://172.21.51.109:60610/debug/pprof/heap
- 使用
base
把001文件作为基准,然后使用002和001进行对比
go tool pprof -base pprof.demo2.alloc_objects.alloc_space.inuse_objects.inuse_space.001.pb.gz pprof.demo2.alloc_objects.alloc_space.inuse_objects.inuse_space.002.pb.gz
网友评论