Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use arthas in java

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Java how to use arthas, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Download Arthas

Wget https://alibaba.github.io/arthas/arthas-boot.jar2.2 runs Arthas

Put the downloaded arthas-boot.jar package on the server where you want to monitor the java application, just like the Spring Boot application, just use the java command to run it.

Java-jar arthas-boot.jar

Note:

The first time you run it, you can use-- repo-mirror aliyun-- use-http to download slowly.

After startup, a list of current java applications will be listed (a bit like jps-l), and the output sequence number can be selected to monitor the application.

The command line interface of Arthas is carried out immediately after startup, and the commands provided by Arthas can be used to achieve the functions that need to be monitored. As shown in the figure below, the java application to be monitored is the sample java-monitor-example.

2.3 exit

If you just quit the current connection, you can use the quit or exit command. The arthas on the Attach 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 exit arthas completely, you can execute the shutdown command.

3 Arthas usage

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., similar to linux commands.

Jvm related: dashboard, thread, jvm, sysenv, etc., mainly monitoring JVM information, similar to learning java command line tools such as jinfo, jmap, jstack, etc.

Class/classloader related: sc, sm, jad, dump, classloader and so on.

Monitor/watch/trace related: monitor, watch, trace, stack and so on, these functions basically cover the functions implemented in BTrace, including timing detection, method parameters, return value, call duration and so on.

The following is a description of several commonly used commands. Please refer to the official documentation for a detailed list of commands.

3.1 Overview: dashboard

After starting Arthas,-h view the usage help:

Dashboard-h USAGE: dashboard [- b] [- h] [- I] [- n] 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, vm and tomcat information in an interface. The following figure (an example of the official document):

The information of this overview is refreshed every 5 seconds by default, and it is clear at a glance about memory changes, thread usage, and the number of GC. Use ctrl+c to exit.

3.2 Thread information: thread

Remember jstack, we need to first find the thread ID, 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 dashboard above, there is already ID, so you can use thread id directly. -h View help documentation:

Thread-h USAGE: thread [- h] [- b] [- I] [- n] [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 Specify the sampling interval (in ms) when calculating cpu usage. -n,-- top-n-threads The number of thread (s) to show, ordered by cpu utilization,-1 to show all. Show thread stack

As shown in the EXAMPLES above, using the thread command, you can find out the top N threads (- n) that occupy the highest CPU, print the run stack (id) of specified threads, and find out the threads (- b) that are currently blocking other threads, so it is convenient to analyze thread problems.

3.3 JVM Information: jvm

The jvm command is simple and has no parameters, and its output information includes running parameters, class loading information, memory conditions, system information, thread number information, file descriptors, and so on. It's a bit like the overview in jvisualvm, but more detailed than it.

3.4 decompilation: jad

Sometimes you need to check whether the new code is used or updated in online applications, and you can decompile the loaded classes to see if the source code is up-to-date. Jad is useful. -h print usage help:

$jad-h USAGE: jad [- c] [- 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 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 name pattern, use either'. Or'/'as separator method name pattern, decompile a specific method instead of the whole class

As shown above, EXAMPLES,jad can decompile the class (class-pattern), decompile a method of the class (method-name), and if you have multiple classLoader, you can use-c to choose which to display, and so on.

3.5 method execution monitoring: monitor

Monitor can periodically output the execution of methods for monitoring, including the number of calls, successes, failures, average time, failure rate, etc., which is a bit like @ Timer in BTrace, but more convenient. -h View usage help:

$monitor-h USAGE: monitor [- c] [- h] [- n] [- 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 The monitor interval (in seconds), 60 seconds by default-h,-- help this help-n,-- limits Threshold of execution times-E -regex Enable regular expression to match (wildcard matching by default) Path and classname of Pattern Matching Method of Pattern Matching

As shown in the EXAMPLES above, you can monitor the execution of the method. The default is 60s output, and you can use-c to modify the output interval.

3.6Method execution data monitoring: watch

Similar to @ OnMethod of BTrace, the watch command is very appropriate if you want to execute the parameters, return values, and exception information of the method in the online application. -h use help:

$watch-h USAGE: watch [- b] [- e] [- x] [- f] [- h] [- n] [- E] [- M] [- 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 Expand level of object (1 by default)-f -- finish Watch after invocation, enable by default-h,-- help this help-n,-- limits Threshold of execution times-E,-- regex Enable regular expression to match (wildcard matching by default)-M,-- sizeLimit Upper size limit in bytes for the result (10 * 1024 * 1024 by default)-s,-- success Watch after successful invocation The full qualified class name you want to watch The method name you want to watch 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 timing is before the method execution (- b), the end of the method execution (- f, the default value), and the method execution success (- s). The monitoring content includes: parameter (params), return value (returnObj), exception (throwExp) and so on.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report