您的位置:首页 >聚焦 >

「GoCN酷Go推荐」使用gops诊断运行中的go程序

2022-03-03 06:10:13    来源:程序员客栈

介绍

gops是一个命令行工具,配合agent,可以用来很方便的诊断运行中的go程序,使用简单,官方维护

安装

gogetgithub.com/google/gops//或者goinstallgithub.com/google/gops@latest

使用

gops本身可以查看所有go程序的进程,如果一个程序使用了agent,gops可以报告更多的信息,比如stack,内存统计,trace等,使用了agent的程序会用*号标记

使用agent

gofunc(){cfg:=agent.Options{Addr:":2022",//远程调试使用,绑定对应的进程pidShutdownCleanup:true,}iferr:=agent.Listen(cfg);err!=nil{panic(err)}}()

gops支持远程诊断,只需要将换成Host:Port即可

列出所有go进程

root@2kpBjdU4m:~#gopsPIDPPIDNameVersionLocation52222850docker-proxygo1.13.8/usr/bin/docker-proxy162404162302goplsgo1.17.5/root/go/bin/gopls169840169738goplsgo1.17.5/root/go/bin/gopls171257162544web*go1.17.5/home/2kpBjdU4m/hub/gen-server/web171289170774gopsgo1.17.5/root/go/bin/gops8501dockerdgo1.13.8/usr/bin/dockerd

查看进程详情

root@2kpBjdU4m:~#gops171257parentPID:162544threads:9memoryusage:0.266%cpuusage:99.498%username:rootcmd+args:./webelapsedtime:02:18local/remote:172.19.0.1:44384<->172.19.0.12:3306(ESTABLISHED)local/remote:172.19.0.1:48492<->172.19.0.7:27017(ESTABLISHED)local/remote:172.19.0.1:45504<->172.19.0.5:9200(ESTABLISHED)local/remote:172.19.0.1:48494<->172.19.0.7:27017(ESTABLISHED)local/remote:172.19.0.1:48490<->172.19.0.7:27017(ESTABLISHED)local/remote::::2022<->:::0(LISTEN)

可以指定收集时间

root@2kpBjdU4m:~#gops17238110sparentPID:162544threads:9memoryusage:0.277%cpuusage:99.678%cpuusage(10s):100.400%username:rootcmd+args:./webelapsedtime:03:06local/remote:172.19.0.1:48600<->172.19.0.7:27017(ESTABLISHED)local/remote:172.19.0.1:48602<->172.19.0.7:27017(ESTABLISHED)local/remote:172.19.0.1:45614<->172.19.0.5:9200(ESTABLISHED)local/remote:172.19.0.1:44496<->172.19.0.12:3306(ESTABLISHED)local/remote:172.19.0.1:48604<->172.19.0.7:27017(ESTABLISHED)local/remote::::2022<->:::0(LISTEN)local/remote::::2023<->:::0(LISTEN)

查看stack信息

root@2kpBjdU4m:~#gopsstack172381goroutine85[running]:runtime/pprof.writeGoroutineStacks({0x1033f60,0xc00050e030})/usr/local/go/src/runtime/pprof/pprof.go:693+0x70runtime/pprof.writeGoroutine({0x1033f60,0xc00050e030},0x0)/usr/local/go/src/runtime/pprof/pprof.go:682+0x2bruntime/pprof.(*Profile).WriteTo(0xe88671,{0x1033f60,0xc00050e030},0x0)/usr/local/go/src/runtime/pprof/pprof.go:331+0x14bgithub.com/google/gops/agent.handle({0x7fb5e4678718,0xc00050e030},{0xc0000382a0,0x9,0xc0000a3fc0})/root/go/pkg/mod/github.com/google/gops@v0.3.22/agent/agent.go:201+0x15dgithub.com/google/gops/agent.listen()/root/go/pkg/mod/github.com/google/gops@v0.3.22/agent/agent.go:145+0x19acreatedbygithub.com/google/gops/agent.Listen/root/go/pkg/mod/github.com/google/gops@v0.3.22/agent/agent.go:123+0x365goroutine1[chanreceive,4minutes]:main.main()/home/2kpBjdU4m/hub/gen-server/cmd/web/main.go:108+0x82b

查看内存统计

root@2kpBjdU4m:~#gopsmemstats172381alloc:2.61MB(2740864bytes)total-alloc:5.63MB(5904904bytes)sys:15.08MB(15811592bytes)lookups:0mallocs:38142frees:21689heap-alloc:2.61MB(2740864bytes)heap-sys:7.38MB(7733248bytes)heap-idle:2.99MB(3137536bytes)heap-in-use:4.38MB(4595712bytes)heap-released:2.47MB(2588672bytes)heap-objects:16453stack-in-use:640.00KB(655360bytes)stack-sys:640.00KB(655360bytes)stack-mspan-inuse:70.92KB(72624bytes)stack-mspan-sys:80.00KB(81920bytes)stack-mcache-inuse:4.69KB(4800bytes)stack-mcache-sys:16.00KB(16384bytes)other-sys:935.69KB(958148bytes)gc-sys:4.69MB(4917416bytes)next-gc:whenheap-alloc>=4.00MB(4194304bytes)last-gc:2022-02-2817:54:14.439966835+0800CSTgc-pause-total:476.578µsgc-pause:111034gc-pause-end:1646042054439966835num-gc:3num-forced-gc:0gc-cpu-fraction:7.774838758506689e-06enable-gc:truedebug-gc:false

查看runtime stats

root@2kpBjdU4m:~#gopsstats172381goroutines:27OSthreads:11GOMAXPROCS:4numCPU:4

查看trace

gops允许你收集5s runtime tracer,然后提供浏览器访问

root@2kpBjdU4m:~#gopstrace172381Tracingnow,willtake5secs...Tracedumpsavedto:/tmp/trace11969407162022/02/2817:57:30Parsingtrace...2022/02/2817:57:30Splittingtrace...2022/02/2817:57:30Openingbrowser.Traceviewerislisteningonhttp://127.0.0.1:35507

强制gc垃圾收集

运行gops gc立即执行垃圾回收,程序会被阻塞,直到gc完成

pprof实时交互

gops支持内存和cpu pprof分析,在收集数据之后,调用go tool pprof,进入交互样本分析界面

root@2kpBjdU4m:~#gopspprof-heap172381Profiledumpsavedto:/tmp/heap_profile2317160970Binaryfilesavedto:/tmp/binary3943624338File:binary3943624338Type:inuse_spaceTime:Feb28,2022at6:08pm(CST)Enteringinteractivemode(type"help"forcommands,"o"foroptions)(pprof)topShowingnodesaccountingfor3620.55kB,100%of3620.55kBtotalShowingtop10nodesoutof40flatflat%sum%cumcum%1025kB28.31%28.31%1025kB28.31%runtime.allocm544.67kB15.04%43.35%544.67kB15.04%github.com/xdg-go/stringprep.init514.63kB14.21%57.57%514.63kB14.21%regexp.makeOnePass.func1512.20kB14.15%71.72%512.20kB14.15%runtime.malg512.04kB14.14%85.86%512.04kB14.14%github.com/segmentio/kafka-go/protocol.structDecodeFuncOf.func1.1512.02kB14.14%100%512.02kB14.14%github.com/segmentio/kafka-go/protocol.structEncodeFuncOf00%100%514.63kB14.21%github.com/go-playground/validator/v10.init00%100%1024.05kB28.28%github.com/segmentio/kafka-go/protocol.Register00%100%512.04kB14.14%github.com/segmentio/kafka-go/protocol.decodeFuncOf00%100%512.02kB14.14%github.com/segmentio/kafka-go/protocol.encodeFuncOf(pprof)

更多

官方文档:https://github.com/google/gops

《酷Go推荐》招募:

各位Gopher同学,最近我们社区打算推出一个类似GoCN每日新闻的新栏目《酷Go推荐》,主要是每周推荐一个库或者好的项目,然后写一点这个库使用方法或者优点之类的,这样可以真正的帮助到大家能够学习到

新的库,并且知道怎么用。

大概规则和每日新闻类似,如果报名人多的话每个人一个月轮到一次,欢迎大家报名!戳「阅读原文」,即可报名

扫码也可以加入 GoCN 的大家族哟~

关键词: 垃圾回收 远程诊断 可以用来

相关阅读