イベントのコーディングおよびフォーマット設定の例

プログラム・ループの実行時間を測定するためのトレース・イベントの使用法です。

#include <sys/trcctl.h>
#include <sys/trcmacros.h>
#include <sys/trchkid.h>
char *ctl_file = "/dev/systrctl";
int ctlfd;
int i;
main()
{
  printf("configuring trace collection \n");
  if (trcstart("-ad")){
    perror("trcstart");
    exit(1);
  }

  printf("opening the trace device  \n");
  if((ctlfd = open(ctl_file,0))<0){
    perror(ctl_file);
    exit(1);
  }

  printf("turning  trace on \n");
  if(ioctl(ctlfd,TRCON,0)){
    perror("TRCON");
    exit(1);
  }

  for(i=1;i<11;i++){
    TRCHKL1T(HKWD_USER1,i);

    /* The code being measured goes here. The interval */
    /* between occurrences of HKWD_USER1 in the trace  */
    /* file is the total time for one iteration.       */
  }

  printf("turning trace off\n");
  if(ioctl(ctlfd,TRCSTOP,0)){
    perror("TRCOFF");
    exit(1);
  }

  printf("stopping the trace daemon \n");
  if (trcstop(0)){
    perror("trcstop");
    exit(1);
  }

  exit(0);
}
サンプル・プログラムをコンパイルする場合、 以下のように librts.a ライブラリーにリンクする必要があります。
# xlc -O3 sample.c -o sample -l rts
HKWD_USER1 はイベント ID で、 値は 16 進数で 010 です (/usr/include/sys/trchkid.h ファイルを調べれば確認できます)。 報告機能は、 トレース・フォーマット・ファイルに規則が示されていない場合には、HKWD_USER1 イベントをフォーマット設定しません。 以下の HKWD_USER1 に関するグループの例を使用することができます。
# User event HKWD_USER1 Formatting Rules Stanza
# An example that will format the event usage of the sample program
010 1.0 L=APPL "USER EVENT - HKWD_USER1" O2.0      \n \
               "The # of loop iterations =" U4     \n \
               "The elapsed time of the last loop = " \
                endtimer(0x010,0x010) starttimer(0x010,0x010)
グループの例を入力する場合、 マスター・フォーマット・ファイル /etc/trcfmt を変更しないでください。その代わりに、コピーを作成して、 ユーザー所有のディレクトリー (例えば、mytrcfmt という名前を付けます) に保持してください。 サンプル・プログラムを実行した場合、trcstart() サブルーチンに対して他のログ・ファイルを指定していないので、 ロー・イベント・データがデフォルトのログ・ファイルに取り込まれます。 出力レポートをフィルター処理すれば、ユーザーのイベントのみを入手することができます。 これを行うには、以下のように trcrpt コマンドを実行してください。
# trcrpt -d 010 -t mytrcfmt -O "exec=on" > sample.rpt

sample.rpt ファイルをブラウズすれば、結果を表示することができます。