Linux 性能工具 - 如何同时分析多个进程?如何提取 CPU 周期的百分比?

Linux perf tools - How to simultaniously profiling multiple processes? How to extract Percentage of CPU cycles?

我正在尝试在我的 CPU 上分析一组流程。为了非常精确,我想使用 perf stat 命令来查看我的进程使用了​​多少个 CPU 周期。 这与 top 不同,在 top 中我只看到快照中使用的 CPU 的百分比。

不幸的是,我没有找到同时分析多个进程的方法。这可能吗?

作为第二个问题:是否可以不仅看到使用的 CPU 个周期,而且还可以看到在同一时间间隔内使用的 CPU 个周期的总量(或百分比) ?

您可以尝试 运行 perf stat -p PID1,PID2,PID3 获取您想要的每个 pid(使用 pidof、pgrep 等获取它们...)http://man7.org/linux/man-pages/man1/perf-stat.1.html

 -p, --pid=<pid>
    stat events on existing process id (comma separated list)

还有有用的 -I msecs 选项来启用定期打印和 --per-thread 来分隔线程。

也尝试使用 -A 或一些 --per-* 选项的系统范围 perf stat -ahttp://man7.org/linux/man-pages/man1/perf-stat.1.html

   -a, --all-cpus
       system-wide collection from all CPUs (default if no target is
       specified)
   -A, --no-aggr
       Do not aggregate counts across all monitored CPUs.

   --per-socket
       Aggregate counts per processor socket for system-wide mode
       measurements. This is a useful mode to detect imbalance between
       sockets. To enable this mode, use --per-socket in addition to -a.
       (system-wide). The output includes the socket number and the
       number of online processors on that socket. This is useful to
       gauge the amount of aggregation.

   --per-core
       Aggregate counts per physical processor for system-wide mode
       measurements. This is a useful mode to detect imbalance between
       physical cores. To enable this mode, use --per-core in addition
       to -a. (system-wide). The output includes the core number and the
       number of online logical processors on that physical processor.

   --per-thread
       Aggregate counts per monitored threads, when monitoring threads
       (-t option) or processes (-p option).