Software Tuning, Performance Optimization & Platform Monitoring
Discussion regarding monitoring and software tuning methodologies, Performance Monitoring Unit (PMU) of Intel microprocessors, and platform updating.

How can I use rdpmc asm ?

kalstein
Beginner
1,020 Views

I want to make an accurate performance measurement for a specific function.

When I searched, rdpmc was available, so I implemented c code as follows.

 

 

root@linuxnotepc:~/tmp# cat perftest.c
#include <stdio.h>
int retval;

unsigned long long rdpmc_instructions()
{
unsigned a, d, c;

c = (1<<30);
__asm__ volatile("rdpmc" : "=a" (a), "=d" (d) : "c" (c));

return ((unsigned long)a) | (((unsigned long)d) << 32);;
}

void main()
{
long long val = rdpmc_instructions();
printf("%lld\n", val);
}

 

cpu, gcc information is :

root@linuxnotepc:~/tmp# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 39 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 8
On-line CPU(s) list: 0-7
Vendor ID: GenuineIntel
BIOS Vendor ID: Intel
Model name: Intel(R) Core(TM) i7-4712MQ CPU @ 2.30GHz
BIOS Model name: Intel(R) Core(TM) i7-4712MQ CPU @ 2.30GHz Fill By OEM CPU @ 2.3GHz
BIOS CPU family: 198
CPU family: 6
Model: 60
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
Stepping: 3
CPU(s) scaling MHz: 33%
CPU max MHz: 3300.0000
CPU min MHz: 800.0000
BogoMIPS: 4589.38
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopolog
y nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fa
ult epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts md_clear flush_l1d
Virtualization features:
Virtualization: VT-x
Caches (sum of all):
L1d: 128 KiB (4 instances)
L1i: 128 KiB (4 instances)
L2: 1 MiB (4 instances)
L3: 6 MiB (1 instance)
NUMA:
NUMA node(s): 1
NUMA node0 CPU(s): 0-7
Vulnerabilities:
Itlb multihit: KVM: Mitigation: VMX disabled
L1tf: Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Mds: Mitigation; Clear CPU buffers; SMT vulnerable
Meltdown: Mitigation; PTI
Mmio stale data: Unknown: No mitigations
Retbleed: Not affected
Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Spectre v2: Mitigation; Retpolines, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Srbds: Mitigation; Microcode
Tsx async abort: Not affected
root@linuxnotepc:~/tmp# gcc --version
gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 

 

And rdpmc is already on. 

root@linuxnotepc:/sys/bus/event_source/devices/cpu# cat rdpmc
1

 

What's the problem?

0 Kudos
1 Reply
kalstein
Beginner
986 Views
echo "2" > /sys/bus/event_source/devices/cpu/rdpmc
I input upper cmd, then only 'actual cycle' works.
 
static unsigned long long rdpmc_actual_cycles()
{
    unsigned c, low, high;
    c = (1<<30) + 1;
    rdpmc(c, low, high);
    return ((unsigned long)low) | (((unsigned long)high) << 32);;
}
 
when I use c = (1<<30) or c = (1<<30)+2, 
return value is always zero.
0 Kudos
Reply