Arthas is what

When our system encounters JVM or memory overflow and other problems, how to effectively monitor and check our program, we found several more commonly used tools, such as JDK built-in JConsole, JVisualVM and one of the most useful tools – Jprofiler, but this is charged, Or very few people use it except for very rich companies, and Arthas is our main character today, why are we focusing on that?

Arthas.giitee. IO/GitHub: github.com/alibaba/art…

Arthas is an open source Java diagnostic tool for Alibaba, which uses the command line interaction mode and provides a wide range of functions. Arthas is a free plug-in for CHECKING JVM. After understanding this tool, Arthas is found to be quite useful and supports a comprehensive range of functions. So what exactly can Arthas do for us?

  1. Provides performance kanban, including thread, CPU, memory, and other information, and is periodically refreshed.
  2. View thread snapshots based on various criteria. Find the n threads with the highest CPU usage
  3. Outputs various JVM information, such as GC algorithm, JDK version, ClassPath, and so on
  4. If a problem occurs, you cannot debug it online. Replace it with hot deployment and logs
  5. To view the static properties of a class, you can also execute statements using ogNL syntax
  6. View details about the loaded class, which JAR package the class was loaded from, and information about the class’s methods
  7. Dump class bytecode to specified directory
  8. Decompile the specified class directly
  9. Quickly locate application hot spots and generate flame maps
  10. You can monitor the real-time health of the JVM

In the old days, when you had these problems, the answer was mostly to change the code and go back online. But in large companies, the process of going live can be tedious, and it can be very frustrating to re-release a version just to add one more log line. But Alibaba’s open source Arthas has a more elegant approach to debugging online.

Arthas supports JDK6 and runs on Linux/Mac/Windows with automatic Tab completion, making it easier to locate and diagnose problems

Download address: arthas. Gitee. IO/download. Ht… You can download the arthas-packaging-3.5.0-bin.zip package or run the arthas-packaging-3.5.0-bin.zip command

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

The manual

1. Quick startup

Once we’ve downloaded it, we can just boot it with the command java-jar arthas-boot.jar, but before we can do that we need to be attached to arthas by checking the code

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class FullGCTest {


    // A class that simulates a bank card
    private static class CardInfo {
        // Smallholder's bank card information records
        BigDecimal price = new BigDecimal(10000000.0);
        String name = "Small farmer";
        int age = 18;
        Date birthdate = new Date();

        public void m(a) {}}// Thread pool Timing thread pool
    // Set the rejection policy
    private static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(50.new ThreadPoolExecutor.DiscardOldestPolicy());

    public static void main(String[] args) throws Exception {
        executor.setMaximumPoolSize(50);

        for(;;) { modelFit(); Thread.sleep(100); }}/** * Risk assessment for bank cards */
    private static void modelFit(a){
        List<CardInfo> taskList = getAllCardInfo();
        // Take out every piece of information
        taskList.forEach(info -> {
            // do something
            executor.scheduleWithFixedDelay(() -> {
                // Call M
                info.m();

            }, 2.3, TimeUnit.SECONDS);
        });
    }

    private static List<CardInfo> getAllCardInfo(a){
        List<CardInfo> taskList = new ArrayList<>();
        // query 100 cards at a time
        for (int i = 0; i < 100; i++) {
            CardInfo ci = new CardInfo();
            taskList.add(ci);
        }

        returntaskList; }}Copy the code

This is the case described in the previous article, if you are interested, you can know about it.

Javac fullgctest. Java to compile and print GC logs for risk monitoring: java -Xms200M -Xmx200M -XX:+PrintGC FullGCTest

Arthas startup command: Java -jar arthas-boot.jar

So here we have the FullGCTest application that we just launched, so we type 1 and press enter, and we have Arthas attached to our application, and then we just need to do the corresponding commands

Order details document: arthas.aliyun.com/doc/command…

2. Feature list

The command Detailed instructions
jvm View current JVM information
thread View thread stack information for the current JVM
watch Method to perform data observations
dashboard Real-time data panel for the current system
trace Method internally calls the path and prints the time spent on each node on the method path
stack Prints the call path to which the current method is called
tt A space-time tunnel of method execution data, recording the input and return information of each call to the specified method, and observing these different time calls
vmoption View and update the JVM loaded class information
sc View information about classes loaded by the JVM
sm View method information for loaded classes
jad Decompile the source code that specifies the loaded class
classloader View the classloader inheritance tree, urls, class loading information
heapdump The heap dump function is similar to the jmap command

jvm

OPERATING-SYSTEM: indicates the SYSTEM parameters

THREAD related:

  • COUNT: The number of threads currently active in the JVM
  • Daemon-count: indicates the number of daemons currently active in the JVM
  • Peak-count: The maximum number of threads that have been alive since JVM startup
  • Start-count: The total number of threads STARTED since the JVM STARTED
  • DEADLOCK-COUNT: specifies the number of threads currently deadlocked in the JVM

MEMORY

File-descriptor (FILE DESCRIPTOR dependent) :

  • Max-file-descriptor -COUNT: maximum number of FILE descriptors that a JVM process can open
  • Open-file-descriptor -COUNT: the number of FILE descriptors currently OPEN by the JVM

Thread command

Parameter Description:

The command Detailed instructions
id Thread id
[n:] Specify the first N busiest threads and print the stack
[b] Find the thread that is currently blocking another thread
[i] Specifies the sampling interval for CPU usage statistics, in milliseconds. The default value is 200
[–all] Displays all matching threads

Prints the current N busiest threads and prints the stack thread-n 3

Thread Displays all threads

Thread 17: Displays the running stack for the specified thread

Thread-i: specifies the sampling interval

Thread-i 1000: collects statistics on the thread CPU time in the last 1000ms. Thread-n 3 -i 1000: Lists the 3 busiest thread stacks in 1000ms

Dashboard command

When running the program, it displays real-time information about the current program, such as QPS, RT, error count, thread pool information, and so on

Data description:

  • ID: Java-level thread ID
  • NAME: thread
  • GROUP: indicates the name of the thread GROUP
  • PRIORITY: PRIORITY of a thread. The value ranges from 1 to 10. A larger number indicates a higher PRIORITY
  • STATE: indicates the status of the thread. CPU%: indicates the CPU usage of the thread. For example, if the sampling interval is 1000ms and the incremental CPU time of a thread is 100ms, the CPU usage =100/1000=10%
  • DELTA_TIME: the incremental CPU time that the thread has run since the last sampling, in the format of seconds
  • TIME: indicates the total CPU running TIME of a thread. The data format is minute: second
  • INTERRUPTED: Indicates the current INTERRUPTED bit status of a thread
  • DAEMON: Whether it is a DAEMON thread

Parameter Description:

The parameter name Detailed instructions
id Interval for refreshing real-time data (ms). The default value is 5000ms
[n:] Number of times that real-time data was refreshed

Sc command

By looking at the JVM loaded classes, we can see the details of our class from SC, including which JAR it was read from, whether it was an interface/enumeration class, and even which class loader it was loaded from.

Parameter Description:

The parameter name Detailed instructions
class-pattern Class name expression matches
method-pattern Method name expression matches
[d] Output details about the current class, including the source of the original file loaded by the class, the declaration of the class, and the ClassLoader loaded. This occurs multiple times if a class is loaded by more than one ClassLoader
[E] Enable regular expression matching. The default value is wildcard matching

sc -d *CardInfo: Prints class details

Sc-d -f *CardInfo: Prints Fiedld information of the class

Heapdump + JHAT analysis

Heapdump: similar to the jmap command

Create to specified folder:

[arthas@365564]$ heapdump /usr/local/mxn/dump.hprof
Dumping heap to /usr/local/mxn/dump.hprof ...
Heap dump file created
Copy the code

Once created, we can see the dump file in the specified folder and run the commandjhat dump.hprof, generate the file, after success we can access through IP+ port

Access:

Then we can access it through IP+ port, there is his other, we pull to the bottom, findShow instance counts for all classes (including platform)

From the following we can analyze which class contains the most objects and which class produces the objects

The most powerful feature in this is calledExecute Object Query Language (OQL) queryThis will show you which objects there are, how many bytes and references there are, and you can observe which objects are causing problems, as shown in the figure below, showing all the objects corresponding to strings

After the search point is entered, we can also see how many bytes the Object occupies and how many references refer to the Object. The syntax of this OQL is also flexible, we can use the WHERE condition to filter

jad

Jad: a problem with a class or a method of a class that is dynamically generated by a proxy

Some people may ask what is the use of this, the source code I don’t know? Because sometimes we are not sure if the package on line or in the test environment is the one we have modified, we can decompile jad to see if it is the latest code

redafine

The JVM currently has some restrictions: you can only change the method implementation (the method has already been run), you cannot change the method name, you cannot change the property m() -> mm()

For example, if we have a class problem on the online environment and want to replace it, we can only stop the server and re-publish it under normal circumstances. This is ok in ordinary small companies, but it cannot be stopped in large-scale companies like Jingdong and Taobao, because the whole process is very complicated. What should we do then? You can see the following example

First we create a new test case:

public class T{
    public static void main(String[] args) throws Exception{
                    for(;;) { System.in.read();newTT().m(); }}}Copy the code
public class TT{
        public void m(a){
        System.out.println(2); }}Copy the code

Using the command javac *.java, compile to a class file and then run the T file

[root@VM-0-7-centos t]# java T
a
2
2
Copy the code

When we typed a, we printed 2, but when we went live, we realized that we needed the output 1, which is to republish live if we want to change from local. For this one change, obviously not worth it, but if we used Redafine hot deployment it would help us replace it directly without having to republish the JVM

Then we attached the T program to Arthas

Java (vi tt.java), and change the value of 2 to 1

public class TT{
        public void m(a){
            System.out.println(1); }}Copy the code

Then compile and execute javac tt.java ““

Arthas top executive of call in back to us redefine/usr/local/MXN fuccGc/t/TT. Class

[arthas@398842]$ redefine /usr/local/mxn/fuccGc/t/TT.class
redefine success, size: 1, classes:
TT
Copy the code

Success You can see that we have successfully replaced the class file without restarting it

watch

Watch: Data observations performed by the arthas method. You can monitor a class using the Watch command. After monitoring, run your function and reproduce the scene

trace

Output method call path, and output time, this instruction is very useful for optimizing the code, you can see the specific execution time of each method, if it is for loop and other repeated statements, you can also see n times in the loop maximum time, minimum time, and average time, perfect!

tt

After we turn on tt for a method, the input and return parameters for each call (we can set the maximum number of times to monitor) are recorded and these different time calls can be observed

[arthas@405136]$ tt -t FullGCTest modelFit

-t The tt command has many main parameters. -t is one of them. This parameter indicates that you want to record each execution of the print method of class *Test. -n 3 If you execute a low-volume method you may have enough time to interrupt the TT command recording process with CTRL+C, but if you run a very high-volume method, your JVM memory can be overwhelmed in an instant.

At this point you can specify the number of times you need to log with the -n parameter. Arthas will actively interrupt the TT command when this number of times is reached to prevent manual operations from being stopped.

Ognl expressions

Ognl expressions

OGNL special usage: github.com/alibaba/art… OGNL expressions official guidelines: commons.apache.org/proper/comm…

Call static function: ognl ‘@[email protected](“hello”)’ get static field of static class: ogNL ‘@FullGCTest@random’

Arthas also supports Web Console, see: alibaba. Making. IO/Arthas/Web -…

conclusion

Arthas is an online Debug tool. Compared to other tools, Arthas has a comprehensive set of features and is easy to use. It is also easy for beginners to master.

Original is not easy, I hope you can support a lot, remember a key three even!!

I am a small farmer, afraid of what infinite truth, further have further joy, everyone come on!