一、计算CPU利用率(计算方式与top指令源码方式相同)
1.计算宿主机的cpu核数
2. 从文件中读取本身进程的一段时间使用情况,即隔一段时间分别获取使用情况,通过这段时间的(使用时间)/(总时间),即可得到cpu利用率,代码如下:
#include "stdlib.h"
#include "stdio.h"
#include "unistd.h"
#include
#include
#include
typedef struct cpu_usage_info
{
unsigned long cpu_total_usage;
unsigned long system_useage;
}CPU_USAGE_INFO;
typedef struct cpu_sys_info
{
char szName[16];
unsigned long user; // 从系统启动开始累计到当前时刻,用户态的CPU时间(单位:jiffies) ,不包含 nice值为负进程。1jiffies=0.01秒
unsigned long nice; // 从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间(单位:jiffies)
unsigned long system; // 从系统启动开始累计到当前时刻,核心时间(单位:jiffies)
unsigned long idle; // 从系统启动开始累计到当前时刻,除硬盘IO等待时间以外其它等待时间(单位:jiffies)
unsigned long iowait; // 从系统启动开始累计到当前时刻,硬盘IO等待时间(单位:jiffies) ,
unsigned long irq; // 从系统启动开始累计到当前时刻,硬中断时间(单位:jiffies)
unsigned long softirq; // 从系统启动开始累计到当前时刻,软中断时间(单位:jiffies)
}CPU_SYS_INFO;
long GetCpuNum()
{
std::fstream fIn("/proc/stat", std::ios::in);
if(false == fIn.is_open())
{
return -1;
}
std::string strLine1;
std::string strLineCpu; // 含有CPU字段的文本
do
{
std::getline(fIn, strLine1);
auto nPos = strLine1.find("cpu");
// 本行文本不包含cpu字段则跳出
if(std::string::npos == nPos)
{
break;
}
else{
// 本行包含cpu字段则保存起来
strLineCpu = strLine1;
}
}
while(!fIn.eof());
if(true == strLineCpu.empty())
{
std::cout << "get cpu num failed." << std::endl;
return -1;
}
else{
auto nPos = strLineCpu.find(" ");
std::string strCpu = strLineCpu.substr(3, nPos-3);
//std::cout << "--------" << strCpu << std::endl;
long lCpuNum = atoi(strCpu.c_str()) + 1;
//std::cout << "CpuNum: " << lCpuNum << std::endl;
return lCpuNum;
}
}
long getCPUInfo(CPU_USAGE_INFO *pstCpuUsageInfo)
{
// 读取CPU使用时间,单位纳秒
char szCpuAcctUsagePath[256] = "/sys/fs/cgroup/cpuacct/cpuacct.usage";
char szBuff[256];
FILE* fd;
fd = fopen(szCpuAcctUsagePath,"r");
if(NULL == fd)
{
printf("open file cpuacct.usage failed.");
return -1;
}
fgets(szBuff, sizeof(szBuff),fd);
sscanf(szBuff,"%lu",&pstCpuUsageInfo->cpu_total_usage);
fclose(fd);
fd = NULL;
// 读取系统CPU使用时间,单位jiffies(0.01s)
CPU_SYS_INFO stCpuSysInfo;
char szCpuSystemUsagePath[256] = "/proc/stat";
fd = fopen(szCpuSystemUsagePath,"r");
if(NULL == fd)
{
printf("open file stat failed.");
return -1;
}
fgets(szBuff, sizeof(szBuff), fd);
sscanf(szBuff, "%s %lu %lu %lu %lu %lu %lu %lu",
stCpuSysInfo.szName, &stCpuSysInfo.user, &stCpuSysInfo.nice, &stCpuSysInfo.system, &stCpuSysInfo.idle,
&stCpuSysInfo.iowait, &stCpuSysInfo.irq, &stCpuSysInfo.softirq);
pstCpuUsageInfo->system_useage = stCpuSysInfo.user+stCpuSysInfo.nice+stCpuSysInfo.system+
stCpuSysInfo.idle+stCpuSysInfo.iowait+ stCpuSysInfo.irq+ stCpuSysInfo.softirq;
fclose(fd);
return 0;
}
long getCpuRate()
{
CPU_USAGE_INFO stPreCpuUsageInfo;
CPU_USAGE_INFO stCpuUsageInfo;
long lRet = -1;
lRet = getCPUInfo(&stPreCpuUsageInfo);
if(0 != lRet)
{
printf("get PreCpuUsage failed.\n");
return -1;
}
// 等待200ms
usleep(200000);
//sleep(1);
lRet = getCPUInfo(&stCpuUsageInfo);
if(0 != lRet)
{
printf("get CpuUsage failed.\n");
return -1;
}
/* printf("Usage:%lu, %lu;\n", stCpuUsageInfo.cpu_total_usage, stPreCpuUsageInfo.cpu_total_usage);
printf("Sys:%lu,%lu\n", stCpuUsageInfo.system_useage,stPreCpuUsageInfo.system_useage);
printf("cpu delta:%lu\n", (stCpuUsageInfo.cpu_total_usage- stPreCpuUsageInfo.cpu_total_usage));
printf("sys delta:%lu\n", stCpuUsageInfo.system_useage-stPreCpuUsageInfo.system_useage); */
//
float fCpuRate = (float)(stCpuUsageInfo.cpu_total_usage- stPreCpuUsageInfo.cpu_total_usage)/(float)((stCpuUsageInfo.system_useage-stPreCpuUsageInfo.system_useage)*100000);
long lCpuNum = GetCpuNum();
printf("Using Cpu rate:%f\n", fCpuRate*lCpuNum);
return (long)fCpuRate;
//return 0;
}
int main()
{
std::cout << "system Cpu Number: " <
如果想获取本身的使用情况,需要获取pid,进入对应的文件夹下读取信息,百度一下就知道;
附送C版本getNum
long GetCpuNum()
{
char szBuff[1024];
char szLine[1024];
char *szCpuNum = NULL;
FILE *fIn = fopen("/proc/stat", "r");
if(NULL == fIn)
{
printf("open /proc/stat failed, error getting cpu num\n");
return -1;
}
while(fgets(szBuff, sizeof(szBuff), fIn) != NULL)
{
printf("%s\n", szBuff);
char *s = strstr(szBuff,"cpu");
if(NULL == s)
{
break;
}
else
{
strncpy(szLine, szBuff, sizeof(szLine)-1);
}
}
if(strlen(szLine) == 0)
{
printf("get cpu line failed\n");
return -1;
}
else
{
printf("\n");
printf("szLine:%s", szLine);
szCpuNum = strtok(szLine, " ");
printf("cpuNum:%d\n", atoi(szCpuNum+3));
return (long)atoi(szCpuNum);
}
}
二、获取mem使用情况
未完待续