怎么样写C程序得到 cpu 占用率?
好像没有现成的接口?
都是从 /proc/stat 里去计算? /proc/stat 多久更新一次呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
好像没有现成的接口?
都是从 /proc/stat 里去计算? /proc/stat 多久更新一次呢?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
本帖最后由 reer 于 2011-06-15 10:24 编辑
to chenyx:
谢谢啊!
应该就是读取/proc下的文件把
回复 1# reer
就是这个文件,这个文件描述了到你当前时刻的整个系统状态,你可以自己设定时间段,做比较吧~
如果只有这一个接口,你也只能使用它的精度了。
本帖最后由 reer 于 2011-06-15 16:24 编辑
写好了,帖出来大家看看,有错误或者不足的地方也随便扔砖头啊:
#define get_cpu_time(us, ni, sy, idle, io, ir, so, st, gu) \
fp = fopen("/proc/stat", "rb"); \
memset (CpuStatBuf, 0, sizeof CpuStatBuf); \
fread (CpuStatBuf, 1, sizeof CpuStatBuf, fp); \
fclose (fp); \
\
line = strstr (CpuStatBuf, " "); us = atoi (line); \
line = strstr (line+2, " "); ni = atoi (line); \
line = strstr (line+1, " "); sy = atoi (line); \
line = strstr (line+1, " "); idle = atoi (line); \
line = strstr (line+1, " "); io = atoi (line); \
line = strstr (line+1, " "); ir = atoi (line); \
line = strstr (line+1, " "); so = atoi (line); \
line = strstr (line+1, " "); st = atoi (line); \
line = strstr (line+1, " "); gu = atoi (line);
/*==================================================================
//函数名 : CpuUseRate
//描述 : 从 /proc/stat 文件里获取cpu 使用信息, 计算出cpu使用率, 并打印到屏幕上;
//
//作者 : llb
//日期 : 2011-6-14
//
//输入参数 : 无;
//输出参数 : 无;
//
//返回值 : 无;
//备注1 :
// 参考 man proc 的/proc/stat 这段:
// /proc/stat
// kernel/system statistics. Varies with architecture. Common entries include:
//
// cpu 3357 0 4313 1362393
// The amount of time, measured in units of USER_HZ (1/100ths of a second on most architectures, use
// sysconf(_SC_CLK_TCK) to obtain the right value), that the system spent in user mode, user mode with low priority
// (nice), system mode, and the idle task, respectively. The last value should be USER_HZ times the second entry in
// the uptime pseudo-file.
//
// In Linux 2.6 this line includes three additional columns: iowait - time waiting for I/O to complete (since
// 2.5.41); irq - time servicing interrupts (since 2.6.0-test4); softirq - time servicing softirqs (since
// 2.6.0-test4).
//
// Since Linux 2.6.11, there is an eighth column, steal - stolen time, which is the time spent in other operating
// systems when running in a virtualized environment
//
// Since Linux 2.6.24, there is a ninth column, guest, which is the time spent running a virtual CPU for guest oper‐
// ating systems under the control of the Linux kernel.
//
// ...
//
//备注2 :
// 本函数上方定义了一个宏, 使用本函数内部变量来获得相应的cpu 时间;
//=================================================================*/
Void Cpuuserate (Void)
{
Char Cpustatbuf[256];
File *Fp;
Char *Line;
// 见本函数注解, 根据 Man Proc 的/Proc/Stat 这段, 声明下面这些变量, 依次分别对应:
// User Mode, User Mode With Low Priority, System Mode, Idle Task, Iowait, Irq, Softirq, Steal, Guest;
Int Us1 = 0, Ni1 = 0, Sy1 = 0, Idle1 = 0, Io1 = 0, Ir1 = 0, So1 = 0, St1 = 0, Gu1 = 0;
Int Us2 = 0, Ni2 = 0, Sy2 = 0, Idle2 = 0, Io2 = 0, Ir2 = 0, So2 = 0, St2 = 0, Gu2 = 0;
Int Used = 0;
Int Total = 0;
Double Rate = 0;
get_cpu_time(Us1, Ni1, Sy1, Idle1, Io1, Ir1, So1, St1, Gu1);
// 睡眠200Ms后再取下一组值;
Usleep (200000);
get_cpu_time(Us2, Ni2, Sy2, Idle2, Io2, Ir2, So2, St2, Gu2);
Used = (Us2 + Ni2 + Sy2 + Io2 + Ir2 + So2 + St2 + Gu2) - (Us1 + Ni1 + Sy1 + Io1 + Ir1 + So1 + St1 + Gu1);
Total = (Us2 + Ni2 + Sy2 + Io2 + Ir2 + So2 + St2 + Gu2 + Idle2) - (Us1 + Ni1 + Sy1 + Io1 + Ir1 + So1 + St1 + Gu1 + Idle1);
Rate = (Double) (100 * Used) / (Double) Total;
Printf ("Cpu Use Rate: %.1F %%\N", Rate);
}
这段代码有问题。。。