preface

1. What is Arthas?

Arthas is an open source Java diagnostic tool for Alibaba that is well loved by developers (as of 2020.9.19 github Star is 23K). Arthas allows us to troubleshoot problems online without having to restart; Dynamically trace Java code; Monitor JVM status in real time.

What are the features of Arthas

  • View the running status of the system in real time
  • View the arguments, return values, and exceptions of the function call
  • Code online hot update
  • Solve class conflicts in seconds and locate the class loading path
  • Quickly locate application hot spots and generate flame maps
  • Online diagnosis, click on the web diagnostic online application

3. What problems does Arthas help us solve

Arthas can help you when you are stuck with a problem like the following:

  • From which JAR is this class loaded? Why are all kinds of class-related exceptions reported?
  • Why didn’t the code I changed execute? Did I not commit? Got the branch wrong?
  • If you encounter a problem, you cannot debug it online. Can you only re-publish it by logging?
  • There is a problem with a user’s data processing online, but it cannot be debugged online, and it cannot be reproduced offline!
  • Is there a global view of the health of the system?
  • Is there any way to monitor the real-time health of the JVM?
  • How to quickly locate application hot spots, generate flame map?

4, installation,

Download arthas-boot.jar and start it with java-jar:

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

The sample project in this article runs on Docker, so it takes a different approach

docker exec -it  ${containerId} /bin/bash -c "wget https://arthas.aliyun.com/arthas-boot.jar && java -jar arthas-boot.jar"
Copy the code

But in the process of execution, it might appear

/bin/bash: wget: command not found
Copy the code

The solution is as follows

Docker exec it ${containerId} /bin/bash apt-get update apt-get install wgetCopy the code

Command interpretation

1. Help command related

Help (View command help information)

It’s a personal habit. Whenever learning a new thing, will be used to look at the help, read throughTo learn more about the usage of a specific command, use Thread as an example

help thread
Copy the code

There are detailed thread parameters and examples. This feels like the essence of this article, after all, you want other commands, direct

The help commandCopy the code

But for the sake of hydrology, a few more

2. JVM related

Dashboard (a real-time display of current system information such as threads, memory usage, GC, etc., refreshed every 5 seconds by default)

note: Press CTRL + C to exit the panel

JVM (view current JVM information such as gc collector count and elapsed time)

Thread (View current thread information, view thread stack)

A. View the current top N busiest threads and print the stack

thread -n 3
Copy the code

The above command achieves the same effect as we typed before

top -H -p pid
printf '%x\n'pid
jstack pid |grep 'nid'- C5 - colorCopy the code

similar

B, thread-b, find the thread that is currently blocking another thread

Note: currently only support to find out the synchronized keyword blocked threads, if is a Java util. Concurrent. The Lock, it is not support

C, thread –state: displays the status of the specified thread

3. Log related

Logger (View logger information, update Logger level)

3.1,Viewing Logger Information 3.2,Dynamically update Logger level

Procedure For modifying log levels

A, find the classloader hashCode of the current class

sc -d com.example.springdemo.user.service.impl.UserServiceImpl | grep classLoaderHash
Copy the code

B. Obtain logger with OGNL

ognl -c 31cefde0 '@com.example.springdemo.user.service.impl.UserServiceImpl@log'
Copy the code

Can know from above com. Example. Springdemo. User. Service. Impl. Actual use logback UserServiceImpl @ the log. You can see that level=null, indicating that the actual final level came from the root Logger.

C, Set UserServiceImpl logger level to WARN


ognl -c 31cefde0 '@com.example.springdemo.user.service.impl.UserServiceImpl@log.setLevel(@ch.qos.logback.classic.Level@WARN)'
Copy the code

You can see that the log level has been changed to WARN

Class /classloader

Jad (decompile source code specifying loaded classes)

Sc (View information about classes loaded by JVM)

MC (memory compiler, compile.java file generation.class)

Define (Load an external.class file, define the classes that the JVM has loaded)

Why introduce these several, because these several together can achieve dynamic online update code. The steps are as follows

A. Jad decompiles the code to be updated


jad --source-only com.example.springdemo.user.service.impl.UserServiceImpl > /tmp/UserServiceImpl.java
Copy the code

B, SC finds the ClassLoader to load the code to update

sc -d com.example.springdemo.user.service.impl.UserServiceImpl | grep classLoaderHash
Copy the code

C, keep/TMP/UserServiceImpl. Java, using the MC (Memory Compiler) command to compile, and through – classLoaderClass parameter to specify this


mc --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader /tmp/UserServiceImpl.java -d /tmp
Copy the code

D. Run the re-define command to reload the newly compiled userServicePl. class

redefine /tmp/com/example/springdemo/user/service/impl/UserServiceImpl.class
Copy the code

5. Monitoring related

Monitor (the method performs monitoring, which can monitor the number of calls to the method, the number of successes, the number of failures, the average response time, and the failure rate)

Note: This is a non-real-time return command, statistical period, default value is 120 seconds

monitor -c 5 com.example.springdemo.user.service.impl.UserServiceImpl getUserById
Copy the code

Watch (Watches calls to the specified method. Observable range: return value, throw exception, input parameter)

watch com.example.springdemo.user.service.impl.UserServiceImpl getUserById "{params,returnObj}" -x 2
Copy the code

Watch Parameter Description

Trace (method internal call path and output time spent on each node on the method path)

Note: Trace can conveniently help you locate and find performance defects caused by high RT, but it can only trace the call link of first-level methods each time.

trace com.example.springdemo.user.service.impl.UserServiceImpl getUserById
Copy the code

Stack (prints the call path of the current method being called)

stack com.example.springdemo.user.service.impl.UserServiceImpl getUserById
Copy the code

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)

The beauty of this command is that it records the context of each invocation of the current method and can be played back

tt -t com.example.springdemo.user.service.impl.UserServiceImpl getUserById
Copy the code

B. Select an index to replay

tt -i 1000 -p
Copy the code

note: these monitoring commands, all through the bytecode enhancement technology to realize, will insert some aspects in the specified class methods to implement the data statistics and observation, so online, use pretest, please try to clear need classes, methods and conditions for observation, diagnosis end to stop or class that will enhance the reset command.

conclusion

This article focuses on some of the uses of Arthas, which are covered in detail on the official website. For those of you interested in Arthas, visit Arthas’s website

arthas.aliyun.com/zh-cn/

It is also recommended to visit

Arthas.aliyun.com/doc/arthas-…

In addition to practicing Arthas online, you can also view user stories to help you get started with Arthas

Reference documentation

arthas.aliyun.com/zh-cn/