meminfo 詳解
1、meminfo統(tǒng)計(jì)信息原文? "cat /proc/meminfo":

MemTotal:? 1031016 kB
MemFree:? 13548 kB
MemShared:? 0 kB
Buffers:? 98064 kB
Cached:? ?692320 kB
SwapCached:? 2244 kB
Active:? ?563112 kB
Inact_dirty:? 309584 kB
Inact_clean:? 79508 kB
Inact_target:? 190440 kB
HighTotal:? 130992 kB
HighFree:? 1876 kB
LowTotal:? 900024 kB
LowFree:? 11672 kB
SwapTotal:? 1052248 kB
SwapFree:? 1043908 kB
Committed_AS:? 332340 kB
The information comes in the form of both high-level and low-level statistics. At the top you see a quick summary of the most common values people would like to look at. Below you find the individual values we will discuss. First we will discuss the high-level statistics.
High-Level Statistics
MemTotal: Total usable ram (i.e. physical ram minus a few reserved bits and the kernel binary code)
MemFree: Is sum of LowFree+HighFree (overall stat)
MemShared: 0; is here for compat reasons but always zero.
Buffers: Memory in buffer cache. mostly useless as metric nowadays
Cached: Memory in the pagecache (diskcache) minus SwapCache
SwapCache: Memory that once was swapped out, is swapped back in but still also is in the swapfile (if memory is needed it doesn't need to be swapped out AGAIN because it is already in the swapfile. This saves I/O)
Detailed Level Statistics
VM Statistics
VM splits the cache pages into "active" and "inactive" memory. The idea is that if you need memory and some cache needs to be sacrificed for that, you take it from inactive since that's expected to be not used. The vm checks what is used on a regular basis and moves stuff around.
When you use memory, the CPU sets a bit in the pagetable and the VM checks that bit occasionally, and based on that, it can move pages back to active. And within active there's an order of "longest ago not used" (roughly, it's a little more complex in reality). The longest-ago used ones can get moved to inactive. Inactive is split into two in the above kernel (2.4.18-24.8.0). Some have it three.
Active: Memory that has been used more recently and usually not reclaimed unless absolutely necessary.
Inact_dirty: Dirty means "might need writing to disk or swap." Takes more work to free. Examples might be files that have not been written to yet. They aren't written to memory too soon in order to keep the I/O down. For instance, if you're writing logs, it might be better to wait until you have a complete log ready before sending it to disk.
Inact_clean: Assumed to be easily freeable. The kernel will try to keep some clean stuff around always to have a bit of breathing room.
Inact_target: Just a goal metric the kernel uses for making sure there are enough inactive pages around. When exceeded, the kernel will not do work to move pages from active to inactive. A page can also get inactive in a few other ways, e.g. if you do a long sequential I/O, the kernel assumes you're not going to use that memory and makes it inactive preventively. So you can get more inactive pages than the target because the kernel marks some cache as "more likely to be never used" and lets it cheat in the "last used" order.
Memory Statistics
HighTotal: is the total amount of memory in the high region. Highmem is all memory above (approx) 860MB of physical RAM. Kernel uses indirect tricks to access the high memory region. Data cache can go in this memory region.
LowTotal: The total amount of non-highmem memory.
LowFree: The amount of free memory of the low memory region. This is the memory the kernel can address directly. All kernel datastructures need to go into low memory.
SwapTotal: Total amount of physical swap memory.
SwapFree: Total amount of swap memory free.
Committed_AS: An estimate of how much RAM you would need to make a 99.99% guarantee that there never is OOM (out of memory) for this workload. Normally the kernel will overcommit memory. That means, say you do a 1GB malloc, nothing happens, really. Only when you start USING that malloc memory you will get real memory on demand, and just as much as you use. So you sort of take a mortgage and hope the bank doesn't go bust. Other cases might include when you mmap a file that's shared only when you write to it and you get a private copy of that data. While it normally is shared between processes. The Committed_AS is a guesstimate of how much RAM/swap you would need worst-case.
2、meminfo統(tǒng)計(jì)信息譯文? "cat /proc/meminfo":
MemTotal: 507480 kB
MemFree: 10800 kB
Buffers: 34728 kB
Cached: 98852 kB
SwapCached: 128 kB
Active: 304248 kB
Inactive: 46192 kB
HighTotal: 0 kB
HighFree: 0 kB
LowTotal: 507480 kB
LowFree: 10800 kB
SwapTotal: 979956 kB
SwapFree: 941296 kB
Dirty: 32 kB
Writeback: 0 kB
AnonPages: 216756 kB
Mapped: 77560 kB
Slab: 22952 kB
SReclaimable: 15512 kB
SUnreclaim: 7440 kB
PageTables: 2640 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
CommitLimit: 1233696 kB
Committed_AS: 828508 kB
VmallocTotal: 516088 kB
VmallocUsed: 5032 kB
VmallocChunk: 510580 kB
MemTotal: 所有可用RAM大小 (即物理內(nèi)存減去一些預(yù)留位和內(nèi)核的二進(jìn)制代碼大小)
MemFree: LowFree與HighFree的總和,被系統(tǒng)留著未使用的內(nèi)存
Buffers: 用來給文件做緩沖大小
Cached: 被高速緩沖存儲(chǔ)器(cache memory)用的內(nèi)存的大小(等于 diskcache minus SwapCache ).
SwapCached:被高速緩沖存儲(chǔ)器(cache memory)用的交換空間的大小已經(jīng)
被交換出來的內(nèi)存,但仍然被存放在swapfile中。用來在需要的時(shí)候很快的
被替換而不需要再次打開I/O端口。
Active: 在活躍使用中的緩沖或高速緩沖存儲(chǔ)器頁面文件的大小,除非非常必要否則不會(huì)被移作他用.
Inactive: 在不經(jīng)常使用中的緩沖或高速緩沖存儲(chǔ)器頁面文件的大小,可能被用于其他途徑.
HighTotal:
HighFree: 該區(qū)域不是直接映射到內(nèi)核空間。內(nèi)核必須使用不同的手法使用該段內(nèi)存。
LowTotal:
LowFree: 低位可以達(dá)到高位內(nèi)存一樣的作用,而且它還能夠被內(nèi)核用來記錄
一些自己的數(shù)據(jù)結(jié)構(gòu)。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: 等待被寫回到磁盤的內(nèi)存大小。
Writeback: 正在被寫回到磁盤的內(nèi)存大小。
AnonPages:未映射頁的內(nèi)存大小
Mapped: 設(shè)備和文件等映射的大小。
Slab: 內(nèi)核數(shù)據(jù)結(jié)構(gòu)緩存的大小,可以減少申請(qǐng)和釋放內(nèi)存帶來的消耗。
SReclaimable:可收回Slab的大小
SUnreclaim:不可收回Slab的大小(SUnreclaim+SReclaimable=Slab)
PageTables:管理內(nèi)存分頁頁面的索引表的大小。
NFS_Unstable:不穩(wěn)定頁表的大小
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虛擬內(nèi)存大小
VmallocUsed: 已經(jīng)被使用的虛擬內(nèi)存大小。
VmallocChunk: largest contigious block of vmalloc area which is free
下面簡單來個(gè)例子,看看已用內(nèi)存和物理內(nèi)存大小..
#include
#include
#include
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 %dMn", UsedMem, TotalMem);
if(strlen(sStatBuf) > len)
{
return -1;
}
memcpy(Info, sStatBuf, strlen(sStatBuf));
return 0;
}
結(jié)果:Used 488M/Total 495M
3、meminfo 代碼解析
那么內(nèi)核怎么把meminfo信息動(dòng)態(tài)反應(yīng)到meminfo文件中呢
在內(nèi)核 linux/fs/proc/proc_misc.c中
static int meminfo_read_proc(char *page, char **start, off_t off,
int count, int *eof, void *data)
{
struct sysinfo i;
int len;
unsigned long committed;
unsigned long allowed;
struct vmalloc_info vmi;
long cached;
#define K(x) ((x) << (PAGE_SHIFT - 10))
/**
*該宏作用把存儲(chǔ)單位傳換成 kb
*/
si_meminfo(&i);
si_swapinfo(&i);
/**
*這兩個(gè)函數(shù)是對(duì)struct sysinfo結(jié)構(gòu)進(jìn)行初始化的
*/
committed = atomic_read(&vm_committed_space);
allowed = ((totalram_pages - hugetlb_total_pages())
* sysctl_overcommit_ratio / 100) + total_swap_pages;
/**
*其中這項(xiàng)根據(jù)上篇文章CommitLimit解釋計(jì)算的
*/
cached = global_page_state(NR_FILE_PAGES) -
total_swapcache_pages - i.bufferram;
if (cached < 0)
cached = 0;
get_vmalloc_info(&vmi);
/*
* Tagged format, for easy grepping and expansion.
*/
len = sprintf(page,
"MemTotal: %8lu kBn"
"MemFree: %8lu kBn"
"Buffers: %8lu kBn"
"Cached: %8lu kBn"
"SwapCached: %8lu kBn"
"Active: %8lu kBn"
"Inactive: %8lu kBn"
#ifdef CONFIG_HIGHMEM
"HighTotal: %8lu kBn"
"HighFree: %8lu kBn"
"LowTotal: %8lu kBn"
"LowFree: %8lu kBn"
#endif
"SwapTotal: %8lu kBn"
"SwapFree: %8lu kBn"
"Dirty:? ? ?%8lu kBn"
"Writeback: %8lu kBn"
"AnonPages: %8lu kBn"
"Mapped: %8lu kBn"
"Slab:? ? ? %8lu kBn"
"SReclaimable: %8lu kBn"
"SUnreclaim: %8lu kBn"
"PageTables: %8lu kBn"
"NFS_Unstable: %8lu kBn"
"Bounce: %8lu kBn"
"CommitLimit: %8lu kBn"
"Committed_AS: %8lu kBn"
"VmallocTotal: %8lu kBn"
"VmallocUsed: %8lu kBn"
"VmallocChunk: %8lu kBn",
K(i.totalram),
K(i.freeram),
K(i.bufferram),
K(cached),
K(total_swapcache_pages),
K(global_page_state(NR_ACTIVE)),
K(global_page_state(NR_INACTIVE)),
#ifdef CONFIG_HIGHMEM
K(i.totalhigh),
K(i.freehigh),
K(i.totalram-i.totalhigh),
K(i.freeram-i.freehigh),
#endif
K(i.totalswap),
K(i.freeswap),
K(global_page_state(NR_FILE_DIRTY)),
K(global_page_state(NR_WRITEBACK)),
K(global_page_state(NR_ANON_PAGES)),
K(global_page_state(NR_FILE_MAPPED)),
K(global_page_state(NR_SLAB_RECLAIMABLE) +
global_page_state(NR_SLAB_UNRECLAIMABLE)),
K(global_page_state(NR_SLAB_RECLAIMABLE)),
K(global_page_state(NR_SLAB_UNRECLAIMABLE)),
K(global_page_state(NR_PAGETABLE)),
K(global_page_state(NR_UNSTABLE_NFS)),
K(global_page_state(NR_BOUNCE)),
K(allowed),
K(committed),
(unsigned long)VMALLOC_TOTAL >> 10,
vmi.used >> 10,
vmi.largest_chunk >> 10
);
len += hugetlb_report_meminfo(page + len);
return proc_calc_metrics(page, start, off, count, eof, len);
#undef K
}
其中sysinfo結(jié)構(gòu)在 linux/kernel.h? 定義:
struct sysinfo {
long uptime; /* 啟動(dòng)到現(xiàn)在經(jīng)過的時(shí)間 */
unsigned long loads[3];
/* 1, 5, and 15 minute load averages */
unsigned long totalram; /* 總的可用的內(nèi)存大小 */
unsigned long freeram; /* 還未被使用的內(nèi)存大小 */
unsigned long sharedram; /* 共享的存儲(chǔ)器的大小*/
unsigned long bufferram; /* 的存儲(chǔ)器的大小 */
unsigned long totalswap; /* 交換區(qū)大小 */
unsigned long freeswap; /* 還可用的交換區(qū)大小 */
unsigned short procs; /* 當(dāng)前進(jìn)程數(shù)目 */
unsigned short pad; /* explicit padding for m68k */
unsigned long totalhigh; /* 總的高內(nèi)存大小 */
unsigned long freehigh; /* 可用的高內(nèi)存大小 */
unsigned int mem_unit; /* 以字節(jié)為單位的內(nèi)存大小 */
char _f[20-2*sizeof(long)-sizeof(int)];
};
而global_page_state()函數(shù)中的常量定義在 linux/mmzone.h
enum zone_stat_item {
/* First 128 byte cacheline (assuming 64 bit words) */
NR_FREE_PAGES,
NR_INACTIVE,
NR_ACTIVE,
NR_ANON_PAGES, /* Mapped anonymous pages */
NR_FILE_MAPPED, /* pagecache pages mapped into pagetables.
only modified from process context */
NR_FILE_PAGES,
NR_FILE_DIRTY,
NR_WRITEBACK,
/* Second 128 byte cacheline */
NR_SLAB_RECLAIMABLE,
NR_SLAB_UNRECLAIMABLE,
NR_PAGETABLE,? ? ? /* used for pagetables */
NR_UNSTABLE_NFS,? ? ?/* NFS unstable pages */
NR_BOUNCE,
NR_VMSCAN_WRITE,
#ifdef CONFIG_NUMA
NUMA_HIT,? ? ? /* allocated in intended node */
NUMA_MISS,? ? ? /* allocated in non intended node */
NUMA_FOREIGN,? ? ? /* was intended here, hit elsewhere */
NUMA_INTERLEAVE_HIT, /* interleaver preferred this zone */
NUMA_LOCAL,? ? ? /* allocation from local node */
NUMA_OTHER,? ? ? /* allocation from other node */
#endif
NR_VM_ZONE_STAT_ITEMS
};
其中通過global_page_state()函數(shù)根據(jù) zone_stat_item 結(jié)構(gòu)的常量得到不同區(qū)大小,會(huì)跟 vm_stat[NR_VM_ZONE_STAT_ITEMS]對(duì)應(yīng)起來。
vm_stat[]是統(tǒng)計(jì)各區(qū)的大小。
內(nèi)核 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;
}
下面根據(jù)struct sysinfo結(jié)構(gòu),簡單分析CPU和內(nèi)存使用信息。
#include
#include
#include
int main(int argc, char *agrv[])
{
struct sysinfo s_info;
int error;
error = sysinfo(&s_info);
printf("nncode error=%dn",error);
printf("Uptime = %dsnLoad: 1 min%d / 5 min %d / 15 min %dn"
"RAM: total %d / free %d /shared%dn"
"Memory in buffers = %dnSwap:total%d/free%dn"
"Number of processes = %dn",
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;
}
結(jié)果:
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
文章轉(zhuǎn)自異步社區(qū)
軟件開發(fā)
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。