通过“top”计算unix中单个进程的Cpu百分比命令
我想知道“top”命令如何计算任何进程使用的CPU百分比。
我尝试读取 /proc 目录中的“psinfo”二进制文件,但它对查找结果没有帮助。
请提供如何完成此操作的任何信息。
提前致谢。
I would like to know how does the "top" command compute the cpu percentage used by any process.
I have tried reading the "psinfo" binary file in the /proc directory , but it didnt help in finding the result.
Please provide any information how it can be done.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
top
命令使用 proc 文件系统。包含 CPU 使用率数据的实际文件可能因平台而异。例如,在 Linux 中,可以在/proc//stat
对于 Solaris,可以在
/proc//psinfo
。 CPU 使用率的计算方式为进程的累积 CPU 时间差除以更新之间测量的时间量。对于 Linux,您可以检查 procps 源,其中包含
ps
、top
以及来自 http://procps.sourceforge.net< 的其他处理工具/em>。特别是 readproc.c 文件包含检索数据的功能。对于 Solaris,您可以检查 libproc 来源 https: //hg.java.net/hg/solaris~on-src/file/tip/usr/src/lib/libproc。 prog_get_info。 c 文件包含检索数据并将其存储在
psinfo_t
结构。对于 Linux、Solaris 和其他操作系统,您可以从 Unix Top 源代码检查href="http://sourceforge.net/projects/unixtop" rel="nofollow">http://sourceforge.net/projects/unixtop。
machine
目录中特定于平台的源文件包含检索数据的功能。更新
用于检索进程的 CPU 时间的另一个选项(仅限 Solaris)可能会传递到
PIOCPSINFO
或PIOCSTATUS
< /a> 选项ioctl()
系统调用。PIOCPSINFO
选项返回prpsinfo_t
结构。PIOCSTATUS
选项返回prstatus_t
结构。改编自 http://getthegood.com/TechNotes/Papers/ProcStatistics.html 上的示例代码:
注意:此代码未经测试,为了简单起见,省略了错误检查。
The
top
command computes the CPU usage using the data in the proc file system. The actual file containing the CPU usage data can vary from one platform to another. For example, in Linux it is found in/proc/<pid>/stat
and for Solaris it is found in/proc/<pid>/psinfo
. The CPU usage is calculated as the difference in cumulative CPU time for a process divided by the amount of time measured between updates.For Linux, you can inspect the procps source which includes
ps
,top
, and other process tool from http://procps.sourceforge.net. The readproc.c file in particular contains the functionality for retrieving the data.For Solaris, you can inspect the libproc source from https://hg.java.net/hg/solaris~on-src/file/tip/usr/src/lib/libproc. The prog_get_info.c file contains the functionality for retrieving the data and storing it in a
psinfo_t
struct.For Linux, Solaris, and others, you can inspect the Unix Top source from http://sourceforge.net/projects/unixtop. The platform-specific source files within the
machine
directory contain the functionality for retrieving the data.Update
Another option (Solaris only) for retrieving CPU time for a process may be passing to the
PIOCPSINFO
orPIOCSTATUS
option to theioctl()
system call. ThePIOCPSINFO
option returns miscellaneous process information in aprpsinfo_t
struct. ThePIOCSTATUS
option returns status information for the process in aprstatus_t
struct.Adapted from example code at http://getthegood.com/TechNotes/Papers/ProcStatistics.html:
Note: This code is untested and omits error checking for simplicity.