arthas的monitor 、watch、trace命令,都是通過字節碼增強技術來實現的,會在指定類的方法中插入一些切片來實現數據統計和觀測,因此在線上、預先使用時,請儘量明確需要觀測的類、方法以及條件,診斷結束要執行stop
或者將增強過的類執行reset
命令;
1 watch命令使用
[arthas@44607]$ watch --help
##### 1-使用方法
USAGE:
watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class-pattern method-p
attern express [condition-express]
SUMMARY:
Display the input/output parameter, return object, and thrown exception of specified method invoc
ation
The express may be one of the following expression (evaluated dynamically):
target : the object
clazz : the object's class
method : the constructor or method
params : the parameters array of method
params[0..n] : the element of parameters array
returnObj : the returned object of method
throwExp : the throw exception of method
isReturn : the method ended by return
isThrow : the method ended by throwing exception
#cost : the execution time in ms of method invocation
Examples:
watch -b org.apache.commons.lang.StringUtils isBlank params
watch -f org.apache.commons.lang.StringUtils isBlank returnObj
watch org.apache.commons.lang.StringUtils isBlank '{params, target, returnObj}' -x 2
watch -bf *StringUtils isBlank params
watch *StringUtils isBlank params[0]
watch *StringUtils isBlank params[0] params[0].length==1
watch *StringUtils isBlank params '#cost>100'
watch -E -b org\.apache\.commons\.lang\.StringUtils isBlank params[0]
WIKI:
https://alibaba.github.io/arthas/watch
OPTIONS:
-b, --before Watch before invocation
-e, --exception Watch after throw exception
-x, --expand <value> Expand level of object (1 by default)
-f, --finish Watch after invocation, enable by default
-h, --help this help
-n, --limits <value> Threshold of execution times
-E, --regex Enable regular expression to match (wildcard matching by default)
-M, --sizeLimit <value> Upper size limit in bytes for the result (10 * 1024 * 1024 by def
ault)
-s, --success Watch after successful invocation
<class-pattern> The full qualified class name you want to watch
<method-pattern> The method name you want to watch
#####添加表達式
<express> the content you want to watch, written by ognl.
Examples:
params
params[0]
'params[0]+params[1]'
'{params[0], target, returnObj}'
returnObj
throwExp
target
clazz
method
###### 添加條件表達式
<condition-express> Conditional expression in ognl style, for example:
TRUE : 1==1
TRUE : true
FALSE : false
TRUE : 'params.length>=0'
FALSE : 1==2
1.1 觀察函數入參和返回值
####1 入參和返回值檢查觀察 -x表示遍歷深度,可以調整來打印具體的參數和結果內容,默認值是1。
watch demo.MathGame primeFactors {params,returnObj} -x 2
####2 入參和返回值檢查觀察添加“條件”--條件觀察(最後一個引號後添加表達式)
watch demo.MathGame primeFactors {params,returnObj} -x 2 "params[0]<100"
####3 計算表達式--如圖是一個將參數0、參數0+2以及耗時一起打印的形式###
[arthas@44607]$ watch demo.MathGame primeFactors {params[0],params[0]+2,#cost}
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 28 ms.
ts=2020-02-16 12:23:11; [cost=0.367123ms] result=@ArrayList[
@Integer[-154670],
@Integer[-154668],
@Double[0.367123],
]
1.2 觀察異常
watch demo.MathGame primeFactors "{params[0],throwExp}" -e -x 2
1.3 耗時過濾
####cost>200(單位是ms)表示只有當耗時大於200ms時纔會輸出,過濾掉執行時間小於200ms的調用
####cost>0.2(單位是ms)表示只有當耗時大於0.2ms時纔會輸出
arthas@44607]$ watch demo.MathGame primeFactors '{params, returnObj}' '#cost>0.2' -x 2
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 32 ms.
ts=2020-02-16 12:27:50; [cost=0.49974ms] result=@ArrayList[
@Object[][
@Integer[105631],
],
@ArrayList[
@Integer[73],
@Integer[1447],
],
]
1.4 觀察當前對象中的屬性
#### tartet輸出當前類的所有屬性
[arthas@44607]$ watch demo.MathGame primeFactors target
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 34 ms.
ts=2020-02-16 12:29:03; [cost=0.227132ms] result=@MathGame[
random=@Random[java.util.Random@67424e82],
illegalArgumentCount=@Integer[669],
]
#### target.illegalArgumentCount輸出指定屬性
[arthas@44607]$ watch demo.MathGame primeFactors target.illegalArgumentCount
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 38 ms.
ts=2020-02-16 12:30:36; [cost=0.368721ms] result=@Integer[721]
ts=2020-02-16 12:30:37; [cost=0.111554ms] result=@Integer[722]
ts=2020-02-16 12:30:38; [cost=0.108022ms] result=@Integer[723]
2 trace使用
trace命令可以追蹤方法內部調用路徑,並輸出方法路徑上的每個節點上耗時
;
trace
命令能主動搜索 class-pattern
/method-pattern
對應的方法調用路徑,渲染和統計整個調用鏈路上的所有性能開銷和追蹤調用鏈路。
trace命令只會trace匹配到的函數裏的子調用,並不會向下trace多層。因爲trace是代價比較貴的,多層trace可能會導致最終要trace的類和函數非常多。
[arthas@44607]$ trace --help
USAGE:
trace [-h] [-n <value>] [-p <value>] [-E] [--skipJDKMethod <value>] class-pattern method-pattern
[condition-express]
SUMMARY:
Trace the execution time of specified method invocation.
The express may be one of the following expression (evaluated dynamically):
target : the object
clazz : the object's class
method : the constructor or method
params : the parameters array of method
params[0..n] : the element of parameters array
returnObj : the returned object of method
throwExp : the throw exception of method
isReturn : the method ended by return
isThrow : the method ended by throwing exception
#cost : the execution time in ms of method invocation
EXAMPLES:
trace org.apache.commons.lang.StringUtils isBlank
trace *StringUtils isBlank
trace *StringUtils isBlank params[0].length==1
trace *StringUtils isBlank '#cost>100'
trace -E org\\.apache\\.commons\\.lang\\.StringUtils isBlank
trace -E com.test.ClassA|org.test.ClassB method1|method2|method3
trace demo.MathGame run -n 5
trace demo.MathGame run --skipJDKMethod false
WIKI:
https://alibaba.github.io/arthas/trace
OPTIONS:
-h, --help this help
-n, --limits <value> Threshold of execution times
-p, --path <value> path tracing pattern
-E, --regex Enable regular expression to match (wildcard matching by default)
--skipJDKMethod <value> skip jdk method trace, default value true.
<class-pattern> Class name pattern, use either '.' or '/' as separator
<method-pattern> Method name pattern
<condition-express> Conditional expression in ognl style, for example:
TRUE : 1==1
TRUE : true
FALSE : false
TRUE : 'params.length>=0'
FALSE : 1==2
2.1 trace函數
#### 1 簡單調用一次 trace 類表達式 函數表達式
[arthas@44607]$ trace demo.MathGame run -n 1
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 99 ms.
`---ts=2020-02-16 12:54:58;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@55f96302
`---[0.393604ms] demo.MathGame:run()
+---[0.123348ms] demo.MathGame:primeFactors() #24
`---[0.097423ms] demo.MathGame:print() #25
##### 2 使用表達式的方式
[arthas@44607]$ trace *.MathGame ru* -n 1
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 139 ms.
`---ts=2020-02-16 12:55:47;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@55f96302
`---[0.332643ms] demo.MathGame:run()
`---[0.071678ms] demo.MathGame:primeFactors() #24 [throws Exception]
#### 3 通過--skipJDKMethod false包含jdk方法打印,通常情況下默認是不開啓的
[arthas@44607]$ trace --skipJDKMethod false *.MathGame ru* -n 1
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 142 ms.
`---ts=2020-02-16 12:59:14;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@55f96302
`---[0.245148ms] demo.MathGame:run()
+---[0.013229ms] java.util.Random:nextInt() #23
+---[0.031026ms] demo.MathGame:primeFactors() #24
`---[0.096696ms] demo.MathGame:print() #25
2.2 trace根據調用耗時過濾
####1 過濾耗時大於1的🆚
[arthas@44607]$ trace demo.MathGame run '#cost>1'
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 146 ms.
`---ts=2020-02-16 13:01:19;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@55f96302
`---[2.222184ms] demo.MathGame:run()
+---[2.12052ms] demo.MathGame:primeFactors() #24
`---[0.06258ms] demo.MathGame:print() #25
#### 2 過濾入參大於2的情況
[arthas@44607]$ trace demo.MathGame primeFactors 'params[0]>2'
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 147 ms.
`---ts=2020-02-16 13:04:08;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@55f96302
`---[0.053919ms] demo.MathGame:primeFactors()
2.3 過濾多個類和函數
trace命令只會trace匹配到的函數裏的子調用,並不會向下trace多層。因爲trace是代價比較貴的,多層trace可能會導致最終要trace的類和函數非常多。
可以用正則表匹配路徑上的多個類和函數,一定程度上達到多層trace的效果。
trace -E com.test.ClassA|org.test.ClassB method1|method2|method3
3 monitor
monitor的主要功能是對方法執行監控。對匹配 class-pattern method-pattern的類和方法進行監控。
monitor命令是一個非實時返回的命令,其不會立即返回結果,而是不斷等待目標java進程返回信息,知道用戶輸入control +c爲止。
如下所示是monitor命令的幫助信息,相關解釋也附在了上方註釋:
[arthas@44607]$ monitor --help
#### 使用方式 monitor class-pattern method-pattern是基本形式,其他可選
USAGE:
monitor [-c <value>] [-h] [-n <value>] [-E <value>] class-pattern method-pattern
SUMMARY:
Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc
.
Examples:
monitor org.apache.commons.lang.StringUtils isBlank
monitor org.apache.commons.lang.StringUtils isBlank -c 5
monitor -E org\.apache\.commons\.lang\.StringUtils isBlank
WIKI:
https://alibaba.github.io/arthas/monitor
OPTIONS:
#### 循環次數--》默認60s輸入一次,修改辭值後周期變化
-c, --cycle <value> The monitor interval (in seconds), 60 seconds by default
-h, --help this help
#### 執行時間閾值---執行週期數字---執行幾個週期
-n, --limits <value> Threshold of execution times
#### 開啓正則表達式
-E, --regex <value> Enable regular expression to match (wildcard matching by default)
<class-pattern> Path and classname of Pattern Matching
<method-pattern> Method of Pattern Matching
[arthas@44607]$
其監控數據維度說明如下所示:
監控項 | 說明 |
---|---|
timestamp | 時間戳 |
class | Java類 |
method | 方法(構造方法、普通方法) |
total | 調用次數 |
success | 成功次數 |
fail | 失敗次數 |
rt | 平均RT |
fail-rate | 失敗率 |
如下所示是監控的一個樣例展示:
Command execution times exceed limit: 6, so command will exit. You can set it with -n option.
[arthas@44607]$ monitor demo.MathGame run -c 3
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 31 ms.
timestamp class method total success fail avg-rt(ms) fail-rate
-----------------------------------------------------------------------------------------
2020-02-16 16:21:05 demo.MathGame run 3 3 0 0.31 0.00%
timestamp class method total success fail avg-rt(ms) fail-rate
-----------------------------------------------------------------------------------------
2020-02-16 16:21:08 demo.MathGame run 3 3 0 0.47 0.00%
timestamp class method total success fail avg-rt(ms) fail-rate
-----------------------------------------------------------------------------------------
2020-02-16 16:21:11 demo.MathGame run 3 3 0 1.27 0.00%