在linux系统中,若是想要跑一些影响性能的application,一般须要时时关注的CPU、内存、disk的使用状况,由于一般咱们在运行application时老是想着充分利用CPU、内存、disk资源的同时不影响系统处理其余application。因此能够经过下面的代码实现定时监控各个资源的使用状况。linux
- 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(); } }
已亲测,能够运行。app