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%