async-profiler的使用与RocketMQ性能优化案例
1. async-profiler是什么?
async-profiler是一种低开销的Java采样分析器,不会受Safepoint偏差问题的影响。它具有专门为HotSpot设计的API,用于收集堆栈跟踪信息并跟踪内存分配。分析器可以与OpenJDK、Oracle JDK和其他基于HotSpot JVM的Java运行时一起使用。
async-profiler可以跟踪以下类型的事件:
- CPU周期
- 硬件和软件性能计数器,例如缓存丢失、分支丢失、页错误、上下文切换等
- Java堆中的分配
- 内容锁定尝试,包括Java对象监视器和可重入锁
总结接一下就是能够用来对Java项目进行调优通过分析上述的一些指标。从指标中找出项目中可能存在的优化点。
1.1 如何安装
直接从github的 项目地址下载对应的平台的安装包。然后将其解压到目录中即可:
安装的过程即将安装包下载解压即可完成无需复杂的步骤。
1.2 如何使用
用如下命令查看使用帮助:
$ ./profiler.sh -h
Usage: ./profiler.sh [action] [options] <pid>
Actions:
start start profiling and return immediately
resume resume profiling without resetting collected data
stop stop profiling
dump dump collected data without stopping profiling session
check check if the specified profiling event is available
status print profiling status
meminfo print profiler memory stats
list list profiling events supported by the target JVM
collect collect profile for the specified period of time
and then stop (default action)
Options:
-e event profiling event: cpu|alloc|lock|cache-misses etc.
-d duration run profiling for <duration> seconds
-f filename dump output to <filename>
-i interval sampling interval in nanoseconds
-j jstackdepth maximum Java stack depth
-t profile different threads separately
-s simple class names instead of FQN
-g print method signatures
-a annotate Java methods
-l prepend library names
-o fmt output format: flat|traces|collapsed|flamegraph|tree|jfr
-I include output only stack traces containing the specified pattern
-X exclude exclude stack traces with the specified pattern
-v, --version display version string
--title string FlameGraph title
--minwidth pct skip frames smaller than pct%
--reverse generate stack-reversed FlameGraph / Call tree
--loop time run profiler in a loop
--alloc bytes allocation profiling interval in bytes
--live build allocation profile from live objects only
--lock duration lock profiling threshold in nanoseconds
--total accumulate the total value (time, bytes, etc.)
--all-user only include user-mode events
--sched group threads by scheduling policy
--cstack mode how to traverse C stack: fp|dwarf|lbr|no
--begin function begin profiling when function is executed
--end function end profiling when function is executed
--ttsp time-to-safepoint profiling
--jfrsync config synchronize profiler with JFR recording
--lib path full path to libasyncProfiler.so in the container
--fdtransfer use fdtransfer to serve perf requests
from the non-privileged target
<pid> is a numeric process ID of the target JVM
or 'jps' keyword to find running JVM automatically
or the application's name as it would appear in the jps tool
Example: ./profiler.sh -d 30 -f profile.html 3456
./profiler.sh start -i 999000 jps
./profiler.sh stop -o flat jps
./profiler.sh -d 5 -e alloc MyAppName
了解了大致如何使用接下来用这个工具来给RocketMQ进行性能优化。