如何获取Linux系统资源
来源:互联网 发布:姐妹团推广 网络诈骗 编辑:程序博客网 时间:2024/06/05 11:08
在Linux上的选择,起初看起来很明显是使用像getrusage的POSIX的API()等我花了一些时间,试图让这项工作,但一直没有得到有意义的值。当我最后检查了自己的内核源代码,我发现这些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;
}
- 如何获取Linux系统资源
- 如何获取windows系统资源
- LoadRunner如何监控Linux系统资源
- LoadRunner如何监控Linux系统资源
- LoadRunner如何监控Linux系统资源
- LoadRunner如何监控Linux系统资源
- 如何获取Mac OS X系统资源
- linux: 获取系统资源(getconf(1), sysconf(3))
- Linux系统资源获取并写入数据库
- LoadRunner如何监控Linux下的系统资源
- 【LoadRunner技术讲座3】如何监控Linux系统资源
- LoadRunner如何监控Linux下的系统资源
- LoadRunner如何监控Linux下的系统资源
- LoadRunner如何监控Linux下的系统资源
- linux各发行版本的系统资源获取方式调研
- 如何使用LR监视LINUX服务器的系统资源
- android 获取系统资源
- 获取系统资源的方法
- 第二周 项目五:数组作数据成员(1)
- 获取用户的五个核心步骤
- 黑马程序员——Objective-C学习之从手动内存管理到ARC机制
- spring 注解方式 集成struts2(不需要写Struts2的常量)
- 第三周——基于对象的程序设计——三角形类(3)多文件组织
- 如何获取Linux系统资源
- 给需求做一个DNA检测
- DES加密算法
- 时间选择器(滚动选择器)的优化和修改
- 如何获取windows系统资源
- sass初识笔记
- QAxWidget调用JS(使用IE内核)
- Incoming conferences
- YTUOJ-重复字符串