Alibaba Arthas快速入门 简单易懂
简介
Arthas 是一款线上监控诊断产品,通过全局视角实时查看应用 load、内存、gc、线程的状态信息,并能在不修改应用代码的情况下,对业务问题进行诊断,包括查看方法调用的出入参、异常,监测方法执行耗时,类加载信息等,大大提升线上问题排查效率。
#Arthas(阿尔萨斯)能为你做什么?
Arthas
是 Alibaba 开源的 Java 诊断工具,深受开发者喜爱。
当你遇到以下类似问题而束手无策时,Arthas
可以帮助你解决:
- 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
- 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
- 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
- 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
- 是否有一个全局视角来查看系统的运行状况?
- 有什么办法可以监控到 JVM 的实时运行状态?
- 怎么快速定位应用的热点,生成火焰图?
- 怎样直接从 JVM 内查找某个类的实例?
Arthas
支持 JDK 6+,支持 Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab
自动补全功能,进一步方便进行问题的定位和诊断。
快速入门
#1. 启动 math-game
curl -O https://arthas.aliyun.com/math-game.jar
java -jar math-game.jar
math-game
是一个简单的程序,每隔一秒生成一个随机数,再执行质因数分解,并打印出分解结果。
math-game
源代码:查看在新窗口打开
#2. 启动 arthas
在命令行下面执行(使用和目标进程一致的用户启动,否则可能 attach 失败):
curl -O https://arthas.aliyun.com/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 进程:
$ $ java -jar arthas-boot.jar
* [1]: 35542[2]: 71560 math-game.jar
math-game
进程是第 2 个,则输入 2,再输入回车/enter
。Arthas 会 attach 到目标进程上,并输出日志:
[INFO] Try to attach process 71560
[INFO] Attach process 71560 success.
[INFO] arthas-client connect 127.0.0.1 3658,---. ,------. ,--------.,--. ,--. ,---. ,---./ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'wiki: https://arthas.aliyun.com/doc
version: 3.0.5.20181127201536
pid: 71560
time: 2018-11-28 19:16:24$
#3. 查看 dashboard
输入dashboard,按回车/enter
,会展示当前进程的信息,按ctrl+c
可以中断执行。
$ dashboard
ID NAME GROUP PRIORI STATE %CPU TIME INTERRU DAEMON
17 pool-2-thread-1 system 5 WAITIN 67 0:0 false false
27 Timer-for-arthas-dashb system 10 RUNNAB 32 0:0 false true
11 AsyncAppender-Worker-a system 9 WAITIN 0 0:0 false true
9 Attach Listener system 9 RUNNAB 0 0:0 false true
3 Finalizer system 8 WAITIN 0 0:0 false true
2 Reference Handler system 10 WAITIN 0 0:0 false true
4 Signal Dispatcher system 9 RUNNAB 0 0:0 false true
26 as-command-execute-dae system 10 TIMED_ 0 0:0 false true
13 job-timeout system 9 TIMED_ 0 0:0 false true
1 main main 5 TIMED_ 0 0:0 false false
14 nioEventLoopGroup-2-1 system 10 RUNNAB 0 0:0 false false
18 nioEventLoopGroup-2-2 system 10 RUNNAB 0 0:0 false false
23 nioEventLoopGroup-2-3 system 10 RUNNAB 0 0:0 false false
15 nioEventLoopGroup-3-1 system 10 RUNNAB 0 0:0 false false
Memory used total max usage GC
heap 32M 155M 1820M 1.77% gc.ps_scavenge.count 4
ps_eden_space 14M 65M 672M 2.21% gc.ps_scavenge.time(m 166
ps_survivor_space 4M 5M 5M s)
ps_old_gen 12M 85M 1365M 0.91% gc.ps_marksweep.count 0
nonheap 20M 23M -1 gc.ps_marksweep.time( 0
code_cache 3M 5M 240M 1.32% ms)
Runtime
os.name Mac OS X
os.version 10.13.4
java.version 1.8.0_162
java.home /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/jre
#4. 通过 thread 命令来获取到math-game
进程的 Main Class
thread 1
会打印线程 ID 1 的栈,通常是 main 函数的线程。
$ thread 1 | grep 'main('at demo.MathGame.main(MathGame.java:17)
#5. 通过 jad 来反编译 Main Class
$ jad demo.MathGameClassLoader:
+-sun.misc.Launcher$AppClassLoader@3d4eac69+-sun.misc.Launcher$ExtClassLoader@66350f69Location:
/tmp/math-game.jar/** Decompiled with CFR 0_132.*/
package demo;import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;public class MathGame {private static Random random = new Random();private int illegalArgumentCount = 0;public static void main(String[] args) throws InterruptedException {MathGame game = new MathGame();do {game.run();TimeUnit.SECONDS.sleep(1L);} while (true);}public void run() throws InterruptedException {try {int number = random.nextInt();List<Integer> primeFactors = this.primeFactors(number);MathGame.print(number, primeFactors);}catch (Exception e) {System.out.println(String.format("illegalArgumentCount:%3d, ", this.illegalArgumentCount) + e.getMessage());}}public static void print(int number, List<Integer> primeFactors) {StringBuffer sb = new StringBuffer("" + number + "=");Iterator<Integer> iterator = primeFactors.iterator();while (iterator.hasNext()) {int factor = iterator.next();sb.append(factor).append('*');}if (sb.charAt(sb.length() - 1) == '*') {sb.deleteCharAt(sb.length() - 1);}System.out.println(sb);}public List<Integer> primeFactors(int number) {if (number < 2) {++this.illegalArgumentCount;throw new IllegalArgumentException("number is: " + number + ", need >= 2");}ArrayList<Integer> result = new ArrayList<Integer>();int i = 2;while (i <= number) {if (number % i == 0) {result.add(i);number /= i;i = 2;continue;}++i;}return result;}
}Affect(row-cnt:1) cost in 970 ms.
#6. watch
通过watch命令来查看demo.MathGame#primeFactors
函数的返回值:
$ watch demo.MathGame primeFactors returnObj
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 107 ms.
ts=2018-11-28 19:22:30; [cost=1.715367ms] result=null
ts=2018-11-28 19:22:31; [cost=0.185203ms] result=null
ts=2018-11-28 19:22:32; [cost=19.012416ms] result=@ArrayList[@Integer[5],@Integer[47],@Integer[2675531],
]
ts=2018-11-28 19:22:33; [cost=0.311395ms] result=@ArrayList[@Integer[2],@Integer[5],@Integer[317],@Integer[503],@Integer[887],
]
ts=2018-11-28 19:22:34; [cost=10.136007ms] result=@ArrayList[@Integer[2],@Integer[2],@Integer[3],@Integer[3],@Integer[31],@Integer[717593],
]
ts=2018-11-28 19:22:35; [cost=29.969732ms] result=@ArrayList[@Integer[5],@Integer[29],@Integer[7651739],
]
更多的功能可以查看进阶使用。
#7. 退出 arthas
如果只是退出当前的连接,可以用quit
或者exit
命令。Attach 到目标进程上的 arthas 还会继续运行,端口会保持开放,下次连接时可以直接连接上。
如果想完全退出 arthas,可以执行stop
命令。
官方文档:快速入门 | arthas
Alibaba Arthas快速入门 简单易懂相关推荐
- html 甘特图_甘特图怎么画?甘特图基础教程,小白快速入门简单易懂
甘特图是什么?可能你是第一次听到,甘特图是通过活动顺序和时间间隔表示某一特定项目其顺序与时间的关系.不同于时间表,或日程规划表,甘特图可以使使用者更直观的知道在某一时间的工作内容和进度. 甘特图常见用 ...
- Arthas 快速入门
Arthas (阿尔萨斯) 能为你做什么? Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱.当你遇到以下类似问题而束手无策时, Arthas 可以帮助你解决: 1. 这个类从哪个 ...
- 【机器学习】快速入门简单线性回归 (SLR)
简单线性回归图(青色散点为实际值,红线为预测值) statsmodels.api.statsmodels.formula.api 和 scikit-learn 的 Python 中的 SLR 今天云朵 ...
- RabbitMQ快速入门--简单队列模型
入门案例 简单队列模式的模型图: 官方的HelloWorld是基于最基础的消息队列模型来实现的,只包括三个角色: publisher:消息发布者,将消息发送到队列queue queue:消息队列,负责 ...
- saltstack php,Saltstack快速入门简单汇总
[root@master~]# salt \* test.ping minion-1: True minion-2: True minion-3: True minion-4: True True代表 ...
- Hibernate快速入门+简单示例
很多人说Hbiernate已经没人用了,市面上有太多可供选择的开源工具.诚然市面上有很多新技术,但是作为初学者,还是要踏踏实实从基础做起,而且Hbiernate有自己的应用场景. 上篇博文 我们介绍了 ...
- 数据校验JSR303快速入门(简单使用、分组效验、自定义注解效验)
前言: 在实际开发中,除了前端需要在表单中验证用户的输入.后台服务也需要对用户传入的参数进行效验,避免他人在得知请求格式后,直接通过类似Postman这样的测试工具进行非常数据请求. JSR303是什 ...
- Protege 使用教程(详细讲解 入门简单易懂)
Protege 使用教程 首先看一下我们的数据库表单,主要是三个类别,五张表单. 并且像actor.movie.genre中包含具有的属性. 接下来,我们的需求就是使用Protege软件,构建实体关系 ...
- 阿里巴巴开源的 Java 诊断工具Arthas【入门篇】
前面: 各位老铁们,好久没和大家见面了,最近一直躲在家里不敢出来门,过着像猪的生活..... 吃喝拉撒睡觉.远程在家办公一段时间也是遇到了比较棘手的问题.所以没有顾得上来和大家分享了,不过大家放心,从 ...
最新文章
- Swift学习: 从Objective-C到Swift
- python如何安装scrapy库_python2安装scrapy库报错解决方案
- 数学建模——主成分分析算法详解Python代码
- 深入理解Java原始数据类型和包装类关于==和equals的比较
- 查找100 sql oracle,Oracle中SQL语句执行效率的查找与解决
- 内容流推荐中的个性化标题生成框架
- 老板让我每天刷群,无奈只能写一个自动群发脚本,不让自己疯狂复制粘贴
- 翻译:用户变量(User-Defined Variable)(已提交到MariaDB官方手册)
- 协鑫集成为泰国Enmax建立10MW光伏电站
- linux thinkphp 计划任务_ThinkPHP定时任务项目案例分析
- Elasticsearch安装-源码
- Linux开机启动流程详细步骤是什么?
- pthread_attr_t 线程属性(一)
- 51单片机c语言数组怎么用,51单片机之C语言-4.2数组
- 动态设置Button图片大小
- dx12 龙书第十六章学习笔记 -- 实例化与视锥体剔除
- uniapp写一个登录页面
- html css径向渐变,如何实现css3径向渐变如何定义中心和大小形状
- 数据分析: kaggle比赛 - 销量预测
- C++第一讲——Demon和Angela的魔法之旅