

Pprof 调试经验汇总
source link: https://pandaychen.github.io/2023/10/03/A-PPROF-REVIEW/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

0x00 前言
0x01 DEBUG 汇总
- 安装
yum install graphviz
go tool pprof main http://localhost:8000/debug/pprof/heap
或者 goroutine,进入命令行- 输入
svg
命令即可保存
- 安装
FlameGraph
,如下 - 安装
go-torch
,拿到bin
文件 - 使用命令
go-torch -u http://localhost:6060 -t 30
生成火焰图
git clone https://github.com/brendangregg/FlameGraph.git
cp FlameGraph/flamegraph.pl /usr/local/bin
0x02 一些经验之谈
关于调用图
一般调用图中高亮的部分就能告诉开发者是哪里发生了内存泄漏了(除非内存是预设的 size,如代码中本地初始化一块缓存)
部署与编译机器分开
比如,需要调试 goroutine,部署机器上通过 curl http://localhost:6060/debug/pprof/goroutine -o goroutine.pprof
保存运行时配置,然后将 goroutine.pprof
传输到编译机器上,通过 go tool pprof goroutine.pprof
即可进行调试
对比前后时间差异
导出时间点 A 的文件:
curl http://localhost:6060/debug/pprof/heap > heap.timeA
curl http://localhost1:6060/debug/pprof/goroutine> goroutine.timeA
导出时间点 B 的文件:
curl http://localhost:6060/debug/pprof/heap > heap.timeB
curl http://localhost1:6060/debug/pprof/goroutine> goroutine.timeB
对比两个时间点的堆栈差异:
go tool pprof --base heap.timeA heap.timeB
go tool pprof --http :8080 --base heap.timeA heap.timeB
runtime.gopark
方法的启示
若查看 goroutine 文件使用 traces
命令,发现大量的 runtime.gopark
函数调用,那么可以初步认定为程序存在 goroutine 泄漏。gopark
函数主要作用就是将当前的 goroutine 放入等待状态,这就意味着 goroutine 被暂时被搁置,也就是被运行时调度器暂停了
- 调用
acquirem
函数 - 获取当前 goroutine 所绑定的
m
,设置各类所需数据 - 调用
releasem
函数将当前 goroutine 和其m
的绑定关系解除 - 调用
park_m
函数 - 将当前 goroutine 的状态从
_Grunning
切换为_Gwaiting
,也就是等待状态 - 删除
m
和当前 goroutinem->curg
(简称gp
)之间的关联 - 调用
mcall
函数,仅会在需要进行 goroutiine 切换时会被调用 - 切换当前线程的堆栈,从
g
的堆栈切换到g0
的堆栈并调用fn(g)
函数 - 将
g
的当前 PC/SP 保存在g->sched
中,以便后续调用goready
函数时可以恢复运行现场
Related Issues not found
Please contact @pandaychen to initialize the comment
Recommend
-
102
Your pprof is showing IPv4 scans reveal exposed net/http/pprof endpoints Sep 27, 2017 Golang’s
-
69
pprof user interface Tue, Oct 10, 2017 pprof now is coming with a Web UI. In order to try it out, go get the pprof tool: $ go get github.com/google/pprof The tool launches a web UI if -ht...
-
128
Profiling Go programs with pprof Last week me and my cool coworker Josh were debugging some memory problems in a Go program using pp...
-
27
I use pprof on mouthful to try and see if I can squeeze out some performance. It turns out I can.
-
40
The2.6.0 release of Prometheus includes WAL loading optimisations to make startup faster. The informal design goal of the Prometheus 2.x TSDB startup was that it should take no more than about a minute. Over t...
-
43
import _ "net/http/pprof" // 如果你的程序没有开启http服务,那么还需要添加如下代码开启http服务 import "net/http" http.ListenAndServe("0.0.0.0:10001", nil) // 可以参考这篇 https://pengrl.com/p/33895/ 关...
-
29
测试 pprof 查看 cpu 占用 首先,我们声明一个耗时函数。传递一个 time.Duration 的数据类型,表示函数执行的时间。注意,这里不能使用 time.Sleep ,它得不到
-
67
前言 如果要说在 golang 开发过程进行性能调优,pprof 一定是一个大杀器般的工具。但在网上找到的教程都偏向简略,难寻真的能应用于实战的教程。这也无可厚非,毕竟 pprof 是当程序占用资源异常时才需要启用的工具,而我相信大家...
-
28
相信很多人都听过“雷神 3”关于性能优化的故事。在一个 3D 游戏引擎的源码里,John Carmack 将 1/sqrt(x) 这个函数的执行效率优化到了极致。 一般我们使用二分法,或者牛顿迭代法计算一个浮点数的平方根。但在这个...
-
32
先说一下这次要做什么, 我们每天都需要同步一次本部门人员详细信息. 但是只有两个接口可用: 获取所有人员姓名 根据指定姓名, 查询详细信息的接口 在获得详细信息以后需要筛选出本部门的人员信...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK