`
shaojiashuai123456
  • 浏览: 262190 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
社区版块
存档分类
最新评论

cpu使用率和内存使用率

 
阅读更多
#include <stdio.h> 
#include <stdlib.h> 
#include <ctype.h> 
#include <string.h> 
#include <fcntl.h> 
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
static char buffer[1024];              
static char cpu_name[10];             
typedef struct 
{
	long cpu_user;				
	long cpu_nice;			
	long cpu_sys;			
	long cpu_idle;			
    long cpu_iowait;
	long cpu_irq;			
	long cpu_softirq; 	
	long cpu_all;				
}CPU_STAT_S;
static CPU_STAT_S old;

char *get_cpu_name(char *data, char *name)
{
   while(!(*data == ' ' ||*data == '\0'))
   {
      *name++ = *data++;
   }
   *name++ = '\0';
   return data;
}
//读取/proc/stat文件内容
CPU_STAT_S get_cpu_data(void)
{
    int fp = 0;
    int len = 0;
    char *p;
    CPU_STAT_S cpu_stat;
  
    fp = open("/proc/stat", O_RDONLY);
    len = read(fp, buffer,sizeof(buffer)-1);
    close(fp);
    buffer[len] = '\0';
    memset(&cpu_stat, 0, sizeof(CPU_STAT_S));
  
    p = get_cpu_name(buffer, cpu_name);

    cpu_stat.cpu_user = strtoul(p, &p, 0);
    cpu_stat.cpu_nice = strtoul(p, &p, 0);
    cpu_stat.cpu_sys = strtoul(p, &p, 0);
    cpu_stat.cpu_idle = strtoul(p, &p, 0);
    cpu_stat.cpu_iowait = strtoul(p, &p, 0);
    cpu_stat.cpu_irq = strtoul(p, &p, 0);
    cpu_stat.cpu_softirq = strtoul(p, &p, 0);
    cpu_stat.cpu_all = cpu_stat.cpu_user + cpu_stat.cpu_nice + cpu_stat.cpu_sys \
     				    + cpu_stat.cpu_idle + cpu_stat.cpu_iowait + cpu_stat.cpu_irq \
                        + cpu_stat.cpu_softirq;	
    
    return cpu_stat;
}
//计算CPU使用率
float get_cpu_stat(void)
{
        float usage = 0.0;
        float child_usage = 0.0;
        float father_usage = 0.0;
        CPU_STAT_S cur;
        memset(&cur, 0, sizeof(CPU_STAT_S));
        cur= get_cpu_data();
        child_usage=(cur.cpu_idle - old.cpu_idle);
        father_usage=(cur.cpu_all-old.cpu_all);
        usage = 100-child_usage/father_usage*100;
        old=cur;
        return usage;
}

///////////////////////////////////////////////////////////////////////
typedef struct meminfo meminfo;
struct meminfo
{
    long total;
    long free;
    long buffers;
    long cached;
};
//先读取/proc/meminfo文件,然后计算内存使用率
float get_mem_stat()
{
    FILE *fp = 0;
    char buf[100];
    char str[100];
    float free;
    float total;
    float usage = 0.0;
    meminfo mem;
    double lv;
    if ((fp = fopen ("/proc/meminfo", "r")) == NULL)
    {
         printf("File open error!\n");
         exit (1);
    }
    long data;
    fgets(buf,100,fp);
    sscanf(buf,"%*s%ld",&mem.total);
    fgets(buf,100,fp);
    sscanf(buf,"%*s%ld",&mem.free);
    fgets(buf,100,fp);
    sscanf(buf,"%*s%ld",&mem.buffers);
    fgets(buf,100,fp);
    sscanf(buf,"%*s%ld",&mem.cached);
    fclose(fp);
    //如果Cached值大于MemTotal值则空闲内存为MemFree值,否则空闲内存为MemFree值+Buffers值+Cached值
    total=mem.total;
    if(mem.cached>mem.total)
    {
           free=mem.free;
    }
    else
    {
           free=mem.free+mem.buffers+mem.cached;
    }
    usage=(free)/total*100;
    return usage;
}

///////////////////////////////////////////////////////////////////////
//1S中打印一次
int main(void)
{

    
    printf("start while! \n"); 
    old= get_cpu_data();
    while(1)
    {
        usleep(1000*1000);
        printf("cpu:%.2f ,  mem:%.2f\n",get_cpu_stat(),get_mem_stat());
    } 

    return 0;
}
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics