当前位置: 技术问答>linux和unix
linux下如何获得系统信息,比如cpu占用率
来源: 互联网 发布时间:2015-11-17
本文导语: RT 谢谢 | #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; ...
RT
谢谢
谢谢
|
#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);
fCpuUsage = cpu_usage;
printf("### CPU Usage : %0.3f %%n", cpu_usage);
return 0;
}
#undef _ULL
#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);
fCpuUsage = cpu_usage;
printf("### CPU Usage : %0.3f %%n", cpu_usage);
return 0;
}
#undef _ULL
|
typedef struct ProcInfo
{
char name[MAX_PROC_NAME];/*process name*/
int PID; /*process id*/
int PPID; /*parent process id*/
int status; /* sleeping or running*/
struct ProcInfo * Next; /*pointer to next process struct*/
}* ProcList;
typedef struct netCards
{
char name[MAX_NET_CARD_NAME];/*net card name*/
int ByteRcv; /*bytes received*/
int ByteSnd; /*bytes sent*/
}* netCardList;
struct eCop_SM_Object
{
char OsName[MAX_OS_NAME];/*do not alarm*/
int CPU_Percent[MAX_CPUS];/*for example 34 means 0.34%*/
int DiskAvail; /*available disk*/
int MemUsed; /*memory used*/
/*including sending bytes and receiving bytes*/
struct netCards netCard[MAX_NET_CARDS];
struct ProcInfo * PrList;
/*
evaluate the time to it when sampling, if
it is -1,we consider it an invalid object
*/
long TimeStamp;
};
/*get os information and store it in a struct*/
int sample(struct eCop_SM_Object * obj);
{
char name[MAX_PROC_NAME];/*process name*/
int PID; /*process id*/
int PPID; /*parent process id*/
int status; /* sleeping or running*/
struct ProcInfo * Next; /*pointer to next process struct*/
}* ProcList;
typedef struct netCards
{
char name[MAX_NET_CARD_NAME];/*net card name*/
int ByteRcv; /*bytes received*/
int ByteSnd; /*bytes sent*/
}* netCardList;
struct eCop_SM_Object
{
char OsName[MAX_OS_NAME];/*do not alarm*/
int CPU_Percent[MAX_CPUS];/*for example 34 means 0.34%*/
int DiskAvail; /*available disk*/
int MemUsed; /*memory used*/
/*including sending bytes and receiving bytes*/
struct netCards netCard[MAX_NET_CARDS];
struct ProcInfo * PrList;
/*
evaluate the time to it when sampling, if
it is -1,we consider it an invalid object
*/
long TimeStamp;
};
/*get os information and store it in a struct*/
int sample(struct eCop_SM_Object * obj);
|
在本论坛搜CPU。
|
top源代码里面machine目录下的m_linux.c里面有一个get_system_info的方法,可以获得内存,CPU,经过适当的的加工后,可以获得网络流量和磁盘空间,另外top还可一个获得进程列表