安装arthas。
curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar
输入需要监控的java进程的序号,然后enter,如:
[INFO] arthas-boot version: 3.5.3
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 150 sc-eureka/target/sc-eureka-1.0-SNAPSHOT.jar
1
attach到指定线程成功之后,可以通过127.0.0.1:3658在webconsole中进行操作,方便打开多个tab页面。
在命令提示符中输入help,可以查看所有支持的命令和每个命令功能的简单介绍,输入某个命令和参数-h,可查看某个具体命令的功能和参数列表,如:
[arthas@150]$ heapdump -h
USAGE:
heapdump [-h] [-l] [file]
SUMMARY:
Heap dump
Examples:
heapdump
heapdump --live
heapdump --live /tmp/dump.hprof
WIKI:
https://arthas.aliyun.com/doc/heapdump
OPTIONS:
-h, --help this help
-l, --live Dump only live objects; if not specified, all objects in the heap are dumped.
<file> Output file
下面结合问题介绍些常用命令,具体命令的文档可参考文末的参考资料。
sc,模糊查询当前jvm中是否加载了包含关键字的类,并获取其全面,-d参数获取详细信息,如classloader的hashcode,方便后面其他命令使用。如:
[arthas@150]$ sc -d *com.ld.App
class-info com.ld.App
code-source file:/mnt/e/code/sc/sc-eureka/target/sc-eureka-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/
name com.ld.App
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name App
modifier public
annotation org.springframework.boot.autoconfigure.SpringBootApplication,org.springframework.cloud.netflix.eurek
a.server.EnableEurekaServer
interfaces
super-class -java.lang.Object
class-loader -org.springframework.boot.loader.LaunchedURLClassLoader@4909b8da
-sun.misc.Launcher$AppClassLoader@18b4aac2
-sun.misc.Launcher$ExtClassLoader@29e52392
classLoaderhash 4909b8da
classloader可查看某个class是来自哪个jar包。-c指定classloader,如上面展示的hash。-r指定具体的class,其中的'.'需要用'/'替换,并且必须以'.class'结尾。如:
[arthas@150]$ classloader -c 4909b8da -r com/ld/App.class
jar:file:/mnt/e/code/sc/sc-eureka/target/sc-eureka-1.0-SNAPSHOT.jar!/BOOT-INF/classes!/com/ld/App.class
Affect(row-cnt:1) cost in 8 ms.
可用watch来观察某个方法的调用,watch后面跟上完全类名和方法名,及一个ognl表达式,-f表示无论是否正常返回都进行观察,-x n表示输出结果属性遍历深度,如:watch com.ld.controller.UserController findById '{params, target, returnObj}' -x 2
[arthas@231]$ watch com.ld.controller.UserController findById '{params, target, returnObj}' -x 2
Press Q or Ctrl C to abort.
Affect(class count: 1 , method count: 1) cost in 169 ms, listenerId: 1
method=com.ld.controller.UserController.findById location=AtExit
ts=2021-08-12 18:21:48; [cost=259.0532ms] result=@ArrayList[
@Object[][
@Long[1],
],
@UserController[
userRepository=@$Proxy160[org.springframework.data.jpa.repository.support.SimpleJpaRepository@1aa6f99a],
],
@User[
id=@Long[1],
username=@String[acc1],
name=@String[zhangsan],
age=@Integer[10],
balance=@BigDecimal[100.01],
],
]
tt可查看多次调用并选择一个观察,但如果返回结果为多层次嵌套就无法查看了,-t记录每次调用得环境,如:
tt -t com.ld.controller.UserController findById
记录完成之后,可以通过tt -i index来查看某次调用得详细信息。如
[arthas@231]$ tt -t com.ld.controller.UserController findById
Press Q or Ctrl C to abort.
Affect(class count: 1 , method count: 1) cost in 76 ms, listenerId: 2
INDEX TIMESTAMP COST(ms IS-RET IS-EX OBJECT CLASS METHOD
) P
------------------------------------------------------------------------------------------------------------------------
1000 2021-08-12 18:35: 2.5972 true false 0x54be6213 UserController findById
08
1001 2021-08-12 18:35: 3.5047 true false 0x54be6213 UserController findById
11
[arthas@231]$ tt -i 1000
INDEX 1000
GMT-CREATE 2021-08-12 18:35:08
COST(ms) 2.5972
OBJECT 0x54be6213
CLASS com.ld.controller.UserController
METHOD findById
IS-RETURN true
IS-EXCEPTION false
PARAMETERS[0] @Long[2]
RETURN-OBJ @User[
id=@Long[2],
username=@String[acc2],
name=@String[lisi],
age=@Integer[12],
balance=@BigDecimal[200.02],
]
Affect(row-cnt:1) cost in 3 ms.
通过jad --source-only可查看某个类的源码,如:
[arthas@231]$ jad --source-only com.ld.App
/*
* Decompiled with CFR.
*
* Could not load the following classes:
* org.springframework.boot.SpringApplication
* org.springframework.boot.autoconfigure.SpringBootApplication
* org.springframework.cloud.client.discovery.EnableDiscoveryClient
*/
package com.ld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class App {
public static void main(String[] args) {
/*16*/ SpringApplication.run(App.class, (String[])args);
}
}
通过redefine可以替换线上代码,但重启之后会失效,如:redefine App.class
对于线下无法重现的bug,可tt -t记录环境并后续分析,也可使用monitor统计方法调用成功失败的情况。
tt -t com.ld.controller.UserController findById | tee /mnt/e/findById
monitor -c 10 com.ld.contorller.UserController findById | tee /mnt/e/findById
dashboard命令可查看当前系统实时数据面板。
jvm可查看jvm的实时运行状态。
profiler支持生成应用热点火焰图。
profiler start
profiler getSamples
profiler status
profiler stop
http://127.0.0.1:3658/arthas-output/
vmtool可查找某个类的实例,实行强制gc等。
vmtool --action getInstances --className com.ld.controller.UserController --limit 5
[arthas@231]$ vmtool --action getInstances --className com.ld.controller.UserController --limit 5
@UserController[][
@UserController[com.ld.controller.UserController@54be6213],
]
vmtool --action forceGc
参考,
1,https://arthas.aliyun.com/doc/en/;
Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved