一、什么是Arthas
官方介紹:Arthas 是Alibaba開源的Java診斷工具,深受開發(fā)者喜愛。
當你遇到以下類似問題而束手無策時,Arthas可以幫助你解決:
這個類從哪個 jar 包加載的?為什么會報各種類相關(guān)的 Exception?
我改的代碼為什么沒有執(zhí)行到?難道是我沒 commit?分支搞錯了?
遇到問題無法在線上 debug,難道只能通過加日志再重新發(fā)布嗎?
線上遇到某個用戶的數(shù)據(jù)處理有問題,但線上同樣無法 debug,線下無法重現(xiàn)!
是否有一個全局視角來查看系統(tǒng)的運行狀況?
有什么辦法可以監(jiān)控到JVM的實時運行狀態(tài)?
怎么快速定位應用的熱點,生成火焰圖?
二、如何下載
下載地址:https://github.com/alibaba/arthas/releases
三、如何使用
可以直接執(zhí)行命令,選擇需要attach的java進程回車
java -jar arthas-boot.jar
在執(zhí)行該命令時可能發(fā)生異常,這里記錄幾點我遇到的問題
1.選擇了java進程回車后報錯
[ERROR] Start arthas failed, exception stack trace:
com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
at sun.tools.attach.LinuxVirtualMachine.<init>(LinuxVirtualMachine.java:106)
at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:78)
at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:250)
at com.taobao.arthas.core.Arthas.attachAgent(Arthas.java:85)
at com.taobao.arthas.core.Arthas.<init>(Arthas.java:28)
at com.taobao.arthas.core.Arthas.main(Arthas.java:123)
解決:在對應JAVA進程啟動時添加JVM參數(shù) -XX:+StartAttachListener
2.選擇了java進程回車后報錯
[ERROR] Start arthas failed, exception stack trace:
java.io.IOException: well-known file /tmp/.java_pid87449 is not secure: file should be owned by the current user (which is 0) but is owned by 30001
at sun.tools.attach.LinuxVirtualMachine.checkPermissions(Native Method)
at sun.tools.attach.LinuxVirtualMachine.<init>(LinuxVirtualMachine.java:117)
at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:78)
at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:250)
at com.taobao.arthas.core.Arthas.attachAgent(Arthas.java:85)
at com.taobao.arthas.core.Arthas.<init>(Arthas.java:28)
at com.taobao.arthas.core.Arthas.main(Arthas.java:123)
[ERROR] attach fail, targetPid: 87449
該問題發(fā)生的原因是執(zhí)行該程序的用戶需要和目標進程具有相同的權(quán)限。
四、常用命令
- dashboard
$ 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
通過該儀表盤我們可以獲取到:
1.對應進程中線程信息,可以直觀看到哪些線程占用的CPU較高
2.JVM內(nèi)存使用情況,包括年輕帶(伊甸區(qū),存活區(qū)),老年帶,元空間(非堆)的GC情況
- thread
查看當前線程信息,查看線程的堆棧
命令:thread -n 3 ->打印當前最忙的top3線程
- monitor
方法執(zhí)行監(jiān)控,可以實時監(jiān)控某個方法的執(zhí)行耗時
$ monitor -c 5 demo.MathGame primeFactors
- trace
方法內(nèi)部調(diào)用路徑,并輸出方法路徑上的每個節(jié)點上耗時,可以直接根據(jù)執(zhí)行時間進行過濾
$ trace demo.MathGame run '#cost > 10'
- stack
輸出當前方法被調(diào)用的調(diào)用路徑,可以直接根據(jù)執(zhí)行時間進行過濾
$ stack demo.MathGame primeFactors '#cost>5'
- jad
將 JVM 中實際運行的 class 的 byte code 反編譯成 java 代碼,便于你理解業(yè)務邏.
命令:jad xx.xx.xx.ClassA
當存在多個源碼時,可以通過-c 指定classLoad
- jvm
查看當前JVM信息
通過此命令可以直接獲取當前死鎖的線程數(shù),JVM當前活躍的線程數(shù)等
- vmoption
查看,更新VM診斷相關(guān)的參數(shù)
- ognl
執(zhí)行ognl表達式,可以查看類的靜態(tài)屬性、執(zhí)行類的靜態(tài)函數(shù)等等,例如
$ ognl '@java.lang.System@out.println("hello")'
$ ognl '@demo.MathGame@random'
同理,如若存在多個相同類路徑代碼,則使用-c參數(shù)指定classload
- sc
查看JVM已加載的類信息,可以打印類的詳細信息,打印出類的Field信息等
- heapdump
dump java heap, 類似jmap命令的heap dump功能。
heapdump /tmp/dump.hprof
heapdump --live /tmp/dump.hprof
五、進階使用
- 火焰圖