在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