tags: java, troubleshooting, monitor,arthas


Arthas is an open source Java diagnostic tool for online analysis and diagnosis of Java applications. Arthas is an aggregator of Java monitoring tools and will be introduced in this article.

1 the introduction

An article in the Java application monitoring (7) – on-line dynamic diagnosis artifact BTrace we mentioned, if you need to keep monitoring was carried out on the online application service case, you need to use the dynamic tracking technology, BTrace dynamic tracking tool is very good, but still a little complicated to use (need to write a script), so there is no more a little bit more simple tools, That is is ali Arthas, it open source Java diagnostic tool, further simplified the dynamic tracking technology, it can directly view class loading information, JVM, thread stack information, tracking method execution, decompiled class files, etc., basic covers the aforementioned Java command line tools and the BTrace functions. You don’t need to do much extra work, just be familiar with the commands Arthas provides, which is handy. Arthas, like BTrace, is based on the JVM Agent, using Instrumentation to modify the bytecode mode and then execute and output. This article will introduce the use of Arthas, and since the official documentation is well written, it is recommended that you read the official documentation to learn about Arthas. Website address: https://alibaba.github.io/arthas

2 Arthas installation and operation

2.1 download Arthas

The official recommendation is to download the JAR directly to run

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

2.2 run Arthas

Put the downloaded arthas-boot.jar package on the server where the Java application you want to monitor resides and run it directly using Java commands, just as with Spring Boot applications.

java -jar arthas-boot.jar
Copy the code

Note:

  • First run, download slow can be used--repo-mirror aliyun --use-http
  • Once started, a list of the current Java applications is displayed (sort ofjps -l), and select the application you want to monitor.

Once Arthas is started, the Arthas command line interface is available and you can use the commands provided by Arthas to implement the functions you need to monitor. In the following figure, the Java application to monitor is the example java-monitor-example.

2.3 quit

To exit the current connection, use the quit or exit command. Arthas attached to the target process will continue to run and the port will remain open for direct connection next time.

If you want to exit Arthas completely, you can execute the shutdown command.

3 Arthas use

Arthas requires learning to use the command functions it provides, which fall into several main categories:

  • Basic commands:help,cat,pwd,history.quitWait, it’s like a Linux command.
  • The JVM related:dashboard,thread,jvm,sysenvAnd so on, mainly on the JVM information monitoring, with the previous learning Java command line toolsjinfo,jmap,jstackAnd so have the same wonderful.
  • The class/this related:sc,sm,jad,dump,classloaderAnd so on.
  • Monitor/watch/trace:monitor,watch,trace,stackWait, that’s pretty much all coveredBTraceIn the function, including timing detection, method parameters, return value, call duration and so on.

The following describes the common commands. For details about the commands, see the official documents.

3.1 the issue:dashboard

After Arthas is started, -h views 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

Copy the code

Equivalent to an overview, showing thread, memory, GC, VM, and Tomcat information in one interface. Here is a sample image from the official document:

This overview is refreshed every 5 seconds by default, with a clear view of memory changes, thread usage, and GC counts. Press CTRL + C to exit.

3.2 Thread Information:thread

Remember jStack, we need to find the thread ID, use it to export the thread stack, and then use the thread ID to look at it. This is much easier in Arthas, as in the dashboard above, where you already have ids, so you can just use thread IDS. -h Viewing help documents:

$ 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
Copy the code

Using the thread command, you can find the top N threads that occupy the CPU (-n). You can print the running stack id of the specified thread (id). You can find the thread that is currently blocking another thread (-b).

3.3 JVM Information:jvm

JVM commands are simple and have no arguments. The output information includes running parameters, classloading information, memory status, system information, thread count information, file descriptors, and so on. Sort of like the JVisualVM overview, but more detailed than that.

3.4 Decompilation:jad

Jad is useful when you need to check whether new code is being used or updated in an application running on the line. You can decompile loaded classes to see if the source code is up to date. -h Print use 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

Copy the code

As shown in EXAMPLES above, jad can decompress a class-pattern, decompress a method-name of a class, or use -c to select which one to display if there are multiple classloaders.

3.5 Method Execution Monitoring:monitor

Monitor can monitor the execution of a method by timing output, including the number of calls, the number of successes, the number of failures, the average duration, and the failure rate. It is similar to @timer in BTrace, but more convenient. -h View the help information:

$ 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
Copy the code

You can use the EXAMPLES shown above to monitor the execution of the method. The default output is 60 seconds. You can use -c to modify the output interval.

3.6 Method execution data monitoring:watch

The watch command, similar to @onMethod of BTrace, is useful if you want to execute method parameters, return values, and exception information in an online application. -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

Copy the code

As shown in the EXAMPLES above, the monitoring times are before method execution (-b), after method execution (-f, default), and after method execution (-s). The monitoring content includes parameters (Params), return values (returnObj), and exceptions (throwExp).

4 summarizes

Arthas is a powerful tool for monitoring Java applications. This article provides a brief introduction to the installation and use of Arthas. Please visit Arthas’s official website, where you can learn more about these commands and examples. To make their Own Java monitoring technology more solid.

Parameter data

  • ArthasWebsite:https://alibaba.github.io/arthas/

reading

  • Java Application Monitoring (1)- Application monitoring techniques that Java programmers should know about
  • Java Application Monitoring (2)- Secrets of Java commands
  • Java Application Monitor (3)- Do you master these command line tools
  • Java Application Monitoring (4)- Online problem detection routines
  • Java Application Monitoring (5)- Visual monitoring tools
  • Java Application Monitoring (6)- Third-party memory analysis tool MAT
  • Java Application Monitoring (7)- Online dynamic diagnostic artifact BTrace