ArmDeveloperEcosystem / arm-learning-paths

Arm Learning Paths: a repository of how-to content for software developers
https://learn.arm.com
Other
80 stars 156 forks source link

Id checked but not used in <Use perf_event_open for counting> #1352

Open CpyKing opened 1 month ago

CpyKing commented 1 month ago

I found some code that confused me in Configure multiple counters Section under Use perf_event_open for counting Chapter. In the code under the annotation // Read the group of counters and print result, it shows like this...

read(fd[0], &counter_results, sizeof(struct read_format));
printf("Num events captured: %"PRIu64"\n", counter_results.nr);
for(int i = 0; i < counter_results.nr; i++) {
  for(int j = 0; j < TOTAL_EVENTS ;j++){
    if(counter_results.values[i].id == id[j]){
      pe_val[i] = counter_results.values[i].value;
    }
  }
}
printf("CPU cycles: %"PRIu64"\n", pe_val[0]);
printf("Instructions retired: %"PRIu64"\n", pe_val[1]);
printf("Frontend stall cycles: %"PRIu64"\n", pe_val[2]);
printf("Backend stall cycles: %"PRIu64"\n", pe_val[3]);
printf("Loads executed speculatively: %"PRIu64"\n", pe_val[4]);
printf("Stores executed speculatively: %"PRIu64"\n", pe_val[5]);

We can see that traverse id[j] here is to check the true_id that value belonged to. However, it assign the value with index i to pe_val with this same index i (not j) and print them sequentially. It make the check part useless.

So, is there any problem around here ?

jsrz commented 2 weeks ago

Hi,

Yes, you can get rid of that inner for loop and check. I don't recall exactly why I placed that check there. I was probably paranoid about something and wanted to make sure the data returned in the counters_results was actually indexed the same way as when the events were created.