The perf-event based instuction count mechanism is not working correctly

Which system do you use? Android, Ubuntu, OOWOW or others?

Android 11

Which version of system do you use? Khadas official images, self built images, or others?

Khadas official Android image - vim4-android-11-64bit-v231120.img.xz

Linux kernal version: 5.4.125

Please describe your issue below:

The pef-event module is reporting zero instruction count(PERF_COUNT_HW_INSTRUCTIONS) while executing the example app provided in the Linux man page (Ubuntu Manpage: perf_event_open - set up performance monitoring)

The example program is provided below. Compiled the program for using android-ndk-r26b-linux.
Compiler command used: clang++ -target aarch64-linux-android21 main.c

 #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;
     pe.exclude_kernel = 1;
     pe.exclude_hv = 1;

     fd = perf_event_open(&pe, 0, -1, -1, 0);
     if (fd == -1) {
        fprintf(stderr, "Error opening leader %llx\n", 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\n");

     ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
     read(fd, &count, sizeof(long long));

     printf("Used %lld instructions\n", count);

     close(fd);
 }

Post a console log of your issue below:

Instruction count details reported by the example program is given below.

VIM4:/data/local/test2 # ./a.out                                               
WARNING: linker: Warning: "/data/local/test2/libc++_shared.so" unused DT entry: unknown processor-specific (type 0x70000001 arg 0x0) (ignoring)
Measuring instruction count for this printf
Used 0 instructions

But this behavior will change if I run the program multiple times. I could see non-zero instruction count once in multiple iterations.

VIM4:/data/local/test2 # ./a.out                                               
WARNING: linker: Warning: "/data/local/test2/libc++_shared.so" unused DT entry: unknown processor-specific (type 0x70000001 arg 0x0) (ignoring)
Measuring instruction count for this printf
Used 0 instructions
VIM4:/data/local/test2 # ./a.out                                               
WARNING: linker: Warning: "/data/local/test2/libc++_shared.so" unused DT entry: unknown processor-specific (type 0x70000001 arg 0x0) (ignoring)
Measuring instruction count for this printf
Used 0 instructions
VIM4:/data/local/test2 # ./a.out                                               
WARNING: linker: Warning: "/data/local/test2/libc++_shared.so" unused DT entry: unknown processor-specific (type 0x70000001 arg 0x0) (ignoring)
Measuring instruction count for this printf
Used 0 instructions
VIM4:/data/local/test2 # ./a.out                                               
WARNING: linker: Warning: "/data/local/test2/libc++_shared.so" unused DT entry: unknown processor-specific (type 0x70000001 arg 0x0) (ignoring)
Measuring instruction count for this printf
Used 2604 instructions
VIM4:/data/local/test2 # ./a.out                                               
WARNING: linker: Warning: "/data/local/test2/libc++_shared.so" unused DT entry: unknown processor-specific (type 0x70000001 arg 0x0) (ignoring)
Measuring instruction count for this printf
Used 0 instructions

Hello @rahul

@goenjoy @xiong.zhang will help you then.

Hi @numbqq, thank you for the help.