GitHub:
一个用于列出和诊断分析系统中正在运行的 Go 程序的命令行工具
安装
go get -u github.com/google/gops
命令帮助
执行 gops help
查看帮助文档:
gops is a tool to list and diagnose Go processes. gops... gops # displays process infoCommands: stack Prints the stack trace. gc Runs the garbage collector and blocks until successful. setgc Sets the garbage collection target percentage. memstats Prints the allocation and garbage collection stats. version Prints the Go version used to build the program. stats Prints the vital runtime stats. help Prints this help text.Profiling commands: trace Runs the runtime tracer for 5 secs and launches "go tool trace". pprof-heap Reads the heap profile and launches "go tool pprof". pprof-cpu Reads the CPU profile and launches "go tool pprof".All commands require the agent running on the Go process.Symbol "*" indicates the process runs the agent.
使用详解
为了能更好的分析程序,需要在我们的项目中加一行 agent
诊断分析代码,用于统计分析程序问题。
package mainimport ( "log" "time" "github.com/google/gops/agent")func main() { if err := agent.Listen(agent.Options{}); err != nil { log.Fatal(err) } time.Sleep(time.Hour)}
其中,agent.
支持更多的参数:
// Code reference: github.com/google/gops/agent/agent.go:42// Options allows configuring the started agent.type Options struct { // Addr is the host:port the agent will be listening at. // Optional. Addr string // ConfigDir is the directory to store the configuration file, // PID of the gops process, filename, port as well as content. // Optional. ConfigDir string // ShutdownCleanup automatically cleans up resources if the // running process receives an interrupt. Otherwise, users // can call Close before shutting down. // Optional. ShutdownCleanup bool}
Addr
可选。为远程分析服务提供监听地址,例如:
:9119
。配置了该项,那我们可以在本机查看分析远程服务器上的 Go 程序,非常有帮助。ConfigDir
可选。用于存放统计数据和配置的目录,默认为当前用户的主目录。也可以通过环境变量
GOPS_CONFIG_DIR
设置。具体参考代码:
const gopsConfigDirEnvKey = "GOPS_CONFIG_DIR"func ConfigDir() (string, error) { if configDir := os.Getenv(gopsConfigDirEnvKey); configDir != "" { return configDir, nil } if runtime.GOOS == "windows" { return filepath.Join(os.Getenv("APPDATA"), "gops"), nil } homeDir := guessUnixHomeDir() if homeDir == "" { return "", errors.New("unable to get current user home directory: os/user lookup failed; $HOME is empty") } return filepath.Join(homeDir, ".config", "gops"), nil}func guessUnixHomeDir() string { usr, err := user.Current() if err == nil { return usr.HomeDir } return os.Getenv("HOME")}
ShutdownCleanup
可选。设置为
true
,则在程序关闭时会自动清理数据。
NOTE: 如果不加 agent
代码,那我们无法更深入的诊断程序,也就是说无法执行gops memstats
、gops pprof-heap
等所有类似于 gops <cmd> <pid|addr> ...
的子命令。
gops
直接执行 gops
命令会列出本机所有正在运行的 Go 程序。
$ gops99288 47636 go go1.10.1 /usr/local/Cellar/go/1.10.1/libexec/bin/go99300 99288 main* go1.10.1 /private/var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/go-build375822490/b001/exe/main99570 2899 gops go1.10.1 /Users/shocker/gowork/bin/gops99154 14655 hugo go1.11.1 /usr/local/Cellar/hugo/0.49.1/bin/hugo
该命令会显示以下内容:
- PID
- PPID
- 程序名称
- 构建该程序的 Go 版本号
- 程序所在绝对路径
注意,列表中有个程序名称后面带了个 *
,表示该程序加入了 gops
的诊断分析代码。
gops <pid>
用法: gops <pid>
PID
Go 程序的基本信息 $ gops 99300parent PID: 99288threads: 11memory usage: 0.157%cpu usage: 0.013%username: shockercmd+args: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/go-build375822490/b001/exe/mainlocal/remote: *:9105 <-> :0 (LISTEN)local/remote: 127.0.0.1:57109 <-> 127.0.0.1:3306 (ESTABLISHED)local/remote: *:8000 <-> :0 (LISTEN)
local/remote
表示本机建立的监听(LISTEN),或者与远程服务器建立的链接(ESTABLISHED)
local/remote: *:9105 <-> :0 (LISTEN)
中的 *:9105
是 gops/agent
提供的服务,
gops tree
用法: gops tree
$ gops tree...├── 2899│ └── 99996 (gops) {go1.10.1}├── 47636│ └── 99288 (go) {go1.10.1}│ └── [*] 99300 (main) {go1.10.1}└── 14655 └── 99154 (hugo) {go1.11.1}
gops stack (<pid>|<addr>)
用法: gops stack (<pid>|<addr>)
$ gops stack 99300goroutine 7 [running]:runtime/pprof.writeGoroutineStacks(0x1882720, 0xc4202b8010, 0xd0, 0xd0) /usr/local/Cellar/go/1.10.1/libexec/src/runtime/pprof/pprof.go:650 +0xa7runtime/pprof.writeGoroutine(0x1882720, 0xc4202b8010, 0x2, 0x30, 0xc420068248) /usr/local/Cellar/go/1.10.1/libexec/src/runtime/pprof/pprof.go:639 +0x44goroutine 1 [IO wait, 9 minutes]:internal/poll.runtime_pollWait(0x1db4da0, 0x72, 0x0) /usr/local/Cellar/go/1.10.1/libexec/src/runtime/netpoll.go:173 +0x57internal/poll.(*pollDesc).wait(0xc4201e7318, 0x72, 0x0, 0x0, 0x0)# more ...
gops memstats (<pid>|<addr>)
用法: gops memstats (<pid>|<addr>)
$ gops memstats 127.0.0.1:9105alloc: 1.36MB (1428632 bytes)total-alloc: 10.21MB (10709376 bytes)sys: 9.07MB (9509112 bytes)lookups: 91mallocs: 102818frees: 91896heap-alloc: 1.36MB (1428632 bytes)heap-sys: 5.22MB (5472256 bytes)heap-idle: 2.34MB (2457600 bytes)heap-in-use: 2.88MB (3014656 bytes)heap-released: 0 bytesheap-objects: 10922stack-in-use: 704.00KB (720896 bytes)stack-sys: 704.00KB (720896 bytes)stack-mspan-inuse: 47.95KB (49096 bytes)stack-mspan-sys: 80.00KB (81920 bytes)stack-mcache-inuse: 6.78KB (6944 bytes)stack-mcache-sys: 16.00KB (16384 bytes)other-sys: 1.21MB (1266624 bytes)gc-sys: 492.00KB (503808 bytes)next-gc: when heap-alloc >= 4.00MB (4194304 bytes)last-gc: 2018-10-18 13:37:04.37511973 +0800 CSTgc-pause-total: 9.209158msgc-pause: 52831num-gc: 60enable-gc: truedebug-gc: false
gops gc (<pid>|<addr>)
用法: gops gc (<pid>|<addr>)
gops setgc (<pid>|<addr>)
用法: gops setgc (<pid>|<addr>)
gops version (<pid>|<addr>)
用法: gops version (<pid>|<addr>)
gops stats (<pid>|<addr>)
用法: gops stats (<pid>|<addr>)
goroutine
数量、GOMAXPROCS
值等信息 $ gops stats 127.0.0.1:9105goroutines: 11OS threads: 14GOMAXPROCS: 4num CPU: 4
gops pprof-cpu (<pid>|<addr>)
用法: gops pprof-cpu (<pid>|<addr>)
go tool pprof
工具中关于 CPU 的性能分析数据,操作与 pprof
一致。 $ gops pprof-cpu 99300Profiling CPU now, will take 30 secs...Profile dump saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/profile881383738Profiling dump saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/profile881383738Binary file saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/binary970030929File: binary970030929Type: cpuTime: Oct 18, 2018 at 2:43pm (CST)Duration: 30s, Total samples = 0Entering interactive mode (type "help" for commands, "o" for options)(pprof)
gops pprof-heap (<pid>|<addr>)
用法: gops pprof-heap (<pid>|<addr>)
go tool pprof
工具中关于 heap 的性能分析数据,操作与 pprof
一致。 $ gops pprof-heap 99300Profile dump saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/profile045800436Profiling dump saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/profile045800436Binary file saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/binary315133123File: binary315133123Type: inuse_spaceTime: Oct 18, 2018 at 2:46pm (CST)Entering interactive mode (type "help" for commands, "o" for options)(pprof)
gops trace (<pid>|<addr>)
用法: gops trace (<pid>|<addr>)
$ gops trace 99300Tracing now, will take 5 secs...Trace dump saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/trace1363107372018/10/18 14:49:06 Parsing trace...2018/10/18 14:49:06 Serializing trace...2018/10/18 14:49:06 Splitting trace...2018/10/18 14:49:06 Opening browser. Trace viewer is listening on http://127.0.0.1:61380
参考资料
原文地址:
更多文章请访问我的个人博客: