Arthas

To quote an official quote:

Arthas is an open source Java diagnostic tool for Alibaba that developers love. 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?
  • Arthas uses command line interaction and rich Tab completion functionality to further facilitate problem location and diagnosis.

And then the last picture:

Here you can see the commands Arthas supports. For details on how to use each command, see the official documentation: Alibaba.github. IO/Arthas.

The reason why Arthas is mentioned here is that although VisualVM has done a great job in dumping, monitoring, snapshots, etc., it still falls short in some areas! This is where Arthas can play to its strengths. Specific areas are as follows:

  • thread You can view the CPU usage of a thread(As you can also see from the previous document, VisualVM has some trouble viewing thread CPU percentages.)
  • redefine Load the external class file into the application(VisualVM doesn’t have this feature, and the plugin market doesn’t seem to find it either.)
  • monitor Monitor method calls, successes, failures, average RT, etc
  • watch tt Before, after, at the end of the execution of the observation method, abnormal, time-consuming, input parameter (the depth of input parameter attribute is adjustable), return value, abnormalTo support real-time monitoring of each method execution and all method invocation execution.
  • jadDecompile the class file (no longer need to decompress it from the JAR and decompile it using jad)
  • sc smQuickly search for class and method information
  • getstaticView class static variables (VisualVM only looks at instance properties)
  • syspropModifying System Attributes
  • traceCheck the methodCall tree TimeVisualVM can also look, but VisualVM feels more like looking globally for a method on a thread, whereas Arthas can directly locate a method to observe, andSupport setting conditionsPrinting, such as when the time exceeds a value, when the input parameter is a value, and so on.
  • stackLook at all of the call tree paths of methods, again VisualVM can actually see it, but it still feels global to find local, while Arthas is direct to local methods, same thingSupport setting conditionsWhen the elapsed time exceeds a certain value, all call paths of the method are output.

To sum up: VisualVM and Arthas can complement each other, the former is more of a global monitor, monitoring the overall performance! The latter is more like a local debugger, which can monitor methods and even refine the parameters before and after the method is executed, so it is more often used for debugging and troubleshooting!

Basic commands and parameters

Here are the basic commands I use:

  • Help View command help, eg: help getstatic
  • CLS CLS
  • Quit Exits the current Arthas client
  • Shutdown exits all open Arthas clients

Here are the basic parameters I use:

  • -n Limits the number of lines to be printed. For example, some methods may print furiously during program execution.
  • -i Sets the printing interval, in milliseconds
  • -x Attribute traversal depth. The default value is 1.

Common expressions:

  • * Any number of arbitrary characters
  • #cost time consumed

View the thread CPU ratio

Thread-n 3 // View the top three threads in CPU usage thread-i 1000 // IntervalCopy the code

Loading an external class

Define -p d:// TMP/test. class // Define -c 256a485d -p d:// TMP/test. class // define -c 256a485d -p d:// TMP /Test. 256a485d is the hashcode of the classloader. You can run the classloader command to obtain the hashcodeCopy the code

Monitor method invocation times, success times, etc

Monitor-c 5 cn. Localhost01.* check // Statistical period 5 seconds, 120 seconds by defaultCopy the code

Monitoring methods such as input parameter return

The real-time detection method is executed once

watch cn.localhost01.* check "params,reurnObj" -x 2 -b -s-b (before calling), -b (before calling), -b (before calling)-e(Abnormal),-s(After return),-f(End) Watch cn. Localhost01.* Check"params,returnObj" "params[0].name.equals('abc')"-x 3 -b // Watch cn. Localhost01.* check is printed only when conditions are met"params,returnObj" #cost>200 -x 3
Copy the code

Detect and record all method executions

tt -t -n 20 cn.localhost01.* check
Copy the code

After logging for a while, you need to look at some of the methods:

tt -l//-list displays the INDEX of each methodCopy the code

Find a way:

tt -s method.name="check"//-search to check the method named checkCopy the code

To pinpoint a method based on INDEX:

Tt -i 1001 //-index to view the method whose index is 1001Copy the code

Redo a call:

Tt -i 1001 -p //-play is executed againCopy the code

decompiling

jad cn.localhost01.demo.Checker
Copy the code

Quick search

Sc cn.*.Checker // Search class sm *.Checker check // search methodCopy the code

Viewing static variables

Checker pool getstatic *.Checker poolCopy the code

Modifying System Attributes

Sysprop Java. Version 1.7Copy the code

View method call tree time

* check params.length==2 // Time for checking the call tree of the check method with two parameters Trace cn. Loacalhost01.* check#cost>500 // Check method call tree time that takes more than 500 ms
Copy the code

View all call tree paths for a method

Stack cn. Loacalhost01.* check params[0]==78 // View the call tree time of the check method whose first parameter value is 78#cost>500 // Check method call tree time that takes more than 500 ms
Copy the code

Pay attention to

  1. The classpath methodName command is often used. For methods, if they have overloads, how do you distinguish which method to monitor?

    • Tt -t cn. Localhost01.* check “params.length==2” // Method containing two parameters
    • Tt -t cn. Localhost01.* check “params[0] instanceof String” // The first parameter must be of String type
    • Tt -t cn. Localhost01.* check “params[1]. Password =’123456′” // Second parameter value The value of the object’s password attribute is 123456
  2. Bat pid is not the pid input, it is a bug.