阿里開源Java診斷工具神器使用及場景詳解
簡介
Arthas 是Alibaba開源的Java診斷工具,動態(tài)跟蹤Java代碼;實時監(jiān)控JVM狀態(tài),可以在不中斷程序執(zhí)行的情況下輕松完成JVM相關(guān)問題排查工作 。支持JDK 6+,支持Linux/Mac/Windows。這個工具真的很好用,而且入門超簡單,十分推薦。
使用場景
- 這個類從哪個 jar 包加載的?為什么會報各種類相關(guān)的 Exception?
- 我改的代碼為什么沒有執(zhí)行到?難道是我沒 commit?分支搞錯了?
- 遇到問題無法在線上 debug,難道只能通過加日志再重新發(fā)布嗎?
- 線上遇到某個用戶的數(shù)據(jù)處理有問題,但線上同樣無法 debug,線下無法重現(xiàn)!
- 是否有一個全局視角來查看系統(tǒng)的運(yùn)行狀況?
- 有什么辦法可以監(jiān)控到JVM的實時運(yùn)行狀態(tài)?接下來,圍繞這6個問題,學(xué)習(xí)下Arthas的基本用法。
安裝
執(zhí)行下面命令下載
wget https://alibaba.github.io/arthas/arthas-boot.jar
用java -jar的方式啟動
java -jar arthas-boot.jar ? [INFO] Found existing java process, please choose one and hit RETURN. * [1]: 79952 cn.test.MobileApplication [2]: 93872 org.jetbrains.jps.cmdline.Launcher
然后輸入數(shù)字,選擇你想要監(jiān)聽的應(yīng)用,回車即可
常用命令
查詢arthas版本
[arthas@79952]$ version 3.1.4
1、stack
輸出當(dāng)前方法被調(diào)用的調(diào)用路徑
很多時候我們都知道一個方法被執(zhí)行,但是有很多地方調(diào)用了它,你并不知道是誰調(diào)用了它,此時你需要的是 stack 命令。
| 參數(shù)名稱 | 參數(shù)說明 |
|---|---|
| class-pattern | 類名表達(dá)式匹配 |
| method-pattern | 方法名表達(dá)式匹配 |
[arthas@79952]$ stack com.baomidou.mybatisplus.extension.service.IService getOne
Press Q or Ctrl+C to abort.
Affect(class-cnt:202 , method-cnt:209) cost in 10761 ms.
ts=2019-11-13 11:49:13;thread_name=http-nio-8801-exec-6;id=2d;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@a6c54c3
@com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.getOne()
at com.baomidou.mybatisplus.extension.service.IService.getOne(IService.java:230)
...... ......
at cn.test.mobile.controller.order.OrderController.getOrderInfo(OrderController.java:500)
可以看到OrderController.java的第500行調(diào)用了這個getOne接口。
注意這個命令需要調(diào)用后才會觸發(fā)日志,相似的還有watch、trace等
2、jad
反編譯指定已加載類的源碼
有時候,版本發(fā)布后,代碼竟然沒有執(zhí)行,代碼是最新的嗎,這時可以使用jad反編譯相應(yīng)的class。
jad cn.test.mobile.controller.order.OrderController
僅編譯指定的方法
jad cn.test.mobile.controller.order.OrderController getOrderInfo
?
ClassLoader:
@RequestMapping(value={"getOrderInfo"}, method={RequestMethod.POST})
public Object getOrderInfo(HttpServletRequest request, @RequestBody Map map) {
ResponseVo responseVo = new ResponseVo();
... ... ... ...
3、sc
“Search-Class” 的簡寫 ,查看JVM已加載的類信息 有的時候,你只記得類的部分關(guān)鍵詞,你可以用sc獲取完整名稱 當(dāng)你碰到這個錯的時候“ClassNotFoundException”或者“ClassDefNotFoundException”,你可以用這個命令驗證下
| 參數(shù)名稱 | 參數(shù)說明 |
|---|---|
| class-pattern | 類名表達(dá)式匹配 |
| method-pattern | 方法名表達(dá)式匹配 |
| [d] | 輸出當(dāng)前類的詳細(xì)信息,包括這個類所加載的原始文件來源、類的聲明、加載的ClassLoader等詳細(xì)信息。如果一個類被多個ClassLoader所加載,則會出現(xiàn)多次 |
模糊搜索
sc *OrderController* cn.test.mobile.controller.order.OrderController
打印類的詳細(xì)信息 sc -d
sc -d cn.test.mobile.controller.order.OrderController
?
class-info cn.test.mobile.controller.order.OrderController
code-source /F:/IDEA-WORKSPACE-TEST-qyb/trunk/BE/mobile/target/classes/
name cn.test.mobile.controller.order.OrderController
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name OrderController
modifier public
annotation org.springframework.web.bind.annotation.RestController,org.springframework.web.bind.annotation.Requ
estMapping
interfaces
super-class +-cn.test.mobile.controller.BaseController
+-java.lang.Object
class-loader +-sun.misc.Launcher$AppClassLoader@18b4aac2
+-sun.misc.Launcher$ExtClassLoader@480bdb19
classLoaderHash 18b4aac2
與之相應(yīng)的還有sm( “Search-Method” ),查看已加載類的方法信息
查看String里的方法
sm java.lang.String java.lang.String <init>([BII)V java.lang.String <init>([BLjava/nio/charset/Charset;)V java.lang.String <init>([BLjava/lang/String;)V java.lang.String <init>([BIILjava/nio/charset/Charset;)V java.lang.String <init>([BIILjava/lang/String;)V ... ... ... ...
查看String中toString的詳細(xì)信息
sm -d java.lang.String toString declaring-class java.lang.String method-name toString modifier public annotation parameters return java.lang.String exceptions classLoaderHash null
4、watch
可以監(jiān)測一個方法的入?yún)⒑头祷刂?/p>
有些問題線上會出現(xiàn),本地重現(xiàn)不了,這時這個命令就有用了
| 參數(shù)名稱 | 參數(shù)說明 |
|---|---|
| class-pattern | 類名表達(dá)式匹配 |
| method-pattern | 方法名表達(dá)式匹配 |
| express | 觀察表達(dá)式 |
| condition-express | 條件表達(dá)式 |
| [b] | 在方法調(diào)用之前觀察 |
| [e] | 在方法異常之后觀察 |
| [s] | 在方法返回之后觀察 |
| [f] | 在方法結(jié)束之后(正常返回和異常返回)觀察,默認(rèn)選項 |
| [E] | 開啟正則表達(dá)式匹配,默認(rèn)為通配符匹配 |
| [x:] | 指定輸出結(jié)果的屬性遍歷深度,默認(rèn)為 1 |
觀察getOrderInfo的出參和返回值,出參就是方法結(jié)束后的入?yún)?/p>
watch cn.test.mobile.controller.order.OrderController getOrderInfo "{params,returnObj}" -x 2
?
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 456 ms.
ts=2019-11-13 15:30:18; [cost=18.48307ms] result=@ArrayList[
@Object[][ # 這個就是出參,params
@RequestFacade[org.apache.catalina.connector.RequestFacade@1d81dbd7],
@LinkedHashMap[isEmpty=false;size=2], # 把遍歷深度x改為3就可以查看map里的值了
],
@ResponseVo[ # 這個就是返回值 returnObj
log=@Logger[Logger[cn.test.db.common.vo.ResponseVo]],
success=@Boolean[true],
message=@String[Ok],
count=@Integer[0],
code=@Integer[1000],
data=@HashMap[isEmpty=false;size=1],
],
]
觀察getOrderInfo的入?yún)⒑头祷刂?/p>
watch cn.test.mobile.controller.order.OrderController getOrderInfo "{params,returnObj}" -x 3 -b
?
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 93 ms.
ts=2019-11-13 15:37:38; [cost=0.012479ms] result=@ArrayList[
@Object[][
@RequestFacade[
request=@Request[org.apache.catalina.connector.Request@d04e652],
sm=@StringManager[org.apache.tomcat.util.res.StringManager@7ae7a97b],
],
@LinkedHashMap[
@String[payNo]:@String[190911173713755288],
@String[catalogId]:@String[6],
],
],
null,# -b是方法調(diào)用之前觀察,所以還沒有返回值
]
如果需要捕捉異常的話,使用throwExp,如{params,returnObj,throwExp}
5、trace
輸出方法內(nèi)部調(diào)用路徑,和路徑上每個節(jié)點(diǎn)的耗時
可以通過這個命令,查看哪些方法耗性能,從而找出導(dǎo)致性能缺陷的代碼,這個耗時還包含了arthas執(zhí)行的時間哦。
| 參數(shù)名稱 | 參數(shù)說明 |
|---|---|
| class-pattern | 類名表達(dá)式匹配 |
| method-pattern | 方法名表達(dá)式匹配 |
| condition-express | 條件表達(dá)式 |
| [E] | 開啟正則表達(dá)式匹配,默認(rèn)為通配符匹配 |
| [n:] | 命令執(zhí)行次數(shù) |
| #cost | 方法執(zhí)行耗時 |
輸出getOrderInfo的調(diào)用路徑
trace -j cn.test.mobile.controller.order.OrderController getOrderInfo
?
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 92 ms.
---ts=2019-11-13 15:46:59;thread_name=http-nio-8801-exec-4;id=2b;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@a6c54c3
---[15.509011ms] cn.test.mobile.controller.order.OrderController:getOrderInfo()
+---[0.03584ms] cn.test.db.common.vo.ResponseVo:<init>() #472
+---[0.00992ms] java.util.HashMap:<init>() #473
+---[0.02176ms] cn.test.mobile.controller.order.OrderController:getUserInfo() #478
+---[0.024ms] java.util.Map:get() #483
+---[0.00896ms] java.lang.Object:toString() #483
+---[0.00864ms] java.lang.Integer:parseInt() #483
+---[0.019199ms] com.baomidou.mybatisplus.core.conditions.query.QueryWrapper:<init>() #500
+---[0.135679ms] com.baomidou.mybatisplus.core.conditions.query.QueryWrapper:allEq() #500
+---[12.476072ms] cn.test.db.service.IOrderMediaService:getOne() #500
+---[0.0128ms] java.util.HashMap:put() #501
+---[0.443517ms] cn.test.db.common.vo.ResponseVo:setSuccess() #503
`---[0.03488ms] java.util.Map:put() #504
輸出getOrderInfo的調(diào)用路徑,且cost大于10ms,-j是指過濾掉jdk中的方法,可以看到輸出少了很多
trace -j cn.test.mobile.controller.order.OrderController getOrderInfo '#cost > 10'
?
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 96 ms.
---ts=2019-11-13 15:53:42;thread_name=http-nio-8801-exec-2;id=29;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@a6c54c3
---[13.803743ms] cn.test.mobile.controller.order.OrderController:getOrderInfo()
+---[0.01312ms] cn.test.db.common.vo.ResponseVo:<init>() #472
+---[0.01408ms] cn.test.mobile.controller.order.OrderController:getUserInfo() #478
+---[0.0128ms] com.baomidou.mybatisplus.core.conditions.query.QueryWrapper:<init>() #500
+---[0.303998ms] com.baomidou.mybatisplus.core.conditions.query.QueryWrapper:allEq() #500
+---[12.675431ms] cn.test.db.service.IOrderMediaService:getOne() #500
`---[0.409917ms] cn.test.db.common.vo.ResponseVo:setSuccess() #503
6、jobs
執(zhí)行后臺異步任務(wù)
線上有些問題是偶然發(fā)生的,這時就需要使用異步任務(wù),把信息寫入文件。
使用 & 指定命令去后臺運(yùn)行,使用 > 將結(jié)果重寫到日志文件,以trace為例
trace -j cn.test.mobile.controller.order.OrderController getOrderInfo > test.out &
jobs——列出所有job
jobs
[76]*
Running trace -j cn.test.mobile.controller.order.OrderController getOrderInfo >> test.out &
execution count : 0
start time : Wed Nov 13 16:13:23 CST 2019
timeout date : Thu Nov 14 16:13:23 CST 2019
session : f4fba846-e90b-4234-959e-e78ad0a5db8c (current)
job id是76, * 表示此job是當(dāng)前session創(chuàng)建,狀態(tài)是Running,execution count是執(zhí)行次數(shù),timeout date是超時時間
異步執(zhí)行時間,默認(rèn)為1天,如果要修改,使用options命令,
options job-timeout 2d
options可選參數(shù) 1d, 2h, 3m, 25s,分別代表天、小時、分、秒
kill——強(qiáng)制終止任務(wù)
kill 76 kill job 76 success
最多同時支持8個命令使用重定向?qū)⒔Y(jié)果寫日志
請勿同時開啟過多的后臺異步命令,以免對目標(biāo)JVM性能造成影響
7、logger
查看logger信息,更新logger level
查看
logger name ROOT class ch.qos.logback.classic.Logger classLoader sun.misc.Launcher$AppClassLoader@18b4aac2 classLoaderHash 18b4aac2 #改日志級別時要用到它 level INFO effectiveLevel INFO ... ... ... ...
更新日志級別
logger --name ROOT --level debug update logger level success.
如果執(zhí)行這個命令時出錯:update logger level fail.
指定classLoaderHash重試一下試試
logger -c 18b4aac2 --name ROOT --level debug update logger level success.
8、dashboard
查看當(dāng)前系統(tǒng)的實時數(shù)據(jù)面板 這個命令可以全局的查看jvm運(yùn)行狀態(tài),比如內(nèi)存和cpu占用情況
dashboard ID NAME GROUP PRIORITY STATE %CPU TIME INTERRUPT DAEMON 17 Abandoned connection cleanup main 5 TIMED_WAI 0 0:0 false true 1009 AsyncAppender-Worker-arthas-c system 5 WAITING 0 0:0 false true 5 Attach Listener system 5 RUNNABLE 0 0:0 false true 23 ContainerBackgroundProcessor[ main 5 TIMED_WAI 0 0:0 false true 55 DestroyJavaVM main 5 RUNNABLE 0 0:11 false false 3 Finalizer system 8 WAITING 0 0:0 false true 18 HikariPool-1 housekeeper main 5 TIMED_WAI 0 0:0 false true 39 NioBlockingSelector.BlockPoll main 5 RUNNABLE 0 0:0 false true 2 Reference Handler system 10 WAITING 0 0:0 false true 4 Signal Dispatcher system 9 RUNNABLE 0 0:0 false true 69 System Clock main 5 TIMED_WAI 0 0:34 false true 25 Thread-2 main 5 TIMED_WAI 0 0:0 false false 37 Timer-0 main 5 TIMED_WAI 0 0:0 false true Memory used total max usage GC heap 216M 415M 3614M 5.99% gc.ps_scavenge.count 96 ps_eden_space 36M 78M 1276M 2.90% gc.ps_scavenge.time(ms) 3054 ps_survivor_space 17M 38M 38M 46.53% gc.ps_marksweep.count 4 ps_old_gen 161M 298M 2711M 5.97% gc.ps_marksweep.time(ms) 804 nonheap 175M 180M -1 97.09% code_cache 35M 35M 240M 14.85%
ID: Java級別的線程ID,注意這個ID不能跟jstack中的nativeID一一對應(yīng) 我們可以通過 thread id 查看線程的堆棧 信息
thread 2
"Reference Handler" Id=2 WAITING on java.lang.ref.Reference$Lock@66ad4272
at java.lang.Object.wait(Native Method)
- waiting on java.lang.ref.Reference$Lock@66ad4272
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
NAME: 線程名
GROUP: 線程組名
PRIORITY: 線程優(yōu)先級, 1~10之間的數(shù)字,越大表示優(yōu)先級越高
STATE: 線程的狀態(tài)
CPU%: 線程消耗的cpu占比,采樣100ms,將所有線程在這100ms內(nèi)的cpu使用量求和,再算出每個線程的cpu使用占比。
TIME: 線程運(yùn)行總時間,數(shù)據(jù)格式為分:秒
INTERRUPTED: 線程當(dāng)前的中斷位狀態(tài)
DAEMON: 是否是daemon線程
9、redefine
redefine jvm已加載的類 ,可以在不重啟項目的情況下,熱更新類。
這個功能真的很強(qiáng)大,但是命令不一定會成功
下面我們來模擬:假設(shè)我想修改OrderController里的某幾行代碼,然后熱更新至jvm:
a. 反編譯OrderController,默認(rèn)情況下,反編譯結(jié)果里會帶有ClassLoader信息,通過--source-only選項,可以只打印源代碼。方便和mc/redefine命令結(jié)合使用
jad --source-only cn.test.mobile.controller.order.OrderController > OrderController.java
生成的OrderController.java在哪呢,執(zhí)行pwd就知道在哪個目錄了
b. 查找加載OrderController的ClassLoader
sc -d cn.test.mobile.controller.order.OrderController | grep classLoaderHash classLoaderHash 18b4aac2
c. 修改保存好OrderController.java之后,使用mc(Memory Compiler)命令來編譯成字節(jié)碼,并且通過-c參數(shù)指定ClassLoader
mc -c 18b4aac2 OrderController.java -d ./
d. 熱更新剛才修改后的代碼
redefine -c 18b4aac2 OrderController.class redefine success, size: 1
然后代碼就更新成功了。
其他
如果java -jar選擇啟動某個應(yīng)用的時候,報下面的錯
java -jar arthas-boot.jar [INFO] arthas-boot version: 3.1.4 [INFO] Process 11544 already using port 3658 [INFO] Process 11544 already using port 8563 [INFO] Found existing java process, please choose one and hit RETURN. * [1]: 11544 [2]: 119504 cn.test.MobileApplication [3]: 136340 org.jetbrains.jps.cmdline.Launcher [4]: 3068 2 #選擇第2個啟動 [ERROR] Target process 119504 is not the process using port 3658, you will connect to an unexpected process. [ERROR] 1. Try to restart arthas-boot, select process 11544, shutdown it first with running the 'shutdown' command. [ERROR] 2. Or try to use different telnet port, for example: java -jar arthas-boot.jar --telnet-port 9998 --http-port -1
注意提示[ERROR] 1,只需要進(jìn)入11544這個應(yīng)用,然后執(zhí)行shutdown關(guān)閉這個應(yīng)用就可以啟動了
Github地址:github.com/Tyson0314/j…
以上就是阿里開源Java診斷工具神器使用及場景詳解的詳細(xì)內(nèi)容,更多關(guān)于阿里開源Java診斷工具的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
spring整合redis實現(xiàn)數(shù)據(jù)緩存的實例代碼
這篇文章主要介紹了spring整合redis實現(xiàn)數(shù)據(jù)緩存,需要的朋友可以參考下2018-09-09
sql于navicat中能運(yùn)行在mybatis中不能運(yùn)行的解決方案
這篇文章主要介紹了sql于navicat中能運(yùn)行在mybatis中不能運(yùn)行的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
在Java項目中實現(xiàn)CI/CD持續(xù)集成與持續(xù)部署
這篇文章主要為大家介紹了在Java項目中實現(xiàn)CI/CD持續(xù)集成與持續(xù)部署詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Intellij IDEA實現(xiàn)SpringBoot項目多端口啟動的兩種方法
有時候使用springboot項目時遇到這樣一種情況,用一個項目需要復(fù)制很多遍進(jìn)行測試,除了端口號不同以外,沒有任何不同。遇到這種情況怎么辦呢?這時候可以使用Intellij IDEA解決2018-06-06
Spring Boot Actuator端點(diǎn)相關(guān)原理解析
這篇文章主要介紹了Spring Boot Actuator端點(diǎn)相關(guān)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-07-07

