当前位置: 技术问答>linux和unix
如何获取CPU利用率?
来源: 互联网 发布时间:2015-11-09
本文导语: 在各论坛搜了两天了,有说从loadavg文件获取的,也有说从stat文件获取的。关键是loadavg文件中给出的是1分钟,5分钟,15分钟的平均进程负载,该怎么用这三个数计算CPU利用率我还不明白。 至于stat文件,说是两次读...
在各论坛搜了两天了,有说从loadavg文件获取的,也有说从stat文件获取的。关键是loadavg文件中给出的是1分钟,5分钟,15分钟的平均进程负载,该怎么用这三个数计算CPU利用率我还不明白。
至于stat文件,说是两次读取stat文件数据再计算,在一个网站上看到了计算方法,但我得出的结果总是0,因为前三个数总是相同的,我两次读取stat的文件的时间间隔分别取过1到5秒,结果都一样。是不是要间隔几分钟呢?我不知道。
有知道的大侠请指点一下。3ks
至于stat文件,说是两次读取stat文件数据再计算,在一个网站上看到了计算方法,但我得出的结果总是0,因为前三个数总是相同的,我两次读取stat的文件的时间间隔分别取过1到5秒,结果都一样。是不是要间隔几分钟呢?我不知道。
有知道的大侠请指点一下。3ks
|
头文件
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define _ULL unsigned long long
int FiveCpuNumbers(_ULL *uret,_ULL *nret,_ULL *sret,_ULL *iret,_ULL *iowait);
int ReadCpuInfo(float &fCpuUsage);
int GetMemUsage();
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define _ULL unsigned long long
int FiveCpuNumbers(_ULL *uret,_ULL *nret,_ULL *sret,_ULL *iret,_ULL *iowait);
int ReadCpuInfo(float &fCpuUsage);
int GetMemUsage();
|
#define SET_IF_DESIRED(x,y) if(x) *(x) = (y)
#define _ULL unsigned long long
int FiveCpuNumbers(_ULL *uret,_ULL *nret,_ULL *sret,_ULL *iret,_ULL *iowait)
{
static _ULL u = 0, m = 0, s = 0, i = 0, iw = 0;
_ULL user_j, nice_j, sys_j, idle_j, iowait_j = 0;
FILE *fp;
size_t byte_read;
char buffer[100];
fp = fopen("/proc/stat", "r");
byte_read = fread(buffer, 1, sizeof(buffer)-1, fp);
fclose(fp);
if (byte_read==0 || byte_read==sizeof(buffer))
return -1;
buffer[byte_read] = '';
sscanf(buffer, "cpu %Lu %Lu %Lu %Lu %Lu", &user_j, &nice_j, &sys_j, &idle_j, &iowait_j);
SET_IF_DESIRED(uret, user_j - u);
SET_IF_DESIRED(nret, nice_j - m);
SET_IF_DESIRED(sret, sys_j - s);
/* Idle can go backwards one tick due to kernel calculation issues */
SET_IF_DESIRED(iret, (idle_j > i) ? (idle_j - i) : 0);
SET_IF_DESIRED(iowait, iowait_j - iw);
u = user_j;
m = nice_j;
s = sys_j;
i = idle_j;
iw = iowait_j;
printf("cpu %Lu %Lu %Lu %Lu %Lu n",user_j,nice_j,sys_j,idle_j,iowait_j);
return 0;
}
/*
* get the cpu usage
*/
int ReadCpuInfo(float &fCpuUsage)
{
float cpu_usage;
_ULL user_j, nice_j, sys_j, idle_j, iowait_j = 0;
if(FiveCpuNumbers(&user_j, &nice_j, &sys_j, &idle_j, &iowait_j)!=0)
return -1;
usleep(500000);
if(FiveCpuNumbers(&user_j, &nice_j, &sys_j, &idle_j, &iowait_j)!=0)
return -1;
usleep(500000);
if(FiveCpuNumbers(&user_j, &nice_j, &sys_j, &idle_j, &iowait_j)!=0)
return -1;
cpu_usage = (idle_j * 100.0) / (user_j + nice_j + sys_j + idle_j + iowait_j);
if (cpu_usage>100)
cpu_usage = 100.0;
/* change to useage */
cpu_usage = 100.0 - (cpu_usage);
printf("### CPU Usage : %0.3f %%n", cpu_usage);
return 0;
}
#undef _ULL
/*
* get the mm usage
*/
int GetMemUsage()
{
float memusage;
FILE *fp;
char buffer[1024];
size_t byte_read;
char *pos;
int totalmem;
int freemem;
fp = fopen("/proc/meminfo", "r");
byte_read = fread(buffer, 1, sizeof(buffer), fp);
fclose(fp);
if (byte_read==0 || byte_read==sizeof(buffer))
return -1;
buffer[byte_read] = '';
pos = strstr(buffer, "MemTotal:");
if (NULL==pos)
return -1;
sscanf(pos, "MemTotal: %d kB", &totalmem);
pos = strstr(pos, "MemFree:");
if (NULL==pos)
return -1;
sscanf(pos, "MemFree: %d kB", &freemem);
memusage = (totalmem-freemem) * 100.0 / totalmem;
// printf("### MemUsage(total:%d free:%d %MEM:%3.2f%)n", totalmem, freemem, memusage);
return 0;
}
#define _ULL unsigned long long
int FiveCpuNumbers(_ULL *uret,_ULL *nret,_ULL *sret,_ULL *iret,_ULL *iowait)
{
static _ULL u = 0, m = 0, s = 0, i = 0, iw = 0;
_ULL user_j, nice_j, sys_j, idle_j, iowait_j = 0;
FILE *fp;
size_t byte_read;
char buffer[100];
fp = fopen("/proc/stat", "r");
byte_read = fread(buffer, 1, sizeof(buffer)-1, fp);
fclose(fp);
if (byte_read==0 || byte_read==sizeof(buffer))
return -1;
buffer[byte_read] = '';
sscanf(buffer, "cpu %Lu %Lu %Lu %Lu %Lu", &user_j, &nice_j, &sys_j, &idle_j, &iowait_j);
SET_IF_DESIRED(uret, user_j - u);
SET_IF_DESIRED(nret, nice_j - m);
SET_IF_DESIRED(sret, sys_j - s);
/* Idle can go backwards one tick due to kernel calculation issues */
SET_IF_DESIRED(iret, (idle_j > i) ? (idle_j - i) : 0);
SET_IF_DESIRED(iowait, iowait_j - iw);
u = user_j;
m = nice_j;
s = sys_j;
i = idle_j;
iw = iowait_j;
printf("cpu %Lu %Lu %Lu %Lu %Lu n",user_j,nice_j,sys_j,idle_j,iowait_j);
return 0;
}
/*
* get the cpu usage
*/
int ReadCpuInfo(float &fCpuUsage)
{
float cpu_usage;
_ULL user_j, nice_j, sys_j, idle_j, iowait_j = 0;
if(FiveCpuNumbers(&user_j, &nice_j, &sys_j, &idle_j, &iowait_j)!=0)
return -1;
usleep(500000);
if(FiveCpuNumbers(&user_j, &nice_j, &sys_j, &idle_j, &iowait_j)!=0)
return -1;
usleep(500000);
if(FiveCpuNumbers(&user_j, &nice_j, &sys_j, &idle_j, &iowait_j)!=0)
return -1;
cpu_usage = (idle_j * 100.0) / (user_j + nice_j + sys_j + idle_j + iowait_j);
if (cpu_usage>100)
cpu_usage = 100.0;
/* change to useage */
cpu_usage = 100.0 - (cpu_usage);
printf("### CPU Usage : %0.3f %%n", cpu_usage);
return 0;
}
#undef _ULL
/*
* get the mm usage
*/
int GetMemUsage()
{
float memusage;
FILE *fp;
char buffer[1024];
size_t byte_read;
char *pos;
int totalmem;
int freemem;
fp = fopen("/proc/meminfo", "r");
byte_read = fread(buffer, 1, sizeof(buffer), fp);
fclose(fp);
if (byte_read==0 || byte_read==sizeof(buffer))
return -1;
buffer[byte_read] = '';
pos = strstr(buffer, "MemTotal:");
if (NULL==pos)
return -1;
sscanf(pos, "MemTotal: %d kB", &totalmem);
pos = strstr(pos, "MemFree:");
if (NULL==pos)
return -1;
sscanf(pos, "MemFree: %d kB", &freemem);
memusage = (totalmem-freemem) * 100.0 / totalmem;
// printf("### MemUsage(total:%d free:%d %MEM:%3.2f%)n", totalmem, freemem, memusage);
return 0;
}
|
stat文件是一直在变的,怎么可能1-5秒都不变呢
|
简单:
用prstat就可以!
用prstat就可以!
|
用vmstat命令