This program displays the count of actual CPU core cycles executed by the current core (using the related PMC which I believe is UNHALTED_CORE_CYCLES)
#include <unistd.h>
#include <cstdio>
int main(int argc, char* argv[]){
unsigned long a, d, c, result;
c = (1UL<<30)+1;
__asm__ volatile("rdpmc" : "=a" (a), "=d" (d) : "c" (c));
result = (a | (d << 32));
printf("Current cycles : %lu\n", result);
}
It works well on Intel processors, but displays a "Segmentation fault" on AMD ones (7001 and 7002). My first guess was to find a new c value related to CPU_CLOCKS_UNHALTED AMD event (0x76) without success for the moment
- I didn't do anything special on the Intel side. Does this PMC is enabled by default?
- How can I make it work on AMD?
- I tried to enable the counter with the wrmsr commands listed here but they also gave me a "Segmentation fault" right away
- I tried the following command
echo 2 | sudo tee /sys/devices/cpu/rdpmc # enable RDPMC always, not just when a perf event is open
The number is wrong, AMD uses different RDPMC values than Intel. Depending on the processor, multiple events are directly supported through
rdpmc, please refer to this AMD manual for further information (sectionrdpmc).The core cycle number should be
0in your case.This code works for me to count
PERF_COUNT_HW_INSTRUCTIONSTested on Ryzen 7950X