JVM系列(四十) JVM调优实战-Arthas 系统相关命令dashboard/thread/jvm/sysprop

958 阅读3分钟

Arthas命令使用

本文主要讲解Arthas 可以帮助我们干哪些事,基本使用命令是什么,我们要怎么使用这些命令,快速的定位问题,解决问题

1.Arthas可以做什么?

从我个人的使用经验来看,Arthas至少帮我解决了以下几个问题:

  • 监控Java进程信息,提供性能看板,观察内存,GC情况
    • 包括线程、cpu、内存等信息,并且会定时的刷新,
  • 根据各种条件查看线程快照
    • 比如当前cpu占用率最高的线程信息
  • 输出jvm的各种信息,如gc算法、jdk版本、ClassPath等
    • 比如查看ClassPath路径,GC垃圾回收算法等
  • 查看已加载的类的静态属性及类加载详细信息
    • 比如这个类从哪个jar包加载的。也可以查看类方法,类加载器信息
  • 直接反编译指定的类,重新加载某个类
    • 可以直接重新加载运行中的类
  • 监控方法的执行,方法入参、出参以及异常信息,追踪方法执行的调用栈
    • 可以监控方法的执行时间,追踪方法调用栈,找出最耗时方法
  • dump某个类的字节码到指定目录
    • Dump 内存快照信息,或者线程堆栈信息,类似jmap,jstack命令

2.Arthas 命令详解

我们看下Arthas都有哪些命令,下面我们来演示实战一下,我们挑选一部分经常使用的命令,方便大家进行了解

[arthas@9152]$ help
 NAME         DESCRIPTION
 help         Display Arthas Help
 auth         Authenticates the current session
 keymap       Display all the available keymap for the specified connection.
 sc           Search all the classes loaded by JVM
 sm           Search the method of classes loaded by JVM
 classloader  Show classloader info
 jad          Decompile class
 getstatic    Show the static field of a class
 monitor      Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc.
 stack        Display the stack trace for the specified class and method
 thread       Display thread info, thread stack
 trace        Trace the execution time of specified method invocation.
 watch        Display the input/output parameter, return object, and thrown exception of specified method invocation
 tt           Time Tunnel
 jvm          Display the target JVM information
 memory       Display jvm memory info.
 perfcounter  Display the perf counter information.
 ognl         Execute ognl expression.
 mc           Memory compiler, compiles java files into bytecode and class files in memory.
 redefine     Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...)
 retransform  Retransform classes. @see Instrumentation#retransformClasses(Class...)
 dashboard    Overview of target jvm's thread, memory, gc, vm, tomcat info.
 dump         Dump class byte array from JVM
 heapdump     Heap dump
 options      View and change various Arthas options
 cls          Clear the screen
 reset        Reset all the enhanced classes
 version      Display Arthas version
 session      Display current session information
 sysprop      Display and change the system properties.
 sysenv       Display the system env.
 vmoption     Display, and update the vm diagnostic options.
 logger       Print logger info, and update the logger level
 history      Display command history
 cat          Concatenate and print files
 base64       Encode and decode using Base64 representation
 echo         write arguments to the standard output
 pwd          Return working directory name
 mbean        Display the mbean information
 grep         grep command for pipes.
 tee          tee command for pipes.
 profiler     Async Profiler. https://github.com/jvm-profiling-tools/async-profiler
 vmtool       jvm tool
 stop         Stop/Shutdown Arthas server and exit the console.
 jfr          Java Flight Recorder Command
[arthas@9152]$

3.JVM相关命令

3.1 Dashboarad 看板 CPU/Memory/GC信息

Dashboard 可以看到CPU信息, Memory信息,GC信息,环境信息等

  • CPU的线程状态,线程消耗CPU占比,线程名字信息
  • Memory内存使用情况 G1 垃圾收集器,Eden区/Survivor区/老年代/元空间 大小及使用比例 GC的次数和时间
  • Runtime系统变量运行信息 版本,进程数等

image.png

  • ID ,Name, Group 是线程信息
    • 31 DestroyJavaVM main 5 UNNABLE 0.0 就是线程ID 31
  • g1_survivor_space 28M 28M -1 100.00% gc.g1_old_generation.count
    • G1 Survivor区使用 28M, 一共28M, 最大大小-1表示没有设置, 使用率100%
  • metaspace 45M 48M -1 94.69%
    • 元空间 大小 45, 一共48M, 最大大小-1表示没有设置, 使用率100%
3.2 Thread 查看线程信息
  • thread 默认打印所有线程信息
  • thread pid 打印线程堆栈执行信息
  • thread -b 打印阻塞线程信息
  • thread -n 3 打印前3个CPU消耗最高的线程信息

执行 thread , 我们看到 线程 pid=54 占用CPU 99%

image.png

执行 thread 54 ,查看线程堆栈信息

[arthas@15704]$ thread 54
"pool-2-thread-1" Id=54 RUNNABLE
    at com.jzj.jvmtest.mythread.MyTask.run(MyTask.java:12)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

看到线程信息 com.jzj.jvmtest.mythread.MyTask.run(MyTask.java:12) 执行出的问题,很快能够定位到CPU飙高的问题

执行 thread -b , 查看线程阻塞信息 but blocks 1 other threads! 有一个阻塞线程信息,Number of locked synchronizers = 1 因为synchronizers 锁信息阻塞

image.png

[arthas@15704]$ thread -b
"pool-2-thread-1" Id=54 RUNNABLE
    at com.jzj.jvmtest.mythread.MyTask.run(MyTask.java:12)
    -  locked java.lang.Object@1059a91b <---- but blocks 1 other threads!
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

    Number of locked synchronizers = 1
    - java.util.concurrent.ThreadPoolExecutor$Worker@6d1b2e

执行 thread -n 3 , 打印前3个CPU消耗最高的线程信息

image.png

3.3 jvm查看JVM参数信息

jvm 命令可以打印出jvm的信息,包括参数和变量,以及用的jvm名字、系统等等信息,下面我们看下执行结果

  • Runtime 系统变量信息, Classpath,JVM参数-Xms500M ,-XX:SurvivorRatio=8 配置等
  • CLASS-LOADING 类加载器信息
  • GARBAGE-COLLECTORS 垃圾收集器信息
  • MEMORY-MANAGERS JVM堆分区信息
  • MEMORY 内存使用情况 init 初始化大小, used使用大小, max 最大值大小
  • THREAD 线程使用情况,线程数,线程总数,死锁数量等

image.png

除了 jvm命令外, 也可以执行 sysprop 命令也可以打印当前系统变量的信息

执行 sysprop

image.png

同样类似的查看系统变量信息,也可以用 sysenv

执行 sysenv

image.png


至此,我们已经学习了 Arthas的基本命令,能够排查一些基本的问题,通过dashboard查看系统面板,thread定位CPU过高线程问题,通过jvm/sysprop/sysenv查看系统变量参数信息等