通过“top”计算unix中单个进程的Cpu百分比命令

发布于 2024-10-07 22:10:06 字数 126 浏览 7 评论 0原文

我想知道“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

真心难拥有 2024-10-14 22:10:06

top 命令使用 proc 文件系统。包含 CPU 使用率数据的实际文件可能因平台而异。例如,在 Linux 中,可以在 /proc//stat对于 Solaris,可以在 /proc//psinfo。 CPU 使用率的计算方式为进程的累积 CPU 时间差除以更新之间测量的时间量。


对于 Linux,您可以检查 procps 源,其中包含 pstop 以及来自 http://procps.sourceforge.net< 的其他处理工具/em>。特别是 readproc.c 文件包含检索数据的功能。


对于 Solaris,您可以检查 libproc 来源 https: //hg.java.net/hg/solaris~on-src/file/tip/usr/src/lib/libprocprog_get_info。 c 文件包含检索数据并将其存储在 psinfo_t 结构。


对于 Linux、Solaris 和其他操作系统,您可以从 Unix Top 源代码检查href="http://sourceforge.net/projects/unixtop" rel="nofollow">http://sourceforge.net/projects/unixtopmachine 目录中特定于平台的源文件包含检索数据的功能。


更新

用于检索进程的 CPU 时间的另一个选项(仅限 Solaris)可能会传递到 PIOCPSINFOPIOCSTATUS< /a> 选项 ioctl() 系统调用。 PIOCPSINFO 选项返回 prpsinfo_t 结构。 PIOCSTATUS 选项返回 prstatus_t 结构。

改编自 http://getthegood.com/TechNotes/Papers/ProcStatistics.html 上的示例代码

int main(int argc, char* argv[])
{
    int fd;
    prpsinfo_t info;
    prstatus_t status;
    char procbuf[50];

    sprintf(procbuf, "/proc/%d", getpid());
    fd = open(procbuf, O_RDONLY);

    ioctl(fd, PIOCPSINFO, &info);
    printf("Process user+sys time = %ld sec %ld nsec\n" 
           "Reaped children user+sys time = %ld sec %ld nsec\n",
           info.pr_time.tv_sec, info.pr_time.tv_nsec,
           info.pr_ctime.tv_sec, info.pr_ctime.tv_nsec);

    ioctl(fd, PIOCSTATUS, &status);
    printf("Process user+sys time = %ld sec %ld nsec\n"
           "Sum of children's user+sys time = %ld sec %ld nsec\n",
           status.pr_utime.tv_sec+status.pr_stime.tv_sec,  
           status.pr_utime.tv_nsec+status.pr_stime.tv_nsec,  
           status.pr_cutime.tv_sec+status.pr_cstime.tv_sec,
           status.pr_cutime.tv_nsec+status.pr_cstime.tv_nsec);

    close(fd);
    exit(0);
}

注意:此代码未经测试,为了简单起见,省略了错误检查。

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 or PIOCSTATUS option to the ioctl() system call. The PIOCPSINFO option returns miscellaneous process information in a prpsinfo_t struct. The PIOCSTATUS option returns status information for the process in a prstatus_t struct.

Adapted from example code at http://getthegood.com/TechNotes/Papers/ProcStatistics.html:

int main(int argc, char* argv[])
{
    int fd;
    prpsinfo_t info;
    prstatus_t status;
    char procbuf[50];

    sprintf(procbuf, "/proc/%d", getpid());
    fd = open(procbuf, O_RDONLY);

    ioctl(fd, PIOCPSINFO, &info);
    printf("Process user+sys time = %ld sec %ld nsec\n" 
           "Reaped children user+sys time = %ld sec %ld nsec\n",
           info.pr_time.tv_sec, info.pr_time.tv_nsec,
           info.pr_ctime.tv_sec, info.pr_ctime.tv_nsec);

    ioctl(fd, PIOCSTATUS, &status);
    printf("Process user+sys time = %ld sec %ld nsec\n"
           "Sum of children's user+sys time = %ld sec %ld nsec\n",
           status.pr_utime.tv_sec+status.pr_stime.tv_sec,  
           status.pr_utime.tv_nsec+status.pr_stime.tv_nsec,  
           status.pr_cutime.tv_sec+status.pr_cstime.tv_sec,
           status.pr_cutime.tv_nsec+status.pr_cstime.tv_nsec);

    close(fd);
    exit(0);
}

Note: This code is untested and omits error checking for simplicity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文