`SIGSEGV` when reading `HW_CPU_CYCLES` on Alder Lake efficiency cores

113 views Asked by At

I want to read the PERF_TYPE_HARDWARE + PERF_COUNT_HW_CPU_CYCLES on the Intel 12Gen. This is my test program (based on cpucycles/amd64rdpmc.c from SUPERCOP) :

#include <linux/perf_event.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

static struct perf_event_attr attr;
static int fdperf = -1;
static struct perf_event_mmap_page *buf = 0;

long long cpucycles_amd64rdpmc(void) {
  long long result;
  unsigned int seq;
  long long index;
  long long offset;

  if (fdperf == -1) {
    attr.type = PERF_TYPE_HARDWARE;
    attr.config = PERF_COUNT_HW_CPU_CYCLES;
    attr.exclude_kernel = 1;
    fdperf = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
    buf = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ, MAP_SHARED, fdperf, 0);
  }

  do {
    seq = buf->lock;
    asm volatile("" ::: "memory");
    index = buf->index;
    offset = buf->offset;
    asm volatile("rdpmc;shlq $32,%%rdx;orq %%rdx,%%rax"
                 : "=a"(result)
                 : "c"(index - 1)
                 : "%rdx");
    asm volatile("" ::: "memory");
  } while (buf->lock != seq);

  result += offset;
  result &= 0xffffffffffff;
  return result;
}

int main() {

  long long c = cpucycles_amd64rdpmc();
  printf("counter: %llx\n", c);
  return 0;
}

If I just run it, it sometimes SIGSEGVs out, some times it prints some counter value.

If I pin it to performance cores taskset -c N ./a.out with N being 1..15, it always prints the counter, If I pin it to efficiency cores, with N being 16..23 it always SIGSEGVs. Hence, I assume that this MSR is not available on those cores.

  1. Where can I find information which MSRs are available on which core?
  2. Is there another PMC for cycles on the efficiency cores (except rdtscp)?
  3. Edit: It SIGSEGV's on the rdpmc instruction with rcx=0x7ffff7ea9bd7
0

There are 0 answers