Applistar

7. 4. 4 プロファイラを使い、実行時間の内訳を調べる

TOP > 7 モデリングガイドライン > 7. 4 ツールと環境整備 > 7. 4. 4 プロファイラを使い、実行時間の内訳を調べる
  7. 4. 4 プロファイラを使い、実行時間の内訳を調べる  

7. 4. 4 プロファイラを使い、実行時間の内訳を調べる

プロファイルは直訳すると断面ですが、プログラムの断面、 というと各関数の実行時間や、 実行回数をいいます。これらを 測定する道具をプロファイラと呼びます。これを使って、 「実行時間を短くするにはどの辺を調整すればよいか」 を調べます。 例えば GNU の gprof というものがあります。

この gprof は プログラムを書いている人には良く知られていると 思いますが、SystemC で設計する時にも大変役立ちますので、紹介します。

gprof はプロファイリング・ツールとも呼ばれるもので、 プログラムの中の、それぞれの関数の実行時間、実行回数を計測する 道具です。例えば、次のようなファイルからなるプログラムがあった として、この中の decoder.c の中の関数を洗い出し、それらについて 計測してくれます。

  c:/cygwin/home/makoto/systemc/mpeg-4-itoh:
  total 1463  free 6567376
  drwxrwxrwx   2 makoto   root        0 Jun 24 15:22 .
  drwxrwxrwx   3 makoto   root        0 Jul  8 10:06 ..
  -rw-rw-rw-   1 makoto   root   230551 Jun 24 15:16 decoder.c
  -rw-rw-rw-   1 makoto   root    16289 Mar  5  2003 decoder.h
  -rw-rw-rw-   1 makoto   root        0 Mar  5  2003 decoder_io_driver.h
  -rw-rw-rw-   1 makoto   root        0 Mar  5  2003 decoder_io_driver_def.h
  -rw-rw-rw-   1 makoto   root      292 Mar  5  2003 decoder_local_data.h
  -rw-rw-rw-   1 makoto   root    23684 Feb 26  2003 mouse_enc.bits

以下では、Unix 環境、または Windows なら cygwin を入れて いることを前提とします。

7. 4. 4. 1 設置

gprof は アセンブラ(as)、リンカ(ld)などと供に binutils に含まれています。 このため、もしまだ用意されていないなら、 binutils を入れます。 Cygwin の場合は Cygin のインストーラである setup.exe を使います。 setup.exe をクリックして起動して Select Packages の画面で

    +Devel   Skip       binutils: The GNU assembler, linker and binary
                        utilities

になっていたら、その Skip をクリックして 20040312-1 などとしてから 「次へ」を選んで設置します。

7. 4. 4. 2 ユーザプログラムのコンパイルと実行

gprof を使うには cc の時に -pg を付けます。 例えば、

$ gcc -O3 -o decoder decoder.c -pg

とします。これで次の実行形式が出来ます。

-rwxrwxrwx   1 makoto   root    89317 Jun 24 15:41 decoder.exe

この後に、decoder を、例えば次のようにして普通どおり実行します。

$ ./decoder mouse_enc.bits

すると、同じディレクトリに通常の出力以外に gmon.out という名前のファイル が出来ます。

-rw-rw-rw-   1 makoto   root    32492 Jun 24 15:42 gmon.out
-rw-rw-rw-   1 makoto   root  1064448 Jun 24 15:42 test.yuv

7. 4. 4. 3 gprof を実行してプロファイルを得る

gprof を使って 上で出来た gmon.out の 中身を見ます。一つの方法として出力をファイルに保存します。

$ gprof decoder.exe > gprof.txt

 -rw-rw-rw-   1 makoto   root    40100 Jun 24 15:43 gprof.txt

例えば、この作成された gprof.txt の最初の方は次のようになっています。

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 25.00      0.04     0.04     8868     0.00     0.00  nnBlockIDCT
 18.75      0.07     0.03     2871     0.01     0.01  FillVop
 12.50      0.09     0.02     6237     0.00     0.00  Rhalfpel_motion_compensationYUV
 12.50      0.11     0.02       21     0.95     0.95  out_qcif_comp
  6.25      0.12     0.01     8868     0.00     0.00  BlockDequantMPEG
  6.25      0.13     0.01      700     0.01     0.05  GetMBblockdata_BVOP
  6.25      0.14     0.01      198     0.05     0.05  MB_clip
  6.25      0.15     0.01       28     0.36     0.36  VopUpdata
  6.25      0.16     0.01        7     1.43     1.43  Rinterpolate_image_QCIF
  0.00      0.16     0.00   177408     0.00     0.00  unrestricted_MC
  0.00      0.16     0.00    87552     0.00     0.00  unrestricted_MC_chro
  0.00      0.16     0.00    68976     0.00     0.00  InBitstreamShowBits
  0.00      0.16     0.00    67275     0.00     0.00  InBitstreamFlushBits
  0.00      0.16     0.00    36049     0.00     0.00  InBitstreamReadBits
  0.00      0.16     0.00    19653     0.00     0.00  VlcDecTCOEF_v2
  0.00      0.16     0.00     8868     0.00     0.00  PutBlock_r_block
  0.00      0.16     0.00     8280     0.00     0.00  Idir_d
  0.00      0.16     0.00     5432     0.00     0.00  VlcDecMV_v2
  0.00      0.16     0.00     3418     0.00     0.00  RVlcGetBlock
  0.00      0.16     0.00     2962     0.00     0.00  VLC
  0.00      0.16     0.00     2900     0.00     0.00  CopyIqBlock

この中の一行目に注目すると、 nnBlockIDCT の実行時間が全体の 25% を占めていることが 分ります。もしこれを H/W 化すると、実行時間が約 75% に短縮され るということが分ります。(「それしか短縮されない」という見方も 出来ます)

man gprof とすると、単純な使い方の他にも、色々な方法があること が分ります。

  7. 4. 4 プロファイラを使い、実行時間の内訳を調べる  
TOP > 7 モデリングガイドライン > 7. 4 ツールと環境整備 > 7. 4. 4 プロファイラを使い、実行時間の内訳を調べる