Following is an example. perf_example.c is a program modified from the perf_event_open man page to measure instruction count for a printf() call:
#include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <sys/ioctl.h> #include <linux/perf_event.h> #include <asm/unistd.h> static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags) { int ret; ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); return ret; } int main(int argc, char **argv) { struct perf_event_attr pe; long long count; int fd; memset(&pe, 0, sizeof(struct perf_event_attr)); pe.type = PERF_TYPE_HARDWARE; pe.size = sizeof(struct perf_event_attr); pe.config = PERF_COUNT_HW_INSTRUCTIONS; pe.disabled = 1; fd = perf_event_open(&pe, 0, -1, -1, 0); if (fd == -1) { fprintf(stderr, "Error opening leader %llx ", pe.config); exit(EXIT_FAILURE); } ioctl(fd, PERF_EVENT_IOC_RESET, 0); ioctl(fd, PERF_EVENT_IOC_ENABLE, 0); printf("Measuring instruction count for this printf "); ioctl(fd, PERF_EVENT_IOC_DISABLE, 0); read(fd, &count, sizeof(long long)); printf("Used %lld instructions ", count); close(fd); return 0; }
For compiling this program externally, we can use the following commands:
$ source /opt/poky/2.4/environment-setup-cortexa9hf-neon-poky-linux-gnueabi $ ${CC} ${CFLAGS} ${LDFLAGS} perf_example.c -o perf_example
After copying the binary to your target, you can then execute it with the help of the following steps:
# chmod a+x perf_example # ./perf_example Measuring instruction count for this printf Used 0 instructions
Obviously, using zero instructions for the printf() call can't be correct. Looking into possible causes, we find a documented erratum (ERR006259) on i.MX6 processors that states that in order for the PMU to be used, the SoC needs to receive at least 4 JTAG clock cycles after power on reset.
Rerunning the example with the JTAG connected and running:
# ./perf_example Measuring instruction count for this printf Used 3977 instructions
Signal name |
Wandboard JTAG1 connector Pin number |
3.3V |
1 |
nTRST |
2 |
TMS |
3 |
TDI |
4 |
TDO |
5 |
nSRST |
6 |
TCK |
7 |
GND |
8 |