特定处理器上的性能监控计数器 (RDPMC)

Performance Monitoring Counter (RDPMC) on a specific processor

我正在尝试使用 RDPMC 指令来计算退役指令,正如英特尔软件开发人员手册第 3 卷附录 A(在性能监控部分)提到的:

• Instructions Retired — Event select C0H, Umask 00H This event counts the number of instructions at retirement. For instructions that consist of multiple micro-ops, this event counts the retirement of the last micro-op of the instruction. An instruction with a REP prefix counts as one instruction (not per iteration). Faults before the retirement of the last micro-op of a multi-ops instruction are not counted.

我使用 here 的答案来启用 Linux 内核模式模块的性能计数器。

here可以看出(RDPMC的描述):

Loads the contents of the 40-bit performance-monitoring counter specified in the ECX register into registers EDX:EAX. The EDX register is loaded with the high-order 8 bits of the counter and the EAX register is loaded with the low-order 32 bits. The Pentium® Pro processor has two performance-monitoring counters (0 and 1), which are specified by placing 0000H or 0001H, respectively, in the ECX register.

之后,我将 0 放入 RAX 并执行 RDPMC(在用户模式下),但在 RDPMC 执行多次后 EDX:EAX 仍然为零。

所以我的问题是:

  1. 如何在用户模式下统计特定进程的 Retired Instructions?
  2. Event select C0HUmask 00H有什么区别,我想知道如何使用C0H00H

I put the 0 to RAX and execute RDPMC

选择器进入 ECX,而不是 EAX。

How to count the Retired Instructions on a specific process in user-mode?

如果您希望 Linux 虚拟化上下文切换和 CPU 迁移的性能计数器以基于每个进程而不是每个 [=44] 跟踪事物,请使用 perf stat ./a.out =] 基础。或者,如果您手动对性能计数器进行编程,请确保将进程固定到核心。

我经常用 perf stat -etask-clock,context-switches,cpu-migrations,page-faults,cycles,branches,instructions,uops_issued.any,uops_executed.thread ./a.out 分析资料。 (例如,查看 中的输出)。

Perf 的 instructions 事件使用 Instructions Retired 计数器。 (实际上它为该事件使用固定计数器,而不是用完其中一个可编程计数器上的插槽。)

非通用 uarch 特定事件的符号名称,如 uops_issued.any 过去仅在 ocperf.py 包装器脚本中可用,但 perf 4.15.gd8a5b8 在 Arch Linux 上支持他们直接。我认为这个变化是最近才发生的。

What are the differences between Event select C0H and Umask 00H and I want to know how to use C0H and 00H?

您必须使用正确的事件和单位掩码对可编程计数器进行编程。 umask 通常选择一些相关事物的变体。有关每个事件的 umask 值在 Haswell 上的作用列表,请参阅 http://oprofile.sourceforge.net/docs/intel-haswell-events.php


除了 Linux 中复杂的大型 perf 子系统之外, 已经有一些开源库用于对性能计数器进行编程以设置为从用户读取它们-space。请参阅 了解 libpfc,其中包括演示。

如果你只是想使用它,你真的不需要自己写。