Linux c++ 獲取內存和cpu

直接上代碼,直接存main.cpp ,編譯 g++ main.cpp,./a.out 執行

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/vfs.h>

#include <sys/param.h>		/* for HZ */
#include <iostream>
using namespace std;

//通過獲取/proc/stat (CPU)和/proc/meminfo(內存和硬盤)文件信息
//proc/meminfo文件結構  
//MemTotal:       515164 kB  
//MemFree:         15024 kB  
//Buffers:         13740 kB  
//Cached:         248824 kB  
//SwapCached:    960 kB  
//Active:         309980 kB  
//Inactive:       63420 kB  
typedef struct MEMPACKED         //定義一個mem occupy的結構體  
{  
    char name1[20];      //定義一個char類型的數組名name有20個元素
    unsigned long MemTotal;
    char name2[20];
    unsigned long MemFree;
    char name3[20];
    unsigned long Buffers;
    char name4[20];
    unsigned long Cached;
    char name5[20];
    unsigned long SwapCached;
}MEM_OCCUPY;  
  
//proc/stat文件結構  
//cpu  633666 46912 249878 176813696 782884 2859 19625 0  
//cpu0 633666 46912 249878 176813696 782884 2859 19625 0  
//intr 5812844  
//ctxt 265816063  
//btime 1455203832  
//processes 596625  
//procs_running 1  
//procs_blocked 0  
  
typedef struct CPUPACKED         //定義一個cpu occupy的結構體  
{  
    char name[20];      //定義一個char類型的數組名name有20個元素
    unsigned int user; //定義一個無符號的int類型的user
    unsigned int nice; //定義一個無符號的int類型的nice
    unsigned int system;//定義一個無符號的int類型的system
    unsigned int idle; //定義一個無符號的int類型的idle
    unsigned int lowait;
    unsigned int irq;
    unsigned int softirq;
}CPU_OCCUPY;  
  
  
void get_memoccupy(MEM_OCCUPY *mem) //對無類型get函數含有一個形參結構體類弄的指針O  
{  
    FILE *fd;
    char buff[256];
    MEM_OCCUPY *m;
    m = mem;

    fd = fopen("/proc/meminfo", "r");
    //MemTotal: 515164 kB
    //MemFree: 7348 kB
    //Buffers: 7892 kB
    //Cached: 241852  kB
    //SwapCached: 0 kB
    //從fd文件中讀取長度爲buff的字符串再存到起始地址爲buff這個空間裏
    fgets(buff, sizeof(buff), fd);
    sscanf(buff, "%s %lu ", m->name1, &m->MemTotal);
    fgets(buff, sizeof(buff), fd);
    sscanf(buff, "%s %lu ", m->name2, &m->MemFree);
    fgets(buff, sizeof(buff), fd);
    sscanf(buff, "%s %lu ", m->name3, &m->Buffers);
    fgets(buff, sizeof(buff), fd);
    sscanf(buff, "%s %lu ", m->name4, &m->Cached);
    fgets(buff, sizeof(buff), fd);
    sscanf(buff, "%s %lu", m->name5, &m->SwapCached);
  
    fclose(fd);     //關閉文件fd
}  
  
  
int get_cpuoccupy(CPU_OCCUPY *cpust) //對無類型get函數含有一個形參結構體類弄的指針O  
{  
    FILE *fd;
    char buff[256];
    CPU_OCCUPY *cpu_occupy;
    cpu_occupy = cpust;

    fd = fopen("/proc/stat", "r");
    fgets(buff, sizeof(buff), fd);

    sscanf(buff, "%s %u %u %u %u %u %u %u", cpu_occupy->name, &cpu_occupy->user, &cpu_occupy->nice, &cpu_occupy->system, &cpu_occupy->idle, &cpu_occupy->lowait, &cpu_occupy->irq, &cpu_occupy->softirq);


    fclose(fd);

    return 0;
}  
  
  
int cal_cpuoccupy(CPU_OCCUPY *o, CPU_OCCUPY *n)  
{  
    unsigned long od, nd;
    double cpu_use = 0;

    od = (unsigned long)(o->user + o->nice + o->system + o->idle + o->lowait + o->irq + o->softirq);//第一次(用戶+優先級+系統+空閒)的時間再賦給od
    nd = (unsigned long)(n->user + n->nice + n->system + n->idle + n->lowait + n->irq + n->softirq);//第二次(用戶+優先級+系統+空閒)的時間再賦給od
    double sum = nd - od;
    double idle = n->idle - o->idle;
    cpu_use = idle / sum;
    printf("cpu_use1(idle) = %f\r\n", cpu_use);
    idle = n->user + n->system + n->nice - o->user - o->system - o->nice;
    cpu_use = idle / sum;
    printf("cpu_use2(user+system+nice) = %f\r\n", cpu_use);
    return 0;
}  
  
int main( int argc, char **argv )  
{  
    MEM_OCCUPY mem_stat;
    CPU_OCCUPY cpu_stat1;
    CPU_OCCUPY cpu_stat2;

    //CAfcLowerTools m_afcLowerTools;


    get_memoccupy((MEM_OCCUPY *)&mem_stat);
    printf(" [MemTotal] = %lu \n [MemFree] = %lu \n [Buffers] = %lu \n [Cached] = %lu \n [SwapCached] = %lu \n", mem_stat.MemTotal, mem_stat.MemFree, mem_stat.Buffers, mem_stat.Cached, mem_stat.SwapCached);


    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat1);
    sleep(1);

    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat2);

    cal_cpuoccupy((CPU_OCCUPY *)&cpu_stat1, (CPU_OCCUPY *)&cpu_stat2);

    return 0;
} 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章