Author: threedayman

Source: Hang Seng LIGHT Cloud Community

Arthas: what is

Arthas is an Alibaba open source Java online diagnostic tool. Arthas supports JDK 6+, Linux/Mac/Windows, command-line interaction, and provides rich Tab auto-completion features to facilitate problem location and diagnosis.

Official website address ** : **arthas.aliyun.com/doc/

Github address: ** : **github.com/alibaba/art…

What can Arthas do

Have you encountered any of the following problems in production or test environments?

  • Which JAR is this class loaded from? Why are exceptions related to various classes reported?
  • Why is the code I changed not executed? Did I not commit? Wrong branch?
  • You can’t debug online when you encounter a problem, can you only add logs and republish?
  • There is a problem with the data processing of a user online, but it cannot be debug online or reproduced offline!
  • Is there a global view of the health of the system?
  • Is there any way to monitor the real-time state of the JVM?
  • How to quickly locate the hot spots in the application and generate the flame diagram?
  • How do I find an instance of a class directly from within the JVM?
  • How to locate slow calling methods.

It says on the official website that Arthas can help solve the above problems, but it does not specify how to deal with them. Here we introduce several commands to explain how to solve the above problems through Arthas.

Quick start to Arthas

Download Arthas

curl -O https://arthas.aliyun.com/arthas-boot.jar
Copy the code

Run Arthas

java -jar arthas-boot.jar
Copy the code

After this command is executed, Java processes are displayed. Select the processes that you want to check.

Let’s run the following test code to see Arthas in action

package com.example.arthas;

public class ArthasTest {

    public static void main(String[] args) throws InterruptedException {
        while(true){
            put("test");
        }
    }

    private static String put(String name) throws InterruptedException {
        Thread.sleep(1000L);
        System.out.println(name);
        return "hello "+name;
    }
}
Copy the code

Select the process where our test code runs [3]22756

[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 1200 org.jetbrains.jps.cmdline.Launcher
  [2]: 9120
  [3]: 22756 com.example.arthas.ArthasTest
Copy the code

Which JAR is this class loaded from? Why are exceptions related to various classes reported?

Use the sc command to find out which JAR package loaded the class

[arthas@22756]$ sc -d com.example.arthas.ArthasTest
 class-info        com.example.arthas.ArthasTest
 code-source       /D:/ideaproject/jsoupservice/target/classes/
 name              com.example.arthas.ArthasTest
 isInterface       false
 isAnnotation      false
 isEnum            false
 isAnonymousClass  false
 isArray           false
 isLocalClass      false
 isMemberClass     false
 isPrimitive       false
 isSynthetic       false
 simple-name       ArthasTest
 modifier          public
 annotation
 interfaces
 super-class       +-java.lang.Object
 class-loader      +-sun.misc.Launcher$AppClassLoader@18b4aac2
                     +-sun.misc.Launcher$ExtClassLoader@593cfc78
 classLoaderHash   18b4aac2

Affect(row-cnt:1) cost in 77 ms.
Copy the code

Where, the code-source information indicates the class loading path. If it is loaded from the JAR package, the JAR package information will be displayed, as follows

code-source file:/home/trade/u3/u3-mnager/u-mnager-1.jar! The/BOOT - INF/lib/uc - do - 1.0 - the SNAPSHOT. The jar! /Copy the code

This command can be used to troubleshoot dependency conflicts.

Why is the code I changed not executed? Did I not commit? Wrong branch?

We use the jad command to decompile to see the source information

[arthas@22756]$ jad com.example.arthas.ArthasTest

ClassLoader:
+-sun.misc.Launcher$AppClassLoader@18b4aac2
  +-sun.misc.Launcher$ExtClassLoader@593cfc78

Location:
/D:/ideaproject/jsoupservice/target/classes/

       /*
        * Decompiled with CFR.
        */
       package com.example.arthas;

       public class ArthasTest {
           public static void main(String[] args) throws InterruptedException {
               while (true) {
/* 7*/             ArthasTest.put("test");
               }
           }

           private static String put(String name) throws InterruptedException {
/*12*/         Thread.sleep(1000L);
/*13*/         System.out.println(name);
               return "hello " + name;
           }
       }

Affect(row-cnt:1) cost in 164 ms.
Copy the code

Verify that the committed code is running by looking at the decompiled code.

You can’t debug online when you encounter a problem, can you only add logs and republish?

Watch watches the input parameters and return values of the method execution

[arthas@22756]$ watch com.example.arthas.ArthasTest put '{params,returnObj}' -x 2 -n2 Press Q or Ctrl+C to abort. Affect(class count: 1 , method count: 1) cost in 12 ms, listenerId: 3 method=com.example.arthas.ArthasTest.put location=AtExit ts=2021-08-04 17:07:19; [cost=3.10595639883E7ms] result= @arrayList [@object [][@string [test],], @string [hello test], ] method=com.example.arthas.ArthasTest.put location=AtExit ts=2021-08-04 17:07:20; [cost=1009.9733ms] result= @arrayList [@object [][@string [test],], @string [hello test],]Copy the code

-x Traversal depth of output result attribute

-n Indicates the number of execution times

Retransform loads the external.class file and retransforms the classes that the JVM has loaded.

Combined with the JAD and MC commands, decompile through the JAD, modify the source code through the editor, and add related logs. MC command to compile the modified code in memory.

Load the new bytecode with the retransform command. Log statements are added by hot loading.

jad --source-only com.example.arthas.ArthasTest > D:/ArthasTest.java
mc d:/ArthasTest.java -d d:
retransform d:/com/example/arthas/ArthasTest.class
Copy the code

There is a problem with the data processing of a user online, but it cannot be debug online or reproduced offline!

This problem can also be observed by using the above watch method to observe the related call parameters online. Based on the known survey results, you can add the corresponding conditional parameters to the Watch method to filter out the requests that do not need to be observed. Watch command document.

Is there a global view of the health of the system?

Using the dashboard real-time data panel commands, you can view information such as threads, memory, and system parameters. For details about parameters, see the Dashboard documentation

[arthas@17396]$ dashboard -n 1 ID NAME GROUP PRIORITY STATE %CPU DELTA_TIM TIME INTERRUPT DAEMON -1 C1 CompilerThread3 - -1-0.0 0.000 0:0.437 false true -1 C2 CompilerThread2 - -1-0.0 0.000 0:0.375 false true 1 main main 5 TIMED_WA 0.0 0.000 0:0.328 false false -1 C2 CompilerThread0 - -1-0.0 0.000 0:0.312 false true -1 C2 CompilerThread1 - -1-0.0 0.000 0:0.281 false true 23 Arthas -NettyHttptelNetBootstr System 5 RUNNABLE 0.0 0.000 0:0.234 false true 5 Attach Listener System 5 RUNNABLE 0.0 0.000 0:0.031 false True -1 GC task Thread# 7 (ParallelGC) - -1-0.0 0.000 0:0.031 false True-1 GC task thread#6 (ParallelGC) - -1-0.0 0.000 0.031 false true-1 GC task thread#0 (ParallelGC) - -1-0.0 0.031 false true-1 GC task thread#0 (ParallelGC) - -1-0.0 0 0.000 0:0.031 false true-1 GC task Thread #1 (ParallelGC) - -1-0.0 0.000 0:0.031 false true-1 VM Thread - -1-0.0 0.000 0:0.031 false true -1 GC task thread#2 (ParallelGC) - -1-0.00.000 0:0.031 false true -1 GC task thread#3 (ParallelGC) - -1-0.00.000 0:0.031 false true -1 GC task thread#5 (ParallelGC) - -1-0.00.000 0:0.031 false true -1 GC task thread#4 (ParallelGC) - -1-0.00.000 0:0.031 false true 16 arthas-NettyHttpTelnetBootstr system 5 RUNNABLE 0.0 0.000 0:0.015 false true 6 Monitor Ctrl-Break main 5 RUNNABLE 0.0 0.000 0:0.015 false true 2 Reference Handler System 10 WAITING 0.0 0.000 0:0.000 false true 3 Finalizer system 8 WAITING 0.0 0.000 0:0.000 false true 4 Signal Dispatcher system 9 RUNNABLE 0.0 0.000 0:0.000 false true 13 Arthas-timer system 5 WAITING 0.0 0.000 0:0.000 false true 17 Arthas-NettyWebSocketttyBoots System 5 RUNNABLE 0.0 0.000 0:0.000 false true 18 Arthas-NettyWebSocketttyBoots System 5 RUNNABLE 0.00.000 0:0.000 false true Memory Used total Max Usage GC heap 26M 165M 3591M 0.75% gc.ps_scexploiture. Count 2 Exploiture (ms) 16 ps_widow_space 0K 10752K 10752K 0.00% Count 1 ps_old_gen 17M 91M 2693M 0.67% Gc.ps_marksweep. time(ms) 41 nonheap 29M 30M -1 97.08% code_cache 6M 6M 2M 2.57% metaspace 20M 21M -1 97.06% compressed_class_space 2M 2M 1024M 0.25% direct 0K 0K - 0.00% mapped 0K 0K -0.00% Runtime os.name Windows 10 OS. Version 10.0 java.version 1.8.0_291 java.home C:\Program Files\Java\jdk1.8.0_291\jre systemload. Average -1.00 processors 8 timestamp/uptime Fri Aug 06 09:43:41 GMT+08:00 2021/326sCopy the code

Classloader looks at the classloader and the number of classes it loads.

[arthas@17396]$ classloader name numberOfInstances loadedCountTotal BootstrapClassLoader 1 2782 com.taobao.arthas.agent.ArthasClassloader 1 1373 sun.misc.Launcher$ExtClassLoader 1 60 sun.reflect.DelegatingClassLoader  15 15 sun.misc.Launcher$AppClassLoader 1 7 Affect(row-cnt:5) cost in 3 ms.Copy the code

Is there any way to monitor the real-time state of the JVM?

Using JVM commands, you can view the real-time running status of the JVM. It contains machine information, compiler information, garbage collector, class loading information, memory management, thread related information, file descriptor related information. JVM command document

[arthas@17396]$ jvm RUNTIME ----------------------------------------------------------------------------------------------------------------------- Machine-name 17396@WINDOWS-D2B420I jvm-start-time 2021-08-06 09:38:14 management-spec-version 1.2 spec-name Java Virtual Machine Specification spec-vendor Oracle Corporation SPEC-VERSION 1.8 vm-name Java HotSpot(TM) 64-bit Server VM Vm-vendor Oracle Corporation VM-version 25.291-b10 input-arguments-javaAgent :D:\JetBrains\IntelliJ IDEA 2021.1\lib\idea_rt.jar=61057:D:\JetBrains\Int elliJ IDEA 2021.1\ bin-dfile. encoding=UTF-8 class-path C:\Program Files \ Java \ jdk1.8.0 _291 \ jre \ lib \ charsets jar; C: \ Program Files \ Java \ jdk1 8.0 _291 \ jre \ lib \ deploy the jar. C:\Program Files\Java\jdk1.8.0_291\jre\lib\resources.jar; C:\Program Files\Java\jdk1.8.0_291\jre\lib\resources.jar; C: \ Program Files \ Java \ jdk1 8.0 _291 \ jre \ lib \ rt jar; C:\Program ----------------------------------------------------------------------------------------------------------------------- CLASS-LOADING ----------------------------------------------------------------------------------------------------------------------- LOADED-CLASS-COUNT 3914 TOTAL-LOADED-CLASS-COUNT 3914 UNLOADED-CLASS-COUNT 0 IS-VERBOSE false ----------------------------------------------------------------------------------------------------------------------- COMPILATION ----------------------------------------------------------------------------------------------------------------------- NAME HotSpot 64-Bit Tiered Compilers TOTAL-COMPILE-TIME 1317 [time (ms)] ----------------------------------------------------------------------------------------------------------------------- GARBAGE-COLLECTORS ----------------------------------------------------------------------------------------------------------------------- PS Scavenge name : PS Scavenge [count/time (ms)] collectionCount : 2 collectionTime : 16 PS MarkSweep name : PS MarkSweep [count/time (ms)] collectionCount : 1 collectionTime : 41 ----------------------------------------------------------------------------------------------------------------------- MEMORY-MANAGERS ----------------------------------------------------------------------------------------------------------------------- CodeCacheManager Code Cache Metaspace Manager Metaspace Compressed Class Space PS Scavenge PS Eden Space PS Survivor Space PS MarkSweep PS Eden Space PS Survivor Space PS Old Gen ----------------------------------------------------------------------------------------------------------------------- MEMORY ----------------------------------------------------------------------------------------------------------------------- HEAP- memory-usage init: 266338304(254.0 MiB) [MEMORY in bytes] Used: 29895120(28.5 MiB) committed: 173015040(165.0 MiB) Max: 3765960704(3.5gib) no-heap-memory-usage init: 2555904(2.4 MiB) [MEMORY in bytes] Used: 31363048(29.9 MiB) COMMITTED: 32161792(30.7 MiB) Max: -1(-1 B) PENDING-FINALIZE-COUNT 0 ----------------------------------------------------------------------------------------------------------------------- OPERATING-SYSTEM ----------------------------------------------------------------------------------------------------------------------- OS Windows 10 ARCH AMd64 PROCESSORS -count 8 load-average -1.0 VERSION 10.0 ----------------------------------------------------------------------------------------------------------------------- THREAD ----------------------------------------------------------------------------------------------------------------------- COUNT 15 DAEMON-COUNT 14 PEAK-COUNT 16 STARTED-COUNT 21 DEADLOCK-COUNT 0 ----------------------------------------------------------------------------------------------------------------------- FILE-DESCRIPTOR ----------------------------------------------------------------------------------------------------------------------- MAX-FILE-DESCRIPTOR-COUNT -1 OPEN-FILE-DESCRIPTOR-COUNT -1Copy the code

How to quickly locate the hot spots in the application and generate the flame diagram?

The profiler command supports the generation of flame diagrams of application hotspots. Essentially, you take samples over and over again, and then you collect the results to generate a flame graph (Linux/MAC only).

$ profiler start
Started [cpu] profiling
$ profiler status
[cpu] profiling is running for 4 seconds
$ profiler stop
profiler output file: /tmp/demo/arthas-output/20191125-135546.svg
OK
Copy the code

Use port 3658 by default, arthas, you can open the: http://localhost:3658/arthas-output/ view to arthas – the output directory the profiler results below:

Click to see the detailed results:

How do I find an instance of a class directly from within the JVM?

The VMtool uses the JVMTI interface to query memory objects and force GC.

[arthas@17396]$ vmtool -action getInstances --className java.lang.String --limit 10
@String[][
    @String[sun/nio/ch/FileDispatcherImpl],
    @String[sun/nio/ch/FileDispatcherImpl],
    @String[sun/nio/ch/FileDispatcher],
    @String[sun/nio/ch/FileDispatcher],
    @String[sun/nio/ch/FileDispatcher],
    @String[direct],
    @String[sun/nio/ch/FileChannelImpl$Unmapper],
    @String[sun/nio/ch/FileChannelImpl$Unmapper],
    @String[sun/nio/ch/FileChannelImpl$Unmapper],
    @String[sun/nio/ch/FileDispatcherImpl],
]
Copy the code

With the –limit argument, you can limit the number of returned values and avoid straining the JVM when getting too much data. The default value is 10.

How to locate slow calling methods.

Trace shows the method internal invocation path and prints the elapsed time on each node on the method path

[arthas@18584]$ trace com.example.arthas.ArthasTest bridge Press Q or Ctrl+C to abort. Affect(class count: 1 , method count: 1) cost in 137 ms, listenerId: 1 `---ts=2021-08-06 10:30:11; thread_name=main; id=1; is_daemon=false; priority=5; TCCL = sun. Misc. The Launcher $AppClassLoader @ 18 b4aac2 ` - [1011.4579] ms com. Example. Arthas. ArthasTest: bridge () ` - [1009.9123] ms com. Example. Arthas. ArthasTest: the put () # 13 ` - ts = 2021-08-06 10:30:12; thread_name=main; id=1; is_daemon=false; priority=5; TCCL = sun. Misc. The Launcher $AppClassLoader @ 18 b4aac2 ` - [1002.9228] ms com. Example. Arthas. ArthasTest: bridge () ` - [1002.8016] ms com. Example. Arthas. ArthasTest: the put () # 13Copy the code

The bridge method is called at 1011.4579ms and 1009.9123ms is called at 1009.9123ms. Therefore, we can analyze the PUT method and trace it if necessary.

Based on the above understanding, use the Arthas command to troubleshoot the previous problems. Arthas provides a variety of command functions. For more information about how to use the command, please refer to the command list

Reference documentation

Arthas user document