Arthas(阿尔萨斯)--(一)
Arthas代码开源地址
dashboard:显示当前系统的实时数据面板,按q或ctrl+c退出
ID: Java 级别的线程 ID,注意这个 ID 不能跟 jstack 中的 nativeID 一一对应。
NAME: 线程名
GROUP: 线程组名
PRIORITY: 线程优先级, 1~10 之间的数字,越大表示优先级越高
STATE: 线程的状态
CPU%: 线程的 cpu 使用率。比如采样间隔 1000ms,某个线程的增量 cpu 时间为 100ms,则 cpu 使用率=100/1000=10%
DELTA_TIME: 上次采样之后线程运行增量 CPU 时间,数据格式为秒
TIME: 线程运行总 CPU 时间,数据格式为分:秒
INTERRUPTED: 线程当前的中断位状态
DAEMON: 是否是 daemon 线程
thread:查看当前JVM的线程堆栈信息
参数:数字:线程id
[n:]:指定最忙的前N个线程并打印堆栈
[arthas@14156]$ thread -n 3
"main" Id=1 cpuUsage=0.0% deltaTime=0ms time=218ms TIMED_WAITING
at [email protected]/java.lang.Thread.sleep0(Native Method)
at [email protected]/java.lang.Thread.sleep(Thread.java:558)
at [email protected]/java.util.concurrent.TimeUnit.sleep(TimeUnit.java:446)
at app//demo.MathGame.main(MathGame.java:17)
"Reference Handler" Id=9 cpuUsage=0.0% deltaTime=0ms time=0ms RUNNABLE
at [email protected]/java.lang.ref.Reference.waitForReferencePendingList(Native Method)
at [email protected]/java.lang.ref.Reference.processPendingReferences(Reference.java:246)
at [email protected]/java.lang.ref.Reference$ReferenceHandler.run(Reference.java:208)
"Finalizer" Id=10 cpuUsage=0.0% deltaTime=0ms time=0ms WAITING on java.lang.ref.NativeReferenceQueue$Lock@2b55402b
at [email protected]/java.lang.Object.wait0(Native Method)
- waiting on java.lang.ref.NativeReferenceQueue$Lock@2b55402b
at [email protected]/java.lang.Object.wait(Object.java:366)
at [email protected]/java.lang.Object.wait(Object.java:339)
at [email protected]/java.lang.ref.NativeReferenceQueue.await(NativeReferenceQueue.java:48)
at [email protected]/java.lang.ref.ReferenceQueue.remove0(ReferenceQueue.java:158)
at [email protected]/java.lang.ref.NativeReferenceQueue.remove(NativeReferenceQueue.java:89)
at [email protected]/java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:173)
[arthas@14156]$
[b]:找出当前阻塞其他线程的线程
有时候我们发现应用卡住了, 通常是由于某个线程拿住了某个锁, 并且其他线程都在等待这把锁造成的。 为了排查这类问题, arthas 提供了thread -b
运行一个死锁程序
public class DeadLock {
static Object a = new Object();
static Object b = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (a) {
System.out.println(Thread.currentThread().getName() + "持有锁a,试图获取锁b");
synchronized (b) {
System.out.println(Thread.currentThread().getName() + "持有锁b");
}
}
}, "t1").start();
new Thread(() -> {
synchronized (b) {
System.out.println(Thread.currentThread().getName() + "持有锁b,试图获取锁a");
synchronized (a) {
System.out.println(Thread.currentThread().getName() + "持有锁a");
}
}
}, "t2").start();
}
}
运行arthas,粘附到这个死锁程序中
[arthas@11400]$ thread -b
"t2" Id=34 BLOCKED on java.lang.Object@1e8eb080 owned by "t1" Id=33
at app//com.lwz.xxx.DeadLock.lambda$main$1(DeadLock.java:21)
- blocked on java.lang.Object@1e8eb080
- locked java.lang.Object@6fe0e9fb <---- but blocks 1 other threads!
at app//com.lwz.controller.DeadLock$$Lambda/0x0000020111003418.run(Unknown Source)
at [email protected]/java.lang.Thread.runWith(Thread.java:1596)
at [email protected]/java.lang.Thread.run(Thread.java:1583)
[arthas@11400]$
[i
thread -i 1000 : 统计最近 1000ms 内的线程 CPU 时间。
thread -n 3 -i 1000 : 列出 1000ms 内最忙的 3 个线程栈
[arthas@14156]$ thread -i 1000
Threads Total: 16, NEW: 0, RUNNABLE: 9, BLOCKED: 0, WAITING: 3, TIMED_WAITING: 4, TERMINATED: 0
ID NAME GROUP PRIORITY STATE %CPU DELTA_TIM TIME INTERRUPT DAEMON
1 main main 5 TIMED_WA 0.0 0.000 0:0.234 false false
9 Reference Handler system 10 RUNNABLE 0.0 0.000 0:0.000 false true
10 Finalizer system 8 WAITING 0.0 0.000 0:0.000 false true
11 Signal Dispatcher system 9 RUNNABLE 0.0 0.000 0:0.000 false true
12 Attach Listener system 5 RUNNABLE 0.0 0.000 0:0.046 false true
30 Notification Thread system 9 RUNNABLE 0.0 0.000 0:0.000 false true
31 Common-Cleaner InnocuousThrea 8 TIMED_WA 0.0 0.000 0:0.000 false true
33 arthas-timer system 5 WAITING 0.0 0.000 0:0.000 false true
59 arthas-NettyHttpTelnetBootstr system 5 RUNNABLE 0.0 0.000 0:0.046 false true
60 arthas-NettyWebsocketTtyBoots system 5 RUNNABLE 0.0 0.000 0:0.000 false true
61 arthas-NettyWebsocketTtyBoots system 5 RUNNABLE 0.0 0.000 0:0.000 false true
62 arthas-shell-server system 5 TIMED_WA 0.0 0.000 0:0.000 false true
63 arthas-session-manager system 5 TIMED_WA 0.0 0.000 0:0.000 false true
64 arthas-UserStat system 5 WAITING 0.0 0.000 0:0.000 false true
66 arthas-NettyHttpTelnetBootstr system 5 RUNNABLE 0.0 0.000 0:0.140 false true
67 arthas-command-execute system 5 RUNNABLE 0.0 0.000 0:0.046 false true
[arthas@14156]$ thread -n 3 -i 1000
"main" Id=1 cpuUsage=0.0% deltaTime=0ms time=234ms TIMED_WAITING
at [email protected]/java.lang.Thread.sleep0(Native Method)
at [email protected]/java.lang.Thread.sleep(Thread.java:558)
at [email protected]/java.util.concurrent.TimeUnit.sleep(TimeUnit.java:446)
at app//demo.MathGame.main(MathGame.java:17)
"Reference Handler" Id=9 cpuUsage=0.0% deltaTime=0ms time=0ms RUNNABLE
at [email protected]/java.lang.ref.Reference.waitForReferencePendingList(Native Method)
at [email protected]/java.lang.ref.Reference.processPendingReferences(Reference.java:246)
at [email protected]/java.lang.ref.Reference$ReferenceHandler.run(Reference.java:208)
"Finalizer" Id=10 cpuUsage=0.0% deltaTime=0ms time=0ms WAITING on java.lang.ref.NativeReferenceQueue$Lock@2b55402b
at [email protected]/java.lang.Object.wait0(Native Method)
- waiting on java.lang.ref.NativeReferenceQueue$Lock@2b55402b
at [email protected]/java.lang.Object.wait(Object.java:366)
at [email protected]/java.lang.Object.wait(Object.java:339)
at [email protected]/java.lang.ref.NativeReferenceQueue.await(NativeReferenceQueue.java:48)
at [email protected]/java.lang.ref.ReferenceQueue.remove0(ReferenceQueue.java:158)
at [email protected]/java.lang.ref.NativeReferenceQueue.remove(NativeReferenceQueue.java:89)
at [email protected]/java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:173)
例:
thread --state WAITING :查看线程中处于等待状态的线程
与JVM相关的信息
[arthas@14156]$ jvm
RUNTIME
-----------------------------------------------------------------------------------------------------------------------
MACHINE-NAME 14156@lwzoozyc
JVM-START-TIME 2023-11-13 23:10:24
MANAGEMENT-SPEC-VERSION 4.0
SPEC-NAME Java Virtual Machine Specification
SPEC-VENDOR Oracle Corporation
SPEC-VERSION 21
VM-NAME Java HotSpot(TM) 64-Bit Server VM
VM-VENDOR Oracle Corporation
VM-VERSION 21.0.1+12-LTS-29
INPUT-ARGUMENTS []
CLASS-PATH math-game.jar
BOOT-CLASS-PATH
LIBRARY-PATH C:\Program Files\Java\jdk-21\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Win
dows;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Comm
on Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wb
em;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Prog
ram Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation
\NVIDIA NvDLISR;"C:\Program Files\Java\jdk-21\bin;C:\Program Files\Java\jdk-21\jre\
bin;";C:\Program Files\Go\bin;C:\Program Files\Go\bin;C:\Program Files\PuTTY\;C:\Pr
ogram Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Program Files\Microsoft VS
Code\bin;C:\Program Files\Java\jdk-21\bin;C:\Users\Administrator\AppData\Local\Micr
osoft\WindowsApps;C:\Users\Administrator\go\bin;C:\Program Files\JetBrains\GoLand 2
023.2\bin;;C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.3\bin;;.
-----------------------------------------------------------------------------------------------------------------------
CLASS-LOADING
-----------------------------------------------------------------------------------------------------------------------
LOADED-CLASS-COUNT 4623
TOTAL-LOADED-CLASS-COUNT 4623
UNLOADED-CLASS-COUNT 0
IS-VERBOSE false
-----------------------------------------------------------------------------------------------------------------------
COMPILATION
-----------------------------------------------------------------------------------------------------------------------
NAME HotSpot 64-Bit Tiered Compilers
TOTAL-COMPILE-TIME 3011
[time (ms)]
-----------------------------------------------------------------------------------------------------------------------
GARBAGE-COLLECTORS
-----------------------------------------------------------------------------------------------------------------------
G1 Young Generation name : G1 Young Generation
[count/time (ms)] collectionCount : 1
collectionTime : 11
G1 Concurrent GC name : G1 Concurrent GC
[count/time (ms)] collectionCount : 0
collectionTime : 0
G1 Old Generation name : G1 Old Generation
[count/time (ms)] collectionCount : 0
collectionTime : 0
-----------------------------------------------------------------------------------------------------------------------
MEMORY-MANAGERS
-----------------------------------------------------------------------------------------------------------------------
CodeCacheManager CodeHeap 'non-nmethods'
CodeHeap 'profiled nmethods'
CodeHeap 'non-profiled nmethods'
Metaspace Manager Metaspace
Compressed Class Space
G1 Young Generation G1 Eden Space
G1 Survivor Space
G1 Old Gen
G1 Concurrent GC G1 Old Gen
G1 Old Generation G1 Eden Space
G1 Survivor Space
G1 Old Gen
-----------------------------------------------------------------------------------------------------------------------
MEMORY
-----------------------------------------------------------------------------------------------------------------------
HEAP-MEMORY-USAGE init : 1073741824(1.0 GiB)
[memory in bytes] used : 27318272(26.1 MiB)
committed : 1073741824(1.0 GiB)
max : 17146314752(16.0 GiB)
NO-HEAP-MEMORY-USAGE init : 7667712(7.3 MiB)
[memory in bytes] used : 31832208(30.4 MiB)
committed : 36438016(34.8 MiB)
max : -1(-1 B)
PENDING-FINALIZE-COUNT 0
-----------------------------------------------------------------------------------------------------------------------
OPERATING-SYSTEM
-----------------------------------------------------------------------------------------------------------------------
OS Windows 10
ARCH amd64
PROCESSORS-COUNT 36
LOAD-AVERAGE -1.0
VERSION 10.0
-----------------------------------------------------------------------------------------------------------------------
THREAD
-----------------------------------------------------------------------------------------------------------------------
COUNT 16
DAEMON-COUNT 15
PEAK-COUNT 16
STARTED-COUNT 19
DEADLOCK-COUNT 0
-----------------------------------------------------------------------------------------------------------------------
FILE-DESCRIPTOR
-----------------------------------------------------------------------------------------------------------------------
MAX-FILE-DESCRIPTOR-COUNT -1
OPEN-FILE-DESCRIPTOR-COUNT -1
sysprop:查看和修改JVM的系统属性
[arthas@14156]$ sysprop
KEY VALUE
-----------------------------------------------------------------------------------------------------------------------
java.specification.ver 21
sion
sun.cpu.isalist amd64
sun.jnu.encoding GBK
java.class.path math-game.jar
java.vm.vendor Oracle Corporation
sun.arch.data.model 64
user.variant
java.vendor.url https://java.oracle.com/
user.timezone Asia/Shanghai
java.vm.specification. 21
version
os.name Windows 10
sun.java.launcher SUN_STANDARD
user.country CN
sun.boot.library.path C:\Program Files\Java\jdk-21\bin
sun.java.command math-game.jar
jdk.debug release
sun.cpu.endian little
user.home C:\Users\Administrator
user.language zh
java.specification.ven Oracle Corporation
dor
java.version.date 2023-10-17
java.home C:\Program Files\Java\jdk-21
file.separator \
java.vm.compressedOops Zero based
Mode
java.vm.specification. Oracle Corporation
vendor
java.specification.nam Java Platform API Specification
e
user.script
sun.management.compile HotSpot 64-Bit Tiered Compilers
r
java.runtime.version 21.0.1+12-LTS-29
user.name Administrator
stdout.encoding ms936
path.separator ;
os.version 10.0
java.runtime.name Java(TM) SE Runtime Environment
file.encoding UTF-8
java.vm.name Java HotSpot(TM) 64-Bit Server VM
java.vendor.url.bug https://bugreport.java.com/bugreport/
java.io.tmpdir C:\Users\ADMINI~1\AppData\Local\Temp\
java.version 21.0.1
user.dir C:\Users\Administrator\.arthas\lib\3.7.1\arthas
os.arch amd64
java.vm.specification. Java Virtual Machine Specification
name
sun.os.patch.level
native.encoding GBK
java.library.path C:\Program Files\Java\jdk-21\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Pr
ogram Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Oracle\Java
\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\Windows
PowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX
\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;"C:\Program Files\Java\jdk-21\bin;
C:\Program Files\Java\jdk-21\jre\bin;";C:\Program Files\Go\bin;C:\Program Files\Go\bin;C:\Pro
gram Files\PuTTY\;C:\Program Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Program Files\
Microsoft VS Code\bin;C:\Program Files\Java\jdk-21\bin;C:\Users\Administrator\AppData\Local\M
icrosoft\WindowsApps;C:\Users\Administrator\go\bin;C:\Program Files\JetBrains\GoLand 2023.2\b
in;;C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.3\bin;;.
java.vm.info mixed mode, sharing
stderr.encoding ms936
java.vendor Oracle Corporation
java.vm.version 21.0.1+12-LTS-29
sun.io.unicode.encodin UnicodeLittle
g
java.class.version 65.0
[arthas@14156]$
查看单个属性
[arthas@14156]$ sysprop java.version
KEY VALUE
------------------------------------------------------------------------------------------
java.version 21.0.1
[arthas@14156]$
修改单个属性
[arthas@14156]$ sysprop user.country
KEY VALUE
------------------------------------------------------------------------------------------
user.country CN
[arthas@14156]$ sysprop user.country US
Successfully changed the system property.
KEY VALUE
------------------------------------------------------------------------------------------
user.country US
[arthas@14156]$ sysprop user.country CN
Successfully changed the system property.
KEY VALUE
------------------------------------------------------------------------------------------
user.country CN
[arthas@14156]$
sysenv:查看当前JVM的环境变量属性(System Environment Variables)
查看所有环境变量
$>sysenv
查看单个环境变量
$>sysenv path
vmoption:查看,更新VM诊断相关参数
查看所有的选项
$>vmoption
查看指定的选项
$>vmoption HeapDumpBeforeFullGC
更新指定的选项
$>vmoption HeapDumpBeforeFullGC true
[arthas@9272]$ vmoption
KEY VALUE ORIGIN WRITEABLE
-----------------------------------------------------------------------------------------------
HeapDumpBeforeFullGC false DEFAULT true
HeapDumpAfterFullGC false DEFAULT true
HeapDumpOnOutOfMemoryError false DEFAULT true
HeapDumpPath DEFAULT true
HeapDumpGzipLevel 0 DEFAULT true
ShowCodeDetailsInExceptionMe true DEFAULT true
ssages
PrintClassHistogram false DEFAULT true
MinHeapFreeRatio 40 DEFAULT true
MaxHeapFreeRatio 70 DEFAULT true
PrintConcurrentLocks false DEFAULT true
G1PeriodicGCInterval 0 DEFAULT true
G1PeriodicGCSystemLoadThresh 0.0 DEFAULT true
old
SoftMaxHeapSize 17146314752 ERGONOMIC true
[arthas@9272]$ vmoption HeapDumpBeforeFullGC
KEY VALUE ORIGIN WRITEABLE
-----------------------------------------------------------------------------------------------
HeapDumpBeforeFullGC false DEFAULT true
[arthas@9272]$ vmoption HeapDumpBeforeFullGC true
Successfully updated the vm option.
NAME BEFORE-VALUE AFTER-VALUE
-------------------------------------------------
HeapDumpBeforeFullGC false true
getstatic:通过getstatic命令可以方便的查看类的静态属性
语法:
getstatic 类名 属性名
例:
显示demo.MathGame类中的静态属性random
$>getstatic demo.MathGame random
[arthas@4416]$ getstatic demo.MathGame random
field: random
ERROR DATA!!! object class: class java.util.Random, exception class: class java.lang.reflect.InaccessibleObjectException, exception message: Unable to make field static final long java.util.Random.serialVersionUID accessible: module java.base does not "opens java.util" to unnamed module @2a22da95
Affect(row-cnt:1) cost in 4 ms.
官网显示
$ getstatic demo.MathGame random
field: random
@Random[
serialVersionUID=@Long[3905348978240129619],
seed=@AtomicLong[120955813885284],
multiplier=@Long[25214903917],
addend=@Long[11],
mask=@Long[281474976710655],
DOUBLE_UNIT=@Double[1.1102230246251565E-16],
BadBound=@String[bound must be positive],
BadRange=@String[bound must be greater than origin],
BadSize=@String[size must be non-negative],
seedUniquifier=@AtomicLong[-3282039941672302964],
nextNextGaussian=@Double[0.0],
haveNextNextGaussian=@Boolean[false],
serialPersistentFields=@ObjectStreamField[][isEmpty=false;size=3],
unsafe=@Unsafe[sun.misc.Unsafe@2eaa1027],
seedOffset=@Long[24],
]
ognl:执行ognl表达式,这是从3.0.5版本新增的功能
参数说明
参数名称 | 参数说明 |
---|---|
express | 执行的表达式 |
[c:] |
执行表达式的 ClassLoader 的 hashcode,默认值是 SystemClassLoader |
[classLoaderClass:] |
指定执行表达式的 ClassLoader 的 class name |
[x] | 结果对象的展开层次,默认值 1 |
调用静态函数:
$ ognl '@[email protected]("hello")'
null
[arthas@4416]$ ognl '@System@getProperty("java.home")'
@String[C:\Program Files\Java\jdk-21]
调用静态类的静态字段
[arthas@4416]$ ognl '@demo.MathGame@random'
ERROR DATA!!! object class: class java.util.Random, exception class: class java.lang.reflect.InaccessibleObjectException, exception message: Unable to make field static final long java.util.Random.serialVersionUID accessible: module java.base does not "opens java.util" to unnamed module @2a22da95
执行多行表达式,赋值给临时变量,返回一个 List:
[arthas@4416]$ ognl '#value1=@System@getProperty("java.home"), #value2=@System@getProperty("java.runtime.name"), {#value1, #value2}'
@ArrayList[
@String[C:\Program Files\Java\jdk-21],
@String[Java(TM) SE Runtime Environment],
]
Arthas(阿尔萨斯)--(一)
一个程序员最重要的能力是:写出高质量的代码!!
有道无术,术尚可求也,有术无道,止于术。
无论你是年轻还是年长,所有程序员都需要记住:时刻努力学习新技术,否则就会被时代抛弃!