Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1038178
  • 博文数量: 836
  • 博客积分: 43880
  • 博客等级: 大将
  • 技术积分: 5485
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-20 17:36
文章分类

全部博文(836)

文章存档

2011年(1)

2008年(835)

我的朋友

分类: LINUX

2008-08-20 18:18:51


PS命令中的%CPU是指一个进程占用CPU的时间百分比,那么具体的含义是什么呢? PS的man手册的解释是这样的:
cpuutilizationoftheprocessin"##.#"format.
Currently,itistheCPUtimeuseddividedbythetimethe
processhasbeenrunning(cputime/realtimeratio),
expressedasapercentage.Itwillnotaddupto100%
unlessyouarelucky.(aliaspcpu).
ps的代码中是这样处理的
staticintpr_pcpu(char*restrictconstoutbuf,constproc_t*restrictconstpp){
unsignedlonglongtotal_time;/*jiffiesusedbythisprocess*/
unsignedpcpu=0;/*scaled%cpu,999means99.9%*/
unsignedlonglongseconds;/*secondsofprocesslife*/
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)
returnsnprintf(outbuf,COLWID,"%u",pcpu/10U);
returnsnprintf(outbuf,COLWID,"%u.%u",pcpu/10U,pcpuU);
}
其中seconds_since_boot是用当前时间减去系统启动时的时间得到的,启动的时间通过读/proc/stat中的btime获得。而start_time是进程被fork时设置的。另外进程的时间包括在用户态运行的时间和内核态运行的时间。这样,这个百分比的含义就是从进程被创建到执行ps操作这段时间T内,这个进程运行的时间和T的比值。 如果在ps中指定了include_dead_children选项,那么这个进程的时间还包括它的它创建的但已经死去的进程的运行时间,cutime和cstime会在父进程为子进程收尸的时候调用wait_task_zombie来累加。比如在bash中执行updatedb,在执行完成后,运行 ps-eopcpu,comm,stat,pid|grepbash psS-eopcpu,comm,stat,pid|grepbash 后者的百分比更在。 (责任编辑:云子)


下载本文示例代码
阅读(306) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~