免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 709 | 回复: 0
打印 上一主题 下一主题

ps命令中的%CPU的含义 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-02-22 12:32 |只看该作者 |倒序浏览
ps命令中的%CPU是指一个进程占用CPU的时间百分比,那么具体的含义是什么呢? ps的man手册的解释是这样的:
                    cpu utilization of the process in "##.#" format.
                    Currently, it is the CPU time used divided by the time the
                    process has been running (cputime/realtime ratio),
                    expressed as a percentage. It will not add up to 100%
                    unless you are lucky. (alias pcpu).
ps的代码中是这样处理的
static int pr_pcpu(char *restrict const outbuf, const proc_t *restrict const pp){
  unsigned long long total_time;   /* jiffies used by this process */
  unsigned pcpu = 0;               /* scaled %cpu, 999 means 99.9% */
  unsigned long long seconds;      /* seconds of process life */
  total_time = pp->utime + pp->stime;
  if(include_dead_children) total_time += (pp->cutime + pp->cstime);
  seconds = seconds_since_boot - pp->start_time / Hertz;
  if(seconds) pcpu = (total_time * 1000ULL / Hertz) / seconds;
  if (pcpu > 999U)
    return snprintf(outbuf, COLWID, "%u", pcpu/10U);
  return snprintf(outbuf, COLWID, "%u.%u", pcpu/10U, pcpu%10U);
}
其中seconds_since_boot是用当前时间减去系统启动时的时间得到的,启动的时间通过读/proc/stat中的btime获得。而start_time是进程被fork时设置的。另外进程的时间包括在用户态运行的时间和内核态运行的时间。这样,这个百分比的含义就是从进程被创建到执行ps操作这段时间T内,这个进程运行的时间和T的比值。
如果在ps中指定了include_dead_children选项,那么这个进程的时间还包括它的它创建的但已经死去的进程的运行时间,cutime和cstime会在父进程为子进程收尸的时候调用wait_task_zombie来累加。比如在bash中执行updatedb,在执行完成后,运行
ps -eo pcpu,comm,stat,pid|grep bash

ps S -eo pcpu,comm,stat,pid|grep bash
后者的百分比更大


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/22326/showart_482197.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP