Linux 动态获取CPU利用率

本文介绍了一种实时监测计算机系统CPU使用率及内存使用情况的方法,通过读取/proc/stat文件并计算每秒CPU使用变化来估算CPU利用率,并获取内存总容量和空闲容量,最终展示系统的资源使用效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值