随笔-调试-perf on-cpu off-cpu
perf on-cpu
xpid=$(cat /var/run/xx.pid); perf record -F 99 -p $xpid --call-graph dwarf -- sleep 60
直接在控制台上查看:perf report
或者生成火焰图:
perf script --header > out.stacks
/opt/FlameGraph/stackcollapse-perf.pl < out.stacks | /opt/FlameGraph/flamegraph.pl --hash > out.svg
FlameGraph: git clone https://github.com/brendangregg/FlameGraph
perf off-cpu
# test in centos7
echo 1 | sudo tee /proc/sys/kernel/sched_schedstats
xpid=$(cat /var/run/xx.pid); perf record -e sched:sched_stat_sleep -e sched:sched_switch -e sched:sched_process_exit -p $xpid -g -o perf.data.raw sleep 10
perf inject -v -s -i perf.data.raw -o perf.data # The step is merging sched_start and sched_switch events
perf script -F comm,pid,tid,cpu,time,period,event,ip,sym,dso,trace | 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 }' | \
/opt/FlameGraph/stackcollapse.pl | \
/opt/FlameGraph/flamegraph.pl --countname=ms --title="Off-CPU Time Flame Graph" --colors=io > offcpu.svg
参考
本文来自博客园,作者:LiYanbin,转载请注明原文链接:https://www.cnblogs.com/stellar-liyanbin/p/18178521