jvm 调优之 Arthas (阿里巴巴开源的 Java 诊断工具)

一、Arthas是什么鬼?

Arthas是一款阿里巴巴开源的 Java 线上诊断工具,功能非常强大,可以解决很多线上不方便解决的问题。

Arthas诊断使用的是命令行交互模式,支持JDK6+,Linux、Mac、Windows 操作系统,命令还支持使用 tab 键对各种信息的自动补全,诊断起来非常利索。

官网:https://alibaba.github.io/arthas/index.html
Github地址:https://github.com/alibaba/arthas

二、Arthas能解决什么问题?

让我们来看下 Arthas 的官方解释。

当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:

1、这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?

2、我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?

3、遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?

4、线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!

5、是否有一个全局视角来查看系统的运行状况?

6、有什么办法可以监控到JVM的实时运行状态?

看完是不是觉得太牛逼了?

特别是它可以在线反编译类,可以不用加日志在线调试跟踪问题代码。

既然这么牛逼,肯定有不少公司在使用吧,下面列出了官方给出的使用登录顺序图。
file

三、快速安装

官方推荐使用 arthas-boot 进行安装,非常方便,以下是基于 Linux 系统环境进行演示,一般解决线上问题也是基于 Linux 环境。

第一步:下载

在任何目录下载 arthas-boot 这个包。

wget https://alibaba.github.io/arthas/arthas-boot.jar

第二步:运行

使用 java -jar 命令执行 arthas-boot 包。

java -jar arthas-boot.jar

第三步:选择进程

运行 arthas-boot 后,控制台会显示所有 Java 进程,选择一个你需要诊断的进程。

如第二步所示,这里有只有一个 Java 进程,输入序号1,回车,Arthas会附到目标进程上,并输出日志:

[INFO] Start download arthas from remote server: https://maven.aliyun.com/repository/public/com/taobao/arthas/arthas-packaging/3.1.1/arthas-packaging-3.1.1-bin.zip
[INFO] Download arthas success.
[INFO] arthas home: /root/.arthas/lib/3.1.1/arthas
[INFO] Try to attach process 13062
[INFO] Attach process 13062 success.
[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       13062                                                                 
time      2019-07-30 14:49:34

至此,安装、启动完成。

更多其他安装方式见:https://alibaba.github.io/artha

四、实战使用

启动完成后,当前光标会进入 arthas 的控制台,接受各种操作命令。

下面,栈长会做几个常用命令的演示,让大家对它有一个基本的认识和快速上手的能力。

1、dashboard

显示当前系统的实时数据面板,按 ctrl+c 即可退出。

$ dashboard

file

2、thread

查看当前 JVM 的线程堆栈信息。

thread id, 显示指定线程的运行堆栈:

$ thread 20

file

显示当前最忙的前N个线程并打印堆栈:

$ thread -n 3

file

3、jad

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

$ jad cn.javastack.springbootbestpractice.SpringBootBestPracticeApplication

file

五、小试牛刀:监控DS进程

(base) ➜  arthas java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.6.6
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 33141 org.apache.dolphinscheduler.server.StandaloneServer
  [2]: 24797 
1
[INFO] Start download arthas from remote server: https://arthas.aliyun.com/download/3.6.6?mirror=aliyun
[INFO] File size: 12.89 MB, downloaded size: 788.40 KB, downloading ...
[INFO] File size: 12.89 MB, downloaded size: 4.02 MB, downloading ...
[INFO] File size: 12.89 MB, downloaded size: 9.74 MB, downloading ...
[INFO] Download arthas success.
[INFO] arthas home: /Users/kaiyi/.arthas/lib/3.6.6/arthas
[INFO] Try to attach process 33141
Picked up JAVA_TOOL_OPTIONS: 
[INFO] Attach process 33141 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.                           
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'                          
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.                          
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |                         
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'                          

wiki       https://arthas.aliyun.com/doc                                        
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html                  
version    3.6.6                                                                
main_class org.apache.dolphinscheduler.server.StandaloneServer                  
pid        33141                                                                
time       2022-10-15 20:07:22              

dashboard查看

file


[arthas@33141]$ dashboard
ID    NAME                                  GROUP              PRIORITY    STATE        %CPU        DELTA_TIME   TIME        INTERRUPTED  DAEMON      
-1    C2 CompilerThread2                    -                  -1          -            0.0         0.000        0:13.705    false        true        
-1    C2 CompilerThread0                    -                  -1          -            0.0         0.000        0:10.992    false        true        
-1    C2 CompilerThread1                    -                  -1          -            0.0         0.000        0:10.459    false        true        
122   DestroyJavaVM                         main               5           RUNNABLE     0.0         0.000        0:7.210     false        false       
-1    C1 CompilerThread3                    -                  -1          -            0.0         0.000        0:4.508     false        true        
50    MasterSchedulerService                main               5           TIMED_WAITIN 0.0         0.000        0:0.911     false        false       
-1    VM Periodic Task Thread               -                  -1          -            0.0         0.000        0:0.799     false        true        
-1    VM Thread                             -                  -1          -            0.0         0.000        0:0.268     false        true        
47    main-SendThread(localhost:59950)      main               5           RUNNABLE     0.0         0.000        0:0.251     false        true        
29    qtp749030825-29                       main               5           TIMED_WAITIN 0.0         0.000        0:0.229     false        false       
14    SyncThread:0                          main               5           WAITING      0.0         0.000        0:0.204     false        false       
12    NIOServerCxn.Factory:0.0.0.0/0.0.0.0: main               5           RUNNABLE     0.0         0.000        0:0.185     false        true        
54    ServerNodeManagerExecutor_1           main               5           TIMED_WAITIN 0.0         0.000        0:0.181     false        true        
117   QuartzScheduler_DolphinScheduler-Macd main               7           TIMED_WAITIN 0.0         0.000        0:0.161     false        false       
121   pool-20-thread-1                      main               5           TIMED_WAITIN 0.0         0.000        0:0.149     false        false       
127   qtp749030825-127                      main               5           RUNNABLE     0.0         0.000        0:0.140     false        false       
-1    Gang worker#5 (Parallel GC Threads)   -                  -1          -            0.0         0.000        0:0.125     false        true        
-1    Gang worker#1 (Parallel GC Threads)   -                  -1          -            0.0         0.000        0:0.121     false        true        
-1    Gang worker#4 (Parallel GC Threads)   -                  -1          -            0.0         0.000        0:0.121     false        true        
-1    Gang worker#0 (Parallel GC Threads)   -                  -1          -            0.0         0.000        0:0.120     false        true        
-1    Gang worker#6 (Parallel GC Threads)   -                  -1          -            0.0         0.000        0:0.120     false        true        
83    LowerWeightHostManagerExecutor_1      main               5           TIMED_WAITIN 0.0         0.000        0:0.119     false        true        
Memory                          used       total      max       usage      GC                                                                         
heap                            118M       247M       4012M     2.95%      gc.parnew.count                       37                                   
par_eden_space                  45M        68M        665M      6.78%      gc.parnew.time(ms)                    226                                  
par_survivor_space              8M         8M         83M       10.22%     gc.concurrentmarksweep.count          0                                    
cms_old_gen                     64M        170M       3264M     1.99%      gc.concurrentmarksweep.time(ms)       0                                    
nonheap                         132M       137M       488M      27.12%                                                                                
code_cache                      33M        34M        240M      14.07%                                                                                
metaspace                       87M        92M        128M      68.64%                                                                                
compressed_class_space          10M        11M        120M      8.95%                                                                                 
direct                          337K       337K       -         100.00%                                                                               
mapped                          0K         0K         -         0.00%                                                                                 
Runtime                                                                                                                                               
os.name                                                                    Mac OS X                                                                   
os.version                                                                 12.4                                                                       
java.version                                                               1.8.0_333                                                                  
java.home                                                                  /Library/Java/JavaVirtualMachines/jdk1.8.0_333.jdk/Contents/Home/jre       
systemload.average                                                         1.86                                                                       
processors                                                                 12           

六、总结

总结下来,使用 Arthas 可以很方便的诊断一个 Java 应用程序,如:系统数据面板、JVM实时运行状态、类加载情况、监控方法执行情况、显示方法执行路径等。

Arthas这些实用的功能确实可以帮助我们解决一些常见的线上问题,也能独立于应用程序代码,但仅局限于在一个 JVM 进程内,如果是分布式系统,Arthas就有点难了。


相关文章:
知乎 | 推荐一款阿里开源的 Java 诊断工具,好用到爆!

为者常成,行者常至