Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1959142
  • 博文数量: 261
  • 博客积分: 8073
  • 博客等级: 中将
  • 技术积分: 2363
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-10 15:23
文章分类

全部博文(261)

文章存档

2013年(1)

2012年(1)

2011年(50)

2010年(34)

2009年(4)

2008年(17)

2007年(55)

2006年(99)

分类:

2010-09-14 14:12:45

Linux上的选择,起初看起来很明显是使用像getrusagePOSIXAPI()等我花了一些时间,试图让这项工作,但一直没有得到有意义的值。当我最后检查了自己的内核源代码,我发现这些API显然还没有完全作为Linux 2.6内核的实施!?

最后我得到通过阅读伪组合中的所有值,文件系统/ proc和内核调用。

 

  • 总的虚拟内存:

 


    #include "sys/types.h"
    #include "sys/sysinfo.h"
   
    struct sysinfo memInfo;
   
    sysinfo (&memInfo);
    long long totalVirtualMem = memInfo.totalram;
    //Add other values in next statement to avoid int overflow on right hand side...
    totalVirtualMem += memInfo.totalswap;
    totalVirtualMem *= memInfo.mem_unit;

 

 

  • 目前使用的虚拟内存:

相同的代码,如“总的虚拟内存”,然后


    long long virtualMemUsed = memInfo.totalram - memInfo.freeram;
    //Add other values in next statement to avoid int overflow on right hand side...
    virtualMemUsed += memInfo.totalswap - memInfo.freeswap;
    virtualMemUsed *= memInfo.mem_unit;

 

 

  • 目前使用的虚拟内存的当前进程:

 


    #include "stdlib.h"
    #include "stdio.h"
    #include "string.h"
   
    int parseLine(char* line){
    int i = strlen(line);
    while (*line < '0' || *line > '9') line++;
    line[i-3] = '\0';
    i = atoi(line);
    return i;
    }
   
    int getValue(){ //Note: this value is in KB!
    FILE* file = fopen("/proc/self/status", "r");
    int result = -1;
    char line[128];
   
    while (fgets(line, 128, file) != NULL){
    if (strncmp(line, "VmSize:", 7) == 0) result = parseLine(line);
    break;
    }
    fclose(file);
    return result;
    }

 

 

  • 总物理内存(RAM):

相同的代码,如“总的虚拟内存”,然后


    long long totalPhysMem = memInfo.totalram;
    //Multiply in next statement to avoid int overflow on right hand side...
    totalPhysMem *= memInfo.mem_unit;

 

 

  • 目前使用的物理内存:

相同的代码,如“总的虚拟内存”,然后


    long long physMemUsed = memInfo.totalram - memInfo.freeram;
    //Multiply in next statement to avoid int overflow on right hand side...
    physMemUsed *= memInfo.mem_unit;

 

 

  • 目前使用的物理内存的当前进程:

变更getValue()在“虚拟内存目前使用的当前进程”如下:


    int getValue(){ //Note: this value is in KB!
    FILE* file = fopen("/proc/self/status", "r");
    int result = -1;
    char line[128];
   
    while (fgets(line, 128, file) != NULL){
    if (strncmp(line, "VmRSS:", 6) == 0) result = parseLine(line);
    break;
    }
    fclose(file);
    return result;
    }

 

 

  • 目前使用的CPU

 


    #include "stdlib.h"
    #include "stdio.h"
    #include "string.h"
   
    static unsigned long long lastTotalUser, lastTotalUserLow, lastTotalSys, lastTotalIdle;
   
    void init(){
    FILE* file = fopen("/proc/stat", "r");
    fscanf(file, "cpu %Ld %Ld %Ld %Ld", &lastTotalUser, &lastTotalUserLow,
    &lastTotalSys, &lastTotalIdle);
    fclose(file);
    }
   
    double getCurrentValue(){
    double percent;
    FILE* file;
    unsigned long long totalUser, totalUserLow, totalSys, totalIdle, total;
   
    file = fopen("/proc/stat", "r");
    fscanf(file, "cpu %Ld %Ld %Ld %Ld", &totalUser, &totalUserLow,
    &totalSys, &totalIdle);
    fclose(file);
   
    if (totalUser < lastTotalUser || totalUserLow < lastTotalUserLow ||
    totalSys < lastTotalSys || totalIdle < lastTotalIdle){
    //Overflow detection. Just skip this value.
    percent = -1.0;
    }
    else{
    total = (totalUser - lastTotalUser) + (totalUserLow - lastTotalUserLow) +
    (totalSys - lastTotalSys);
    percent = total;
    total += (totalIdle - lastTotalIdle);
    percent /= total;
    percent *= 100;
    }
   
    lastTotalUser = totalUser;
    lastTotalUserLow = totalUserLow;
    lastTotalSys = totalSys;
    lastTotalIdle = totalIdle;
   
    return percent;
    }

 

 

  • 目前使用的CPU的当前进程:

 


    #include "stdlib.h"
    #include "stdio.h"
    #include "string.h"
    #include "sys/times.h"
    #include "sys/vtimes.h"
   
    static clock_t lastCPU, lastSysCPU, lastUserCPU;
    static int numProcessors;
   
    void init(){
    FILE* file;
    struct tms timeSample;
    char line[128];
   
    lastCPU = times(&timeSample);
    lastSysCPU = timeSample.tms_stime;
    lastUserCPU = timeSample.tms_utime;
   
    file = fopen("/proc/cpuinfo", "r");
    numProcessors = 0;
    while(fgets(line, 128, file) != NULL){
    if (strncmp(line, "processor", 9) == 0) numProcessors++;
    }
    fclose(file);
    }
   
    double getCurrentValue(){
    struct tms timeSample;
    clock_t now;
    double percent;
   
    now = times(&timeSample);
    if (now <= lastCPU || timeSample.tms_stime < lastSysCPU ||
    timeSample.tms_utime < lastUserCPU){
    //Overflow detection. Just skip this value.
    percent = -1.0;
    }
    else{
    percent = (timeSample.tms_stime - lastSysCPU) +
    (timeSample.tms_utime - lastUserCPU);
    percent /= (now - lastCPU);
    percent /= numProcessors;
    percent *= 100;
    }
    lastCPU = now;
    lastSysCPU = timeSample.tms_stime;
    lastUserCPU = timeSample.tms_utime;
   
    return percent;
    }

阅读(748) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~