定时获取当前linux系统下的CPU、内存、disk使用效率

在linux系统中,如果想要跑一些影响性能的application,通常需要时时关注的CPU、内存、disk的使用情况,因为通常我们在运行application时总是想着充分利用CPU、内存、disk资源的同时不影响系统处理其他application。所以可以通过下面的代码实现定时监控各个资源的使用情况。

  • get_cpu_info():函数主要是用来获取CPU的一些参数。可以通过old_info - new_info获取cpu使用情况。
  • get_mem_info():主要是获取内存使用情况。
  • get_disk_info():主要是获取磁盘使用情况。
//
// Created by wr on 2020/11/24.
//
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <string.h>
typedef struct cpu_info {
    char name[20];
    unsigned int user;
    unsigned int nice;
    unsigned int system;
    unsigned int idle;
    unsigned int iowait;
    unsigned int irq;
    unsigned int softirq;
}cpu_info_ms;

typedef struct mem_info {
    char name[20]; //memory_total
    unsigned long total;
    char unit[20]; //单位:KB
}mem_info_ms;

void get_cpu_info(cpu_info_ms* info) {
    FILE* file = NULL;
    file = fopen("/proc/stat", "r");
    if(file == NULL) {
        printf("get cpu info failed\n");
        return;
    }
    char buffer[256] = {0};
    fgets(buffer, sizeof(buffer), file);
    sscanf(buffer, "%s %u %u %u %u %u %u %u", info->name, &info->user, &info->nice, &info->system,
            &info->idle, &info->iowait, &info->irq, &info->softirq);
    fclose(file);
}

double calc_cpu_rate(cpu_info_ms* old_info, cpu_info_ms* new_info) {
    double od, nd;
    double usr_dif, syd_dif, nice_dif;
    double user_cpu_rate;
    double kernel_cpu_rate;
    od = (double)(old_info->user + old_info->nice + old_info->system + old_info->idle
            + old_info->iowait + old_info->irq + old_info->softirq);
    nd = (double)(new_info->user + new_info->nice + new_info->system + new_info->idle
            + old_info->iowait + new_info->irq + new_info->softirq);
    if (nd - od > 0) {
        user_cpu_rate = (new_info->user - old_info->user) / (nd - od) * 100;
        kernel_cpu_rate = (new_info->system - old_info->system) / (nd - od) * 100;
        return user_cpu_rate + kernel_cpu_rate;
    }
    return 0;
}

void get_memory_info() {
    FILE* file = NULL;
    char buffer[256] = {0};
    file = fopen("/proc/meminfo", "r");
    if(file == NULL) {
        printf("get memory info failed\n");
    }
    mem_info_ms info;
    fgets(buffer, sizeof(buffer), file);
    sscanf(buffer, "%s %lu %s", info.name, &info.total, info.unit);
    double mem_total, mem_used_rate;
    mem_total = info.total;
    fgets(buffer, sizeof(buffer), file);
    sscanf(buffer, "%s %lu %s", info.name, &info.total, info.unit);
    mem_used_rate = (1.0 - info.total / mem_total) * 100;
    mem_total = mem_total / (1024 * 1024);
    printf("memory: %.0lfG, used rate: %.2lf\n", mem_total, mem_used_rate);
}
void get_disk_info() {
    //Filesystem / Size / Used / Avail / Use% / Mounted on
    FILE* file = NULL;
    int h = 0;
    char buffer[256], filesys[80], avail[80], use[80], mount[80];
    double size, used;
    double total = 0.0;
    double useTotal = 0.0;
    file = popen("df", "r");
    if(file == NULL) {
        printf("get disk info failed\n");
        return;
    }
    fgets(buffer, sizeof(buffer), file);
    while(fscanf(file, "%s %lf %lf %s %s %s", filesys, &size, &used, avail, use, mount) != EOF) {
        total += size;
        useTotal += used;
    }
    pclose(file);
    printf("disk info\ttotal: %.4lfG, used rate: %.2lf%%\n", total, useTotal);
}
int main() {
    /*test success, calc_cpu_rate and get_cpu_info and get_mem_info and get_disk_info*/
    cpu_info_ms info1;
    cpu_info_ms info2;
    get_cpu_info(&info1);
    mem_info_ms mem_info;
    while(1) {
        sleep(10);
        get_cpu_info(&info2);
        printf("cpu use rate: %.2lf\n", calc_cpu_rate(&info1, &info2));
        info1 = info2;
        get_memory_info();
        get_disk_info();
    }
}

已亲测,可以运行。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值