首页 > 数据库 > 数据库开发技术 > 线上诊断神器 Arthas 专治疗疑难杂症
❝当线上发生问题时是很烦人的,领导们心急如焚,程序员被夺命连环 call 加钉钉轰炸, "快看看这个 XXX 功能怎么不行了......很紧急快点来公司一趟处理下"。一般大家都会先通过日志来定位问题,能从日志中定位的问题一般是比较简单的逻辑问题或编程错误,当通过日志无法定位问题时这时候一部分同学就开启了瞎猜模式,好像蒙着眼睛开战斗机。瞎猜它行么?行! 毕竟总有蒙对的时候。今天就给大家推荐一款神器提高效率专治瞎猜。当再遇到问题,你就掏出这个大宝贝,一顿疯狂操作后分分钟把问题解决掉, 瞬间 B 格就高了, 拯救加班的小伙伴于水火之中,事了拂衣去,深藏功与名,从此以后公司里只留下你的传说。
❞
Arthas 是 Alibaba 开源的 Java 诊断工具产品,通过全局视角实时查看应用 load、内存、gc、线程的状态信息,并能在不修改应用代码的情况下,对业务问题进行诊断,包括查看方法调用的出入参、异常,监测方法执行耗时,类加载信息等,大大提升线上问题排查效率。
通常,本地开发环境无法访问生产环境。如果在生产环境中遇到问题,则无法使用 IDE 远程调试。更糟糕的是,在生产环境中调试是不可接受的,因为它会暂停所有线程,导致服务暂停。
开发人员可以尝试在测试环境或者预发环境中复现生产环境中的问题。但是,某些问题无法在不同的环境中轻松复现,甚至在重新启动后就消失了。
Arthas 旨在解决这些问题。「开发人员可以在线解决生产问题。无需 JVM 重启,无需代码更改。Arthas 作为观察者永远不会暂停正在运行的线程。」
❝想象一下一个英国绅士,站在你身边一口地道的伦敦腔 "What can I do for you, sir"
❞
当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:
❝Arthas 支持 JDK 6+,支持 Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。
❞
Github 下载: https://github.com/alibaba/arthas/releases Maven 仓库下载 : https://arthas.aliyun.com/download/latest_version?mirror=aliyun
离线文档: https://arthas.aliyun.com/download/doc/latest_version?mirror=aliyun
在命令行下面执行(使用和目标进程一致的用户启动,否则可能 attach 失败):
java -jar arthas-boot.jar
❝会输出如下信息,包括了使用示例,和支持的命令和参数
❞
[INFO] JAVA_HOME: /usr/local/jdk8u152-x64/jdk1.8.0_152/jre
[INFO] arthas-boot version: 3.7.1
Usage: arthas-boot [-h] [--target-ip <value>] [--telnet-port <value>]
[--http-port <value>] [--session-timeout <value>] [--arthas-home <value>]
[--use-version <value>] [--repo-mirror <value>] [--versions] [--use-http]
[--attach-only] [-c <value>] [-f <value>] [--height <value>] [--width
<value>] [-v] [--tunnel-server <value>] [--agent-id <value>] [--app-name
<value>] [--username <value>] [--password <value>] [--stat-url <value>]
[--select <value>] [--disabled-commands <value>] [pid]
Bootstrap Arthas
EXAMPLES:
java -jar arthas-boot.jar <pid>
java -jar arthas-boot.jar --telnet-port 9999 --http-port -1
java -jar arthas-boot.jar --username admin --password <password>
java -jar arthas-boot.jar --tunnel-server 'ws://192.168.10.11:7777/ws'
--app-name demoapp
java -jar arthas-boot.jar --tunnel-server 'ws://192.168.10.11:7777/ws'
--agent-id bvDOe8XbTM2pQWjF4cfw
java -jar arthas-boot.jar --stat-url 'http://192.168.10.11:8080/api/stat'
java -jar arthas-boot.jar -c 'sysprop; thread' <pid>
java -jar arthas-boot.jar -f batch.as <pid>
java -jar arthas-boot.jar --use-version 3.7.1
java -jar arthas-boot.jar --versions
java -jar arthas-boot.jar --select math-game
java -jar arthas-boot.jar --session-timeout 3600
java -jar arthas-boot.jar --attach-only
java -jar arthas-boot.jar --disabled-commands stop,dump
java -jar arthas-boot.jar --repo-mirror aliyun --use-http
WIKI:
https://arthas.aliyun.com/doc
Options and Arguments:
-h,--help Print usage
--target-ip <value> The target jvm listen ip, default 127.0.0.1
--telnet-port <value> The target jvm listen telnet port, default
3658
--http-port <value> The target jvm listen http port, default 8563
--session-timeout <value> The session timeout seconds, default 1800
(30min)
--arthas-home <value> The arthas home
--use-version <value> Use special version arthas
--repo-mirror <value> Use special remote repository mirror, value is
center/aliyun or http repo url.
--versions List local and remote arthas versions
--use-http Enforce use http to download, default use
https
--attach-only Attach target process only, do not connect
-c,--command <value> Command to execute, multiple commands
separated by ;
-f,--batch-file <value> The batch file to execute
--height <value> arthas-client terminal height
--width <value> arthas-client terminal width
-v,--verbose Verbose, print debug info.
--tunnel-server <value> The tunnel server url
--agent-id <value> The agent id register to tunnel server
--app-name <value> The app name
--username <value> The username
--password <value> The password
--stat-url <value> The report stat url
--select <value> select target process by classname or
JARfilename
--disabled-commands <value> disable some commands
<pid> Target pid
选择应用 java 进程:
$ $ java -jar arthas-boot.jar
* [1]: 26125
[2]: 33682 demo.jar
当启动了 arthas 却没有检测到 Java 进程时,我们可以先查到 Java 进程的进程 ID .
java -jar arthas-boot.jar
[INFO] JAVA_HOME: /usr/local/jdk8u152-x64/jdk1.8.0_152/jre
[INFO] arthas-boot version: 3.7.1
[INFO] Can not find java process. Try to run `jps` command lists the instrumented Java HotSpot VMs on the target system.
Please select an available pid.
java -jar arthas-boot.jar <pid>
输入 dashboard ,按回车/enter,会展示当前进程的信息,按ctrl+c可以中断执行。
$ dashboard
ID NAME GROUP PRIORI STATE %CPU TIME INTERRU DAEMON
17 pool-2-thread-1 system 5 WAITIN 67 0:0 false false
27 Timer-for-arthas-dashb system 10 RUNNAB 32 0:0 false true
11 AsyncAppender-Worker-a system 9 WAITIN 0 0:0 false true
9 Attach Listener system 9 RUNNAB 0 0:0 false true
3 Finalizer system 8 WAITIN 0 0:0 false true
2 Reference Handler system 10 WAITIN 0 0:0 false true
4 Signal Dispatcher system 9 RUNNAB 0 0:0 false true
26 as-command-execute-dae system 10 TIMED_ 0 0:0 false true
13 job-timeout system 9 TIMED_ 0 0:0 false true
1 main main 5 TIMED_ 0 0:0 false false
14 nioEventLoopGroup-2-1 system 10 RUNNAB 0 0:0 false false
18 nioEventLoopGroup-2-2 system 10 RUNNAB 0 0:0 false false
23 nioEventLoopGroup-2-3 system 10 RUNNAB 0 0:0 false false
15 nioEventLoopGroup-3-1 system 10 RUNNAB 0 0:0 false false
Memory used total max usage GC
heap 32M 155M 1820M 1.77% gc.ps_scavenge.count 4
ps_eden_space 14M 65M 672M 2.21% gc.ps_scavenge.time(m 166
ps_survivor_space 4M 5M 5M s)
ps_old_gen 12M 85M 1365M 0.91% gc.ps_marksweep.count 0
nonheap 20M 23M -1 gc.ps_marksweep.time( 0
code_cache 3M 5M 240M 1.32% ms)
Runtime
os.name Mac OS X
os.version 10.13.4
java.version 1.8.0_162
java.home /Library/Java/JavaVir
tualMachines/jdk1.8.0
_162.jdk/Contents/Hom
e/jre
thread 1会打印线程 ID 1 的栈,通常是 main 函数的线程。
$ thread 1 | grep 'main('
at demo.MathGame.main(MathGame.java:17)
比如 Spring 生成的某个代理对象
$ jad com.demo.Bean1$$EnhancerBySpringCGLIB$$5974b8b5
如果只是退出当前的连接,可以用quit或者exit命令。Attach 到目标进程上的 arthas 还会继续运行,端口会保持开放,下次连接时可以直接连接上。
如果想完全退出 arthas,可以执行stop命令。
❝注意 请注意,这些命令,都通过字节码增强技术来实现的,会在指定类的方法中插入一些切面来实现数据统计和观测,因此在线上、预发使用时,请尽量明确需要观测的类、方法以及条件,诊断结束要执行 stop 或将增强过的类执行 reset 命令。
❞
Arthas 支持使用管道对上述命令的结果进行进一步的处理,如sm java.lang.String * | grep 'index'
当线上出现偶发的问题,比如需要 watch 某个条件,而这个条件一天可能才会出现一次时,异步后台任务就派上用场了,详情请参考这里
下载 Plugin :
鼠标右键使用,可以快速复制得到 Arthas 命令:
trace io.grpc.Context * -n 5 --skipJDKMethod false
watch io.grpc.Context * '{params,returnObj,throwExp}' -n 5 -x 3
stack io.grpc.Context * -n 5
monitor io.grpc.Context * -n 10 --cycle 10
sc -d io.grpc.Context
sm io.grpc.Context *
jad --source-only io.grpc.Context
heapdump /tmp/dump.hprof
thread -i 3000 -n 5
看完本文有收获?转发给更多的朋友吧
DevXTalk 不止于技术
原文作者:DevXTalk;如有侵权,请联系管理员删除。