Using perf to debug CPU usage issue
Installation
Ubuntu
apt-get install linux-tools-common linux-tools-generic linux-tools-`uname -r`
Ubuntu WSL2
Ubuntu in WSL2 uses a customized Linux kernel. Therefore you have to compile your own version of perf.
sudo apt install build-essential flex bison libssl-dev libelf-dev
git clone --depth=1 https://github.com/microsoft/WSL2-Linux-Kernel.git
cd WSL2-Linux-Kernel/tools/perf
make
If no surprise, perf
will be available in WSL2-Linux-Kernel/tools/perf
.
However, I do see some issues are reported, like:
State of hardware performance monitoring in WSL2 #8480
perf is not working perfectly in WSL. I recommend you use perf in a real Linux environment.
Execution
perf record -a -F 99 -g --
Flame Graph
To generate a flame graph, we need some scripts from https://github.com/brendangregg/FlameGraph.
You can download it directly from GitHub page, or git clone it:
git clone https://github.com/brendangregg/FlameGraph
perf stores data in binary format. Firstly we need to convert binary data into human-readable format, using perf script
:
perf script > perf.script
And then, we can generate a svg file using FlameGraph tool:
./FlameGraph/stackcollapse-perf.pl perf.script | ./FlameGraph/flamegraph.pl > flamegraph.svg
flamegraph.svg
is what we need. Open it browser.
References
perf-record(1) — Linux manual page
perf Examples | Brendan Gregg