1.Arthas 能为你做什么?
Arthas是Alibaba开源的Java诊断工具,深受开发者喜爱。
当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:
这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
是否有一个全局视角来查看系统的运行状况?
有什么办法可以监控到JVM的实时运行状态?
Arthas支持JDK 6+,支持Linux/Mac/Winodws,采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。
GitHub地址:https://github.com/alibaba/arthas
用户文档:https://alibaba.github.io/arthas/
2.Arthas Install
2.1使用arthas-boot
下载arthas-boot.jar,然后用java -jar的方式启动:
wget https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar
打印帮助信息
java -jar arthas-boot.jar -h
2.2使用as.sh安装
Arthas 支持在 Linux/Unix/Mac 等平台上一键安装,请复制以下内容,并粘贴到命令行中,敲 回车 执行即可:
curl -L https://alibaba.github.io/arthas/install.sh | sh
上述命令会下载启动脚本文件 as.sh 到当前目录,你可以放在任何地方或将其加入到 $PATH 中。
直接在shell下面执行./as.sh,就会进入交互界面。
也可以执行./as.sh -h来获取更多参数信息。
2.3通过Cloud Toolkit插件使用Arthas
IDEA与eclipse插件安装图文教程
Cloud Toolkit插件官方介绍
2.4卸载
在 Linux/Unix/Mac 平台
删除下面文件:
rm -rf ~/.arthas/
rm -rf ~/logs/arthas
Windows平台直接删除user home下面的.arthas和logs/arthas目录
3.命令列表
3.1基础命令
help——查看命令帮助信息
cls——清空当前屏幕区域
session——查看当前会话的信息
reset——重置增强类,将被 Arthas 增强过的类全部还原,Arthas 服务端关闭时会重置所有增强过的类
version——输出当前目标 Java 进程所加载的 Arthas 版本号
history——打印命令历史
quit——退出当前 Arthas 客户端,其他 Arthas 客户端不受影响
shutdown——关闭 Arthas 服务端,所有 Arthas 客户端全部退出
keymap——Arthas快捷键列表及自定义快捷键
3.2Jvm相关
dashboard——当前系统的实时数据面板
thread——查看当前 JVM 的线程堆栈信息
jvm——查看当前 JVM 的信息
sysprop——查看和修改JVM的系统属性
sysenv——查看JVM的环境变量
getstatic——查看类的静态属性
ognl——执行ognl表达式
mbean——查看 Mbean 的信息
3.3class/classloader相关
sc——查看JVM已加载的类信息
sm——查看已加载类的方法信息
jad——反编译指定已加载类的源码
mc——内存编绎器,内存编绎.java文件为.class文件
redefine——加载外部的.class文件,redefine到JVM里
dump——dump 已加载类的 byte code 到特定目录
classloader——查看classloader的继承树,urls,类加载信息,使用classloader去getResource
3.4monitor/watch/trace相关
请注意,这些命令,都通过字节码增强技术来实现的,会在指定类的方法中插入一些切面来实现数据统计和观测,因此在线上、预发使用时,请尽量明确需要观测的类、方法以及条件,诊断结束要执行 shutdown 或将增强过的类执行 reset 命令。
monitor——方法执行监控
watch——方法执行数据观测
trace——方法内部调用路径,并输出方法路径上的每个节点上耗时
stack——输出当前方法被调用的调用路径
tt——方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测
3.5options
options——查看或设置Arthas全局开关
4实例
4.1.启动Arthas
在命令行下面执行(使用和目标进程一致的用户启动,否则可能attach失败):
wget https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar
执行该程序的用户需要和目标进程具有相同的权限。比如以admin用户来执行:sudo su admin && java -jar arthas-boot.jar 或 sudo -u admin -EH java -jar arthas-boot.jar。
如果attach不上目标进程,可以查看~/logs/arthas/ 目录下的日志。
如果下载速度比较慢,可以使用aliyun的镜像:java -jar arthas-boot.jar --repo-mirror aliyun --use-http
java -jar arthas-boot.jar -h 打印更多参数信息。
选择应用java进程:
[[email protected] gidata]# java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.1.1
[INFO] Process 54355 already using port 3658
[INFO] Process 54355 already using port 8563
[INFO] Found existing java process, please choose one and hit RETURN.
[1]: 54355 /home/xxx.jar
[2]: 417757 /usr/lib/jenkins/jenkins.war
xxx进程是第1个,则输入1,再输入回车/enter。Arthas会attach到目标进程上,并输出日志:
[INFO] arthas home: /root/.arthas/lib/3.1.1/arthas
[INFO] The target process already listen port 3658, skip attach.
[INFO] arthas-client connect 127.0.0.1 3658
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki https://alibaba.github.io/arthas
tutorials https://alibaba.github.io/arthas/arthas-tutorials
version 3.1.1
pid 54355
time 2019-07-29 11:13:34
4.2查看dashboard
$ dashboard
ID NAME GROUP PRIORITY STATE %CPU TIME INTERRUPTED DAEMON
152 Timer-for-arthas-dashboard-838ac88e-342e-4e52-83ba-b07c3cf5 system 10 RUNNABLE 57 0:0 false true
64 SimplePauseDetectorThread_0 main 5 TIMED_WAITING 12 15:30 false true
66 SimplePauseDetectorThread_2 main 5 TIMED_WAITING 11 15:44 false true
65 SimplePauseDetectorThread_1 main 5 TIMED_WAITING 9 15:32 false true
57 http-nio-9015-ClientPoller-0 main 5 RUNNABLE 2 0:10 false true
143 nioEventLoopGroup-2-1 system 10 RUNNABLE 2 0:0 false false
80 DataPublisher main 5 TIMED_WAITING 1 0:13 false true
35 SimplePauseDetectorThread_0 system 9 TIMED_WAITING 1 1:35 false true
33 Abandoned connection cleanup thread main 5 TIMED_WAITING 0 0:6 false true
140 AsyncAppender-Worker-arthas-cache.result.AsyncAppender system 9 WAITING 0 0:0 false true
41 AsyncResolver-bootstrap-0 main 5 TIMED_WAITING 0 0:0 false true
88 AsyncResolver-bootstrap-executor-0 main 5 WAITING 0 0:0 false true
138 Attach Listener system 9 RUNNABLE 0 0:0 false true
31 Catalina-utility-1 main 1 TIMED_WAITING 0 0:17 false false
87 DestroyJavaVM main 5 RUNNABLE 0 0:31 false false
42 DiscoveryClient-0 main 5 TIMED_WAITING 0 0:1 false true
43 DiscoveryClient-1 main 5 WAITING 0 0:1 false true
83 DiscoveryClient-CacheRefreshExecutor-0 main 5 WAITING 0 0:11 false true
84 DiscoveryClient-HeartbeatExecutor-0 main 5 WAITING 0 0:10 false true
44 DiscoveryClient-InstanceInfoReplicator-0 main 5 TIMED_WAITING 0 0:0 false true
36 Druid-ConnectionPool-Create-438123546 main 5 WAITING 0 0:0 false true
38 Druid-ConnectionPool-Create-609389093 main 5 WAITING 0 0:0 false true
37 Druid-ConnectionPool-Destroy-438123546 main 5 TIMED_WAITING 0 0:0 false true
39 Druid-ConnectionPool-Destroy-609389093 main 5 TIMED_WAITING 0 0:0 false true
40 Eureka-JerseyClient-Conn-Cleaner2 main 5 TIMED_WAITING 0 0:0 false true
3 Finalizer system 8 WAITING 0 0:0 false true
Memory used total max usage GC
heap 566M 1057M 7033M 8.06% gc.ps_scavenge.count 889
ps_eden_space 323M 602M 2623M 12.32% gc.ps_scavenge.time(ms) 15532
ps_survivor_space 6M 7M 7M 99.88% gc.ps_marksweep.count 4
ps_old_gen 236M 448M 5275M 4.48% gc.ps_marksweep.time(ms) 637
nonheap 196M 203M -1 96.52%
code_cache 63M 64M 240M 26.37%
metaspace 118M 124M -1 95.62%
compressed_class_space 14M 15M 1024M 1.43%
direct 88K 88K - 100.00%
mapped 0K 0K - NaN%
Runtime
os.name Linux
os.version 3.10.0-957.21.3.el7.x86_64
java.version 1.8.0_212
java.home /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.212.b04-0.el7_6.x86_64/jre
systemload.average 0.30
processors 32
uptime 246111s
4.3. 使用命令反编译class文件
$ jad com.gbcom.gidata.alarm.entity.primary.ActiveAlarm
ClassLoader:
[email protected]e
[email protected]
[email protected]
Location:
file:/home/gidata/GiDataServer/GiDataServer.jar!/BOOT-INF/classes!/
/*
* Decompiled with CFR 0_132.
*
* Could not load the following classes:
* javax.persistence.Entity
* javax.persistence.GeneratedValue
* javax.persistence.GenerationType
* javax.persistence.Id
* javax.persistence.Table
*/
package com.gbcom.gidata.alarm.entity.primary;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="alarm_active")
public class ActiveAlarm {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String alarmName;
private String sno;
private String name;
private Byte gender;
private String departmentName;
private String professionName;
private String className;
private Date reportTime;
private String idNo;
private String phone;
private String phoneMac;
private String dorm;
private int admissionYear;
private String politic;
private int status;
private String reportDate;
private String operationLog;
public void setClassName(String className) {
this.className = className;
}
public void setId(int id) {
this.id = id;
}
public String getIdNo() {
return this.idNo;
}
public String getPhone() {
return this.phone;
}
public String getPhoneMac() {
return this.phoneMac;
}
public void setIdNo(String idNo) {
this.idNo = idNo;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setPhoneMac(String phoneMac) {
this.phoneMac = phoneMac;
}
public void setProfessionName(String professionName) {
this.professionName = professionName;
}
public String getProfessionName() {
return this.professionName;
}
public String getDepartmentName() {
return this.departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public void setGender(Byte gender) {
this.gender = gender;
}
public String getOperationLog() {
return this.operationLog;
}
public void setStatus(int status) {
this.status = status;
}
public void setOperationLog(String operationLog) {
this.operationLog = operationLog;
}
public void setReportDate(String reportDate) {
this.reportDate = reportDate;
}
public String getAlarmName() {
return this.alarmName;
}
public String getSno() {
return this.sno;
}
public Date getReportTime() {
return this.reportTime;
}
public int getStatus() {
return this.status;
}
public String getReportDate() {
return this.reportDate;
}
public void setReportTime(Date reportTime) {
this.reportTime = reportTime;
}
public void setSno(String sno) {
this.sno = sno;
}
public Byte getGender() {
return this.gender;
}
public String getDorm() {
return this.dorm;
}
public void setDorm(String dorm) {
this.dorm = dorm;
}
public void setAdmissionYear(int admissionYear) {
this.admissionYear = admissionYear;
}
public String getPolitic() {
return this.politic;
}
public void setPolitic(String politic) {
this.politic = politic;
}
public void setAlarmName(String alarmName) {
this.alarmName = alarmName;
}
public int getAdmissionYear() {
return this.admissionYear;
}
public String toString() {
return "ActiveAlarm{id=" + this.id + ", alarmName='" + this.alarmName + '\'' + ", sno='" + this.sno + '\'' + ", name='" + this.name + '\'' + ", gender=" + this.gender + ", departmentName='" + this.departmentName + '\'' + ", professionName='" + this.professionName + '\'' + ", className='" + this.className + '\'' + ", reportTime=" + this.reportTime + ", idNo='" + this.idNo + '\'' + ", phone='" + this.phone + '\'' + ", phoneMac='" + this.phoneMac + '\'' + ", dorm='" + this.dorm + '\'' + ", admissionYear=" + this.admissionYear + ", politic='" + this.politic + '\'' + ", status=" + this.status + ", reportDate='" + this.reportDate + '\'' + ", operationLog='" + this.operationLog + '\'' + '}';
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return this.id;
}
public String getClassName() {
return this.className;
}
}
Affect(row-cnt:2) cost in 350 ms.
4.4.Watch
4.4.1监控返回值
$ watch com.gbcom.gidata.alarm.controller.ActiveAlarmController findByPage returnObj
Press Q or Ctrl+C to abort.
Affect(class-cnt:2 , method-cnt:2) cost in 361 ms.
ts=2019-07-29 14:46:08; [cost=1677.776414ms] [email protected][
[email protected][200],
[email protected][操作成功],
[email protected][Page 1 of 48 containing com.gbcom.gidata.alarm.entity.primary.ActiveAlarm instances],
]
ts=2019-07-29 14:46:08; [cost=1848.329463ms] [email protected][
[email protected][200],
[email protected][操作成功],
[email protected][Page 1 of 48 containing com.gbcom.gidata.alarm.entity.primary.ActiveAlarm instances],
]
4.4.2监控参数
监控所有参数
$ watch com.gbcom.gidata.alarm.controller.ActiveAlarmController findByPage params
Press Q or Ctrl+C to abort.
Affect(class-cnt:2 , method-cnt:2) cost in 231 ms.
ts=2019-07-29 14:47:28; [cost=42.880745ms] [email protected][][
@AlarmDto[AlarmDto [pageNumber=1, pageSize=10, date=null, alamName=null, num=null, idNo=null, phone=null, phoneMac=null]],
]
ts=2019-07-29 14:47:28; [cost=50.596983ms] [email protected][][
@AlarmDto[AlarmDto [pageNumber=1, pageSize=10, date=null, alamName=null, num=null, idNo=null, phone=null, phoneMac=null]],
]
监控指定参数的某一个值
$ watch com.gbcom.gidata.alarm.controller.ActiveAlarmController findByPage params[0].pageSize
Press Q or Ctrl+C to abort.
Affect(class-cnt:2 , method-cnt:2) cost in 214 ms.
ts=2019-07-29 14:48:40; [cost=28.2795ms] [email protected][10]
ts=2019-07-29 14:48:40; [cost=77.127107ms] [email protected][10]
4.4.3监控异常
$ watch com.gbcom.gidata.alarm.controller.ActiveAlarmController findByPage throwExp
Press Q or Ctrl+C to abort.
Affect(class-cnt:2 , method-cnt:2) cost in 167 ms.
ts=2019-07-29 14:51:21; [cost=13.121286ms] result=null
ts=2019-07-29 14:51:21; [cost=18.437713ms] result=null
result=null无异常
4.5退出arthas
如果只是退出当前的连接,可以用quit或者exit命令。Attach到目标进程上的arthas还会继续运行,端口会保持开放,下次连接时可以直接连接上。
如果想完全退出arthas,可以执行shutdown命令。
5日志
5.1执行结果存日志
将命令的结果完整保存在日志文件中,便于后续进行分析
默认情况下,该功能是关闭的,如果需要开启,请执行以下命令:
$ options save-result true
NAME BEFORE-VALUE AFTER-VALUE
----------------------------------------
save-result false true
Affect(row-cnt:1) cost in 3 ms.
看到上面的输出,即表示成功开启该功能;
日志文件路径
结果会异步保存在:{user.home}/logs/arthas-cache/result.log,请定期进行清理,以免占据磁盘空间
5.2使用新版本Arthas的异步后台任务将结果存日志文件
$ trace Test t >> &
job id : 2
cache location : /Users/admin/logs/arthas-cache/28198/2
此时命令会在后台异步执行,并将结果异步保存在文件(~/logs/arthas-cache/ P I D / {PID}/ PID/{JobId})中;
此时任务的执行不受session断开的影响;任务默认超时时间是1天,可以通过全局 options 命令修改默认超时时间;
此命令的结果将异步输出到文件中;此时不管 save-result 是否为true,都不会再往~/logs/arthas-cache/result.log 中异步写结果