CPU profiling with linux perf
tool.
Based on Brendan Gregg's posts: 1 2
linux perf
tool
sudo apt install linux-perf
Make all events available to all users:
echo "kernel.perf_event_paranoid = -1" | sudo tee -a /etc/sysctl.conf
reboot
flamegraphs
git clone https://github.com/brendangregg/FlameGraph
record data
Sample for 60 seconds at 99 Hertz (-F 99) across all CPUs (-a), capturing stack traces so that a call graph (-g) of function ancestry can be generated later.
The samples are saved in a perf.data
file, which are read by perf script
.
For the entire system:
perf record -F 99 -a -g -- sleep 60
For specific process:
perf record -F 99 -p 12345 -a -g -- sleep 60
See more one-liners for recording here
process data
perf script > out.perf
collapse stack
./FlameGraph/stackcollapse-perf.pl out.perf > out.folded
generate flamegraph
./FlameGraph/flamegraph.pl --inverted out.folded > out.svg
view flamegraph(for example with chromium
)
chromium out.svg