#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define ONE_MB (1024 * 1024)
int cpu_num;
int is_total = 0;
struct cpuinfo
{
char name[20];
unsigned int user;
unsigned int nice;
unsigned int system;
unsigned int idle;
unsigned int iowait;
unsigned int irq;
unsigned int softirq;
unsigned int stealstolen;
unsigned int guest;
};
void getCpuInfo (struct cpuinfo *cpu);
void calCpuUseRatio (struct cpuinfo *old, struct cpuinfo *new);
int getCpuTotalUseRatio();
int getCpuDetailUseRatio();
int getMemInfo(void);
int main(void)
{
getCpuTotalUseRatio();
//getCpuDetailUseRatio();
//getMemInfo();
}
/**
* 获取总的CPU利用率
*/
int getCpuTotalUseRatio()
{
struct cpuinfo oldinfo[1];
struct cpuinfo newinfo[1];
cpu_num = 1;
is_total = 1;
getCpuInfo(oldinfo);
sleep(1);
getCpuInfo(newinfo);
calCpuUseRatio(&oldinfo[0], &newinfo[0]);
}
/**
* 获取每个CPU利用率
*/
int getCpuDetailUseRatio()
{
struct cpuinfo oldinfo[100];
struct cpuinfo newinfo[100];
int i;
cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
getCpuInfo(oldinfo);
sleep(1);
getCpuInfo(newinfo);
for (i=0; i < cpu_num; i++)
{
calCpuUseRatio(&oldinfo[i], &newinfo[i]);
}
}
/**
* 计算CPU利用率
*/
void calCpuUseRatio (struct cpuinfo *old, struct cpuinfo *new)
{
double oldTotal, newTotal, total, idle, user, system, softirq, irq;
float totalRatio, userRatio, systemRatio, softirqRatio, irqRatio;
oldTotal = (double)(old->user + old->nice + old->system + old->idle + old->iowait +
old->irq + old->softirq + old->stealstolen + old->guest);
newTotal = (double)(new->user + new->nice + new->system + new->idle + new->iowait +
new->irq + new->softirq + new->stealstolen + new->guest);
total = newTotal - oldTotal;
idle = new->idle - old->idle;
user = new->user - old->user;
system = new->system - old->system;
softirq = new->softirq - old->softirq;
irq = new->irq - old->irq;
//CPU利用率
totalRatio = (total - idle) / total;
//用户空间CPU利用率
userRatio = user / total;
//内核空间CPU利用率
systemRatio = system / total;
//软中断CPU利用率
softirqRatio = softirq / total;
//硬中断CPU利用率
irqRatio = irq / total;
//printf("total:%f, idle:%f, new->idle:%u, old->idle:%u\n", total, idle, new->idle, old->idle);
printf("总的CPU利用率为:%f(%f + %f + %f + %f)\n", totalRatio, userRatio, systemRatio, softirqRatio, irqRatio);
}
/**
* 获取当前CPU信息
*/
void getCpuInfo (struct cpuinfo *cpu)
{
FILE *fd;
char buff[1024];
fd = fopen ("/proc/stat", "r");
if(is_total == 0)
{
fgets (buff, sizeof(buff), fd);
}
int n;
for(n = 0; n < cpu_num; n++)
{
fgets (buff, sizeof(buff), fd);
sscanf (buff, "%s %u %u %u %u %u %u %u %u %u", &cpu[n].name, &cpu[n].user, &cpu[n].nice, &cpu[n].system,
&cpu[n].idle,&cpu[n].iowait, &cpu[n].irq, &cpu[n].softirq, &cpu[n].stealstolen, &cpu[n].guest);
printf ("%s %u %u %u %u %u %u %u %u %u\n", cpu[n].name, cpu[n].user, cpu[n].nice, cpu[n].system,
cpu[n].idle, cpu[n].iowait, cpu[n].irq, cpu[n].softirq, cpu[n].stealstolen, cpu[n].guest);
}
fclose(fd);
}
/**
* 获取内存使用情况
*/
int getMemInfo(void)
{
long num_procs;
long page_size;
long num_pages;
long free_pages;
long long mem;
long long free_mem;
float mem_per;
num_procs = sysconf (_SC_NPROCESSORS_CONF);
printf ("CPU 个数为: %ld 个\n", num_procs);
page_size = sysconf (_SC_PAGESIZE);
printf ("系统页面的大小为: %ld K\n", page_size / 1024 );
num_pages = sysconf (_SC_PHYS_PAGES);
printf ("系统中物理页数个数: %ld 个\n", num_pages);
free_pages = sysconf (_SC_AVPHYS_PAGES);
printf ("系统中可用的页面个数为: %ld 个\n", free_pages);
mem = (long long) ((long long)num_pages * (long long)page_size);
mem /= ONE_MB;
free_mem = (long long)free_pages * (long long)page_size;
free_mem /= ONE_MB;
printf ("总共有 %lld MB 的物理内存, 空闲的物理内存有: %lld MB\n", mem, free_mem);
mem_per = (mem-free_mem)*100.0/mem;
printf ("内存占用率*100: %f\n", mem_per);
return (0);
}
Linux 动态获取CPU利用率
最新推荐文章于 2021-12-24 17:42:30 发布