proc文件系统探索 之 根目录下的文件[七]
主要參考內(nèi)核文檔和紅帽文檔對(duì)
> cat /proc/meminfo?? 讀出的內(nèi)核信息進(jìn)行解釋,
下篇文章會(huì)簡(jiǎn)單對(duì)讀出該信息的代碼進(jìn)行簡(jiǎn)單的分析。
MemTotal: 507480 kB |
相應(yīng)選項(xiàng)中文意思想各位高手已經(jīng)知道,如果翻譯有什么錯(cuò)誤,請(qǐng)務(wù)必指出:
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
下面簡(jiǎn)單來個(gè)例子,看看已用內(nèi)存和物理內(nèi)存大小..
#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;
}
結(jié)果:Used 488M/Total 495M
上面文章對(duì)meminfo里的信息做出簡(jiǎn)單的解釋了
那么內(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, #define K(x) ((x) << (PAGE_SHIFT - 10)) /** get_vmalloc_info(&vmi); return proc_calc_metrics(page, start, off, count, eof, len); |
其中sysinfo結(jié)構(gòu)在 linux/kernel.h? 定義:
struct sysinfo { |
而global_page_state()函數(shù)中的常量定義在 linux/mmzone.h
enum zone_stat_item { |
其中通過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),簡(jiǎn)單分析CPU和內(nèi)存使用信息。
#include <stdio.h>
#include <linux/unistd.h> /* 包含調(diào)用 _syscallX 宏等相關(guān)信息*/
#include <linux/kernel.h> /* 包含sysinfo結(jié)構(gòu)體信息*/
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;
}
結(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)載于:https://blog.51cto.com/sunzeduo/1562203
總結(jié)
以上是生活随笔為你收集整理的proc文件系统探索 之 根目录下的文件[七]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MapReduce数据连接
- 下一篇: Servlet登陆功能的实现