介绍
gops是一个命令行工具,配合agent,可以用来很方便的诊断运行中的go程序,使用简单,官方维护
安装
go get github.com/google/gops // 或者 go install github.com/google/gops@latest
讯享网
使用
gops本身可以查看所有go程序的进程,如果一个程序使用了agent,gops可以报告更多的信息,比如stack,内存统计,trace等,使用了agent的程序会用*号标记
- 使用agent
讯享网go func() { cfg := agent.Options{ Addr: ":2022", //远程调试使用,绑定对应的进程pid ShutdownCleanup: true, } if err := agent.Listen(cfg); err != nil { panic(err) } }()
gops支持远程诊断,只需要将换成Host:Port即可
- 列出所有go进程
root@2kpBjdU4m:~# gops PID PPID Name Version Location 52222 850 docker-proxy go1.13.8 /usr/bin/docker-proxy gopls go1.17.5 /root/go/bin/gopls gopls go1.17.5 /root/go/bin/gopls web * go1.17.5 /home/2kpBjdU4m/hub/gen-server/web gops go1.17.5 /root/go/bin/gops 850 1 dockerd go1.13.8 /usr/bin/dockerd
- 查看进程详情
讯享网root@2kpBjdU4m:~# gops parent PID: threads: 9 memory usage: 0.266% cpu usage: 99.498% username: root cmd+args: ./web elapsed time: 02:18 local/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:~# gops 10s parent PID: threads: 9 memory usage: 0.277% cpu usage: 99.678% cpu usage (10s): 100.400% username: root cmd+args: ./web elapsed time: 03:06 local/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:~# gops stack goroutine 85 [running]: runtime/pprof.writeGoroutineStacks({0x1033f60, 0xc00050e030}) /usr/local/go/src/runtime/pprof/pprof.go:693 +0x70 runtime/pprof.writeGoroutine({0x1033f60, 0xc00050e030}, 0x0) /usr/local/go/src/runtime/pprof/pprof.go:682 +0x2b runtime/pprof.(*Profile).WriteTo(0xe88671, {0x1033f60, 0xc00050e030}, 0x0) /usr/local/go/src/runtime/pprof/pprof.go:331 +0x14b github.com/google/gops/agent.handle({0x7fb5e, 0xc00050e030}, {0xc0000382a0, 0x9, 0xc0000a3fc0}) /root/go/pkg/mod/github.com/google/gops@v0.3.22/agent/agent.go:201 +0x15d github.com/google/gops/agent.listen() /root/go/pkg/mod/github.com/google/gops@v0.3.22/agent/agent.go:145 +0x19a created by github.com/google/gops/agent.Listen /root/go/pkg/mod/github.com/google/gops@v0.3.22/agent/agent.go:123 +0x365 goroutine 1 [chan receive, 4 minutes]: main.main() /home/2kpBjdU4m/hub/gen-server/cmd/web/main.go:108 +0x82b
- 查看内存统计
root@2kpBjdU4m:~# gops memstats alloc: 2.61MB ( bytes) total-alloc: 5.63MB ( bytes) sys: 15.08MB ( bytes) lookups: 0 mallocs: 38142 frees: 21689 heap-alloc: 2.61MB ( bytes) heap-sys: 7.38MB ( bytes) heap-idle: 2.99MB ( bytes) heap-in-use: 4.38MB ( bytes) heap-released: 2.47MB ( bytes) heap-objects: 16453 stack-in-use: 640.00KB ( bytes) stack-sys: 640.00KB ( bytes) stack-mspan-inuse: 70.92KB (72624 bytes) stack-mspan-sys: 80.00KB (81920 bytes) stack-mcache-inuse: 4.69KB (4800 bytes) stack-mcache-sys: 16.00KB (16384 bytes) other-sys: 935.69KB ( bytes) gc-sys: 4.69MB ( bytes) next-gc: when heap-alloc >= 4.00MB ( bytes) last-gc: 2022-02-28 17:54:14. +0800 CST gc-pause-total: 476.578µs gc-pause: gc-pause-end: num-gc: 3 num-forced-gc: 0 gc-cpu-fraction: 7.6689e-06 enable-gc: true debug-gc: false
- 查看runtime stats
讯享网root@2kpBjdU4m:~# gops stats goroutines: 27 OS threads: 11 GOMAXPROCS: 4 num CPU: 4
- 查看trace
gops允许你收集5s runtime tracer,然后提供浏览器访问
root@2kpBjdU4m:~# gops trace Tracing now, will take 5 secs... Trace dump saved to: /tmp/trace 2022/02/28 17:57:30 Parsing trace... 2022/02/28 17:57:30 Splitting trace... 2022/02/28 17:57:30 Opening browser. Trace viewer is listening on http://127.0.0.1:35507
- 强制gc垃圾收集
运行gops gc立即执行垃圾回收,程序会被阻塞,直到gc完成
- pprof实时交互
gops支持内存和cpu pprof分析,在收集数据之后,调用go tool pprof,进入交互样本分析界面
讯享网root@2kpBjdU4m:~# gops pprof-heap Profile dump saved to: /tmp/heap_profile Binary file saved to: /tmp/binary File: binary Type: inuse_space Time: Feb 28, 2022 at 6:08pm (CST) Entering interactive mode (type "help" for commands, "o" for options) (pprof) top Showing nodes accounting for 3620.55kB, 100% of 3620.55kB total Showing top 10 nodes out of 40 flat flat% sum% cum cum% 1025kB 28.31% 28.31% 1025kB 28.31% runtime.allocm 544.67kB 15.04% 43.35% 544.67kB 15.04% github.com/xdg-go/stringprep.init 514.63kB 14.21% 57.57% 514.63kB 14.21% regexp.makeOnePass.func1 512.20kB 14.15% 71.72% 512.20kB 14.15% runtime.malg 512.04kB 14.14% 85.86% 512.04kB 14.14% github.com/segmentio/kafka-go/protocol.structDecodeFuncOf.func1.1 512.02kB 14.14% 100% 512.02kB 14.14% github.com/segmentio/kafka-go/protocol.structEncodeFuncOf 0 0% 100% 514.63kB 14.21% github.com/go-playground/validator/v10.init 0 0% 100% 1024.05kB 28.28% github.com/segmentio/kafka-go/protocol.Register 0 0% 100% 512.04kB 14.14% github.com/segmentio/kafka-go/protocol.decodeFuncOf 0 0% 100% 512.02kB 14.14% github.com/segmentio/kafka-go/protocol.encodeFuncOf (pprof)
更多
官方文档:https://github.com/google/gops
各位Gopher同学,最近我们社区打算推出一个类似GoCN每日新闻的新栏目《酷Go推荐》,主要是每周推荐一个库或者好的项目,然后写一点这个库使用方法或者优点之类的,这样可以真正的帮助到大家能够学习到
新的库,并且知道怎么用。
大概规则和每日新闻类似,如果报名人多的话每个人一个月轮到一次,欢迎大家报名!戳「阅读原文」,即可报名
扫码也可以加入 GoCN 的大家族哟~



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/59423.html