golang-pprof 实战笔记
pprof实战笔记
一、如何使用
【单个本地程序】
1.如果想收集该程序运行时cpu 耗时的数据,则需要在程序中加入收集cpu profile 的代码,下面是一个示例hello-cpu-profile.go:
比如我的示例程序是:
注:整个仅仅是一个示例,仅打印一个hello字符,其实没有太大的分析价值,这里仅仅是为了举例如何使用
如果要手机cpu profile,则需要在上述demo程序中嵌入手机cpu profile数据的代码:
首先引入runtime/pprof包,然后嵌入收集cpu profile的必要代码:
然后编译该程序:
生成可执行文件./hello-cpu-profile
下面就可以在当前目录下执行该可执行的文件,并手机cpu profile数据了,方法就是在执行的时候,加上--cpuprofile项,
将cpu profile 保存到类 .prof 的文件中,比如
程序成功执行,输出hello,并生成了cpu profile 的文件/tmp/hello-cpu-profile.prof
整个时候,cpu profile 数据的收集工作就完成了,拿接下来就是分析了。如何分析,就用go tool pprof 工具:
看到已经进入pprof命令行,当前Type是cpu
具体如何分析,就可以学学pprof 工具具体的使用了.
比如我们看一下top:
(pprof) top
Showing nodes accounting for 0, 0% of 0 total
flat flat% sum% cum cum%
(pprof)
注:因为我们当前的demo程序过于简单,仅仅打印一个hello而已。刚才说了,仅是作为示例,并无太大分析价值;
所以,这里我们看top时,并未打印任何信息.
下面我们换一个稍微复杂一点的程序:斐波那契数列
Xxx
以上是针对cpu profile的收集与分析,下面我们介绍一些对单个程序的内存分析:
过程跟cpu分析非常类似,首先引入runtime/pprof包,然后嵌入收集mem profile的必要代码:
Types of profiles available:
Count Profile
2 allocs
0 block
0 cmdline
5 goroutine
2 heap
0 mutex
0 profile
10 threadcreate
0 trace
full goroutine stack dump
Profile Descriptions:
allocs: A sampling of all past memory allocations
block: Stack traces that led to blocking on synchronization primitives
cmdline: The command line invocation of the current program
goroutine: Stack traces of all current goroutines
heap: A sampling of memory allocations of live objects. You can specify the gc GET parameter to run GC before taking the heap sample.
mutex: Stack traces of holders of contended mutexes
profile: CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.
threadcreate: Stack traces that led to the creation of new OS threads
trace: A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.
/debug/pprof/
➜ pprof go tool pprof profileab10000_runing
Type: cpu
Time: Apr 10, 2020 at 2:00pm (CST)
Duration: 30s, Total samples = 5.11s (17.03%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof)
➜ go tool pprof -http=:9092 profileab10000_runing
Serving web UI on http://localhost:9092
➜ pprof go tool pprof http://localhost:9909/debug/pprof/profile
Fetching profile over HTTP from http://localhost:9909/debug/pprof/profile
Saved profile in /Users/bawenmao/pprof/pprof.samples.cpu.008.pb.gz
Type: cpu
Time: Apr 10, 2020 at 2:30pm (CST)
Duration: 30s, Total samples = 1.45s ( 4.83%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 1200ms, 82.76% of 1450ms total
Showing top 10 nodes out of 123
flat flat% sum% cum cum%
600ms 41.38% 41.38% 600ms 41.38% syscall.syscall
170ms 11.72% 53.10% 170ms 11.72% runtime.pthreadcondwait
100ms 6.90% 60.00% 100ms 6.90% runtime.pthreadcondsignal
70ms 4.83% 64.83% 70ms 4.83% runtime.usleep
60ms 4.14% 68.97% 60ms 4.14% runtime.kevent
50ms 3.45% 72.41% 50ms 3.45% runtime.(*semaRoot).queue
40ms 2.76% 75.17% 120ms 8.28% bufio.(*Writer).Flush
40ms 2.76% 77.93% 40ms 2.76% runtime.nanotime
40ms 2.76% 80.69% 100ms 6.90% runtime.netpoll
30ms 2.07% 82.76% 30ms 2.07% internal/poll.(*pollDesc).prepare
(pprof)
go tool pprof -http=:9092 http://localhost:9909/debug/pprof/profile
➜ pprof go tool pprof -http=:9092 http://localhost:9909/debug/pprof/profile
Fetching profile over HTTP from http://localhost:9909/debug/pprof/profile
Saved profile in /Users/bawenmao/pprof/pprof.samples.cpu.013.pb.gz
Serving web UI on http://localhost:9092
➜ pprof go tool pprof http://localhost:9909/debug/pprof/profile
Fetching profile over HTTP from http://localhost:9909/debug/pprof/profile
Saved profile in /Users/bawenmao/pprof/pprof.samples.cpu.009.pb.gz
Type: cpu
Time: Apr 10, 2020 at 2:31pm (CST)
Duration: 30s, Total samples = 0
No samples were found with the default sample value type.
Try "sample_index" command to analyze different sample values.
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 0, 0% of 0 total
flat flat% sum% cum cum%
(pprof)
(pprof)
import "github.com/valyala/fasthttp/pprofhandler"
router.Any("/debug/pprof/", func(ctx routing.Context) error {
pprofhandler.PprofHandler(ctx.RequestCtx)
return nil
})
➜ ~ go tool pprof http://xxx.com/debug/pprof/heap
Fetching profile over HTTP from http://xxx.com/debug/pprof/heap
Saved profile in /Users/jordy/pprof/pprof.imeserver.allocobjects.allocspace.inuseobjects.inuse_space.011.pb.gz
File: ime_server
Type: inuse_space
Time: Apr 27, 2020 at 1:46pm (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top
Showing nodes accounting for 3874.55kB, 100% of 3874.55kB total
Showing top 10 nodes out of 36
flat flat% sum% cum cum%
1056.33kB 27.26% 27.26% 1056.33kB 27.26% bufio.NewReaderSize
896.99kB 23.15% 50.41% 896.99kB 23.15% git.sogou-inc.com/iweb/go-util/logserver.GetLogServer.func1
896.99kB 23.15% 73.57% 896.99kB 23.15% github.com/valyala/fasthttp/stackless.NewFunc
512.19kB 13.22% 86.78% 512.19kB 13.22% runtime.malg
512.05kB 13.22% 100% 512.05kB 13.22% github.com/valyala/fasthttp.(*URI).parse
0 0% 100% 512.05kB 13.22% git.sogou-inc.com/iweb/go-util.HttpGet
0 0% 100% 512.05kB 13.22% git.sogou-inc.com/iweb/go-util.WriteMicroServerAccessLog
0 0% 100% 896.99kB 23.15% git.sogou-inc.com/iweb/go-util.init
0 0% 100% 896.99kB 23.15% git.sogou-inc.com/iweb/go-util/logserver.GetLogServer
0 0% 100% 512.05kB 13.22% git.sogou-inc.com/iweb/ime-dict/internal/srv/controllers/v1.(*DictController).Lbsdict
(pprof)
(pprof)
```
二、如何分析
待完善
版权声明: 本文为 InfoQ 作者【卓丁】的原创文章。
原文链接:【http://xie.infoq.cn/article/61b93b26c597e696d20b374b6】。文章转载请联系作者。
评论