当前位置: 技术问答>linux和unix
在linux下如何编程得到系统的CPU利用率,内存利用率等
来源: 互联网 发布时间:2015-11-02
本文导语: 在linux下如何编程得到系统的CPU利用率,内存利用率等。 简单地说,就是如何编写一个像windows下的任务管理器一样的linux任务管理器 | 起先我也是用这种读内存文件/proc/stat /proc/meminfo的方法...
在linux下如何编程得到系统的CPU利用率,内存利用率等。
简单地说,就是如何编写一个像windows下的任务管理器一样的linux任务管理器
简单地说,就是如何编写一个像windows下的任务管理器一样的linux任务管理器
|
起先我也是用这种读内存文件/proc/stat /proc/meminfo的方法。
但是这样程序的可移植性似乎不太好。
在red hat linux8.0中,读出的/proc/stat文件的cpu有四个数据项,而在fedora core 4中,
读出的/proc/stat文件的cpu不只四个数据项。这样说来,不同版本的linux有/proc/*文件数据。(当然文件是相同的)。
所以我想,是不是能实时读取系统运行信息,而不通过内存文件呢?
但是这样程序的可移植性似乎不太好。
在red hat linux8.0中,读出的/proc/stat文件的cpu有四个数据项,而在fedora core 4中,
读出的/proc/stat文件的cpu不只四个数据项。这样说来,不同版本的linux有/proc/*文件数据。(当然文件是相同的)。
所以我想,是不是能实时读取系统运行信息,而不通过内存文件呢?
|
cpu利用率/proc/stat
mm利用率/proc/meminfo
mm利用率/proc/meminfo
|
看看ps,top, vmstat源码是怎么实现的吧.
|
我的代码
#define SET_IF_DESIRED(x,y) if(x) *(x) = (y)
#define _ULL unsigned long long
static int
five_cpu_numbers(_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;
__u16 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;
return 0;
}
/*
* get the cpu usage
*/
int
ncm_cab_read_cpu(float *cpu_usage)
{
_ULL user_j, nice_j, sys_j, idle_j, iowait_j = 0;
if(five_cpu_numbers(&user_j, &nice_j, &sys_j, &idle_j, &iowait_j)!=0)
return -1;
usleep(500000);
if(five_cpu_numbers(&user_j, &nice_j, &sys_j, &idle_j, &iowait_j)!=0)
return -1;
usleep(500000);
if(five_cpu_numbers(&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);
ncm_debug_msg("### CPU Usage : %0.3f %%n", *cpu_usage);
return 0;
}
#undef _ULL
/*
* get the mm usage
*/
int
getOnceMemUsage(float *memusage)
{
FILE *fp;
char buffer[1024];
__u16 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;
ncm_debug_msg("### MemUsage(total:%d free:%d %MEM:%3.2f%)n", totalmem, freemem, *memusage);
return 0;
}
#define SET_IF_DESIRED(x,y) if(x) *(x) = (y)
#define _ULL unsigned long long
static int
five_cpu_numbers(_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;
__u16 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;
return 0;
}
/*
* get the cpu usage
*/
int
ncm_cab_read_cpu(float *cpu_usage)
{
_ULL user_j, nice_j, sys_j, idle_j, iowait_j = 0;
if(five_cpu_numbers(&user_j, &nice_j, &sys_j, &idle_j, &iowait_j)!=0)
return -1;
usleep(500000);
if(five_cpu_numbers(&user_j, &nice_j, &sys_j, &idle_j, &iowait_j)!=0)
return -1;
usleep(500000);
if(five_cpu_numbers(&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);
ncm_debug_msg("### CPU Usage : %0.3f %%n", *cpu_usage);
return 0;
}
#undef _ULL
/*
* get the mm usage
*/
int
getOnceMemUsage(float *memusage)
{
FILE *fp;
char buffer[1024];
__u16 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;
ncm_debug_msg("### MemUsage(total:%d free:%d %MEM:%3.2f%)n", totalmem, freemem, *memusage);
return 0;
}
|
我写过类似软件, 这些代码需要移植在不同的系统.
linux hp-ux AIX上都不一样.
如果只是在linux 上楼上已经可以实现.
当然如果不考虑执行速度的话可以用shell + awk
linux hp-ux AIX上都不一样.
如果只是在linux 上楼上已经可以实现.
当然如果不考虑执行速度的话可以用shell + awk