'How to use rdpmc instruction on AMD (EPYC) processor?

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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source