我们在Linux系统上常常会定时统计系统的Memory信息,从而排查一些其他的问题,在Linux 中,我们可以使用如下方式查看系统情况:
(1)free -m
(2)cat /proc/meminfo
在程序中我们可以定时统计这些信息,那么我们可以使用如下方式:
方法一:该方法比较简单,但是有缺陷,每个系统的字段顺序不一样,可能会存在一些错误
//proc/meminfo文件结构
//MemTotal: 515164 kB
//MemFree: 15024 kB
//Buffers: 13740 kB
//Cached: 248824 kB
typedef struct MEMPACKED
{
char name1[20];
unsigned long MemTotal;
char name2[20];
unsigned long MemFree;
char name3[20];
unsigned long Buffers;
char name4[20];
unsigned long Cached;
}MEM_OCCUPY;
int get_meminfo(MEM_OCCUPY * lpMemory)
{
FILE *fd;
char buff[128];
fd = fopen("/proc/meminfo", "r");
if(fd <0) return -1;
fgets(buff, sizeof(buff), fd);
sscanf(buff, "%s %lu ", lpMemory->name1, &lpMemory->MemTotal);
fgets(buff, sizeof(buff), fd);
sscanf(buff, "%s %lu ", lpMemory->name2, &lpMemory->MemFree);
fgets(buff, sizeof(buff), fd);
sscanf(buff, "%s %lu ", lpMemory->name3, &lpMemory->Buffers);
fgets(buff, sizeof(buff), fd);
sscanf(buff, "%s %lu ", lpMemory->name4, &lpMemory->Cached);
fclose(fd);
}
方法二:该方法很通用,完全是C方法,也很容易理解
typedef struct MEMPACKED
{
char name1[20];
unsigned long MemTotal;
char name2[20];
unsigned long MemFree;
char name3[20];
unsigned long Buffers;
char name4[20];
unsigned long Cached;
}MEM_OCCUPY;
typedef struct os_line_data
{
char * val;
int len;
} os_line_data;
static char * os_getline(char *sin, os_line_data * line, char delim)
{
char *out = sin;
if (*out == '\0') return NULL;
// while (*out && (*out == delim)) { out++; }
line->val = out;
while (*out && (*out != delim)) { out++; }
line->len = out - line->val;
// while (*out && (*out == delim)) { out++; }
if (*out && (*out == delim)) { out++; }
if (*out == '\0') return NULL;
return out;
}
int Parser_EnvInfo(char * buffer,int size ,MEM_OCCUPY * lpMemory)
{
int state = 0;
char * p = buffer;
while (p)
{
os_line_data line = { 0 };
p = os_getline(p, &line, ':');
if (p == NULL || line.len <= 0) continue;
if (line.len == 8&& strncmp(line.val, "MemTotal", 8) == 0)
{
char *point = strtok(p," ");
memcpy(lpMemory->name1,"MemTotal",8);
lpMemory->MemTotal = atol(point);
}
else if(line.len == 7&& strncmp(line.val, "MemFree", 7) == 0)
{
char *point = strtok(p," ");
memcpy(lpMemory->name2,"MemFree",7);
lpMemory->MemFree = atol(point);
}
else if(line.len == 7&& strncmp(line.val, "Buffers", 7) == 0)
{
char *point = strtok(p," ");
memcpy(lpMemory->name3,"Buffers",7);
lpMemory->Buffers = atol(point);
}
else if(line.len == 6&& strncmp(line.val, "Cached", 6) == 0)
{
char *point = strtok(p," ");
memcpy(lpMemory->name4,"Cached",6);
lpMemory->Cached = atol(point);
}
}
}
int get_procmeminfo(MEM_OCCUPY * lpMemory)
{
FILE *fd;
char buff[128]={0};
fd = fopen("/proc/meminfo", "r");
if(fd <0) return -1;
fgets(buff, sizeof(buff), fd);
Parser_EnvInfo(buff,sizeof(buff),lpMemory);
fgets(buff, sizeof(buff), fd);
Parser_EnvInfo(buff,sizeof(buff),lpMemory);
fgets(buff, sizeof(buff), fd);
Parser_EnvInfo(buff,sizeof(buff),lpMemory);
fgets(buff, sizeof(buff), fd);
Parser_EnvInfo(buff,sizeof(buff),lpMemory);
fclose(fd);
}
一般在程序中,我们不太提倡使用system进行脚本调用得到这些信息,尤其是在嵌入式环境;