Examples of coding and formatting events
Trace events can be used to time the execution of a program loop.
#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);
}
When you compile the sample program, you must link to the librts.a library
as follows:
# xlc -O3 sample.c -o sample -l rts
HKWD_USER1 is event ID 010 hexadecimal (you can verify this by
examining the /usr/include/sys/trchkid.h file). The report
facility does not format the HKWD_USER1 event, unless rules are provided in
the trace format file. The following example of a stanza for HKWD_USER1 could
be used:
# 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)
When you enter the example stanza, do not modify the master format
file /etc/trcfmt, but instead make a copy and keep it
in your own directory (assume you name it mytrcfmt). When you run the
sample program, the raw event data is captured in the default log file because
no other log file was specified to the trcstart() subroutine. You can
filter the output report to get only your events. To do this, run the trcrpt command
as follows:
# trcrpt -d 010 -t mytrcfmt -O "exec=on" > sample.rpt
You can browse the sample.rpt file to see the result.