1. 安装perf
sudo apt-get install linux-tools-common linux-tools-generic linux-tools-`uname -r`
2. 安装火焰图工具
git clone https://github.com/brendangregg/FlameGraph.git
3. 绘制火焰图
PID=$(ps aux | grep 'nginx: worker process' | grep -v grep | awk '{print $2}' | head -n 1)
perf record -F max -p $PID -a -g -- sleep 30
perf script > out.perf
./FlameGraph/stackcollapse-perf.pl out.perf > out.folded
./FlameGraph/flamegraph.pl --width=3000 out.folded > flamegraph.svg
rm out.perf out.folded
4. 查看火焰图
使用Chrome浏览器打开flamegraph.svg即可