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) ]
Performance Counters for Linux (PCL) ツールと OProfile の競合
OProfile と Performance Counters for Linux (PCL) は、どちらも同じハードウェアの Performance Monitoring Unit (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 report を使用して 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
左のコラムはサンプルの相対頻度を示しています。この出力では、make が xsltproc および pdfxmltex で最も多くの時間を消費していることを示しています。make が完了する時間を短縮するには、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