工具

https://github.com/alibaba/arthas

集成支持采集火焰图
https://github.com/alibaba/arthas/issues/951

  1. ## 启动命令
  2. java -jar arthas-boot.jar
  3. [root@dapr1 bin]# java -jar ../tmp/arthas-boot.jar
  4. [INFO] arthas-boot version: 3.5.1
  5. [INFO] Process 245797 already using port 3658
  6. [INFO] Process 245797 already using port 8563
  7. [INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
  8. * [1]: 245797 /export/App/web-launcher.jar
  9. 1
  10. [INFO] arthas home: /export/App/tmp
  11. [INFO] The target process already listen port 3658, skip attach.
  12. [INFO] arthas-client connect 127.0.0.1 3658
  13. ,---. ,------. ,--------.,--. ,--. ,---. ,---.
  14. / O \ | .--. ''--. .--'| '--' | / O \ ' .-'
  15. | .-. || '--'.' | | | .--. || .-. |`. `-.
  16. | | | || |\ \ | | | | | || | | |.-' |
  17. `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
  18. wiki https://arthas.aliyun.com/doc
  19. tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html
  20. version 3.5.1
  21. main_class
  22. pid 245797
  23. time 2021-06-09 07:16:01
  24. [arthas@245797]$ profiler start ##开始采集profiler, 默认只采集cpu ,可以通过 参数修改 -e, --event <value>,(cpu, alloc, lock, cache-misses etc.)
  25. Started [cpu] profiling
  26. [arthas@245797]$ profiler status ##查看状态
  27. [perf] profiling is running for 2 seconds
  28. [arthas@245797]$ profiler status
  29. [perf] profiling is running for 3 seconds
  30. [arthas@245797]$ profiler status
  31. [perf] profiling is running for 4 seconds
  32. [arthas@245797]$ profiler status
  33. [perf] profiling is running for 5 seconds
  34. [arthas@245797]$ profiler stop ##停止采集profiler
  35. OK
  36. profiler output file: /export/App/bin/arthas-output/20210609-072454.svg ##生成的火焰图
  37. 通过浏览器查看arthas-output下面的profiler结果
  38. 默认情况下,arthas使用3658端口,则可以打开: http://localhost:3658/arthas-output/ 查看到arthas-output目录下面的profiler结果

profiler命令参数

  1. [arthas@245797]$ profiler --help
  2. USAGE:
  3. profiler [--allkernel] [--alluser] [-d <value>] [-e <value>] [--exclude <value>] [-f <value>] [--format <value>] [-b <value>] [-h] [--include <value>] [-i <value>] [--threads] action [actionArg]
  4. SUMMARY:
  5. Async Profiler. https://github.com/jvm-profiling-tools/async-profiler
  6. EXAMPLES:
  7. profiler start
  8. profiler stop
  9. profiler list # list all supported events
  10. profiler actions # list all supported actions
  11. profiler start --event alloc
  12. profiler stop --format svg # output file format, support svg,html,jfr
  13. profiler stop --file /tmp/result.html
  14. profiler stop --threads
  15. profiler start --include 'java/*' --include 'demo/*' --exclude '*Unsafe.park*'
  16. profiler status
  17. profiler resume # Start or resume profiling without resetting collected data.
  18. profiler getSamples # Get the number of samples collected during the profiling session
  19. profiler dumpFlat # Dump flat profile, i.e. the histogram of the hottest methods
  20. profiler dumpCollapsed # Dump profile in 'collapsed stacktraces' format
  21. profiler dumpTraces # Dump collected stack traces
  22. profiler execute 'start,framebuf=5000000' # Execute an agent-compatible profiling command
  23. profiler execute 'stop,file=/tmp/result.svg' # Execute an agent-compatible profiling command
  24. WIKI:
  25. https://arthas.aliyun.com/doc/profiler
  26. OPTIONS:
  27. --allkernel include only kernel-mode events
  28. --alluser include only user-mode events
  29. -d, --duration <value> run profiling for <duration> seconds
  30. -e, --event <value> which event to trace (cpu, alloc, lock, cache-misses etc.), default value is cpu
  31. --exclude <value> exclude stack traces containing PATTERN, for example: '*Unsafe.park*'
  32. -f, --file <value> dump output to <filename>
  33. --format <value> dump output file format(svg, html, jfr), default valut is svg
  34. -b, --framebuf <value> size of the buffer for stack frames (default: 1'000'000)
  35. -h, --help this help
  36. --include <value> include stack traces containing PATTERN, for example: 'java/*'
  37. -i, --interval <value> sampling interval in ns (default: 10'000'000, i.e. 10 ms)
  38. --threads profile different threads separately
  39. <action> Action to execute
  40. <actionArg> Attribute name pattern.

命令行工具

https://github.com/jvm-profiling-tools/async-profiler
https://github.com/brendangregg/FlameGraph

生成火焰图

  1. 进入async-profiler那个项目的目录
  2. ./profiler.sh -d 60 -o collapsed -f /tmp/test_01.txt ${pid}
  3. -d表示的是持续时长,后面60代表持续采集时间60s,-o表示的是采集规范,这里用的是collapsed,-f后面的路径,表示的是数据采集后生成的数据存放的文件路径(这里放在了/tmp/test_01.txt),${pid}表示的是采集目标进程的pid

生成svg

  1. perl flamegraph.pl --colors=java /tmp/test_01.txt > test_01.svg
  2. 因为是perl文件,这里使用perl指令运行该文件,后面--colors表示着色风格,这里是java,后面的是数据文件的路径,这里是刚刚上面生成的那个文件/tmp/test_01.txt,最后的test_01.svg就是最终生成的火焰图文件存放的路径和文件命名,这里是命名为test_01.svg并保存在当前路径,运行后看到该文件已经存在于当前目录下
  3. 生成文件后,下载,使用浏览器打开