取得系统资源信息
【内存使用率】
float CHWMSchedule::GetMemUsedRate()
{
unsigned long long ullFreePhyMem = 0;
unsigned long long ullUsedPhyMem = 0;
unsigned long long ullTotalPhyMem = 0;
perfstat_memory_total_t minfo;
if(perfstat_memory_total(NULL, &minfo, sizeof(perfstat_memory_total_t), 1) != 1)
{
printf("Get System Memory Fail");
return 0.0;
}
//minfo.real_free的单位是4kb
ullFreePhyMem = minfo.real_free;
ullTotalPhyMem = minfo.real_total;
ullUsedPhyMem = ullTotalPhyMem - ullFreePhyMem;
return (float)ullUsedPhyMem/ullTotalPhyMem;
}
{
unsigned long long ullFreePhyMem = 0;
unsigned long long ullUsedPhyMem = 0;
unsigned long long ullTotalPhyMem = 0;
perfstat_memory_total_t minfo;
if(perfstat_memory_total(NULL, &minfo, sizeof(perfstat_memory_total_t), 1) != 1)
{
printf("Get System Memory Fail");
return 0.0;
}
//minfo.real_free的单位是4kb
ullFreePhyMem = minfo.real_free;
ullTotalPhyMem = minfo.real_total;
ullUsedPhyMem = ullTotalPhyMem - ullFreePhyMem;
return (float)ullUsedPhyMem/ullTotalPhyMem;
}
【cpu使用率】
float CHWMSchedule::GetCpuUsedRate()
{
//perfstat_cpu_total_t结构定义了cpu信息,具体可以看头文件libperfstat.h
static perfstat_cpu_total_t t1,t2;
double total_cpu;
//为了准确增加查询次数
int nOffset = 10;
total_cpu = 0;
int nCpuSys = 0;
int nCpuWait = 0;
int nCpuUser = 0;
int nCpuIdle = 0;
if (t1.psys == 0)
{
//获取当前值的函数,具体定义可以参考头文件
if (perfstat_cpu_total(NULL,&t1,sizeof(perfstat_cpu_total_t),1) < 0)
{
return 0.0;
}
usleep(100000);
}
// 这里为什么要取10次,因为psys等变量记录的是CPU当前跳数,因此需要统计当前的值减去上次的值,
// 因此 static perfstat_cpu_total_t t1,t2定义成static
for(int i=0; i<nOffset; i++)
{
t2 = t1;
if (perfstat_cpu_total(NULL,&t1,sizeof(perfstat_cpu_total_t),1) < 0)
{
return 0.0;
}
total_cpu = (t1.psys+t1.puser+t1.pidle+t1.pwait-(t2.psys+t2.puser+t2.pidle+t2.pwait));
nCpuSys += (int)((t1.psys-t2.psys)*10000/total_cpu);
nCpuWait += (int)((t1.pwait-t2.pwait)*10000/total_cpu);
nCpuUser += (int)((t1.puser-t2.puser)*10000/total_cpu);
nCpuIdle += (int)((t1.pidle-t2.pidle)*10000/total_cpu);
usleep(1000);
}
nCpuSys = nCpuSys/nOffset;
nCpuWait = nCpuWait/nOffset;
nCpuUser = nCpuUser/nOffset;
nCpuIdle = nCpuIdle/nOffset;
// ZX_TRACK_0(LOG_TRACK_CODE, "系统占用:%.3f, 用户占用:%.3f, 等待:%.3f, 空闲:%.3f\n",
// nCpuSys/100.0, nCpuUser/100.0, nCpuWait/100.0, nCpuIdle/100.0);
return (nCpuSys/100.0 + nCpuUser/100.0 + nCpuWait/100.0);
}
//perfstat_cpu_total_t结构定义了cpu信息,具体可以看头文件libperfstat.h
static perfstat_cpu_total_t t1,t2;
double total_cpu;
//为了准确增加查询次数
int nOffset = 10;
total_cpu = 0;
int nCpuSys = 0;
int nCpuWait = 0;
int nCpuUser = 0;
int nCpuIdle = 0;
if (t1.psys == 0)
{
//获取当前值的函数,具体定义可以参考头文件
if (perfstat_cpu_total(NULL,&t1,sizeof(perfstat_cpu_total_t),1) < 0)
{
return 0.0;
}
usleep(100000);
}
// 这里为什么要取10次,因为psys等变量记录的是CPU当前跳数,因此需要统计当前的值减去上次的值,
// 因此 static perfstat_cpu_total_t t1,t2定义成static
for(int i=0; i<nOffset; i++)
{
t2 = t1;
if (perfstat_cpu_total(NULL,&t1,sizeof(perfstat_cpu_total_t),1) < 0)
{
return 0.0;
}
total_cpu = (t1.psys+t1.puser+t1.pidle+t1.pwait-(t2.psys+t2.puser+t2.pidle+t2.pwait));
nCpuSys += (int)((t1.psys-t2.psys)*10000/total_cpu);
nCpuWait += (int)((t1.pwait-t2.pwait)*10000/total_cpu);
nCpuUser += (int)((t1.puser-t2.puser)*10000/total_cpu);
nCpuIdle += (int)((t1.pidle-t2.pidle)*10000/total_cpu);
usleep(1000);
}
nCpuSys = nCpuSys/nOffset;
nCpuWait = nCpuWait/nOffset;
nCpuUser = nCpuUser/nOffset;
nCpuIdle = nCpuIdle/nOffset;
// ZX_TRACK_0(LOG_TRACK_CODE, "系统占用:%.3f, 用户占用:%.3f, 等待:%.3f, 空闲:%.3f\n",
// nCpuSys/100.0, nCpuUser/100.0, nCpuWait/100.0, nCpuIdle/100.0);
return (nCpuSys/100.0 + nCpuUser/100.0 + nCpuWait/100.0);
}