25.2. 使用 Perf
使用基本的 PCL 基础架构收集统计数据或程序执行示例相对简单。本节提供了完整的统计数据和抽样示例。
要收集 make 及其子项的统计信息,请使用以下命令:
# perf stat -- make all
perf 命令收集了大量不同的硬件和软件计数器。然后输出以下信息:
Performance counter stats for 'make all':
244011.782059 task-clock-msecs # 0.925 CPUs
53328 context-switches # 0.000 M/sec
515 CPU-migrations # 0.000 M/sec
1843121 page-faults # 0.008 M/sec
789702529782 cycles # 3236.330 M/sec
1050912611378 instructions # 1.331 IPC
275538938708 branches # 1129.203 M/sec
2888756216 branch-misses # 1.048 %
4343060367 cache-references # 17.799 M/sec
428257037 cache-misses # 1.755 M/sec
263.779192511 seconds time elapsed
perf 工具也可以记录示例。例如,要记录 make 命令及其子项上的数据,请使用:
# perf record -- make all
这将打印保存样本的文件,以及所收集的样本数量:
[ perf record: Woken up 42 times to write data ]
[ perf record: Captured and wrote 9.753 MB perf.data (~426109 samples) ]
Linux(PCL)工具的性能计数器与 OProfile 冲突
Linux(PCL)的 OProfile 和性能计数器均使用相同的硬件性能监控单元(PMU)。如果在尝试使用 PCL perf 命令时运行 OProfile,则启动 OProfile 时会出现类似以下内容的错误消息:
Error: open_counter returned with 16 (Device or resource busy). /usr/bin/dmesg may provide additional information.
Fatal: Not all events could be opened.
要使用 perf 命令,首先关闭 OProfile:
# opcontrol --deinit
然后,您可以分析 perf.data,以确定示例相对频率。报告输出包括示例的命令、对象和功能。使用 perf 报告 输出 perf.data 分析。例如,以下命令可生成消耗最多时间的可执行文件报告:
# perf report --sort=comm
生成的输出:
# Samples: 1083783860000
#
# Overhead Command
# ........ ...............
#
48.19% xsltproc
44.48% pdfxmltex
6.01% make
0.95% perl
0.17% kernel-doc
0.05% xmllint
0.05% cc1
0.03% cp
0.01% xmlto
0.01% sh
0.01% docproc
0.01% ld
0.01% gcc
0.00% rm
0.00% sed
0.00% git-diff-files
0.00% bash
0.00% git-diff-index
左侧的列显示了相对示例数量。此输出显示,将大部分时间花费在 xsltproc 和 pdfxmltex 中。为缩短完成的时间 ,请 关注 xsltproc 和 pdfxmltex。要列出 xsltproc 执行的功能,请运行:
# perf report -n --comm=xsltproc
这会生成:
comm: xsltproc
# Samples: 472520675377
#
# Overhead Samples Shared Object Symbol
# ........ .......... ............................. ......
#
45.54%215179861044 libxml2.so.2.7.6 [.] xmlXPathCmpNodesExt
11.63%54959620202 libxml2.so.2.7.6 [.] xmlXPathNodeSetAdd__internal_alias
8.60%40634845107 libxml2.so.2.7.6 [.] xmlXPathCompOpEval
4.63%21864091080 libxml2.so.2.7.6 [.] xmlXPathReleaseObject
2.73%12919672281 libxml2.so.2.7.6 [.] xmlXPathNodeSetSort__internal_alias
2.60%12271959697 libxml2.so.2.7.6 [.] valuePop
2.41%11379910918 libxml2.so.2.7.6 [.] xmlXPathIsNaN__internal_alias
2.19%10340901937 libxml2.so.2.7.6 [.] valuePush__internal_alias