struct CPU_USAGE 结构中的数据是从/proc/stat中获得的, 目录下的文件中记录了许多进程信息,如何获得需要的进程信息,谢谢??
如何获得cpu的利用率的算法,下面是一位高手查看linux系统cpu利用率的算法。那单个软件所占用的cpu利用率的算法是什么呢?use_rate = ( 1 - idle / total ) * 100改为
use_rate=(cpu_user+cpu_sys)/total*100,对么??谢谢~~
struct CPU_USAGE
{
unsigned long cpu_user;
unsigned long cpu_sys;
unsigned long cpu_nice;
unsigned long cpu_idle;
};
double Get_Cpu_Use_Rate( const struct CPU_USAGE *cur, const struct CPU_USAGE *old )
{
double user, sys, nice, idle, total;
double use_rate;
user = (double)(cur->cpu_user - old->cpu_user);
sys = (double)(cur->cpu_sys - old->cpu_sys);
nice = (double)(cur->cpu_nice - old->cpu_nice);
idle = (double)(cur->cpu_idle - old->cpu_idle);
total = user + sys + nice + idle;
use_rate = ( 1 - idle / total ) * 100;
return use_rate;
}
double Get_Cpu_Free_Rate( const struct CPU_USAGE *cur, const struct CPU_USAGE *old )
{
double user, sys, nice, idle, total;
double free_rate;
user = (double)(cur->cpu_user - old->cpu_user);
sys = (double)(cur->cpu_sys - old->cpu_sys);
nice = (double)(cur->cpu_nice - old->cpu_nice);
idle = (double)(cur->cpu_idle - old->cpu_idle);
total = user + sys + nice + idle;
free_rate = ( idle / total ) * 100;
return free_rate;
}
|
论坛里搜cpu。