环境ubuntu1804 x86_64
#!/bin/bash
if [ "$2_" = "_" ];thenecho "usage ./fire.sh oncpu/offcpu pid"exit
fiif [ "$1_" = "oncpu_" ];thensudo perf record -F 99 -p $2 -g -- sleep 10syncsudo perf script > out.perfsyncsudo ./FlameGraph-master/stackcollapse-perf.pl out.perf > out.foldedsyncelif [ "$1_" = "offcpu_" ];thensudo 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 syncsudo perf inject -v -s -i perf.data.raw -o perf.datasyncsudo perf script -F comm,pid,tid,cpu,time,period,event,ip,sym,dso > out.perfawk ' 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.perf2syncsudo ./FlameGraph-master/stackcollapse.pl out.perf2 > out.foldedsyncelseecho "usage ./fire.sh oncpu/offcpu pid"exit
fiif [ "$1_" = "oncpu_" ];thensudo ./FlameGraph-master/flamegraph.pl out.folded > oncpu_$2.svg
elsesudo ./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