Go性能分析工具pprof详解


一、什么是pprof

pprof是Go官方提供的性能分析工具,可以分析程序的运行情况,并且提供可视化的功能。prof是profile(画像)的缩写,使用pprof可以分析以下几种指标数据:
在这里插入图片描述

  • allocs:程序启动之后内存分配的情况
  • block:导致阻塞操作的一些堆栈跟踪信息
  • cmdline:当前程序启动的命令行
  • goroutine:所有当前 goroutine 的堆栈跟踪信息
  • heap:程序在当前堆上内存分配的情况
  • mutex:锁资源的竞争的堆栈信息
  • profileCPU profile文件。可以在 debug/pprof?seconds=x秒 GET 参数中指定持续时间。获取pprof文件后,使用 go tool pprof x.prof命令分析pprof文件。
  • threadcreate:系统线程的使用情况
  • trace:当前系统的代码执行的链路情况

使用pprof工具主要分析以下几种指标:

  • CPU ProfilingCPU 分析,按照一定的频率采集所监听的应用程序 CPU(含寄存器)的使用情况,可确定应用程序在主动消耗 CPU 周期时花费时间的位置

  • Memory Profiling:内存分析,在应用程序进行堆分配时记录堆栈跟踪,用于监视当前和历史内存使用情况,以及检查内存泄漏

  • Block Profiling:阻塞分析,记录 goroutine 阻塞等待同步(包括定时器通道)的位置

  • Mutex Profiling:互斥锁分析,报告互斥锁的竞争情况。

当程序存在内存或者CPU飙升的情况时,我们可以通过pprof工具来查询问题出现的根源。


二、怎么使用pprof

pprof包含两个相关的库:

  1. runtime/pprof
    主要应用于工具型应用。包含脚本、定时任务等。
    如:对于只跑一次的程序,例如每天只跑一次的离线预处理程序,调用 pprof 包提供的函数,手动开启性能数据采集
  2. net/http/pprof
    主要应用于服务型应用。包含HTTP服务,GRPC服务等。
    如:对于在线服务,对于一个 HTTP Server,访问 pprof 提供的 HTTP 接口,获得性能数据。当然,实际上这里底层也是调用的 runtime/pprof提供的函数,封装成接口对外提供网络访问。

1. 工具型应用

工具型应用主要使用runtime/pprof包实现性能分析。

func main() {
	// --- cpu 分析示例 start---
	// 创建cpu分析文件
	fc, err := os.Create("./cpu.prof")
	if err != nil {
		fmt.Println("create cpu.prof err:", err.Error())
		return
	}
	defer fc.Close()

	// 开始分析cpu
	err = pprof.StartCPUProfile(fc)
	if err == nil {
		defer pprof.StopCPUProfile()
	}
	// --- cpu 分析示例 end---
	
	var count int
	for i := 0; i < 10000; i++ {
		count++
	}

	// --- 内存 分析示例 start---
	fm, err := os.Create("./memory.prof")
	if err != nil {
		fmt.Println("create memory.prof err:", err.Error())
		return
	}
	defer fm.Close()

	// 开始分析内存
	err = pprof.WriteHeapProfile(fm)
	if err != nil {
		fmt.Println("write heap prof err:", err.Error())
		return
	}
	// --- 内存 分析示例 end---

	for i := 0; i < 10000; i++ {
		count++
	}
	fmt.Println("do finish......count:", count)
}

执行go run main.go后,在代码目录下,可以看到生成了cpu.profmemory.prof文件。
通过执行go tool pprof ./memory.prof或者
go tool pprof -http=:8888 ./memory.prof进入命令行模式或者web页面进行性能分析。

执行go tool pprof ./memory.prof进入命令行:
cmd memory.prof

执行go tool -http=:8888 pprof ./memory.prof可进入web页面,更方便查看:
http memory.prof
页面展示效果:
web memory.prof
SAMPLE各个标签的含义解释:
在这里插入图片描述

2. 服务型应用

对于服务类型的应用,主要在服务内部匿名引入net/http/pprof包,然后通过HTTP访问pprof页面。
匿名引入方式为:import _ "net/http/pprof"

package main

import (
	"fmt"
	"net/http"
	_ "net/http/pprof"
)

func main() {
	http.HandleFunc("/", hello)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		fmt.Println("ListenAndServe Err:", err.Error())
		return
	}
}

func hello(resp http.ResponseWriter, req *http.Request) {
	fmt.Fprintln(resp, "Hello World, Are You OK?")
}

执行http://localhost:8080/debug/pprof/可以看到画像信息:
在这里插入图片描述

但是需要注意,如果HTTP服务不是通过
http.ListenAndServe(":8080", nil)启动的,而是指定第二个参数启动的话,需要自己注册pprof路由。

net/http/pprof/pprof.go的官方源码注释中也提到此种情况:

If you are not using DefaultServeMux, you will have to register handlers with the mux you are using.
如果您不使用DefaultServeMux,则必须向所使用的多路复用器注册pprof处理程序
http.ListenAndServe函数可以传递handler,如果handler不为nil,则说明研发自定义了 ServeMux,否则用的是默认DefaultServeMux

net/http/pprof包中,有init函数

func init() {
   http.HandleFunc("/debug/pprof/", Index)
   http.HandleFunc("/debug/pprof/cmdline", Cmdline)
   http.HandleFunc("/debug/pprof/profile", Profile)
   http.HandleFunc("/debug/pprof/symbol", Symbol)
   http.HandleFunc("/debug/pprof/trace", Trace)
}

所以如果使用默认ServeMux,则不需要注册,但是如果使用自定义的ServeMux,则需要增加注册后,才能获取到pprof

// 自己注册这几个函数
r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)

另外一种启动pprof的方式在生产环境中更加常用:通过协程另起一个HTTP服务,单独用作pprof分析:

func RegisterProf() {
	go func() {
		if err := http.ListenAndServe(":6060", nil); err != nil {
			panic("pprof server start error: " + err.Error())
		}
	}()
}

三、pprof数据分析

GO官方提供了go tool pprof工具来帮助我们分析pprof生成的数据文件。
使用go tool pprof分析数据,主要有两种模式:

  1. 命令行交互模式
    go tool pprof [file_pprof|url_pprof]
  2. web页面模式
    go tool pprof -http=:6666 [file_pprof|url_pprof]
    其中,file_pprof表示生成的prof分析文件,如cpu.profurl_pprof表示远端服务开启的pprof访问,如http://localhost:8080/debug/pprof/profile

进入命令行交互模式后,可以使用help查看所有子命令,使用help <cmd|option>查看子命令使用方法。如 help、help top

CPU Profiling

浏览器访问/debug/pprof/profile会自动进行 CPU profiling,默认持续 30s,并生成一个文件供下载,可以通过带参数?seconds=60进行60秒的数据采集。

为了模拟请求,使用ab进行压测,
ab -k -c 1 -t 180 -n 100000000 http://localhost:8080/hello

执行go tool pprof http://localhost:8080/debug/pprof/profile后,默认需要等30s才会显示交互

top指令排序展示

在这里插入图片描述

每一行表示一个函数的信息,列信息字段解释:

  • flat:函数在 CPU 上运行的时间
  • flat%:函数在CPU上运行时间的百分比
  • sum%:是从第一行到当前行所有函数累加使用 CPU 的比例,如第二行sum=53.85=30.77+23.08
  • cum:这个函数以及子函数运行所占用的时间,应该大于等于flat
  • cum%:这个函数以及子函数运行所占用的比例,应该大于等于flat%
  • 最后一列:函数的名字

web指令生成图示

在交互模式下输入 web,会自动生成一个 svg 文件,并跳转到浏览器打开。

改功能需要安装graphviz后才能使用,安装方法https://shidawuhen.github.io/2020/02/08/go-callvis/ 。

调用链
图中每个方框对应应用程序运行的一个函数,方框越大代表函数执行的时间越久(函数执行时间会包含它调用的子函数的执行时间,但并不是正比的关系);方框之间的箭头代表着调用关系,箭头上的数字代表被调用函数的执行时间。

具体细节可以参考:https://github.com/google/pprof/tree/master/doc#interpreting-the-callgraph

runtime netpoll为例:
箭头上的230ms表示该函数总共的执行时间,包含自身和下游所有子节点;
方框中的10ms表示自身的执行时间;
方框中的2.70%表示自身执行时间在该函数总共执行时间的占比在总时间中的占比:2.70 = (10/230)*62.16
方框中的62.16%表示总时间占比,即整个程序耗时占比。

list指令分析函数

确定出哪个函数耗时之后,可以用pprof分析函数中的哪一行导致的耗时,使用子命令:list 函数名。
在这里插入图片描述

堆内存分析示例

内存分配既可以发生在堆上也可以在栈上。堆上分配的内存需要垃圾回收或者手动回收(对于没有垃圾回收的语言,例如 C++),栈上的内存则通常在函数退出后自动释放。

Go 语言通过逃逸分析会将尽可能多的对象分配到栈上,以使程序可以运行地更快。

这里说明一下,有两种内存分析策略:一种是当前的(这一次采集)内存或对象的分配,称为 inuse;另一种是从程序运行到现在所有的内存分配,不管是否已经被 gc 过了,称为 alloc

As mentioned above, there are two main memory analysis strategies with pprof. One is around looking at the current allocations (bytes or object count), called inuse. The other is looking at all the allocated bytes or object count throughout the run-time of the program, called alloc. This means regardless if it was gc-ed, a summation of everything sampled.

加上 -sample_index 参数后,可以切换内存分析的类型:
go tool pprof -sample_index=alloc_space http://localhost:8080/debug/pprof/heap
或者
go tool pprof -alloc_space http://localhost:8080/debug/pprof/heap

四种标签:
在这里插入图片描述


四、pprof数据分析类型汇总

其他数据的分析和CPU的基本一致。下面列举所有的分类:

http://localhost:8080/debug/pprof/ :获取概况信息,即本文第一张图的信息
go tool pprof http://localhost:8080/debug/pprof/allocs : 分析内存分配
go tool pprof http://localhost:8080/debug/pprof/block : 分析堆栈跟踪导致阻塞的同步原语
go tool pprof http://localhost:8080/debug/pprof/cmdline : 分析命令行调用的程序,web下调用报错
go tool pprof http://localhost:8080/debug/pprof/goroutine : 分析当前 goroutine 的堆栈信息
go tool pprof http://localhost:8080/debug/pprof/heap : 分析当前活动对象内存分配
go tool pprof http://localhost:8080/debug/pprof/mutex : 分析堆栈跟踪竞争状态互斥锁的持有者
go tool pprof http://localhost:8080/debug/pprof/profile : 分析一定持续时间内CPU的使用情况
go tool pprof http://localhost:8080/debug/pprof/threadcreate : 分析堆栈跟踪系统新线程的创建
go tool pprof http://localhost:8080/debug/pprof/trace : 分析追踪当前程序的执行状况

参考连接

  1. 一文搞懂pprof
  2. 深度解密Go语言之 pprof
  3. go性能分析工具pprof
  4. Go语言:利用pprof工具排查内存泄漏的示例
  5. Go语言:利用pprof工具查找goroutine(协程)泄漏的示例
  6. go 程序性能调优 pprof 的使用
  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值