[Java Virtual Machine] Getting Started with JVM Diagnostic Artifact Arthas

1. Introduction to Arthas Quick Start

insert image description here

  • Alibaba's open source Java diagnostic tool, which can dynamically diagnose and debug Java applications at runtime

  • When you encounter the following similar problems and feel at a loss, Arthaswe can help you solve them

    • Which jar package is this class loaded from? Why are various types of related Exception reported?
    • Why is the code I changed not executed? Could it be that I didn't commit? Branching wrong?
    • If you encounter a problem and cannot debug online, can you only republish it by adding a log?
    • I encountered a problem with the data processing of a certain user online, but it also cannot be debugged online, and cannot be reproduced offline!
    • Is there a global view into the health of the system?
    • Is there any way to monitor the real-time running status of the JVM?
    • How to quickly locate application hotspots and generate flame graphs?
    • How to find an instance of a class directly from within the JVM?
  • github address: https://github.com/alibaba/arthas

  • Official website: https://arthas.aliyun.com/

  • Version: Arthas-3.6.7

  • Environmental description

    • ArthasSupport JDK 6+, support Linux/Mac/Windows, use command line interactive mode,
    • Provides rich Tabauto- completion functions to further facilitate problem location and diagnosis
    • It also supports the browser to directly access the corresponding ip+port, fixed port8563
    • By default, arthas only listens to 127.0.0.1, so if you want to connect remotely, use --target-ipthe parameter to specify the IP of listen
  • quick use

    • start upjava -jar arthas-boot.jar

    insert image description here

    • run log path~/logs/arthas/arthas.log

    insert image description here

  • quit arthas

    • If you just exit the current connection and other clients are not affected, you can use the quit or exit command
    • The arthas on the target process will continue to run, the port will remain open, and the next time you connect, java -jar arthas-boot.jaryou can directly connect to it
    • If you want to completely exit arthas, you can execute the stop command

2. Arthas commonly used basic commands in actual combat

  • version - Outputs the version number of Arthas currently loaded by the target Java process

  • base64 - base64 encoding conversion, similar to the base64 command in linux

  • cat - print the contents of the file, similar to the cat command in linux

  • cls - clear the current screen area

  • echo - print parameters, similar to the echo command in linux

  • grep - match search, similar to the grep command in linux

  • help - view command help information

  • history - print command history

  • keymap - Arthas shortcut key list and custom shortcut keys

  • pwd - returns the current working directory, similar to the linux command

  • quit - exit the current Arthas client, other Arthas clients are not affected

  • reset - Reset the enhanced class, restore all the classes enhanced by Arthas, all enhanced classes will be reset when the Arthas server is closed

  • session - View information about the current session

  • stop - closes the Arthas server, all Arthas clients exit

  • tee - copy standard input to standard output and the specified file, similar to the tee command in linux

insert image description here

3. Arthas common JVM command case combat

(1) JVM related commands

  • dashboard - real-time data panel of the current system
  • heapdump - dump java heap, heap dump function similar to jmap command
  • jvm - view information about the current JVM
  • memory - view JVM memory information
  • ognl - execute ognl expressions
  • perfcounter - View the Perf Counter information of the current JVM
  • sysenv - View JVM environment variables
  • sysprop - View and modify system properties of the JVM
  • thread - view the thread stack information of the current JVM
  • vmoption - View and modify diagnostic-related options in the JVM

(2) dashboard - the real-time data panel of the current system

  • Overall large panel

insert image description here

  • Top - basic information about the thread

insert image description here

field illustrate
id Java-level thread ID
name thread name
group thread group name
priority Thread priority, a number between 1 and 10, the larger the priority, the higher the priority
state state of the thread
cpu thread cpu usage
delta_time Incremental CPU time of the thread running since the last sampling, the data format is seconds
time The total CPU time of thread running, the data format is分:秒
interupted Whether the current thread is interrupted
daemon Is it a daemon daemon thread
  • Central - heap memory usage

insert image description here

field illustrate
used How much memory is currently used
total How much memory is allocated in total
max How much is the maximum used
usage Use ratio
gc garbage collector
  • Bottom - OS info, JDK version

insert image description here

(3) thead - view the thread stack information of the current JVM

  • Common parameters

    • –all : Display all matching threads, the default is the first page of thread information

    insert image description here

    • -i: Set the sampling interval for cpu statistics, in millisecondsthread -i 2000

    insert image description here

    • [ id ]: view the thread stack of the specified IDthread 54

    insert image description here

    • -n : View the TopN threads with the highest CPU usage, if the value is -1, it means display all threadsthread -n 3

    insert image description here

    • -b : show blocked threadsthread -b

    insert image description here

    • --state : filter threads based on thread statethread --state TIMED_WAITING
      • Status type: NEW, RUNNABLE, TIMED_WAITING, WAITING, BLOCKED, TERMINATED

    insert image description here

(4) heapdump - similar to the heap dump function of the jmap command

  • Take a stack snapshotheapdump /Users/mac/Desktop/heapdump.hprof
    insert image description hereinsert image description here

(5) jvm - view the information of the current JVM

insert image description here

(6) sysenv - view the environment variables of the JVM

insert image description here

(7) sysprop - view and modify the system properties of the JVM

  • View all properties: sysprop

insert image description here

  • View individual properties: sysprop java.version

insert image description here

  • Modify a property: sysprop user.country CN

insert image description here

(8) sc - View the class information loaded by the JVM

  • -d details, -f class attribute output
  • sc -d -f com.lixiang.controller.SpringTestController

insert image description here

(9) sm - view the method information of the loaded class

  • sm -d com.lixiang.controller.SpringTestController

insert image description here

(10) jad - decompile the source code of the specified loaded class

  • decompile the entire classjad com.lixiang.SpringTestApplication

insert image description here

  • With --source-onlyoptions, it is possible to print only the source code

insert image description here

  • Application Scenario
    • View the business logic and method logic of a class
    • Check whether the locally modified code takes effect online successfully

4. Arthas method diagnosis command case actual combat

(1) monitor - method execution monitoring

  • For non-real-time response, the corresponding method needs to be called, so a web interface request needs to be triggered
  • Monitor the execution times, success times, failure times, time-consuming and other information of the specified method in a period of time
  • monitor -c 2 com.lixiang.controller.SpringTestController query

insert image description here
insert image description hereinsert image description here

(2) stack - output the call path where the current method is called. There are many paths for a method to be executed. If you don’t know where this method is executed, you can use

  • the casestack com.lixiang.controller.SpringTestController query

insert image description hereinsert image description here

insert image description here

(3) trace - method internal call, output time-consuming on each node on the method path, and locate performance problems caused by high RT

  • Only the call link of the first-level method can be traced each time

  • Case output all methodstrace com.lixiang.controller.SpringTestController *

    • ts: Timestamp, indicating the log record time, the value of this field is 5:48:46 PM on March 26, 2023.
    • thread_name: Thread name, indicating the thread name currently executing the log record, the value of this field is http-nio-8080-exec-10.
    • id: Thread ID, indicating the thread ID currently executing the log record, the value of this field is 20.
    • is_daemon: Whether it is a daemon thread, the value of this field is true, indicating that the thread is a daemon thread.
    • priority: Thread priority, the value of this field is 5, indicating that the thread priority is 5.
    • TCCL: Thread context class loader, indicating that the context class loader of the current thread is TomcatEmbeddedWebappClassLoader.

insert image description here

  • By default, trace will not include function calls in jdk. If you want to trace functions in jdk, you need to explicitly set --skipJDKMethod false
  • Right nowtrace --skipJDKMethod false com.lixiang.controller.SpringTestController *

insert image description here

(4) watch - method execution data observation

  • Application scenario: view method call stack, parameter entry, return value, etc. debugging
  • The default watch expression, the default value is{params, target, returnObj}
  • You can also specify the observed return valuewatch com.lixiang.controller.SpringTestController * {params,returnObj}

insert image description here

  • Expand the specific value in the -x parameter to indicate the traversal depth, which can be adjusted to print specific parameters and result content. The default value is 1, and the maximum is 4.

  • watch com.lixiang.controller.SpringTestController * {params,returnObj} -x 4

insert image description here

5. Arthas Online Debugging Case Actual Combat

  • The java program is running in the production environment, and online debugging is required, without restarting the JVM program, dynamically adjusting, printing parameters or modifying the internal logic of the method.
  • Steps and environment preparation

insert image description here

  • The first step: jad decompiles the bytecode file into source code

insert image description here

insert image description here

  • Step 2: mc compiles the source code into a bytecode file in memory

insert image description here

insert image description here

  • Step 3: redefine executes the newly generated bytecode file in memory

insert image description hereinsert image description here

insert image description here

Guess you like

Origin blog.csdn.net/weixin_47533244/article/details/130651336