关于配置文件:如何使用linux`perf`工具生成“ CPU外”配置文件

How to use linux `perf` tool to generate “Off-CPU” profile

Brendan D. Gregg(DTrace书的作者)有一个有趣的配置文件变体:" Off-CPU"配置文件(和Off-CPU Flame Graph;幻灯片2013,p112-137)查看线程或应用程序被阻塞的位置(是不是由CPU执行,而是由于CPU资源不足而正在等待I / O,页面错误处理程序或调度的):

This time reveals which code-paths are blocked and waiting while off-CPU, and for how long exactly. This differs from traditional profiling which often samples the activity of threads at a given interval, and (usually) only examine threads if they are executing work on-CPU.

他还可以将CPU外配置文件数据和CPU上配置文件组合在一起:http://www.brendangregg.com/FlameGraphs/hotcoldflamegraphs.html

Gregg给出的示例是使用dtrace制作的,在Linux OS中通常不可用。但是有一些类似的工具(ktap,systemtap,perf)和perf,因为我认为安装范围最广。通常perf生成CPU上的配置文件(该功能在CPU上执行得更多)。

  • 如何在Linux中将Gregg的Off-CPU示例转换为perf分析工具?

PS:在LISA13,p124的幻灯片中,有离CPU火焰图的systemtap变体的链接:"张一春创建了这些,并已在Linux上使用它们与SystemTap来收集特性数据。请参阅:http:// /agentzh.org/misc/slides/off-cpu-flame-graphs.pdf"(2013年8月23日的CloudFlare啤酒会议)


我发表的perf技术[1]是一个高开销的解决方法,直到perf对此提供BPF支持为止。

目前,在Linux上生成CPU外火焰图的成本最低的方法是在4.6+内核(具有BPF堆栈跟踪支持)上,并且具有bcc / BPF。我为此编写了一个工具offcputime [2],该工具可以与-f选项一起运行以用于"折叠输出",适合于将其输入flamegraph.pl。这个offcputime工具对内核内容中的所有内容进行计时和堆栈计数,并转储报告,然后用符号打印。

我希望有一天,perf本身也将能够做到这一点:运行一个BPF程序,该程序执行内核内计数和报告转储。

同时,我们可以使用密件抄送/ BPF。如果由于某种原因不能使用bcc,则可以立即使用该offcputime程序并将其用C编写。Linux源代码中提供了一个更复杂的版本,如samples / bpf / offwaketime *。有了Linux上的新BPF功能,只要有意愿,就有办法。

[1] http://www.brendangregg.com/blog/2015-02-26/linux-perf-off-cpu-flame-graph.html

[2] https://github.com/iovisor/bcc/blob/master/tools/offcputime_example.txt


Brendan Gregg发布了有关Off-cpu火焰图生成的说明:
http://www.brendangregg.com/blog/2015-02-26/linux-perf-off-cpu-flame-graph.html
和https://github.com/brendangregg/FlameGraph/issues/47#

Off-CPU time flame graphs may solve (say) 60% of the issues, with the remainder requiring walking the thread wakeups to find root cause. I explained off-CPU time flame graphs, this wakeup issue, and additional work, in my LISA13 talk on flame graphs (slides, youtube).

Here I'll show one way to do off-CPU time flame graphs using Linux perf_events.

1
2
3
4
5
6
7
8
9
10
11
12
# perf record -e sched:sched_stat_sleep -e sched:sched_switch \
 -e sched:sched_process_exit -a -g -o perf.data.raw sleep 1
# perf inject -v -s -i perf.data.raw -o perf.data
# 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
%d

", exec, period_ms }' | \
./stackcollapse.pl | \
./flamegraph.pl --countname=ms --title="Off-CPU Time Flame Graph" --colors=io > offcpu.svg

Gregg的stackcollapse.pl和flamegraph.pl用于绘制Flamegraph。

从3.17内核和更新的内核中使用了perf选项...