环境ubuntu1804 x86_64
#!/bin/bash
if [ "$2_" = "_" ];then
echo "usage ./fire.sh oncpu/offcpu pid"
exit
fi
if [ "$1_" = "oncpu_" ];then
sudo perf record -F 99 -p $2 -g -- sleep 10
sync
sudo perf script > out.perf
sync
sudo ./FlameGraph-master/stackcollapse-perf.pl out.perf > out.folded
sync
elif [ "$1_" = "offcpu_" ];then
sudo bash -c "echo 1 > /proc/sys/kernel/sched_schedstats"
sudo perf record -e sched:sched_stat_sleep -e sched:sched_switch -e sched:sched_process_exit -p $2 -g -o perf.data.raw sleep 5
sync
sudo perf inject -v -s -i perf.data.raw -o perf.data
sync
sudo perf script -F comm,pid,tid,cpu,time,period,event,ip,sym,dso > out.perf
awk ' NF > 4 { exec = $1; period_ms = int($5 / 1000000) } NF > 1 && NF <= 4 && period_ms > 0 { print $2 } NF < 2 && period_ms > 0 { printf "%s\n%d\n\n", exec, period_ms } ' ./out.perf > out.perf2
sync
sudo ./FlameGraph-master/stackcollapse.pl out.perf2 > out.folded
sync
else
echo "usage ./fire.sh oncpu/offcpu pid"
exit
fi
if [ "$1_" = "oncpu_" ];then
sudo ./FlameGraph-master/flamegraph.pl out.folded > oncpu_$2.svg
else
sudo ./FlameGraph-master/flamegraph.pl out.folded > offcpu_$2.svg
fi
火焰图生成脚本资源下载https://download.csdn.net/download/shenhuxi_yu/88518447
生成的svg文件用浏览器打开即可,注意事项如下
--> ERROR: No stack counts found: sudo apt-upgrade perl
--> echo -1 > /proc/sys/kernel/perf_event_paranoid
--> unknown symbol 编译参数加-g