Linux 性能监控,有什么方法可以监控每个线程?

Linux Performance Monitoring, any way to monitor per-thread?

我正在使用 Linux Ubuntu,并使用 C++ 进行编程。我已经能够使用 perf_event 访问性能计数器(指令计数、缓存未命中等)(实际上使用来自 link 的程序:https://github.com/castl/easyperf)。

但是,现在我 运行 是一个使用 pthreads 的多线程应用程序,需要指令计数和周期来分别完成每个线程。关于如何解决这个问题有什么想法吗?

谢谢!

请查看 perf 工具文档 here,它支持您正在使用的一些事件(例如:instructionscache-misses)寻找个人资料。摘自上面链接的 wiki 页面:

The perf tool can be used to count events on a per-thread, per-process, per-cpu or system-wide basis. In per-thread mode, the counter only monitors the execution of a designated thread. When the thread is scheduled out, monitoring stops. When a thread migrated from one processor to another, counters are saved on the current processor and are restored on the new one.

perf 是您可以使用的系统分析工具。它不像 https://github.com/castl/easyperf),它是一个库,您可以在代码中使用它。按照以下步骤使用它来分析您的程序:

  1. 在 Ubuntu 上安装 perf。安装在不同的 Linux 发行版中可能会有很大不同。你可以找到安装教程行。

  2. 只需运行您的程序并获取您程序的所有线程ID:

    ps -eLf | grep [application name]

  3. 打开单独的终端并运行根据手册页perf stat -t [threadid]执行:

    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有一个analysis article,大家可以感受一下。

您可以使用标准工具访问 perf_event - perf(来自 linux-工具)。它可以与程序的所有线程一起使用,并报告摘要概要文件和每个线程 (per-pid/per-tid) 概要文件。

此配置文件不是精确的硬件计数器,而是每 N 个事件采样的结果,N 调整为达到 99 Hz(每秒次数)左右。您还可以尝试 -c 2000000 选项以每 200 万个硬件事件获取样本。例如,循环事件(完整列表 - perf list 或尝试 perf stat ./program 中列出的一些事件)

perf record -e cycles -F 99 ./program
perf record -e cycles -c 2000000 ./program

所有线程的摘要。 -n 将显示样本总数

perf report -n

Per pid(这里实际上使用了 tids,所以它允许你 select 任何线程)。

文本变体将列出所有记录的线程以及摘要样本计数(使用 -c 2000000 您可以将样本计数乘以 200 万来估计线程的硬件事件计数)

perf report -n -s pid | cat

或类似 ncurses 的交互式变体,您可以在其中 select 任何线程并查看其自己的配置文件:

perf report -n -s pid