工具
https://github.com/alibaba/arthas
集成支持采集火焰图
https://github.com/alibaba/arthas/issues/951
## 启动命令
java -jar arthas-boot.jar
[root@dapr1 bin]# java -jar ../tmp/arthas-boot.jar
[INFO] arthas-boot version: 3.5.1
[INFO] Process 245797 already using port 3658
[INFO] Process 245797 already using port 8563
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 245797 /export/App/web-launcher.jar
1
[INFO] arthas home: /export/App/tmp
[INFO] The target process already listen port 3658, skip attach.
[INFO] arthas-client connect 127.0.0.1 3658
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki https://arthas.aliyun.com/doc
tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html
version 3.5.1
main_class
pid 245797
time 2021-06-09 07:16:01
[arthas@245797]$ profiler start ##开始采集profiler, 默认只采集cpu ,可以通过 参数修改 -e, --event <value>,(cpu, alloc, lock, cache-misses etc.)
Started [cpu] profiling
[arthas@245797]$ profiler status ##查看状态
[perf] profiling is running for 2 seconds
[arthas@245797]$ profiler status
[perf] profiling is running for 3 seconds
[arthas@245797]$ profiler status
[perf] profiling is running for 4 seconds
[arthas@245797]$ profiler status
[perf] profiling is running for 5 seconds
[arthas@245797]$ profiler stop ##停止采集profiler
OK
profiler output file: /export/App/bin/arthas-output/20210609-072454.svg ##生成的火焰图
通过浏览器查看arthas-output下面的profiler结果
默认情况下,arthas使用3658端口,则可以打开: http://localhost:3658/arthas-output/ 查看到arthas-output目录下面的profiler结果
profiler命令参数
[arthas@245797]$ profiler --help
USAGE:
profiler [--allkernel] [--alluser] [-d <value>] [-e <value>] [--exclude <value>] [-f <value>] [--format <value>] [-b <value>] [-h] [--include <value>] [-i <value>] [--threads] action [actionArg]
SUMMARY:
Async Profiler. https://github.com/jvm-profiling-tools/async-profiler
EXAMPLES:
profiler start
profiler stop
profiler list # list all supported events
profiler actions # list all supported actions
profiler start --event alloc
profiler stop --format svg # output file format, support svg,html,jfr
profiler stop --file /tmp/result.html
profiler stop --threads
profiler start --include 'java/*' --include 'demo/*' --exclude '*Unsafe.park*'
profiler status
profiler resume # Start or resume profiling without resetting collected data.
profiler getSamples # Get the number of samples collected during the profiling session
profiler dumpFlat # Dump flat profile, i.e. the histogram of the hottest methods
profiler dumpCollapsed # Dump profile in 'collapsed stacktraces' format
profiler dumpTraces # Dump collected stack traces
profiler execute 'start,framebuf=5000000' # Execute an agent-compatible profiling command
profiler execute 'stop,file=/tmp/result.svg' # Execute an agent-compatible profiling command
WIKI:
https://arthas.aliyun.com/doc/profiler
OPTIONS:
--allkernel include only kernel-mode events
--alluser include only user-mode events
-d, --duration <value> run profiling for <duration> seconds
-e, --event <value> which event to trace (cpu, alloc, lock, cache-misses etc.), default value is cpu
--exclude <value> exclude stack traces containing PATTERN, for example: '*Unsafe.park*'
-f, --file <value> dump output to <filename>
--format <value> dump output file format(svg, html, jfr), default valut is svg
-b, --framebuf <value> size of the buffer for stack frames (default: 1'000'000)
-h, --help this help
--include <value> include stack traces containing PATTERN, for example: 'java/*'
-i, --interval <value> sampling interval in ns (default: 10'000'000, i.e. 10 ms)
--threads profile different threads separately
<action> Action to execute
<actionArg> Attribute name pattern.
命令行工具
https://github.com/jvm-profiling-tools/async-profiler
https://github.com/brendangregg/FlameGraph
生成火焰图
进入async-profiler那个项目的目录
./profiler.sh -d 60 -o collapsed -f /tmp/test_01.txt ${pid}
-d表示的是持续时长,后面60代表持续采集时间60s,-o表示的是采集规范,这里用的是collapsed,-f后面的路径,表示的是数据采集后生成的数据存放的文件路径(这里放在了/tmp/test_01.txt),${pid}表示的是采集目标进程的pid
生成svg
perl flamegraph.pl --colors=java /tmp/test_01.txt > test_01.svg
因为是perl文件,这里使用perl指令运行该文件,后面--colors表示着色风格,这里是java,后面的是数据文件的路径,这里是刚刚上面生成的那个文件/tmp/test_01.txt,最后的test_01.svg就是最终生成的火焰图文件存放的路径和文件命名,这里是命名为test_01.svg并保存在当前路径,运行后看到该文件已经存在于当前目录下
生成文件后,下载,使用浏览器打开