如何在AMD(EPYC)处理器上使用RDPMC指令?
该程序显示由当前核心执行的实际CPU核心周期的计数(使用我认为是未核心的_core_cycles的相关PMC),
#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);
}
它在英特尔处理器上效果很好,但在AMD上显示了“分段故障”(7001和7002)。我的第一个猜测是找到一个与CPU_CLOCKS_UNHALTED AMD事件(0x76)有关的新c
值,而我暂时没有成功,
- 我在英特尔方面没有做任何特别的事情。默认情况下是否启用此PMC?
- 我该如何使其在AMD上工作?
- 我试图使用wrmsr命令在这里列出,但他们也给我一个了分割故障“马上
- 我尝试了以下命令
echo 2 | sudo tee/sys/devices/cpu/rdpmc#启用rdpmc始终,不仅是打开perf事件
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数字是错误的,AMD使用的RDPMC值与英特尔不同。根据处理器,通过
rdpmc
直接支持多个事件,请参阅此 amd手册有关更多信息(e节rdpmc
)。在您的情况下,核心周期编号应为
0
。此代码适合我计数
perf_count_hw_instructions
在Ryzen 7950x上测试
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
0
in your case.This code works for me to count
PERF_COUNT_HW_INSTRUCTIONS
Tested on Ryzen 7950X