Java application monitoring (8)-Alibaba diagnostic tool arthas

Java application monitoring (8)-Alibaba diagnostic tool arthas

Welcome everyone to pay attention to github.com/hsfxuebao/j... I hope it will be helpful to you. If you think it is possible, please give me some Star

Reprinted from: juejin.im/post/684490...

In one sentence: It Arthasis Alibaba's open source Java diagnostic tool, suitable for online analysis and diagnosis of Java applications. It is a master of Java monitoring tools. This article will introduce it.

1 Introduction

In the previous article "Java Application Monitoring (7)-Online Dynamic Diagnosis Tool BTrace", we mentioned that if you need to monitor online applications without stopping the service, you need to use dynamic tracking technology, which BTraceis a good dynamic tracking tool, 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 method execution, decompiled class files, etc., basically cover the functions and BTracefunctions of the java command line tool mentioned earlier . There is no need for much extra work, just be familiar with the Arthasprovided commands, which is very convenient. In fact, the Arthasbottom layer is the BTracesame, it is based on the jvm Agentway, using the Instrumentationway to modify the bytecode way and then execute and output. This article will Arthasintroduce the usage. Since the official documents have been written in detail, I suggest you read the official documents directly to learn. Official website address :https://alibaba.github.io/arthas

2 Arthas installation and operation

2.1 Download Arthas

The official recommendation is to download and run the jar directly

wget https://alibaba.github.io/arthas/arthas-boot.jar
 
 

2.2 Run Arthas

Put the downloaded arthas-boot.jarpackage on the server where the java application you want to monitor is located, just like the Spring Bootapplication, you can run it directly with the java command.

java -jar arthas-boot.jar
 
 

note:

  • Run for the first time, download is slow and can be used--repo-mirror aliyun --use-http
  • After startup, the current java application list will be listed (a bit like jps -l), output the serial number and select the application you want to monitor.

ArthasThe command line interface that is carried out immediately after startup , you can use the Arthasprovided commands to realize the functions that need to be monitored. As shown in the figure below, the java application that needs to be monitored is an example java-monitor-example.

2.3 Exit

If you just exit the current connection, you can use the quitor exitcommand. The arthas attached to the target process will continue to run, the port will remain open, and you can directly connect to it next time you connect.

If you want to exit arthas completely, you can execute the shutdowncommand.

3 Arthas use

ArthasTo use is to learn to use the command functions it provides, which are mainly divided into several categories:

  • Basic helpcommands: cat, pwd, history, , quitand so on, almost with linux commands.
  • jvm Related: dashboard, thread, jvm, sysenvand so on, mainly to monitor JVM information, learn java command-line tool with before jinfo, jmap, jstacketc. have the same purpose.
  • class/classloader scRelated: sm, jad, dump, , classloaderand so on.
  • monitor/watch/trace related: monitor, watch, trace, stacketc., these functions covering the BTracefunctions implemented, includes a timing detecting process parameters, return values, call duration and so on.

The following is a description of several commonly used commands. For a detailed list of commands, please refer to the official documentation.

3.1 Overview: dashboard

After starting Arthas, -hcheck 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

 
 

It is equivalent to an overview, displaying thread, memory, gc status, vm status and tomcat information in one interface. As shown in the figure below (a sample figure of the official document):

The information in this overview is refreshed every 5 seconds by default, and it is clear at a glance for memory changes, thread occupancy, and GC times. Use ctrl+cexit.

3.2 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 Arthasis much more convenient in the middle, like the above dashboard, there is already an ID, just use it directly thread id. -hView the help document:

$ 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, using the threadcommand, you can find out the top N threads occupying the highest CPU ( -n), you can print the running stack of the specified thread ( id), and find out which thread is currently blocking other threads ( -b), so as to analyze thread problems. Convenience.

3.3 JVM information: jvm

jvmThe command is very simple and has no parameters. The information it outputs includes operating parameters, class loading information, memory conditions, system information, thread number information, file descriptors, and so on. It's a bit like jvisualvmthe one , but it's more detailed.

3.4 Decompilation: jad

Sometimes it is necessary to check whether the new code is used or updated in the online application. You can decompile the loaded class and check whether the source code is the latest, which is jadvery useful at this time . -hPrinting 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 a class ( class-pattern), decompile a certain method of a class ( method-name), if there are more than one classLoader, you can also use it -cto choose which one to display, etc.

3.5 Method execution monitoring: monitor

monitorYou can monitor the execution of the method at regular intervals, including the number of calls, the number of successes, the number of failures, the average duration, and the failure rate. It's a bit like the BTracemiddle one @Timer, but it's more convenient. -hView 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 above EXAMPLES, you can monitor the execution of the method. The default is to output once in 60s, and you can use it -cto modify the output interval.

3.6 Method execution data monitoring: watch

Similar to BTracethe @OnMethodapplication on-line if you want to perform in the parameters of the method, the return value, exception information, watchthe command is very appropriate. -hUsing 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 monitoring timing is before method execution ( -b), method execution ends ( -f, default value), and method execution is successful ( -s). Monitoring content includes: parameters ( params), return value ( returnObj), exception ( throwExp), etc.

4 summary

ArthasThe powerful function of the Java application can basically be used for comprehensive monitoring of java applications. It is a good helper for online application monitoring. This article is relatively simple to Arthasexplain the installation and use of the introductory, and I hope everyone can go to the Arthasofficial website to learn more. There are detailed explanations and examples for each command, so as to make your java monitoring technology more solid.

Parameter data

  • ArthasOfficial website:https://alibaba.github.io/arthas/