Using perf to debug CPU usage issue


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
cd WSL2-Linux-Kernel/tools/perf

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.


perf record -a -F 99 -g --

Flame Graph

To generate a flame graph, we need some scripts from

You can download it directly from GitHub page, or git clone it:

git clone

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/ perf.script | ./FlameGraph/ > flamegraph.svg

flamegraph.svg is what we need. Open it browser.


perf-record(1) — Linux manual page
perf Examples | Brendan Gregg