Golang gops 程序性能诊断工具

发布时间: 更新时间: 总字数:1117 阅读时间:3m 作者: IP上海 分享 网址

gogs 是一个用于列出和诊断当前系统上运行的 Go 进程的工具,支持命令行和 agent 两种使用方式

介绍

  • gops 是一条命令,用于列出和诊断当前系统上运行的 Go 进程。
  • 作为 lib 库,对于启动诊断 agent 的进程,gops 可以报告更多信息,如当前堆栈跟踪、Go 版本、内存统计等

命令安装

$ go install github.com/google/gops@latest
  • help
gops --help
gops is a tool to list and diagnose Go processes.

Usage:
  gops [flags]
  gops [command]

Examples:
  gops <cmd> <pid|addr> ...
  gops <pid> # displays process info
  gops help  # displays this help message

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  gc          Runs the garbage collector and blocks until successful.
  help        Help about any command
  memstats    Prints the allocation and garbage collection stats.
  pprof-cpu   Reads the CPU profile and launches "go tool pprof".
  pprof-heap  Reads the heap profile and launches "go tool pprof".
  process     Prints information about a Go process.
  setgc       Sets the garbage collection target percentage. To completely stop GC, set to 'off'
  stack       Prints the stack trace.
  stats       Prints runtime stats.
  trace       Runs the runtime tracer for 5 secs and launches "go tool trace".
  tree        Display parent-child tree for Go processes.
  version     Prints the Go version used to build the program.

Flags:
  -h, --help   help for gops

Use "gops [command] --help" for more information about a command.

程序诊断

  • 对于启动诊断代理的进程,gops 可以报告更多信息,如当前堆栈跟踪、Go 版本、内存统计等,示例
    • 可以设置 GOPS_CONFIG_DIR 环境变量来指定配置目录。默认情况下,gops 会使用当前用户的主目录(windows 下为 AppData)
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)
}

运行:

$ go run main.go
$ ps -ef | grep go
root       64511   64493  0 15:49 pts/1    00:00:00 go run main.go
root       64615   64511  0 15:49 pts/1    00:00:00 /tmp/go-build552187492/b001/exe/main
$ gops
64615 64511 main          * go1.22.2  /tmp/go-build552187492/b001/exe/main
...

gops

  • 要报告某个进程的更多信息
# gops <pid>
$ gops 64615
parent PID:	64511
threads:	5
memory usage:	0.246%
cpu usage:	0.000%
username:	root
cmd+args:	/tmp/go-build552187492/b001/exe/main
elapsed time:	01:05
local/remote:	127.0.0.1:36769 <-> 0.0.0.0:0 (LISTEN)

# gops <pid> 2s
$ gops 64615 2s
parent PID:	64511
threads:	5
memory usage:	0.246%
cpu usage:	0.000%
cpu usage (2s):	0.000%
username:	root
cmd+args:	/tmp/go-build552187492/b001/exe/main
elapsed time:	01:37
local/remote:	127.0.0.1:36769 <-> 0.0.0.0:0 (LISTEN)

gops tree

  • 显示一个进程树所有的运转进程
$ gops tree
...
├── 1
│   └── 834 (aliyun-service) {go1.20.10}
├── 63383
│   └── 64644 (gops) {go1.22.2}
└── 64493
    └── 64511 (go) {go1.22.2}
        └── [*]  64615 (main) {go1.22.2}

gops stack (|)

  • 打印目标程序的当前堆栈跟踪
$ gops stack 64615
goroutine 6 [running]:
runtime/pprof.writeGoroutineStacks({0x7fd1da8a8040, 0xc000044058})
	/usr/lib/go-1.22/src/runtime/pprof/pprof.go:743 +0x6a
runtime/pprof.writeGoroutine({0x7fd1da8a8040?, 0xc000044058?}, 0x13?)
	/usr/lib/go-1.22/src/runtime/pprof/pprof.go:732 +0x25
runtime/pprof.(*Profile).WriteTo(0x644290?, {0x7fd1da8a8040?, 0xc000044058?}, 0x0?)
	/usr/lib/go-1.22/src/runtime/pprof/pprof.go:369 +0x14b
github.com/google/gops/agent.handle({0x7fd1da8a8018, 0xc000044058}, {0xc00001417a?, 0x1?, 0x1?})
	/root/test/vendor/github.com/google/gops/agent/agent.go:200 +0x28b2
github.com/google/gops/agent.listen({0x57c110, 0xc000060020})
	/root/test/vendor/github.com/google/gops/agent/agent.go:144 +0x1b4
created by github.com/google/gops/agent.Listen in goroutine 1
	/root/test/vendor/github.com/google/gops/agent/agent.go:122 +0x36f

goroutine 1 [sleep]:
time.Sleep(0x34630b8a000)
	/usr/lib/go-1.22/src/runtime/time.go:195 +0x115
main.main()
	/root/test/main.go:14 +0x65

gops memstats (|)

  • 打印当前内存统计
$ gops memstats 64615
alloc: 1.12MB (1175192 bytes)
total-alloc: 1.12MB (1175192 bytes)
sys: 7.46MB (7820552 bytes)
lookups: 0
mallocs: 218
frees: 9
heap-alloc: 1.12MB (1175192 bytes)
heap-sys: 3.72MB (3899392 bytes)
heap-idle: 2.31MB (2424832 bytes)
heap-in-use: 1.41MB (1474560 bytes)
heap-released: 2.28MB (2392064 bytes)
heap-objects: 209
stack-in-use: 288.00KB (294912 bytes)
stack-sys: 288.00KB (294912 bytes)
stack-mspan-inuse: 12.81KB (13120 bytes)
stack-mspan-sys: 15.94KB (16320 bytes)
stack-mcache-inuse: 2.34KB (2400 bytes)
stack-mcache-sys: 15.23KB (15600 bytes)
other-sys: 567.08KB (580686 bytes)
gc-sys: 1.49MB (1566856 bytes)
next-gc: when heap-alloc >= 4.00MB (4194304 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

gc

  • 如果要在目标程序上强制运行垃圾回收,请运行 gc。它会阻塞,直到 GC 完成
gops gc (<pid>|<addr>)
  • 将垃圾回收目标值设置为某个百分比
# 以下命令将其设置为 10%
gops setgc (<pid>|<addr>) 10

# 以下命令将关闭垃圾回收器
gops setgc (<pid>|<addr>) off

gops version (|)

$ gops version 64615
go1.22.2

gops stats (|)

  • 打印运行时的统计数据,如 goroutines 和 GOMAXPROCS 的数量
$ gops stats 64615
goroutines: 2
OS threads: 6
GOMAXPROCS: 2
num CPU: 2

Profiling

  • gops 支持 CPU 和堆 pprof 配置文件
    • 读取堆或 CPU 配置文件后,它将输出到 go 工具 pprof,让你以交互方式检查配置文件

gops pprof-cpu (|)

$ gops pprof-cpu 64615
Profiling CPU now, will take 30 secs...
Profile dump saved to: /tmp/cpu_profile3661146860
Binary file saved to: /tmp/binary1894945919
File: binary1894945919
Type: cpu
Time: Mar 9, 2025 at 3:59pm (CST)
Duration: 30.03s, 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)

gops pprof-cpu (|)

$ gops pprof-heap 64615
Profile dump saved to: /tmp/heap_profile2190703419
Binary file saved to: /tmp/binary2409413710
File: binary2409413710
Type: inuse_space
Time: Mar 9, 2025 at 4:00pm (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof)

gops trace (|)

  • gops 允许您启动运行时跟踪器 5 秒钟并检查结果
gops trace 64615
Tracing now, will take 5 secs...
Trace dump saved to: /tmp/trace3639910133
2025/03/09 16:00:56 Preparing trace for viewer...
2025/03/09 16:00:56 Splitting trace for viewer...
2025/03/09 16:00:56 Opening browser. Trace viewer is listening on http://127.0.0.1:34721

参考

  1. https://github.com/google/gops
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数