gops —— Go 程序诊断分析工具
GitHub: https://github.com/google/gops
一个用于列出和诊断分析系统中正在运行的 Go 程序的命令行工具
安装
go get -u github.com/google/gops
命令帮助
执行 gops help
查看帮助文档:
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
诊断分析代码,用于统计分析程序问题。
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.
支持更多的参数:
// 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 程序。
$ 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 程序的基本信息
$ 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 程序。
$ 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 的堆栈信息、运行状态、运行时长等。
$ 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>)
查看程序的内存统计信息
$ 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
值等信息
$ 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
一致。
$ 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
一致。
$ 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
$ 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
参考资料
原文地址: https://shockerli.net/post/golang-tool-gops/
更多文章请访问我的个人博客: https://shockerli.net
gops —— Go 程序诊断分析工具的更多相关文章
- [原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler
[原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler 1 官方网站:http://memprofiler.com/2 下载地址:http://memprofiler. ...
- mysql查询诊断分析工具
Query Profiler是MYSQL自带的一种query诊断分析工具,通过它可以分析出一条SQL语句的性能瓶颈在什么地方.通常我们是使用的explain,以及slow query log都无法做到 ...
- NET程序内存分析工具CLRProfiler的使用(性能测试)
http://blog.csdn.net/wy3552128/article/details/8158938 大家都知道.net有一套自己的内存(垃圾)回收机制,除非有一些数据(方法)长期占有内存不随 ...
- 11个Visual Studio代码性能分析工具
软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行代码分析和 ...
- Mysql内置的profiling性能分析工具
要想优化一条 Query,我们就需要清楚的知道这条 Query 的性能瓶颈到底在哪里,是消耗的 CPU计算太多,还是需要的的 IO 操作太多?要想能够清楚的了解这些信息,在 MySQL 5.0 和 M ...
- .NET 11 个 Visual Studio 代码性能分析工具
原文地址 软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行 ...
- Mysql优化_内置profiling性能分析工具
如果要进行SQL的调优优化和排查,第一步是先让故障重现,但是这个并不是这一分钟有问题,下一秒就OK.一般的企业一般是DBA数据库工程师从监控里找到问题.DBA会告诉我们让我们来排查问题,那么可能很多种 ...
- 11 个 Visual Studio 代码性能分析工具
软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行代码分析和 ...
- 品味性能之道<六>:图形化SQL分析工具
在上一章里,重点分享了命令行SQL分析工具的使用方法.在本章将重点分享PL/SQL的SQL分析工具. 一.如何打开PL/SQL执行计划 开启PL/SQL这工具,推荐如下方法: 点击 ...
随机推荐
- 手机端-万种bt在线观看器,安卓正版下载!
安卓正版下载, 点击下载 无广告,完全免费!寻找任何你想要的资源!
- _ZNote_Chrom_插件_Chrom运行Android软件_APK
https://chrome.google.com/webstore/detail/arc-welder/emfinbmielocnlhgmfkkmkngdoccbadn?utm_source=chr ...
- SpringBoot 基础01
SpringBoot 基础 pom.xml <!-- Spring Boot 依赖版本控制 --> <parent> <groupId>org.springfram ...
- el表达式与<c:foreach>标签的联合各种使用方法记录
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ tagl ...
- 【hdu2000】ASCII码排序
题目来源:www.acm.hdu.edu.cn 题目编号:2000 ASCII码排序 /*----------------------------------------原题目------------ ...
- 4 年前端狗,2 年 CTO
阅读本文大概需要 14 分钟. 本文系作者:『Scott』原创发布于慕课网. 我,Scott,一家创业公司的 CTO. 从业6年却很少写文章,近一年来接触了几十个刚毕业的前端新人,也面试了100多个前 ...
- Pycharm永久激活方式
1.首先下载pycharm破解包 http://idea.lanyus.com/ 2. 将下载的文件放入PyCharm安装bin目录下: 3.在pycharm.exe.vmoptions ,pycha ...
- 《http权威指南》读书笔记13
概述 最近对http很感兴趣,于是开始看<http权威指南>.别人都说这本书有点老了,而且内容太多.我个人觉得这本书写的太好了,非常长知识,让你知道关于http的很多概念,不仅告诉你怎么做 ...
- php过滤 字符
今天在抓取页面中得到字符串:"卡牌 ",使用str_replace . preg_replace 和 strip_tags过滤都无解. 最后google到2种方式,如下: str_ ...
- Apache Lucene全局搜索引擎入门教程
Lucene简介 Lucent:Apache软件基金会Jakarta项目组的一个子项目,Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻.在Java开发环境里Lucene是一个成熟 ...