linux - 以编程方式使用性能列表中的性能事件

标签 linux performance perf intel-pmu

当我在我的 Linux 系统上运行 perf list 时,我得到了一长串可用的性能事件。

是否可以使用 perf_event_open(2) 从另一个进程以编程方式列出和使用这些事件? ?也就是说,我如何从另一个进程获取此列表并确定要填充到 perf_event_attr 中的相应值?

我不是在寻找使用另一个第三方事件列表的解决方案,例如。 libpfm4 或 jevents。我知道可以从 /sys/devices/cpu/events/ 中的文件(以及其他事件类型的类似文件)中重建一些事件,但这些只是 中事件的一小部分perf list 显示。

最佳答案

没有解决方案可以在不使用第三方(或 first party)列表的情况下从内核(使用任何系统调用,如 perf_event_open(2))获取原始事件的完整列表。 Perf 工具使用从 /sys/bus/event_source/devices/cpu/events 和类似的 sysfs 文件夹扫描的一些基本事件,但它有自己的 cpu 型号特定事件列表:https://elixir.bootlin.com/linux/v5.5.19/source/tools/perf/pmu-events , 和 there is readme file perf 使用哪些点 jevents (perf 有 8 MB 的 x86 json 事件列表,位于 tools/perf/pmu-events/arch/x86 )

The contents of this directory allow users to specify PMU events in their CPUs by their symbolic names rather than raw event codes (see example below). The main program in this directory, is the 'jevents', which is built and executed BEFORE the perf binary itself is built. The 'jevents' program tries to locate and process JSON files in the directory tree tools/perf/pmu-events/arch/foo.

您可以从 https://mirrors.edge.kernel.org/pub/linux/kernel/tools/perf/ 下载 perf 源并使用一些源代码导航工具来检查 cmd_list 函数 builtin-list.c 文件(带有一些未记录的选项)。您还可以从这些来源构建 perf 工具,并且将在 perf 早期编译 jevents(HOSTCC pmu-events/jevents.oLINK pmu-events/jevents)建筑。

当前 cpu 模型是通过从 pmu_add_cpu_aliases 调用的 perf_pmu__find_map (util/pmu.c) 从表 pmu_events_map (pmu-events/pmu-events.c) 中检测到的,调用自 pmu_lookup ,来自 perf_pmu__find,来自 perf_pmu__scan 来自 print_pmu_events 来自 cmd_list(perf list 内置命令的处理程序)。

截至5.5 version of perf (来自 linux 内核 5.5,因为 perf 是 linux 内核的一部分),没有带有描述的事件列表的原始转储。有一个未记录的选项 perf list --raw-dump 它将打印每个可用监控单元的所有事件列表,例如 pmu:perf list --原始转储 pmu |tr ' ' '\n'。此原始转储的输出在 perf 版本之间不稳定。

perf_events 子系统的内核部分在 arch/x86/events 中没有完整的事件列表或 kernel/events文件夹,仅将标准 perf 事件(在 sysfs 中列出)如周期或 cpu/branch-misses/映射到特定 cpu 模型的原始事件。

关于linux - 以编程方式使用性能列表中的性能事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63653406/

相关文章:

linux - 如何在以 <fc=ff00> 开头的行后添加字符串 “</font>”,但不添加连续的两行?

linux - 如何将星号传递给 bash 脚本中的 ls 命令

mysql - 如何加快 MySQL 中缓慢的 SUM + ORDER BY + LIMIT 查询?

在 "perf"事件处捕获用户空间变量

linux - 在 GitHub 之前,开发者是如何共享 git repos 的?

c - Bochs 中的 PintOS 安装

javascript - 在将函数定义为 Object.prototype 扩展或只是函数之前要考虑什么?

c++ - 这段代码的时间复杂度能否进一步降低?

profiling - perf.data 文件没有样本

linux - 在 Linux 上估算任​​务的 WCET