分类: LINUX
2008-08-20 18:18:51
cpuutilizationoftheprocessin"##.#"format. Currently,itistheCPUtimeuseddividedbythetimethe processhasbeenrunning(cputime/realtimeratio), expressedasapercentage.Itwillnotaddupto100% unlessyouarelucky.(aliaspcpu).
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); }