定义
gops
官方对其定义为:
A tool to list and diagnose Go processes currently running on your system
安装
gerrylon@kali:~$ go get -u github.com/google/gops
...省略安装细节
# 如果你的PATH环境变量包含了GOBIN, 就可以直接调gops命令了
gerrylon@kali:~$ gops
5033 4417 gops go1.14.4 /home/gerrylon/workspace/go/bin/gops
hello world
main.go
package main
import (
"log"
"time"
"github.com/google/gops/agent"
)
func main() {
if err := agent.Listen(agent.Options{}); err != nil {
log.Fatal(err)
}
time.Sleep(time.Hour)
}
terminal 1执行 go build main.go
terminal 2执行
# 查看所有的go进程
gerrylon@kali:~$ gops
5670 4417 main* go1.14.4 /home/gerrylon/workspace/learn/gops/main
5677 2606 gops go1.14.4 /home/gerrylon/workspace/go/bin/gops
# gops $pid: 查看指定go进程的情况
gerrylon@kali:~$ gops 5670
parent PID: 4417
threads: 6
memory usage: 0.112%
cpu usage: 0.000%
username: gerrylon
cmd+args: ./main
elapsed time: 00:09
local/remote: 127.0.0.1:36095 <-> 0.0.0.0:0 (LISTEN)
gerrylon@kali:~$
通过gops $pid
这种调用可以看到一个go进程的cpu, memory等使用情况。
其他子命令
还有更多子命令, 可以看查https://github.com/google/gops/blob/master/README.md。
本文在这抄录几个:
查看调用栈
gerrylon@kali:~$ gops stack 5670
goroutine 6 [running]:
runtime/pprof.writeGoroutineStacks(0x58e400, 0xc000010058, 0x0, 0x0)
/opt/soft/go/src/runtime/pprof/pprof.go:665 +0x9d
runtime/pprof.writeGoroutine(0x58e400, 0xc000010058, 0x2, 0x0, 0x0)
/opt/soft/go/src/runtime/pprof/pprof.go:654 +0x44
runtime/pprof.(*Profile).WriteTo(0x66db80, 0x58e400, 0xc000010058, 0x2, 0xc000010058, 0x0)
/opt/soft/go/src/runtime/pprof/pprof.go:329 +0x3da
github.com/google/gops/agent.handle(0x7f2d1c21eff8, 0xc000010058, 0xc000016338, 0x1, 0x1, 0x0, 0x0)
/home/gerrylon/workspace/go/pkg/mod/github.com/google/gops@v0.3.12/agent/agent.go:189 +0x1af
github.com/google/gops/agent.listen()
/home/gerrylon/workspace/go/pkg/mod/github.com/google/gops@v0.3.12/agent/agent.go:133 +0x2bf
created by github.com/google/gops/agent.Listen
/home/gerrylon/workspace/go/pkg/mod/github.com/google/gops@v0.3.12/agent/agent.go:111 +0x386
goroutine 1 [sleep]:
time.Sleep(0x34630b8a000)
/opt/soft/go/src/runtime/time.go:188 +0xba
main.main()
/home/gerrylon/workspace/learn/gops/main.go:14 +0xa3
查看内存使用详情
gerrylon@kali:~$ gops memstats 5670
alloc: 3.21MB (3363928 bytes)
total-alloc: 3.21MB (3363928 bytes)
sys: 68.08MB (71387144 bytes)
lookups: 0
mallocs: 395
frees: 8
heap-alloc: 3.21MB (3363928 bytes)
heap-sys: 63.69MB (66781184 bytes)
heap-idle: 60.23MB (63152128 bytes)
heap-in-use: 3.46MB (3629056 bytes)
heap-released: 60.20MB (63119360 bytes)
heap-objects: 387
stack-in-use: 320.00KB (327680 bytes)
stack-sys: 320.00KB (327680 bytes)
stack-mspan-inuse: 19.39KB (19856 bytes)
stack-mspan-sys: 32.00KB (32768 bytes)
stack-mcache-inuse: 13.56KB (13888 bytes)
stack-mcache-sys: 16.00KB (16384 bytes)
other-sys: 770.62KB (789120 bytes)
gc-sys: 3.28MB (3436808 bytes)
next-gc: when heap-alloc >= 4.27MB (4473924 bytes)
last-gc: -
gc-pause-total: 0s
gc-pause: 0
gc-pause-end: 0
num-gc: 0
num-forced-gc: 0
gc-cpu-fraction: 0
enable-gc: true
debug-gc: false
gerrylon@kali:~$
查看go程序使用的go版本
gerrylon@kali:~$ gops version 5670
go1.14.4
查看goroutine个数等
gerrylon@kali:~$ gops stats 5670
goroutines: 2
OS threads: 7
GOMAXPROCS: 8
num CPU: 8
gerrylon@kali:~$
还有更多, 可以查看官方文档
远程调试
文档上说, 可以远程调试。
Manual
It is possible to use gops tool both in local and remote mode.
Local mode requires that you start the target binary as the same user that runs gops binary. To use gops in a remote mode you need to know target’s agent address.
In Local mode use process’s PID as a target; in Remote mode target is a host:port combination.
本地调试用pid
, 远程用host:port
来指示进程
对上述的hello world的代码稍加改动:
只加了Addr: ":9999",
这一行表示agent监听9999端口,
package main
import (
"log"
"time"
"github.com/google/gops/agent"
)
func main() {
if err := agent.Listen(agent.Options{
Addr: ":9999",
}); err != nil {
log.Fatal(err)
}
time.Sleep(time.Hour)
}
实际使用gops可以是:
# 查看trace
gerrylon@kali:~$ gops trace 'localhost:9999'
Tracing now, will take 5 secs...
Trace dump saved to: /tmp/trace950121522
2020/09/13 11:10:13 Parsing trace...
2020/09/13 11:10:13 Splitting trace...
2020/09/13 11:10:13 Opening browser. Trace viewer is listening on http://127.0.0.1:34487
总结
gops结合了pprof, trace, 非常方便, 值得使用并深入学习。
(完)