导图社区 Arthas常用命令与技巧总结
Arthas 是一款线上监控诊断产品,通过全局视角实时查看应用 load、内存、gc、线程的状态信息,并能在不修改应用代码的情况下,对业务问题进行诊断,包括查看方法调用的出入参、异常,监测方法执行耗时,类加载信息等,大大提升线上问题排查效率。
编辑于2022-09-08 08:59:47 浙江省Arthas
Arthas command plugin
使用arthas的idea插件来生成监控命令
ognl
Invoke Static Method Field
Set Static Field
静态方法,静态字段通过ognl查询反射设置
Get Selected Spring Property
Get All Spring Property
通过Spring context获取环境变量安装优先级打印
watch
查看异常日志,入参,返回值
trace
trace
Trace Multiple Class Method Trace -E
查看调用链(是否调用方法,耗时性能排查
stack
获取方法在哪里执行了调用栈,了解调用流程
monitor
方法监控(性能问题排查,一段时间内的性能指标)
TimeTunnel Tt
方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同时间下调用进行观测(可以重新触发,周期触发,唯一的缺点对于threadLocal信息丢失(隐含参数),引用对象数据变更无效)
spring context invoke
static Spring Context Invoke Method Filed
Watch Spring Context Invoke Method Field
Time Tunnel Context Invoke Method Field
三种方法根据不同地方获取Spring context进行Beanf方法字段的调用
other
Search Classes Loaded By JVM Sc
ognl需要获取classload
Decompile Class Jad
反编译方法 类的源码
Dump Class Byte Array from JVM
Dump 当前class 字节码
Heap Dump
heapdump /tmp/dump.hprof 打印堆栈信息
Top Thread Stack
打印当前top前五线程堆栈
special link
特殊用法的链接发辫查找
install
linux: curl -sk https://arthas.aliyun.com/arthas-boot.jar -o ~/.arthas-boot.jar && echo "alias as.sh='java -jar ~/.arthas-boot.jar --repo-mirror aliyun --use-http 2>&1'" >> ~/.bashrc && source ~/.bashrc && echo "source ~/.bashrc" >> ~/.bash_profile && source ~/.bash_profile
常用命令
基础命令
help——查看命令帮助信息
cls——清空当前屏幕区域
session——查看当前会话的信息
reset——重置增强类,将被 Arthas 增强过的类全部还原,Arthas 服务端关闭时会重置所有增强过的类
version——输出当前目标 Java 进程所加载的 Arthas 版本号
history——打印命令历史
keymap——Arthas快捷键列表及自定义快捷键
退出
stop/shutdown——关闭 Arthas 服务端, 所有 Arthas 客户端全部退出
quit/exit——退出当前 Arthas 客户端, 其他 Arthas 客户端不受影响
jvm相关
dashboard——当前系统的实时数据面板
thread——查看当前 JVM 的线程堆栈信息
jvm——查看当前 JVM 的信息
sysprop——查看和修改JVM的系统属性
sysenv——查看JVM的环境变量
getstatic——查看类的静态属性
New! ognl——执行ognl表达式
class相关
sc——查看JVM已加载的类信息
sc *.CacheController
sm——查看已加载类的方法信息
jad——反编译指定已加载类的源码
jad com.hujie.controller.CacheController
mc——内存编绎器,内存编绎.java文件为.class文件
redefine——加载外部的文件,redefine到JVM里
-- 找到类加载器 的hash sc -d *SysTemplateController| grep classLoaderHash -- 编译,填上一步的hash mc -c 1d56ce6a /root/SysTemplateController.java -d /root 将上一步的输出结果进行加载 redefine /root/com/kuka/business/controller/SysTemplateController.class
dump——dump 已加载类的 byte code 到特定目录
classloader——查看classloader的继承树, urls,类加载信息,使用classloader去getResource
monitor/ watch/trace相关
请注意,这些命令,都通过字节码增强技术来实现的,会在指定类的方法中插入一些切面来实现数据统计和观测,因此在线上、预发使用时,请尽量明确需要观测的类、方法以及条件,诊断结束要执行或将增强过的类执行 命令。
monitor——方法执行监控
watch——方法执行数据观测
trace——方法内部调用路径, 并输出方法路径上的每个节点上耗
stack——输出当前方法被调用的调用路径
tt——方法执行数据的时空隧道,记录下指定方法每次 调用的入参和返回信息,并能对这些不同的时间下调用进行观测
使用场景
1.查看目标服务器应用程序的jvm信息
jvm
2.方法性能的排查和跟踪
如果我们在实际使用过程中发现某个接口很耗时,但是无法在本地环境复现的时候我们可以通过arthas的trace来跟踪,他会输出方法内部路径每个节点的耗时。 使用demo: trace 类路径 类中的接口方法 trace cn.vv.oa.module.workflow.controller.WorkflowApplyController getList
接口偶尔很慢排查
后台任务,打印耗时超过300ms,打印调用路径 trace com.hujie.controller.CacheController costTime -n 5 --skipJDKMethod false '#cost>1000' > test.out & 后台任务,打印耗时超过300ms,打印入参出参 watch com.hujie.controller.CacheController costTime '{params,returnObj,throwExp}' -n 5 -x 3 '#cost>1000' > test1.out &
3.tt 命令的使用重放请求
4.watch:观察到的范围为:返回值、抛出异常、入参
5.stack 跟踪方法的堆栈信息
stack pdai.tech.servlet.TestMyServlet testMethod
6.修改代码后没有执行,查看是否发布成功
反编译 jad cn.vv.oa.module.workflow.controller.WorkflowApplyController getList (指定方法)
7.线程信息
thread -n 3 # 查看最繁忙的三个线程栈信息 thread # 以直观的方式展现所有的线程情况 thread -b #找出当前阻塞其他线程的线程
8.代码热更新
-- 找到类加载器 的hash sc -d *CacheController| grep classLoaderHash -- 编译,填上一步的hash mc -c 20ad9418 /root/CacheController.java -d /root 将上一步的输出结果进行加载 redefine /root/com/hujie/controller/CacheController.class
9.确定某个类是否被系统加载
# 即可以找到需要的类全路径,如果存在的话 sc *MyServlet # 查看这个某个类所有的方法 sm pdai.tech.servlet.TestMyServlet * # 查看某个方法的信息,如果存在的话 sm pdai.tech.servlet.TestMyServlet testMethod