如何在 Redhat Linux 中获取线程 CPU 利用率指标
我需要获取进程中所有线程的 CPU 利用率指标。
- 操作系统 = Redhat Linux
- 编程语言 = 使用 POSIX
- 要求的 C++ = 需要无限期地每隔几秒采样一次,而不仅仅是及时获取一个快照。
约束=不允许在线程中编写额外的代码
我知道你可以使用“top”命令,但是还有什么其他方法呢? 是否有“ps”的标志?
预先感谢您的所有帮助。
I need to get CPU utilization metrics for all the threads in a process.
- Operating system = Redhat linux
- programming language = C++ using POSIX
- requirements = need to take samples every few seconds indefinetly, not just for one snapshot in time.
constraints = not allowed to write additional code in thread
I know you can use the "top" command, but what other ways are there? Is there a flag for "ps"?
Thank you in advance for all your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以读取
/proc/[您的 PID]/stat
的内容来获取整个进程的信息,如果您有 2.6 内核,还有/proc/[您的 PID]/ task/[thread ID]/stat
包含各个线程的信息。 (参见此处)具体来说,您会发现这两个字段:
这里有问题的部分是给出值的单位。 jiffy 是 1/HZ 秒,其中 HZ 是内核时钟节拍速率,确定该时钟速率是困难的部分。
如果您仅需要为一个特定系统提供此值,则只需执行一些测试或查看内核标头并将该值硬编码到您的程序中即可。 如果您想知道如何以更通用的方式确定它,您可以通过查看 其源代码(参见
old_Hertz_hack()
函数和相关注释)You can read the content of
/proc/[your PID]/stat
to get information for the whole process and if you have a 2.6 kernel there is also/proc/[your PID]/task/[thread ID]/stat
with the information for the individual threads. (see here)Specifically, you will find these two fields:
The problematic part here is the unit in which the values are given. A jiffy is 1/HZ seconds, where HZ is the kernel clock tick rate and determining this clock rate is the hard part.
If you need this only for one specific system, you can just do some tests or look at your kernel headers and hardcode this value into your program. If you want to know how to determine it in a more general way, you can look at how a tool like top is doing it by looking at its source code (see the
old_Hertz_hack()
function and the related comments)可能更简单的方法是使用 getrusage 与 linux 特定的
RUSAGE_THREAD
的扩展。 一旦获得这些时间,您只需减去上次采样的时间,然后除以自上次采样以来经过的实际时间。 也就是说,您可以获得 CPU 使用率的百分比。有关 Linux 特定文档,请参阅 rusage liunx 手册页。
Possibly a simpler way of doing this is to use getrusage with the linux specific extension of
RUSAGE_THREAD
. Once you have these times, you can just subtract the times the last time you sampled, and divide by the real time that passed since your last sample. That say you get the CPU usage as a percentage.For linux specific documentation, see the rusage liunx man page.
您可以使用 Linux 中提供的 gnu gprof 来完成此操作。 我相信他们声称支持线程,但这比进程级别的分析更复杂,因此您可能无法获得您正在寻找的结果。
以下是适合您情况的操作方法。
You may be able to do this with gnu gprof which is available in Linux. I believe that they claim to support threading, but this is more complicated than profiling at the process level, so you may not get the results you're looking for.
Here's a howto for your situation.
您可以使用“top”命令首先查找进程ID。
之后,您可以使用以下命令仅显示进程的CPU/内存使用情况
之后,您可以按“Shift”+“h”来显示线程
you can use "top" command to find the process Id first.
After that, you can use the following command to only display CPU/Memory usage for the process
After that, you can press "Shift"+"h" to show the threads