linux 上的线程利用率分析

Thread Utilization profiling on linux

Linux 性能工具非常适合在 CPU 周期中查找热点并优化这些热点。但是一旦某些部分被并行化,就很难发现顺序部分,因为它们占用了大量的时间,但不一定需要很多 CPU 周期(并行部分已经在燃烧那些)。

为了避免 XY 问题:我的潜在动机是找到多线程代码中的顺序瓶颈。尽管由于 amdahl's law.

,顺序阶段主导了壁时间,但并行阶段可以轻松控制聚合 CPU 周期统计数据

对于 java 应用程序,使用具有线程利用时间轴的 visualvm 或 yourkit 可以很容易地实现这一点。

请注意,它显示选定范围或时间点的线程状态(可运行、等待、阻塞)和堆栈样本。

如何在 linux 上实现与 perf 或其他本机分析器相当的东西?它不一定是 GUI 可视化,只是一种查找顺序瓶颈和 CPU 与之相关的样本的方法。

另见,更窄的 侧重于 perf

您可以使用我们用来分析 Off-CPU 分析的强大工具来获得您想要的结果 - Off-CPU Flame Graphs which is apart of Flame Graphs

我用了Off-CPU analysis

Off-CPU analysis is a performance methodology where off-CPU time is measured and studied, along with context such as stack traces. It differs from CPU profiling, which only examines threads if they are executing on-CPU.

此工具基于您提到的首选工具 - perf、bcctools,但是,它提供了一个非常易于使用的输出,称为火焰图,交互式 SVG 文件如下所示 SVG Off-CPU Time Flame Graph

The width is proportional to the total time in the code paths, so look for the widest towers first to understand the biggest sources of latency. The left-to-right ordering has no meaning, and the y-axis is the stack depth.

2 个更有帮助的分析,它们是 Off-CPU 火焰图的一部分,也可以帮助您 - 就个人而言,我没有尝试过它们。

Wakeup

This lets us solve more problems than off-CPU tracing alone, as the wakeup information can explain the real reason for blocking.

Chain Graph

Chain graphs are an experimental visualization that associates off-CPU stacks with their wakeup stacks

还有一个结合了CPU和Off-CPU火焰图的实验可视化Hot/Cold Flame Graphs

This shows all thread time in one graph, and allows direct comparisons between on- and off-CPU code path durations.

阅读此分析工具并理解它的概念需要一些时间,但是,使用它非常简单,而且它的输出比您上面提到的其他工具更容易分析。

祝你好运!

Oracle's Developer Studio Performance Analyzer 可能正是您想要的。 (如果您 运行 在 Solaris 上,我知道它 完全满足您的要求,但我从未在 Linux 上使用过它,而且我现在无法访问适合试用的 Linux 系统。

这是多线程 IO 测试程序的屏幕截图,运行 在 x86 Solaris 11 系统上:

请注意,您可以看到每个线程的调用堆栈以及线程的确切交互方式 - 在发布的示例中,您可以看到实际执行 IO 的线程从哪里开始,并且您可以看到每个执行时的线程。

此视图准确显示线程 2 在突出显示时刻的位置:

此视图启用了同步事件视图,显示线程 2 在突出显示的时间段内卡在 sem_wait 调用中。注意图形数据的附加行,显示同步事件(sem_wait()pthread_cond_wait()pthread_mutex_lock() 等):

其他视图包括调用树:

一个线程概述(只有少数几个线程不是很有用,但如果你有数百个或更多线程可能非常有用

和显示函数 CPU 利用率的视图

并且你可以看到每行代码花费了多少时间:

不出所料,写入大文件以测试 IO 性能的进程几乎所有时间都花在 write() 函数上。

完整的 Oracle 简介位于 https://www.oracle.com/technetwork/server-storage/solarisstudio/documentation/o11-151-perf-analyzer-brief-1405338.pdf

快速使用概述: