tags: java, troubleshooting, monitor,arthas
Summarized in one sentence:
Arthas
is Alibaba open-source Java diagnostic tools for online analysis and diagnosis java application, it is regarded as a master of java monitoring tool, this article it will be introduced.
1 Introduction
An article on the "java application monitoring (7) - line dynamic diagnostic artifacts BTrace" we mentioned, to monitor the application online at if you need non-stop service, the need to use dynamic tracking technology, BTrace
is a good dynamic tracking tools, but use is still a bit complicated (need to write script), then there is no more that a simple tool, that is Arthas
, and it is open-source Java Ali diagnostic tools to simplify the dynamic tracking technology, and with it, you can view the class loading information directly , JVM information, thread stack information, tracking the implementation of the method, decompile class file, etc., covering the aforementioned java command-line tools and functionality BTrace
features. Without too much extra work, just the familiar Arthas
commands can be provided, it is convenient. In fact, Arthas
the bottom with BTrace
, is based jvm Agent
manner using Instrumentation
modify and execute bytecode embodiment, output. This article will Arthas
use are introduced, in view of the official documents have been written very detailed, suggest that you go directly to the official documentation to learn, it can only make start a discussion. Official website address :https://alibaba.github.io/arthas
2 Arthas installation and operation
2.1 download Arthas
Official recommended direct download jar run
wget https://alibaba.github.io/arthas/arthas-boot.jar
复制代码
2.2 running Arthas
Place the downloaded arthas-boot.jar
java application server where you want to pack into the monitor, with the Spring Boot
application directly like the java command run.
java -jar arthas-boot.jar
复制代码
note:
- The first time you run, you can use slow download
--repo-mirror aliyun --use-http
- After the start, it will list the current java application list (a bit like
jps -l
), the output number to select the application you want to monitor.
Carried out after the start Arthas
command line interface can be used Arthas
to achieve the required monitoring function commands provided. Below, the need to monitor the java application is exemplary java-monitor-example
.
2.3 Exit
If only exit the current connection, you can use quit
or exit
command. Attach to arthas the target process will continue to run, will remain open port, you can connect directly when connecting next time.
If you want to completely withdraw arthas, you can execute shutdown
the command.
3 Arthas use
Arthas
The command to use is the need to learn to use the functionality it provides, mainly divided into several categories:
- Basic
help
commands:cat
,pwd
,history
, ,quit
and so on, almost with linux commands. - jvm Related:
dashboard
,thread
,jvm
,sysenv
and so on, mainly to monitor JVM information, learn java command-line tool with beforejinfo
,jmap
,jstack
etc. have the same purpose. - class / classloader
sc
Related:sm
,jad
,dump
, ,classloader
and so on. - monitor / watch / trace related:
monitor
,watch
,trace
,stack
etc., these functions covering theBTrace
functions implemented, includes a timing detecting process parameters, return values, call duration and so on.
The following are a few commonly used commands will be described in detail a list of commands, please refer to the official documentation.
3.1 Overview:dashboard
Start Arthas
after -h
viewing use the 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 overview, show threads, memory, gc situation, vm tomcat circumstances and information in a single interface. As shown below (the example of FIG official document):
The overview of information about the default five seconds to refresh, to memory changes, threads occupancy, GC frequency glance. Use ctrl+c
exit.
3.2 Thread information:thread
Remember jstack
it, we need to first identify the thread ID, use it to export thread stack, and then use the thread ID view. In Arthas
the much more convenient, like the above dashboard
, there have been ID, direct use thread id
can be. -h
Our 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 above EXAMPLES
, use the thread
command, you can find the highest first N threads occupied the CPU ( -n
), you can print the specified thread running stack ( id
) to find out the current thread blocking other threads ( -b
), so the problem is to analyze thread Convenience.
3.3 JVM information:jvm
jvm
Command is very simple, no parameters, it outputs information including operating parameters, class loading information, memory, the system information, the number of threads of information, such as file descriptors. A bit like jvisualvm
in 概述
, but more detailed than it.
3.4 decompile:jad
Sometimes applications need to detect the line running, the new code whether they have used or if there are updates to be loaded to decompile the class out to see whether the latest source, then jad
is useful. -h
Print Using 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 above EXAMPLES
, JAD can decompile class ( class-pattern
), a method is decompiled class ( method-name
), if there are multiple classLoader
, may also be used -c
to select which display the like.
3.5 Monitoring execution method:monitor
monitor
Implementation can be timed output method of monitoring, including the number of calls, the number of successes, failures, the average length of time, failure rate, a bit like BTrace
in @Timer
, but more convenient. -h
View use the 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 above EXAMPLES
, the implementation of the monitoring method can default 60s is output once, and use -c
time interval to modify the output.
3.6 method of performing data monitoring:watch
Similar to BTrace
the @OnMethod
application on-line if you want to perform in the parameters of the method, the return value, exception information, watch
the command is very appropriate. -h
Using 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 above EXAMPLES
, the timing of monitoring the implementation of the method are front ( -b
), the method execution ends ( -f
default value) method is successful ( -s
). Monitoring includes: a parameter ( params
), the return value ( returnObj
), abnormal ( throwExp
) and the like.
4 Summary
Arthas
Powerful features, which can be carried out comprehensive monitoring of java application, is a good helper online application monitoring, the paper is relatively simple to Arthas
install and use the carrying out entry talks, initiate, hope that we can on Arthas
the official website for further learning, which has been It has detailed explanations and examples of each command, so that their monitoring of java technology is more solid.
Data sheet
Arthas
Official website:https://alibaba.github.io/arthas/
Related Reading
- java application monitoring (1) -java programmer should be aware of the application monitoring technology
- java application monitoring secret (2) -java command
- java application monitoring (3) - These command-line tools you have mastered yet
- java application monitoring (4) - online troubleshooting routine
- java application monitoring (5) - visual monitoring tools
- java application monitoring (6) - third-party memory analysis tool MAT
- java application monitoring (7) - line dynamic diagnostic artifacts BTrace