当前位置: 技术问答>linux和unix
怎么获取linux的cpu和内存占有率的数据??
来源: 互联网 发布时间:2017-02-24
本文导语: 我想自己写个命令,这个命令显示cpu和内存的使用情况,可是cpu和内存占用率的数据信息怎么获得?? | static int sysGetCpuTime (SYS_CPU_INFO_S *cpust) { FILE *fd; char buff[2...
我想自己写个命令,这个命令显示cpu和内存的使用情况,可是cpu和内存占用率的数据信息怎么获得??
|
static int sysGetCpuTime (SYS_CPU_INFO_S *cpust)
{
FILE *fd;
char buff[256];
SYS_CPU_INFO_S *cpu_occupy;
cpu_occupy=cpust;
fd = fopen("/proc/stat", "r");
if(!fd)
return ERROR;
fgets (buff, sizeof(buff), fd);
sscanf (buff, "%s %u %u %u %u",
cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice,
&cpu_occupy->system, &cpu_occupy->idle);
fclose(fd);
return OK;
}
static int sysCalcCpuUsage (SYS_CPU_INFO_S *o, SYS_CPU_INFO_S *n)
{
_UINT32 oTotal, nTotal;
_UINT32 user, system;
int cpu_use = 0;
oTotal = o->user + o->nice + o->system +o->idle;
nTotal = n->user + n->nice + n->system +n->idle;
user = n->user - o->user;
system = n->system - o->system;
if((nTotal-oTotal) != 0)
cpu_use = (int)((user+system)*10000)/(nTotal-oTotal);
else
cpu_use = 0;
DBG_INFO(("sysCalcCpuUsage: cpu usage=%dn", cpu_use));
return cpu_use;
}
/*return usage, eg: 5230 (52.3%)*/
int sysCpuInfoGet(void)
{
int ret = ERROR;
SYS_CPU_INFO_S cpust1, cpust2;
ret = sysGetCpuTime(&cpust1);
if(ret != OK)
return ERROR;
sleep(2);
sysGetCpuTime(&cpust2);
if(ret != OK)
return ERROR;
return sysCalcCpuUsage(&cpust1, &cpust2);
}
{
FILE *fd;
char buff[256];
SYS_CPU_INFO_S *cpu_occupy;
cpu_occupy=cpust;
fd = fopen("/proc/stat", "r");
if(!fd)
return ERROR;
fgets (buff, sizeof(buff), fd);
sscanf (buff, "%s %u %u %u %u",
cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice,
&cpu_occupy->system, &cpu_occupy->idle);
fclose(fd);
return OK;
}
static int sysCalcCpuUsage (SYS_CPU_INFO_S *o, SYS_CPU_INFO_S *n)
{
_UINT32 oTotal, nTotal;
_UINT32 user, system;
int cpu_use = 0;
oTotal = o->user + o->nice + o->system +o->idle;
nTotal = n->user + n->nice + n->system +n->idle;
user = n->user - o->user;
system = n->system - o->system;
if((nTotal-oTotal) != 0)
cpu_use = (int)((user+system)*10000)/(nTotal-oTotal);
else
cpu_use = 0;
DBG_INFO(("sysCalcCpuUsage: cpu usage=%dn", cpu_use));
return cpu_use;
}
/*return usage, eg: 5230 (52.3%)*/
int sysCpuInfoGet(void)
{
int ret = ERROR;
SYS_CPU_INFO_S cpust1, cpust2;
ret = sysGetCpuTime(&cpust1);
if(ret != OK)
return ERROR;
sleep(2);
sysGetCpuTime(&cpust2);
if(ret != OK)
return ERROR;
return sysCalcCpuUsage(&cpust1, &cpust2);
}
|
直接top即可
|
还是 top
这个命令吧,不知道就是不知道,知道的就永远知道了 ...
这个命令吧,不知道就是不知道,知道的就永远知道了 ...
|
读取这些文件
/proc/stat
/proc/mem ???
/proc/stat
/proc/mem ???
|
top -n 1 | sed -n '3,4p'
|
top -bn 1 查看当前秒的
百度下top吧
百度下top吧
|
/************************************************************************
函数名称:LinuxSdk_Info_GetMemory
函数功能:获取磁盘信息
参数一:pSt_Memory_Infomation
In/Out:Out
类型:结构体指针
可空:N
意思:获取到的内存信息
返回值
类型:逻辑型
意思:是否获取成功
备注:
************************************************************************/
extern "C" BOOL LinuxSdk_Info_GetMemory(LINUXSDK_MEMORY_INFOMATION *pSt_Memory_Infomation);
/************************************************************************
函数名称:LinuxSdk_Info_GetCpuUsage
函数功能:获取CPU利用率
参数一:pSt_LinuxSdk_CpuInfo
In/Out:In
类型:指向结构体的指针
可空:N
意思:CPU信息结构体
返回值
类型:逻辑型
意思:是否成功获取得到
备注:注意,此结构提在内部是一个结构体数组,基于现在多核心的流行,每个结构体保存了一个CPU信息,你需要调用
LinuxSdk_Info_GetCpuCount() 函数来取得CPU个数,并且开始循环获取
************************************************************************/
extern "C" BOOL LinuxSdk_Info_GetCpuUsage(LINUXSDK_CPU_INFOMATION *pSt_LinuxSdk_CpuInfo);
函数名称:LinuxSdk_Info_GetMemory
函数功能:获取磁盘信息
参数一:pSt_Memory_Infomation
In/Out:Out
类型:结构体指针
可空:N
意思:获取到的内存信息
返回值
类型:逻辑型
意思:是否获取成功
备注:
************************************************************************/
extern "C" BOOL LinuxSdk_Info_GetMemory(LINUXSDK_MEMORY_INFOMATION *pSt_Memory_Infomation);
/************************************************************************
函数名称:LinuxSdk_Info_GetCpuUsage
函数功能:获取CPU利用率
参数一:pSt_LinuxSdk_CpuInfo
In/Out:In
类型:指向结构体的指针
可空:N
意思:CPU信息结构体
返回值
类型:逻辑型
意思:是否成功获取得到
备注:注意,此结构提在内部是一个结构体数组,基于现在多核心的流行,每个结构体保存了一个CPU信息,你需要调用
LinuxSdk_Info_GetCpuCount() 函数来取得CPU个数,并且开始循环获取
************************************************************************/
extern "C" BOOL LinuxSdk_Info_GetCpuUsage(LINUXSDK_CPU_INFOMATION *pSt_LinuxSdk_CpuInfo);