主要参考内核文档和红帽文档对
> cat /proc/meminfo 读出的内核信息进行解释,
下篇文章会简单对读出该信息的代码进行简单的分析。
|
相应选项中文意思想各位高手已经知道,如果翻译有什么错误,请务必指出:
MemTotal: 所有可用RAM大小 (即物理内存减去一些预留位和内核的二进制代码大小)
MemFree: LowFree与HighFree的总和,被系统留着未使用的内存
Buffers: 用来给文件做缓冲大小
Cached: 被高速缓冲存储器(cache memory)用的内存的大小(等于 diskcache minus SwapCache ).
SwapCached:被高速缓冲存储器(cache memory)用的交换空间的大小已经
被交换出来的内存,但仍然被存放在swapfile中。用来在需要的时候很快的
被替换而不需要再次打开I/O端口。
Active: 在活跃使用中的缓冲或高速缓冲存储器页面文件的大小,除非非常必要否则不会被移作他用.
Inactive: 在不经常使用中的缓冲或高速缓冲存储器页面文件的大小,可能被用于其他途径.
HighTotal:
HighFree: 该区域不是直接映射到内核空间。内核必须使用不同的手法使用该段内存。
LowTotal:
LowFree: 低位可以达到高位内存一样的作用,而且它还能够被内核用来记录
一些自己的数据结构。Among many other things, it is where
everything from the Slab is allocated. Bad things happen
when you’re out of lowmem.
SwapTotal: 交换空间的总大小
SwapFree: 未被使用交换空间的大小
Dirty: 等待被写回到磁盘的内存大小。
Writeback: 正在被写回到磁盘的内存大小。
AnonPages:未映射页的内存大小
Mapped: 设备和文件等映射的大小。
Slab: 内核数据结构缓存的大小,可以减少申请和释放内存带来的消耗。
SReclaimable:可收回Slab的大小
SUnreclaim:不可收回Slab的大小(SUnreclaim+SReclaimable=Slab)
PageTables:管理内存分页页面的索引表的大小。
NFS_Unstable:不稳定页表的大小
Bounce:
CommitLimit: Based on the overcommit ratio(‘vm.overcommit_ratio’),
this is the total amount of memory currently available to
be allocated on the system. This limit is only adhered to
if strict overcommit accounting is enabled (mode 2 in
‘vm.overcommit_memory’).
The CommitLimit is calculated with the following formula:
CommitLimit = (‘vm.overcommit_ratio’ * Physical RAM) + Swap
For example, on a system with 1G of physical RAM and 7G
of swap with a `vm.overcommit_ratio` of 30 it would
yield a CommitLimit of 7.3G.
For more details, see the memory overcommit documentation
in vm/overcommit-accounting.
Committed_AS: The amount of memory presently allocated on
the system.
The committed memory is a sum of all of the memory which
has been allocated by processes, even if it has not been
“used” by them as of yet. A process which malloc()’s 1G
of memory, but only touches 300M of it will only show up
as using 300M of memory even if it has the address space
allocated for the entire 1G. This 1G is memory which has
been “committed” to by the VM and can be used at any time
by the allocating application. With strict overcommit
enabled on the system (mode 2 in ‘vm.overcommit_memory’),
allocations which would exceed the CommitLimit (detailed
above) will not be permitted. This is useful if one needs
to guarantee that processes will not fail due to lack of
memory once that memory has been successfully allocated.
VmallocTotal: 可以vmalloc虚拟内存大小
VmallocUsed: 已经被使用的虚拟内存大小。
VmallocChunk: largest contigious block of vmalloc area which is free
下面简单来个例子,看看已用内存和物理内存大小..
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int MemInfo(char* Info, int len);
int main()
{
char buf[128];
memset(buf, 0, 128);
MemInfo(buf, 100);
printf("%s", buf);
return 0;
}
int MemInfo(char* Info, int len)
{
char sStatBuf[256];
FILE* fp;
int flag;
int TotalMem;
int UsedMem;
char* line;
if(system("free -m | awk '{print $2,$3}' > mem"));
memset(sStatBuf, 0, 256);
fp = fopen("mem", "rb");
if(fp < 0)
{
return -1;
}
fread(sStatBuf,1, sizeof(sStatBuf) , fp);
line = strstr(sStatBuf, “\n”);
TotalMem = atoi(line);
line = strstr(line, ” “);
UsedMem = atoi(line);
memset(sStatBuf, 0, 256);
sprintf(sStatBuf, “Used %dM/Total %dM\n”, UsedMem, TotalMem);
if(strlen(sStatBuf) > len)
{
return -1;
}
memcpy(Info, sStatBuf, strlen(sStatBuf));
return 0;
}
结果:Used 488M/Total 495M
上面文章对meminfo里的信息做出简单的解释了
那么内核怎么把meminfo信息动态反应到meminfo文件中呢
在内核 linux/fs/proc/proc_misc.c中
#define K(x) ((x) << (PAGE_SHIFT - 10)) /** get_vmalloc_info(&vmi); return proc_calc_metrics(page, start, off, count, eof, len); |
其中sysinfo结构在 linux/kernel.h 定义:
|
而global_page_state()函数中的常量定义在 linux/mmzone.h
|
其中通过global_page_state()函数根据 zone_stat_item 结构的常量得到不同区大小,会跟 vm_stat[NR_VM_ZONE_STAT_ITEMS]对应起来。
vm_stat[]是统计各区的大小。
内核 linux/vmstat.h定义:static inline unsigned long global_page_state(enum zone_stat_item item)
{
long x = atomic_long_read(&vm_stat[item]);
#ifdef CONFIG_SMP
if (x < 0)
x = 0;
#endif
return x;
}
下面根据struct sysinfo结构,简单分析CPU和内存使用信息。
#include <stdio.h>
#include <linux/unistd.h> /* 包含调用 _syscallX 宏等相关信息*/
#include <linux/kernel.h> /* 包含sysinfo结构体信息*/
int main(int argc, char *agrv[])
{
struct sysinfo s_info;
int error;
error = sysinfo(&s_info);
printf(“\n\ncode error=%d\n”,error);
printf(“Uptime = %ds\nLoad: 1 min%d / 5 min %d / 15 min %d\n”
“RAM: total %d / free %d /shared%d\n”
“Memory in buffers = %d\nSwap:total%d/free%d\n”
“Number of processes = %d\n”,
s_info.uptime, s_info.loads[0],
s_info.loads[1], s_info.loads[2],
s_info.totalram, s_info.freeram,
s_info.totalswap, s_info.freeswap,
s_info.procs);
return 0;
}
结果:
code error=0
Uptime = 8329s
Load: 1 min37152 / 5 min 37792 / 15 min 48672
RAM: total 519659520 / free 9031680 /shared1003474944
Memory in buffers = 937451520
Swap:total223/free-1078732672
Number of processes = -1078732608