测量守护进程在其挂钟运行时间的一部分内的 CPU 利用率
我正在处理一个与网络相关的守护进程:它接收数据,处理它,然后吐出它。我想通过分析该守护进程并降低其 CPU 利用率来提高其性能。我可以使用 gprof 在 Linux 上轻松完成此操作。但是,我还想使用“时间”之类的东西来衡量一段时间内的总 CPU 利用率。如果可能的话,我想在小于其总运行时间的一段时间内进行计时:因此,我想启动守护进程,等待一段时间,生成CPU统计信息,停止生成它们,然后在稍后的时间停止守护进程。
“time”命令对我来说效果很好,但它似乎要求我将守护进程作为时间的子项来启动和停止。有没有办法仅测量守护进程挂钟时间的一部分的 CPU 利用率?
I am dealing with a network-related daemon: it takes data in, processes it, and spits it out. I would like to increase the performance of this daemon by profiling it and reducing it's CPU utilization. I can do this easily on Linux with gprof. However, I would also like to use something like "time" to measure it's total CPU utilization over a period of time. If possible, I would like to time it over a period that is less than its total run time: thus, I would like to start the daemon, wait awhile, generate CPU statistics, stop generating them, then stop the daemon at some later time.
The "time" command would work well for me, but it seems to require that I start and stop the daemon as a child of time. Is there a way to measure CPU utilization for only a portion of the daemon's wall clock time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
/proc//stat
文件包含必要的信息 - 您需要查看utime
和stime
字段。这些是进程使用的用户模式和内核模式 CPU 时间的累积计数器;在测量间隔开始时读取它们,然后在测量结束时再次读取它们并计算差异。这将为您提供以 jiffies 为单位的已用 CPU 时间。要确定以 jiffies 为单位的总运行挂钟时间(以便您可以转换为平均利用率),请将
/proc/stat
中cpu0
行上的数字相加(之前和之后) ,就像/proc//stat
)。这是
/proc//stat
中前几个字段的布局,来自 Linux 源代码中的Documentation/filesystems/proc.txt
:The
/proc/<pid>/stat
file contains the necessary information - you're after theutime
andstime
fields. Those are cumulative counters of the process's user-mode and kernel-mode CPU time used; read them at the start of the measuring interval, then read them again at the end and calculate the difference.That will give you used CPU time in jiffies. To determine the total elapsed wallclock time in jiffies (so you can convert to an average utilisation), sum the numbers on the
cpu0
line in/proc/stat
(before and after, just like/proc/<pid>/stat
).This is the layout of the first few fields in
/proc/<pid>/stat
, fromDocumentation/filesystems/proc.txt
in the Linux source: