banner
lMingyul

lMingyul

记录穿过自己的万物
jike
twitter
github
bilibili

Using the Series - Introduction to Arthas

When diagnosing fault issues in the company, we generally print logs in key parts of the code and then analyze the problem by replacing the Jar package in the environment container. However, this process can be quite cumbersome:

  • First, you need to ensure the comprehensiveness of the logs you print. If some key information is not printed, you will need to reprint, replace the package, and restart the service, which wastes a lot of time.
  • Secondly, not all environments support package replacement and service restarts.

So recently, I have been looking for better methods for fault diagnosis, and I found that Alibaba's open-source Java service diagnostic tool seems quite good. It can view method call parameters, return values, called paths, call durations, method call counts, success counts, failure counts, etc., all of which can be recorded. Therefore, I will record this tool for learning purposes.

What is Arthas#

Official introduction:
Arthas is an online monitoring and diagnostic product that allows real-time viewing of application load, memory, GC, and thread status information from a global perspective. It can diagnose business problems without modifying application code, including viewing method call parameters, exceptions, monitoring method execution time, class loading information, etc., greatly improving the efficiency of online problem troubleshooting.

Operating Environment#

  • Only supports JDK 6 and above
  • Written in Java, supports cross-platform: supports Linux (mainly), Mac, Windows

Features#

  • Uses command-line interactive mode
  • Provides Tab key auto-completion functionality

Initial Use#

Since the environment used by the company is mainly in containers, the following mainly records how to use this tool in a Linux environment.

Download Usage Package#

Due to the company's environment being an intranet, direct access to GitHub for downloading installation packages is not supported. To prevent network issues from preventing downloads, the method used is to manually download from GitHub and copy it to the service container.

Download the complete installation package from GitHub, download address: https://github.com/alibaba/arthas/releases

CleanShot-2023-04-30-00-13-53@2x

Unzip in Container Environment#

CleanShot-2023-04-30-00-44-29@2x

Uninstall#

After locating the problem, it's also important to clean up the battlefield, so the method for uninstalling this tool is also recorded.

You can uninstall the tool by executing the following three steps:

Run#

First, start a Java program service that will not stop. The official installation package comes with a Jar package for practice: math-game.jar (however, our services generally run continuously, so here we use the official package for record-keeping).

Then start arthas

CleanShot-2023-04-30-00-50-48@2x


Common Commands#

help#

dashboard#

Dashboard: Displays the real-time data panel of the current system. When there is no dashboard, we generally can only view system operation information through the built-in top command in Linux.

Enter dashboard, press Enter, and it will display the current process information. Press Ctrl+C or enter q to interrupt execution.
CleanShot-2023-04-30-01-01-50@2x

The displayed information is roughly divided into three main sections:

  • The top section is thread-related information
  • The middle area is JVM memory-related information
  • The bottom section is information about the Java runtime environment
    For specific information in each column, please refer to the official documentation.

thread#

View the current thread information stack

When there are no parameters, display the first page of thread information#

By default, it is sorted in descending order by CPU increment time and only displays the first page of data.

CleanShot-2023-05-03-11-59-07@2x

Supports one-click display of the top N busiest threads and print the stack#

CleanShot-2023-05-03-12-00-56@2x 1

thread --all, display all matching threads#

CleanShot-2023-05-03-16-43-24@2x

thread id, display the running stack of the specified thread#

thread -b, find the thread that is currently blocking other threads#

watch#

Observe the call situation of the specified method
You can observe:
Method return value, parameters, exceptions thrown by the method, and you can also view corresponding variables by writing OGNL expressions.

Observe the parameters, this object, and return value when the function call returns#

Observe both the function call before and after the function returns#

Observe the properties in the current object#

If you want to view the properties in the current object before and after the function runs, you can use the target keyword, where target represents the current object, and then use target.field_name to access a specific property of the current object.

trace#

The internal call path of the method, outputting the time spent at each node along the path, is used when the service call time is too long.

  • In the output result, #24 indicates that the primeFactors() function was called at line 24 of the source file.
  • In the output result, #25 indicates that the print() function was called at line 25 of the source file.

stack#

Output the call path of the current method. When we need to know where this method (which has been called from many places) started executing, we can use this command (suitable for tracing back).

jad#

Decompile the source code of the specified loaded class for easier understanding of business logic online. The decompiled code is syntax-highlighted.

CleanShot-2023-05-11-00-34-53@2x


Reference Materials#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.