gops —— Go 程序診斷分析工具

GitHub: https://github.com/google/gops

一個用於列出和診斷分析系統中正在運行的 Go 程序的命令行工具

安裝


1


go get -u github.com/google/gops

命令幫助

執行 gops help 查看幫助文檔:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22


gops is a tool to list and diagnose Go processes.

gops <cmd> <pid|addr> ...

gops <pid> # displays process info

Commands:

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 診斷分析代碼,用於統計分析程序問題。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16


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)

}

其中,agent. 支持更多的參數:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19


// 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設置。具體參考代碼:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24


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 memstatsgops pprof-heap等所有類似於 gops <cmd> <pid|addr> ... 的子命令。

gops

直接執行 gops 命令會列出本機所有正在運行的 Go 程序。


1

2

3

4

5

6


$ gops

99288 47636 go    go1.10.1 /usr/local/Cellar/go/1.10.1/libexec/bin/go

99300 99288 main* go1.10.1 /private/var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/go-build375822490/b001/exe/main

99570 2899  gops  go1.10.1 /Users/shocker/gowork/bin/gops

99154 14655 hugo  go1.11.1 /usr/local/Cellar/hugo/0.49.1/bin/hugo

該命令會顯示以下內容:

 ●  PID
 ●  PPID
 ●  程序名稱
 ●  構建該程序的 Go 版本號
 ●  程序所在絕對路徑

注意,列表中有個程序名稱後面帶了個 *,表示該程序加入了 gops 的診斷分析代碼。

gops <pid>

用法: gops <pid> 查看本機指定 PID Go 程序的基本信息


1

2

3

4

5

6

7

8

9

10

11


$ gops 99300

parent PID: 99288

threads: 11

memory usage: 0.157%

cpu usage: 0.013%

username: shocker

cmd+args: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/go-build375822490/b001/exe/main

local/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 以目錄樹的形式展示所有 Go 程序。


1

2

3

4

5

6

7

8

9

10


$ 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>) 用於顯示程序所有堆棧信息,包括每個 goroutine 的堆棧信息、運行狀態、運行時長等。


1

2

3

4

5

6

7

8

9

10

11

12

13

14


$ gops stack 99300

goroutine 7 [running]:

runtime/pprof.writeGoroutineStacks(0x1882720, 0xc4202b8010, 0xd0, 0xd0)

/usr/local/Cellar/go/1.10.1/libexec/src/runtime/pprof/pprof.go:650 +0xa7

runtime/pprof.writeGoroutine(0x1882720, 0xc4202b8010, 0x2, 0x30, 0xc420068248)

/usr/local/Cellar/go/1.10.1/libexec/src/runtime/pprof/pprof.go:639 +0x44

goroutine 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 +0x57

internal/poll.(*pollDesc).wait(0xc4201e7318, 0x72, 0x0, 0x0, 0x0)

# more ...

gops memstats (<pid>|<addr>)

用法: gops memstats (<pid>|<addr>) 查看程序的內存統計信息


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29


$ gops memstats 127.0.0.1:9105

alloc: 1.36MB (1428632 bytes)

total-alloc: 10.21MB (10709376 bytes)

sys: 9.07MB (9509112 bytes)

lookups: 91

mallocs: 102818

frees: 91896

heap-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 bytes

heap-objects: 10922

stack-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 CST

gc-pause-total: 9.209158ms

gc-pause: 52831

num-gc: 60

enable-gc: true

debug-gc: false

gops gc (<pid>|<addr>)

用法: gops gc (<pid>|<addr>) 查看指定程序的垃圾回收(GC)信息

gops setgc (<pid>|<addr>)

用法: gops setgc (<pid>|<addr>) 設定指定程序的 GC 目標百分比

gops version (<pid>|<addr>)

用法: gops version (<pid>|<addr>) 查看指定程序構建時的 Go 版本號

gops stats (<pid>|<addr>)

用法: gops stats (<pid>|<addr>) 查看指定程序的 goroutine 數量、GOMAXPROCS 值等信息


1

2

3

4

5

6


$ gops stats 127.0.0.1:9105

goroutines: 11

OS threads: 14

GOMAXPROCS: 4

num CPU: 4

gops pprof-cpu (<pid>|<addr>)

用法: gops pprof-cpu (<pid>|<addr>) 調用並展示 go tool pprof 工具中關於 CPU 的性能分析數據,操作與 pprof 一致。


1

2

3

4

5

6

7

8

9

10

11

12


$ gops pprof-cpu 99300

Profiling CPU now, will take 30 secs...

Profile dump saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/profile881383738

Profiling dump saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/profile881383738

Binary file saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/binary970030929

File: binary970030929

Type: cpu

Time: Oct 18, 2018 at 2:43pm (CST)

Duration: 30s, Total samples = 0

Entering 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 一致。


1

2

3

4

5

6

7

8

9

10


$ gops pprof-heap 99300

Profile dump saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/profile045800436

Profiling dump saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/profile045800436

Binary file saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/binary315133123

File: binary315133123

Type: inuse_space

Time: 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>) 追蹤程序運行5秒,生成可視化報告,並可在瀏覽器中查看: http://127.0.0.1:61380


1

2

3

4

5

6

7

8


$ gops trace 99300

Tracing now, will take 5 secs...

Trace dump saved to: /var/folders/cs/mfl4k8t54_g1thdzvzkdxbbr0000gn/T/trace136310737

2018/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


原文發佈時間爲:2018-10-24

本文作者:Shocker

本文來自雲棲社區合作伙伴“Golang語言社區”,瞭解相關信息可以關注“Golang語言社區”。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章