introduction
It is an open-source Java diagnostic tool by Ali, which further simplifies the dynamic tracking technology. With it, you can directly view class loading information, JVM information, thread stack information, tracking method execution status, decompiled class files, etc., basically covering the above mentioned to the functionality of the java command line tool and the functionality of BTrace. It doesn't require too much extra work, just be familiar with the commands provided by Arthas, which is very convenient. In fact, the bottom layer of Arthas is the same as BTrace, based on the jvmAgent method, using the Instrumentation method to modify the bytecode method and then execute and output it. This article will introduce the use of Arthas. Since the official documents have been written in great detail, it is recommended that you read the official documents directly to learn. This article is just for starters. Official website address: https://alibaba.github.io/arthas
Arthas installation and operation
download Arthas
The official recommendation is to directly download the jar and run it
wget https://alibaba.github.io/arthas/arthas-boot.jar
Run Arthas
Put the downloaded arthas-boot.jar package on the server where the java application you want to monitor is located. Just like the SpringBoot application, you can run it directly with the java command.
java -jar arthas-boot.jar
Notice:
- For the first time running, if the download is slow, you can use --repo-mirror aliyun–use-http
- After startup, it will list the current java application list (a bit like jps-l), and output the serial number to select the application you want to monitor.
After starting, the command line interface of Arthas can be used, and the commands provided by Arthas can be used to realize the functions that need to be monitored. As shown in the figure below, the java application to be monitored is the example java-monitor-example.
quit
If you just exit the current connection, you can use the quit or exit command. Arthas attached to the target process will continue to run, the port will remain open, and you can connect directly the next time you connect.
If you want to completely exit arthas, you can execute the shutdown command.
Arthas use
The use of Arthas is to learn to use the command functions it provides, which are mainly divided into several categories:
- Basic commands: help, cat, pwd, history, quit, etc., are similar to linux commands.
- Jvm-related: dashboard, thread, jvm, sysenv, etc., mainly for monitoring JVM information, which is similar to learning java command-line tools jinfo, jmap, jstack, etc. before.
- class/classloader related: sc, sm, jad, dump, classloader, etc.
- Monitor/watch/trace related: monitor, watch, trace, stack, etc. These functions basically cover the functions implemented in BTrace, including timing detection, method parameters, return value, call duration, etc.
Several commonly used commands are described below. For a detailed list of commands, please refer to the official documentation.
Overview: dashboard
After starting Arthas, use -h to view the usage help:
$ dashboard -h
USAGE:
dashboard [-b] [-h] [-i <value>] [-n <value>]
SUMMARY:
Overview of target jvm's thread, memory, gc, vm, tomcat info.
EXAMPLES:
dashboard
dashboard -n 10
dashboard -i 2000
Equivalent to an overview, it displays threads, memory, gc status, vm status and tomcat information in one interface. As shown in the following picture (an example picture of the official document):
The information in this overview is refreshed every 5 seconds by default. You can see memory changes, thread usage, and GC times at a glance. Use ctrl+c to exit.
Thread information: thread
Remember jstack, we need to find out the thread ID first, use it to export the thread stack, and then use the thread ID to view it. It is much more convenient in Arthas, as in the dashboard above, there is already an ID, just use the thread id directly. -h to view the help documentation:
$ thread -h
USAGE:
thread [-h] [-b] [-i <value>] [-n <value>] [id]
SUMMARY:
Display thread info, thread stack
EXAMPLES:
thread
thread 51
thread -n -1
thread -n 5
thread -b
thread -i 2000
OPTIONS:
-h, --help this help
-b, --include-blocking-thread Find the thread who is holding a lock that blocks the most number of threads.
-i, --sample-interval <value> Specify the sampling interval (in ms) when calculating cpu usage.
-n, --top-n-threads <value> The number of thread(s) to show, ordered by cpu utilization, -1 to show all.
<id> Show thread stack
As shown in the EXAMPLES above, using the thread command, you can find out the top N threads that occupy the most CPU (-n), you can print the running stack (id) of the specified thread, and find out the thread that is currently blocking other threads (-b), This is very convenient to analyze thread problems.
JVM information: jvm
The jvm command is very simple and has no parameters. The information it outputs includes running parameters, class loading information, memory status, system information, thread number information, file descriptors, etc. Kind of like jvisualvm's overview, but more detailed than that.
Decompile: jad
Sometimes it is necessary to check whether the new code is used or updated in the application running online. You can decompile the loaded class to check whether the source code is the latest. At this time, jad is very useful. -h print usage help:
$ jad -h
USAGE:
jad [-c <value>] [-h] [-E] [--source-only] class-pattern [method-name]
EXAMPLES:
jad java.lang.String
jad java.lang.String toString
jad --source-only java.lang.String
jad -c 39eb305e org/apache/log4j/Logger
jad -c 39eb305e -E org\\.apache\\.*\\.StringUtils
OPTIONS:
-c, --code <value> The hash code of the special class's classLoader
-h, --help this help
-E, --regex Enable regular expression to match (wildcard matching by default)
--source-only Output source code only
<class-pattern> Class name pattern, use either '.' or '/' as separator
<method-name> method name pattern, decompile a specific method instead of the whole class
As shown in the EXAMPLES above, jad can decompile a class (class-pattern), decompile a certain method (method-name) of a class, if there are multiple classLoaders, you can also use -c to choose which one to display, etc.
Method execution monitoring: monitor
The monitor can regularly output the execution of the method for monitoring, including the number of calls, the number of successes, the number of failures, the average duration, the failure rate, etc. It is a bit like @Timer in BTrace, but more convenient. -h View usage help:
$ monitor -h
USAGE:
monitor [-c <value>] [-h] [-n <value>] [-E] class-pattern method-pattern
SUMMARY:
Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc.
Examples:
monitor org.apache.commons.lang.StringUtils isBlank
monitor org.apache.commons.lang.StringUtils isBlank -c 5
monitor -E org\.apache\.commons\.lang\.StringUtils isBlank
OPTIONS:
-c, --cycle <value> The monitor interval (in seconds), 60 seconds by default
-h, --help this help
-n, --limits <value> Threshold of execution times
-E, --regex Enable regular expression to match (wildcard matching by default)
<class-pattern> Path and classname of Pattern Matching
<method-pattern> Method of Pattern Matching
As shown in the EXAMPLES above, you can monitor the execution of the method. The default is to output once every 60s. You can use -c to modify the output interval.
Method execution data monitoring: watch
Similar to @OnMethod of BTrace, if you want to execute the parameters, return value, and exception information of the method in the online application, the watch command is very suitable. -h use help:
$ watch -h
USAGE:
watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class-pattern method-pattern express [condition-express]
SUMMARY:
Display the input/output parameter, return object, and thrown exception of specified method invocation
The express may be one of the following expression (evaluated dynamically):
target : the object
clazz : the object's class
method : the constructor or method
params : the parameters array of method
params[0..n] : the element of parameters array
returnObj : the returned object of method
throwExp : the throw exception of method
isReturn : the method ended by return
isThrow : the method ended by throwing exception
#cost : the execution time in ms of method invocation
Examples:
watch -b org.apache.commons.lang.StringUtils isBlank params
watch -f org.apache.commons.lang.StringUtils isBlank returnObj
watch org.apache.commons.lang.StringUtils isBlank '{params, target, returnObj}' -x 2
watch -bf *StringUtils isBlank params
watch *StringUtils isBlank params[0]
watch *StringUtils isBlank params[0] params[0].length==1
watch *StringUtils isBlank params '#cost>100'
watch -E -b org\.apache\.commons\.lang\.StringUtils isBlank params[0]
OPTIONS:
-b, --before Watch before invocation
-e, --exception Watch after throw exception
-x, --expand <value> Expand level of object (1 by default)
-f, --finish Watch after invocation, enable by default
-h, --help this help
-n, --limits <value> Threshold of execution times
-E, --regex Enable regular expression to match (wildcard matching by default)
-M, --sizeLimit <value> Upper size limit in bytes for the result (10 * 1024 * 1024 by default)
-s, --success Watch after successful invocation
<class-pattern> The full qualified class name you want to watch
<method-pattern> The method name you want to watch
<express> the content you want to watch, written by ognl.
Examples:
params
params[0]
'params[0]+params[1]'
'{params[0], target, returnObj}'
returnObj
throwExp
target
clazz
method
As shown in the EXAMPLES above, the monitoring timings are before method execution (-b), method execution end (-f, default value), and method execution success (-s). The monitoring content includes: parameters (params), return value (returnObj), exception (throwExp), etc.
Summarize
The powerful functions of Arthas can basically monitor java applications comprehensively. It is a good helper for online application monitoring. This article provides a relatively simple introduction to the installation and use of Arthas. I hope you can go to the official website of Arthas for further study. , which already has detailed explanations and examples for each command, so as to make your own java monitoring technology more solid.