码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 获取Linux 服务器CPU占用率+内存空闲率(亲测绝对可以运行)

时间:2020-04-27 22:32:12      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:can   void   地址   size   spring   编译   get   details   string   

代码来自网络,部分修改,亲测绝对可用

C++:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <iostream>   
#include <unistd.h>

using namespace std;  

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;  
}  
  
  
void 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;  
    idle = n->user + n->system + n->nice - o->user - o->system - o->nice;  
    cpu_use = idle / sum;  
    printf("%.3f\n",cpu_use);  
}  
  
int main( int argc, char **argv )  
{  
    MEM_OCCUPY mem_stat;  
    CPU_OCCUPY cpu_stat1;  
    CPU_OCCUPY cpu_stat2;

    //获取内存
    //(MemTotal - MemFree)/ MemTotal
    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);  
    printf("%.3f\n", mem_stat.MemFree * 1.0 / ( mem_stat.MemTotal * 1.0  ) );
    //第一次获取cpu使用情况  
    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat1);  
    
    usleep(100000);
    
    //第二次获取cpu使用情况  
    get_cpuoccupy((CPU_OCCUPY *)&cpu_stat2);  
    //计算cpu使用率  
    cal_cpuoccupy((CPU_OCCUPY *)&cpu_stat1, (CPU_OCCUPY *)&cpu_stat2);  
    return 0;
}  

 

编译

g++ get.cpp -o get

运行

[root@xxxxxxx ]# ./get

0.117
1.000
[root@xxxxxxx ]# ./get
0.119
0.000

上方的数值代表 内存空闲 / 内存总数

下方的数值代表 CPU占用率

 

详细解释:

https://blog.csdn.net/nineday/article/details/1928847

 

 

计划搭建一个简易版的分布式集群...

一台主服务器,一台高配的数据库服务器,n 台计算服务器
(1)在每台计算服务器中,写一个定时任务,每隔 1 s 触发一个PHP程序,该PHP将触发上方的CPP程序,获得服务器状态,更新DATABASE中的服务器状态表
(2)主服务器将获得客户端的请求,插入DATABASE中的消息队列表
(3)主服务器中将写一个servlet 定时函数,每隔 0.5 s 轮询消息队列表,一旦发现有新的消息请求集合或个体,则将该消息集合转换成所封装对象的队列
(4)从前往后扫描队列,每次选定 DATABASE中空闲资源最大的 一台计算服务器,发送一个包含JSON格式对象的HTTP请求
(5)计算服务器接收到HTTP请求后,将展开计算,计算完成后,更新DATABASE中的数据字段
(6)客户端AJAX 0.5s 轮询DATABASE,直到完成计算服务器完成计算

【上方集群中,采用 jsp + servlet 实现 java 后台。。。。。。得抓紧学习spring大家族啊,被技术限制住了......虽然servlet本身是单实例多线程的

 

主服务器 servlet多线程 --- 任务队列 --- 分布式并行计算 --- 计算服务器 servlet多线程

不知道并发咋样。。。第一次碰分布式和并发这块...

如果有大佬可以指点下,会非常感谢的!!!!!!

 

C++ 获取Linux 服务器CPU占用率+内存空闲率(亲测绝对可以运行)

标签:can   void   地址   size   spring   编译   get   details   string   

原文地址:https://www.cnblogs.com/expedition/p/12790520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!