如果你經常遇到 Java 線上性能問題束手無策,看着線上服務 CPU 飆升一籌莫展,發現內存不斷泄露滿臉茫然。別慌,這里有一款低開銷、自帶火焰圖、讓你大呼好用的 Java 性能分析工具 - async-profiler。
最近 Arthas 性能分析工具上線了火焰圖分析功能,Arthas 使用 async-profiler 生成 CPU/內存火焰圖進行性能分析,彌補了之前內存分析的不足。在 Arthas 上使用還是比較方便的,使用方式可以看官方文檔。這篇文章介紹 async-profiler 相關內容。
Arthas 火焰圖官方文檔:https://alibaba.github.io/arthas/profiler.html
如果你想了解更多 Arthas 信息,可以參考之前文章:Arthas - Java 線上問題定位處理的終極利器
async-profiler 介紹
async-profiler 是一款開源的 Java 性能分析工具,原理是基於 HotSpot 的 API,以微乎其微的性能開銷收集程序運行中的堆棧信息、內存分配等信息進行分析。
使用 async-profiler 可以做下面幾個方面的分析。
- CPU cycles
- Hardware and Software performance counters like cache misses, branch misses, page faults, context switches etc.
- Allocations in Java Heap
- Contented lock attempts, including both Java object monitors and ReentrantLocks
我們常用的是 CPU 性能分析和 Heap 內存分配分析。在進行 CPU 性能分析時,僅需要非常低的性能開銷就可以進行分析,這也是這個工具的優點之一。
在進行 Heap 分配分析時,async-profiler 工具會收集內存分配信息,而不是去檢測占用 CPU 的代碼。async-profiler 不使用侵入性的技術,例如字節碼檢測工具或者探針檢測等,這也說明 async-profiler 的內存分配分析像 CPU 性能分析一樣,不會產生太大的性能開銷,同時也不用寫出龐大的堆棧文件再去進行進一步處理,。
async-profile 目前支持 Linux 和 macOS 平台(macOS 下只能分析用戶空間的代碼)。
- Linux / x64 / x86 / ARM / AArch64
- macOS / x64
async-profiler 工具在采樣后可以生成采樣結果的日志報告,也可以生成 SVG 格式的火焰圖,在之前生成火焰圖要使用 FlameGraph 工具。現在已經不需要了,從 1.2 版本開始,就已經內置了開箱即用的 SVG 文件生成功能。
其他信息可以看官方文檔:https://github.com/jvm-profiling-tools/async-profiler
async-profiler 安裝
下載 async-profiler 工具可以在官方的 Github 上直接下載編譯好的文件,如果你就是想體驗手動擋的感覺,也可以克隆項目,手動編譯一下,不得不說這個工具十分的易用,我在手動編譯的過程十分順滑,沒有出現任何問題。
如果你想下載編譯好的,可以到這里下載。
https://github.com/jvm-profiling-tools/async-profiler/releases
如果想體驗手動擋的感覺,可以克隆整個項目,進項項目編譯。
手動編譯的環境要求。
- JDK
- GCC
下面是手動安裝的操作命令。
git clone https://github.com/jvm-profiling-tools/async-profiler
cd async-profiler
make
執行 make 命令編譯后會在項目的目錄下生成一個 build 文件夾,里面存放着編譯的結果。下面是我手動編譯的過程輸出。
➜ develop git clone https://github.com/jvm-profiling-tools/async-profiler
Cloning into 'async-profiler'...
remote: Enumerating objects: 69, done.
remote: Counting objects: 100% (69/69), done.
remote: Compressing objects: 100% (54/54), done.
remote: Total 1805 (delta 34), reused 32 (delta 15), pack-reused 1736
Receiving objects: 100% (1805/1805), 590.78 KiB | 23.00 KiB/s, done.
Resolving deltas: 100% (1288/1288), done.
➜ develop cd async-profiler
➜ async-profiler git:(master) make
mkdir -p build
g++ -O2 -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -DPROFILER_VERSION=\"1.6\" -I/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/include/darwin -fPIC -shared -o build/libasyncProfiler.so src/*.cpp -ldl -lpthread
gcc -O2 -DJATTACH_VERSION=\"1.5\" -o build/jattach src/jattach/jattach.c
mkdir -p build/classes
/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/javac -source 6 -target 6 -d build/classes src/java/one/profiler/AsyncProfiler.java src/java/one/profiler/AsyncProfilerMXBean.java src/java/one/profiler/Counter.java src/java/one/profiler/Events.java
警告: [options] 未與 -source 1.6 一起設置引導類路徑
1 個警告
/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/jar cvf build/async-profiler.jar -C build/classes .
已添加清單
正在添加: one/(輸入 = 0) (輸出 = 0)(存儲了 0%)
正在添加: one/profiler/(輸入 = 0) (輸出 = 0)(存儲了 0%)
正在添加: one/profiler/AsyncProfiler.class(輸入 = 1885) (輸出 = 908)(壓縮了 51%)
正在添加: one/profiler/Events.class(輸入 = 405) (輸出 = 286)(壓縮了 29%)
正在添加: one/profiler/Counter.class(輸入 = 845) (輸出 = 473)(壓縮了 44%)
正在添加: one/profiler/AsyncProfilerMXBean.class(輸入 = 631) (輸出 = 344)(壓縮了 45%)
rm -rf build/classes
➜ async-profiler git:(master)
async-profiler 使用
運行項目里的 profiler.sh 可以看到 async-profiler 的使用幫助文檔。
➜ async-profiler git:(master) ./profiler.sh
Usage: ./profiler.sh [action] [options] <pid>
Actions:
start start profiling and return immediately
resume resume profiling without resetting collected data
stop stop profiling
status print profiling status
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
-b bufsize frame buffer size
-t profile different threads separately
-s simple class names instead of FQN
-g print method signatures
-a annotate Java method names
-o fmt output format: summary|traces|flat|collapsed|svg|tree|jfr
-v, --version display version string
--title string SVG title
--width px SVG width
--height px SVG frame height
--minwidth px skip frames smaller than px
--reverse generate stack-reversed FlameGraph / Call tree
--all-kernel only include kernel-mode events
--all-user only include user-mode events
--sync-walk use synchronous JVMTI stack walker (dangerous!)
<pid> is a numeric process ID of the target JVM
or 'jps' keyword to find running JVM automatically
Example: ./profiler.sh -d 30 -f profile.svg 3456
./profiler.sh start -i 999000 jps
./profiler.sh stop -o summary,flat jps
可以看到使用的方式是:Usage: ./profiler.sh [action] [options]
常用的使用的幾個步驟:
- 查看 java 進程的 PID(可以使用 jps )。
- 使用 ./profiler.sh start
開始采樣。 - 使用 ./profiler.sh status
查看已經采樣的時間。 - 使用 ./profiler.sh stop
停止采樣,輸出結果。
這種方式使用起來多費勁啊,而且最后輸出的是文本結果,看起來更是費勁,為了不那么費勁,可以使用幫助里給的采樣后生成 SVG 文件例子。
./profiler.sh -d 30 -f profile.svg 3456
這個命令的意思是,對 PID 為 3456 的 java 進程采樣 30 秒,然后生成 profile.svg 結果文件。
默認情況下是分析 CPU 性能,如果要進行其他分析,可以使用 -e 參數。
-e event profiling event: cpu|alloc|lock|cache-misses etc.
可以看到支持的分析事件有 CPU、Alloc、Lock、Cache-misses 。
async-profiler 案例
上面說完了 async-profiler 工具的作用和使用方式,既然能進行 CPU 性能分析和 Heap 內存分配分析,那么我們就寫幾個不一般的方法分析試試看。看看是不是有像上面介紹的那么好用。
Java 案例編碼
很簡單的幾個方法,hotmethod 方法寫了幾個常見操作,三個方法中很明顯 hotmethod3 方法里的生成 UUID 和 replace(需要正則匹配)操作消耗的 CPU 性能會較多。allocate 方法里因為要不斷的創建長度為 6萬的數組,消耗的內存空間一定是最多的。
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;
/**
* <p>
* 模擬熱點代碼
*
* @Author niujinpeng
*/
public class HotCode {
private static volatile int value;
private static Object array;
public static void main(String[] args) {
while (true) {
hotmethod1();
hotmethod2();
hotmethod3();
allocate();
}
}
/**
* 生成 6萬長度的數組
*/
private static void allocate() {
array = new int[6 * 1000];
array = new Integer[6 * 1000];
}
/**
* 生成一個UUID
*/
private static void hotmethod3() {
ArrayList<String> list = new ArrayList<>();
UUID uuid = UUID.randomUUID();
String str = uuid.toString().replace("-", "");
list.add(str);
}
/**
* 數字累加
*/
private static void hotmethod2() {
value++;
}
/**
* 生成一個隨機數
*/
private static void hotmethod1() {
Random random = new Random();
int anInt = random.nextInt();
}
}
CPU 性能分析
運行上面的程序,然后使用 JPS 命令查看 PID 信息。
➜ develop jps
2800 Jps
2449 HotCode
2450 Launcher
805 RemoteMavenServer36
470 NutstoreGUI
699
➜ develop
上面運行的類名是 HotCode,可以看到對應的 PID 是 2449。
使用 ./profiler.sh -d 20 -f 2449.svg 2449
命令對 2449 號進程采樣20秒,然后得到生成的 2449.svg 文件,然后我們使用瀏覽器打開這個文件,可以看到 CPU 的使用火焰圖。
關於火焰圖怎么看,一言以蔽之:火焰圖里,橫條越長,代表使用的越多,從下到上是調用堆棧信息。在這個圖里可以看到 main 方法上面的調用中 hotmethod3 方法的 CPU 使用是最多的,點擊這個方法。還可能看到更詳細的信息。
可以看到 replace 方法占用的 CPU 最多,也是程序中性能問題所在,是需要注意的地方。
Heap 內存分析
還是上面運行的程序,進程 PID 還是 2449,這次使用 -e 參數分析內存使用情況。
命令:./profiler.sh -d 20 -e alloc -f 2449-alloc.svg 2449
命令的意思是收集進程號是 2449 的進程的內存信息 20 秒,然后輸出為 2449-alloc.svg 文件。20秒后得到 svg 文件使用瀏覽器打開,可以看到內存分配情況。
依舊是橫條越長,代表使用的越多,從下到上是調用堆棧信息。從圖里可以看出來 main 方法調用的 allocate 方法使用的內存最多,這個方法里的 Integer 類型數組占用的內存又最多,為 71%。
文中測試代碼已經上傳到 Github:https://github.com/niumoo/lab-notes
<完>
個人網站:https://www.codingme.net
如果你喜歡這篇文章,可以關注公眾號,一起成長。
關注公眾號回復資源可以沒有套路的獲取全網最火的的 Java 核心知識整理&面試核心資料。