プログラムのパフォーマンス解析

tcov 用のコンパイル

コードカバレージ用にプログラムをコンパイルするには、C コンパイラに対して -xa オプションを使用します。index.assist という名前のプログラムを例として使用する場合、以下のコマンドによって、tcov 用のコンパイルを行ってください。

% cc -xa -o index.assist index.assist.c

C++ または f77 コンパイラに対しては、-a コンパイラオプションを使用します。

C コンパイラは index.assist.c に存在する基本ブロックについてのデータベースエントリを含む index.assist.d ファイルを生成します。プログラム index.assist が実行され、終了した時点で、コンパイラは index.assist.d ファイルを更新します。


注 -

tcov は C および C++ プログラムとは動作しますが、#line または #file 指示が含まれるファイルはサポートしません。tcov は、#include ヘッダーファイルのコードのテストカバレージ解析も行うことができません。-xa (C)、-a (その他のコンパイラ)、+d (C++) オプションを指定してコンパイルされたアプリケーションは通常よりも実行速度が遅くなります。+d オプションは C++ のインライン関数の拡張を禁止するため、各実行時の .d ファイルの更新にかなりの時間がかかります。


index.assist.d ファイルは、環境変数 TCOVDIR が指示するディレクトリに作成されます。TCOVDIR が設定されていない場合、index.assist.d はカレントディレクトリに作成されます。

index.assist.c のコンパイルが済んだら、index.assist を実行してください。


% index.assist 
% ls *.d
index.assist.d

それでは、tcov を実行して、プログラムの各ステートメントの実行カウントの集計結果が含まれるファイルを生成してください。tcovindex.assist.d ファイルを使って、コードの注釈リストを含む index.assist.tcov ファイルを生成します。この出力には、各ソースステートメントが実行された回数が示されます。ファイルの末尾には、短い要約が付いています。


% tcov index.assist.c 
% ls *.tcov
index.assist.tcov

次に、index.assist のモジュールの1つから、C コードの一部分 を示します。問題となっているモジュールは、呼び出し頻度の高い insert_index_entry 関数です。


        struct index_entry * 
11152->	insert_index_entry(node, entry) 
        structindex_entry *node; 
        struct index_entry *entry; 
        { 
            int result; 
            int level; 

            result = compare_entry(node, entry); 
            if (result == 0) { /* exact match */
                               /* Place the page entry for the duplicate */
                               /* into the list of pages for this node */ 
59  ->      insert_page_entry(node, entry->page_entry); 
            free(entry); 
            return(node); 
            } 

11093->     if (result > 0)  /* node greater than new entry -- */
                             /* move to lesser nodes */ 
3956->      if (node->lesser != NULL) 
3626->          insert_index_entry(node->lesser, entry); 
            else { 
330->       node->lesser = entry; 
            return (node->lesser); 
            } 
            else             /* node less than new entry -- */
                             /* move to greater nodes */ 
7137->      if (node->greater != NULL) 
6766->      insert_index_entry(node->greater, entry); 
            else { 
371->       node->greater = entry; 
            return (node->greater); 
            } 
        } 

C コードの左の数値は、各ステートメントが実行された回数を表しています。insert_index_entry 関数は 11,152 回呼び出されます。

tcov は、index.assist.tcov のファイルの末尾に以下のような集計情報を追加します。


           Top 10 Blocks 

         Line        Count 
         240         21563 
         241         21563 
         245         21563 
         251         21563 
         250         21400 
         244         21299 
         255         20612 
         257         16805 
         123         12021 
         124         11962 

77       Basic blocks in this file 
55       Basic blocks executed
71.43    Percent of the file executed

         439144    Total basic block executions
         5703.17   Average executions per basic block

コードカバレージ解析用にコンパイルされたプログラムは、(異なる入力を基にしての) 繰り返し実行が可能です。つまり、プログラムに対して tcov を繰り返し使用し、各実行時の動作を比較できるということです。