Arthas快速开始

在这里插入图片描述

下载
curl -O https://arthas.aliyun.com/arthas-boot.jar
启动
1,执行java -jar arthas-boot.jar
2,选择应用Java进程序号
基础指令
reset: 重置增强类,将被 Arthas 增强过的类全部还原,Arthas 服务端关闭时会重置所有增强过的类
quit: 退出当前 Arthas 客户端,其他 Arthas 客户端不受影响
stop: 关闭 Arthas 服务端,所有 Arthas 客户端全部退出
dashboard

查看当前系统实时数据,可以看到Thread、 Memory、GC和Runtime四部分数据

参数
[-i 1000]: 数据刷新间隔(ms),默认5000
[-n 3]: 数据刷新次数

数据
ID: Java级别的线程ID,注意这个ID不能跟jstack中的nativeID一一对应
NAME: 线程名
GROUP: 线程组名
PRIORITY: 线程优先级, 1~10之间的数字,越大表示优先级越高
STATE: 线程的状态
CPU%: 线程的cpu使用率。比如采样间隔1000ms,某个线程的增量cpu时间为100ms,则cpu使用率=100/1000=10%
DELTA_TIME: 上次采样之后线程运行增量CPU时间,数据格式为秒
TIME: 线程运行总CPU时间,数据格式为分:秒
INTERRUPTED: 线程当前的中断位状态
DAEMON: 是否是daemon线程

使用
dashboard -i 10000 -n 5
thread

查看线程信息、线程堆栈信息

参数
[id]: 线程ID
[-n 3]: 指定最忙的前N个线程并打印堆栈
[-b]: 找出当前阻塞其他线程的线程
[-i 2000]: 指定cpu使用率统计的采样间隔(ms),默认200
[-all]: 显示所有匹配线程
[-state WAITING]: 查看指定状态的线程

使用
thread 1
thread -n 3 -i 1000
thread -b
jvm

查看当前JVM信息

数据
COUNT: JVM当前活跃的线程数
DAEMON-COUNT: JVM当前活跃的守护线程数
PEAK-COUNT: 从JVM启动开始曾经活着的最大线程数
STARTED-COUNT: 从JVM启动开始总共启动过的线程次数
DEADLOCK-COUNT: JVM当前死锁的线程数
MAX-FILE-DESCRIPTOR-COUNT:JVM进程最大可以打开的文件描述符数
OPEN-FILE-DESCRIPTOR-COUNT:JVM当前打开的文件描述符数
heapdump

类似jmap命令的heap dump功能

使用
heapdump /tmp/dump.hprof
其他指令
sysprop: 查看当前JVM的系统属性
sysenv: 查看当前JVM的环境属性
vmoption: 查看或更新VM诊断相关的参数
perfcounter: 查看当前JVM的Perf Counter信息
logger: 查看logger信息或更新logger级别
mbean: 查看mbean的信息
getstatic: 查看类的静态属性
ognl: 执行ognl表达式
sc

查看JVM已加载的类信息

参数
class-pattern: 类名表达式匹配
[-d]: 输出当前类的详细信息,包括这个类所加载的原始文件来源、类的声明、加载的ClassLoader等详细信息。如果一个类被多个ClassLoader所加载,则会出现多次
[-f]: 输出当前类的成员变量信息(需要配合参数-d一起使用)
[-x 0]: 指定输出静态变量时属性的遍历深度,默认为0,即直接使用toString输出

使用
sc com.liu.demo.DemoService -d -f
sm

查看JVM已加载类的方法信息

参数
class-pattern: 类名表达式匹配
[-d]: 展示每个方法的详细信息

使用
sm com.liu.demo.DemoService -d
jad

反编译指定已加载类的源码

参数
class-pattern: 类名表达式匹配
[method-pattern]: 指定函数
[-source-only]: 只显示源码

使用
jad com.liu.demo.DemoService main -source-only
其他指令
mc: 内存编译器,内存编译.java文件为.class文件
redefine: 加载外部的.class文件,redefine到JVM里
dump: dump已加载类的byte code到特定目录
classloader: 查看classloader的继承树,urls,类加载信息,使用classloader去getResource
monitor

方法执行监控

数据
timestamp: 时间戳
class: Java类
method: 方法(构造方法、普通方法)
total: 调用次数
success: 成功次数
fail: 失败次数
rt: 平均RT
fail-rate: 失败率

参数
class-pattern: 类名表达式匹配
method-pattern: 方法名表达式匹配
[condition-express]: 条件表达式
[-c 5]: 统计周期(s),默认120
[-b]: 在方法调用之前计算condition-express

使用
monitor -c 5 com.liu.demo.DemoService hello
watch

方法执行数据观测,观察范围有返回值、抛出异常、入参等

参数
class-pattern: 类名表达式匹配
method-pattern: 方法名表达式匹配
express: 观察表达式
[condition-express]: 条件表达式
[-b]: 在方法调用之前观察
[-e]: 在方法异常之后观察    
[-s]: 在方法返回之后观察
[-f]: 在方法结束之后(正常返回和异常返回)观察
[-x 1]: 指定输出结果的属性遍历深度,默认为 1

补充
观察表达式: 由ognl表达式组成,如: "{params,returnObj}"
特殊用法: https://github.com/alibaba/arthas/issues/71
ognl: https://commons.apache.org/proper/commons-ognl/language-guide.html

使用
watch com.liu.demo.DemoService hello "{params,returnObj}" -x 2
watch com.liu.demo.DemoService hello "{params,returnObj}" -x 2 -b 观察方法入参
watch com.liu.demo.DemoService hello "{params,returnObj}" -x 2 -b -s -n 2 同时观察方法调用前和后([-n 2]表示执行次数)
watch com.liu.demo.DemoService hello "{params,returnObj}" "params[0]<0" 条件表达式,只有满足条件的调用才会有响应
watch com.liu.demo.DemoService hello "{throwExp}" -e 观察异常信息
watch com.liu.demo.DemoService hello "{params,returnObj}" "#cost>200" -x 2 按照耗时过滤
watch com.liu.demo.DemoService hello "{target}" -x 2 观察当前对象中的属性
watch com.liu.demo.DemoService hello "{target.field_name}" 观察当前对象中的某个属性
trace

方法内部调用路径,并输出方法路径上的每个节点上耗时

参数
class-pattern: 类名表达式匹配
method-pattern: 方法名表达式匹配
[condition-express]: 条件表达式
[-n 2]: 命令执行次数
[-skipJDKMethod false]: 是否包含JDK里的函数,默认true不包含

补充
3.3.0后支持动态trace

使用
trace com.liu.demo.DemoService hello
trace com.liu.demo.DemoService hello "#cost>100" 按耗时过滤
trace com.liu.demo.DemoService hello -n 2 捕获2次结果后退出指令
stack

输出当前方法被调用的调用路径

参数
class-pattern: 类名表达式匹配
method-pattern: 方法名表达式匹配
[condition-express]: 条件表达式
[-n 2]: 命令执行次数

使用
stack com.liu.demo.DemoService hello
tt

方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测

数据
INDEX: 时间片段记录编号,每一个编号代表着一次调用,后续tt还有很多命令都是基于此编号指定记录操作,非常重要。
TIMESTAMP: 方法执行的本机时间,记录了这个时间片段所发生的本机时间
COST(ms): 方法执行的耗时
IS-RET: 方法是否以正常返回的形式结束
IS-EXP: 方法是否以抛异常的形式结束
OBJECT: 执行对象的hashCode(),注意,曾经有人误认为是对象在JVM中的内存地址,但很遗憾他不是。但他能帮助你简单的标记当前执行方法的类实体
CLASS: 执行的类名
METHOD: 执行的方法名

参数
-t: 记录方法的每次执行情况
[-n 3]: 记录次数

解决方法重载
tt -t com.liu.demo.DemoService hello params.length==1
tt -t com.liu.demo.DemoService hello 'params[1] instanceof Integer'

解决指定参数
tt -t com.liu.demo.DemoService hello params[0].mobile=="1200000000"

检索调用记录
tt -l
tt -s 'method.name=="hello"'

查看调用信息
tt -i 1001

重做一次调用
tt -i 1001 -p

使用
tt -t com.liu.demo.DemoService hello
profiler

使用async-profiler生成火焰图

参数
action: 要执行的操作
[-i 20000000]: 采样间隔(ns),默认值10'000'000,即10ms
[-f /tmp/output.svg]: 将输出转储到指定路径
[-d 10]: 运行评测指定秒
[-e cpu]: 要跟踪哪个事件cpu、alloc、lock、cache-misses等,默认是cpu

指令
profiler getSamples: 已采集数量
profiler status: 采集状态
profiler list: 支持的采集事件
profiler actions: 支持的action
profiler version: 查看版本

使用
profiler start
profiler start --event alloc
profiler start --framebuf 5000000
profiler start --include 'java/*' --include 'demo/*' --exclude '*Unsafe.park*'
profiler start --duration 300
profiler start --file /tmp/test.jfr
profiler resume
profiler stop
profiler stop --file /tmp/output.svg
profiler stop --format html
profiler execute 'start,framebuf=5000000'
profiler execute 'stop,file=/tmp/result.svg'
其他指令
options: 查看或设置Arthas全局开关
grep: 类似传统的grep命令
异步任务

解决偶现问题

使用 > 将结果重写向到日志文件,使用 & 指定命令是后台运行
session断开不影响任务执行(生命周期默认为1天)

jobs: 列出所有job
kill: 强制终止任务
fg: 将暂停的任务拉到前台执行
bg: 将暂停的任务放到后台执行
连接

首页:https://arthas.aliyun.com/zh-cn/
用户文档:https://arthas.aliyun.com/doc/index.html
GitHub:https://github.com/alibaba/arthas

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值