我正在使用Linux Ubuntu,并用C++编程。我已经能够使用perf_event访问性能计数器(指令计数,缓存未命中等)(实际上使用此链接中的程序:https://github.com/castl/easyperf)。
但是,现在我正在使用 pthreads 运行一个多线程应用程序,并且需要分别完成每个线程的指令计数和周期。关于如何做到这一点的任何想法?
谢谢!
perf 是您可以使用的系统分析工具。 它不像 https://github.com/castl/easyperf),它是一个库,您可以在代码中使用它。按照步骤操作并使用它来分析您的程序:
-
在 Ubuntu 上安装
perf
。在不同的 Linux 发行版中,安装可能会有很大不同。您可以找到安装教程行。 -
只需运行程序并获取程序的所有线程 ID:
ps -eLf | grep [application name]
-
打开单独的终端并根据手册页按
perf stat -t [threadid]
运行 PERF:usage: perf stat [<options>] [<command>]
-e, --event <event> event selector. use 'perf list' to list available events -i, --no-inherit child tasks do not inherit counters -p, --pid <n> stat events on existing process id -t, --tid <n> stat events on existing thread id -a, --all-cpus system-wide collection from all CPUs -c, --scale scale/normalize counters -v, --verbose be more verbose (show counter open errors, etc) -r, --repeat <n> repeat command and print average + stddev (max: 100) -n, --null null run - dont start any counters -B, --big-num print large numbers with thousands' separators
有一篇关于perf
的分析文章,你可以对此有所了解。
标准工具来访问perf_event
- perf
(来自linux-tools)。它可以与程序的所有线程一起使用,并报告摘要配置文件和每线程(每 pid/per-tid)配置文件。
此配置文件不是精确的硬件计数器,而是每 N 个事件采样的结果,N 调整为大约 99 Hz(每秒次数)。您还可以尝试-c 2000000
选项,每 2 百万个硬件事件获取样本。例如,循环事件(完整列表 - perf list
或尝试一些在perf stat ./program
中列出的事件)
perf record -e cycles -F 99 ./program
perf record -e cycles -c 2000000 ./program
所有线程的摘要。 -n
将显示样本总数
perf report -n
每个 pid(实际上这里使用了 tid,因此它将允许您选择任何线程)。
文本变体将列出使用汇总样本计数记录的所有线程(-c 2000000
您可以将样本计数乘以 200 万以估计线程的硬件事件计数)
perf report -n -s pid | cat
或者类似 ncurses 的交互式变体,您可以在其中选择任何线程并查看其自己的配置文件:
perf report -n -s pid
请查看此处的perf
工具文档,它支持您要分析的一些事件(例如:instructions
和cache-misses
)。摘自上面链接的维基页面:
perf 工具可用于按线程、按进程、按 CPU 或系统范围对事件进行计数。在每线程模式下,计数器仅监视指定线程的执行。当线程被调度出来时,监视将停止。当线程从一个处理器迁移到另一个处理器时,计数器将保存在当前处理器上,并在新处理器上恢复。