包括对proc根目录下meminfo文件的解析。

> 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大小 (即物理内存减去一些预留位和内核的二进制代码大小)

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中

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))
/**
*该宏作用把存储单位传换成 kb
*/
si_meminfo(&i);
si_swapinfo(&i);
/**
*这两个函数是对struct sysinfo结构进行初始化的
*/
committed = atomic_read(&vm_committed_space);
allowed = ((totalram_pages - hugetlb_total_pages())
* sysctl_overcommit_ratio / 100) + total_swap_pages; /**
*其中这项根据上篇文章CommitLimit解释计算的
*/
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 kB\n”
“MemFree: %8lu kB\n”
“Buffers: %8lu kB\n”
“Cached: %8lu kB\n”
“SwapCached: %8lu kB\n”
“Active: %8lu kB\n”
“Inactive: %8lu kB\n”
#ifdef CONFIG_HIGHMEM
“HighTotal: %8lu kB\n”
“HighFree: %8lu kB\n”
“LowTotal: %8lu kB\n”
“LowFree: %8lu kB\n”
#endif
“SwapTotal: %8lu kB\n”
“SwapFree: %8lu kB\n”
“Dirty: %8lu kB\n”
“Writeback: %8lu kB\n”
“AnonPages: %8lu kB\n”
“Mapped: %8lu kB\n”
“Slab: %8lu kB\n”
“SReclaimable: %8lu kB\n”
“SUnreclaim: %8lu kB\n”
“PageTables: %8lu kB\n”
“NFS_Unstable: %8lu kB\n”
“Bounce: %8lu kB\n”
“CommitLimit: %8lu kB\n”
“Committed_AS: %8lu kB\n”
“VmallocTotal: %8lu kB\n”
“VmallocUsed: %8lu kB\n”
“VmallocChunk: %8lu kB\n”,
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结构在 linux/kernel.h  定义:

struct sysinfo {
long uptime; /* 启动到现在经过的时间 */
unsigned long loads[3];
/* 1, 5, and 15 minute load averages */
unsigned long totalram; /* 总的可用的内存大小 */
unsigned long freeram; /* 还未被使用的内存大小 */
unsigned long sharedram; /* 共享的存储器的大小*/
unsigned long bufferram; /* 的存储器的大小 */
unsigned long totalswap; /* 交换区大小 */
unsigned long freeswap; /* 还可用的交换区大小 */
unsigned short procs; /* 当前进程数目 */
unsigned short pad; /* explicit padding for m68k */
unsigned long totalhigh; /* 总的高内存大小 */
unsigned long freehigh; /* 可用的高内存大小 */
unsigned int mem_unit; /* 以字节为单位的内存大小 */
char _f[20-2*sizeof(long)-sizeof(int)];
};

而global_page_state()函数中的常量定义在 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()函数根据 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

from: http://www.kerneltravel.net/?p=278

proc文件系统探索 之 根目录下的文件[三]的更多相关文章

  1. proc文件系统探索 之 根目录下的文件[1]

    2.1根目录下的文件2.1.1lock文件内核锁,记录与被打开的文件有关的锁信息. 该文件显示当前被内核锁定的文件.该文件包含的内容是内核调试数据,根据使用的系统的这些数据会变化很大.一个/proc/ ...

  2. proc文件系统探索 之 根目录下的文件[二]

    包括对proc根目录下stat,uptime,swaps三个文件的解析. /proc/stat 文件包含了系统启动后的一些系统统计信息. Cat /proc/stat: cpu 77781 1077 ...

  3. Linux系统根目录下各文件夹介绍

    参考自:[1]Linux 系统根目录下各个文件夹的作用 https://www.cnblogs.com/jiangfeilong/p/10538795.html[2]了解Linux根目录"/ ...

  4. asp.net在网站根目录下创建文件夹

    假设要在asp.net网站的根目录下建立文件夹hovertree,C#代码如下: string m_keleyiFolderName = Server.MapPath("/hovertree ...

  5. 【转】忙里偷闲写的小例子---读取android根目录下的文件或文件夹

    原文网址:http://www.cnblogs.com/wenjiang/p/3140055.html 最近几天真的是各种意义上的忙,忙着考试,还要忙着课程设计,手上又有外包的项目,另一边学校的项目还 ...

  6. 【转】读取android根目录下的文件或文件夹

    原文网址:http://my.oschina.net/Ccx371161810/blog/287823 读取android根目录下的文件或文件夹 SDK的操作 读取android根目录下的文件或文件夹 ...

  7. 用java写一个servlet,可以将放在tomcat项目根目录下的文件进行下载

    用java写一个servlet,可以将放在tomcat项目根目录下的文件进行下载,将一个完整的项目进行展示,主要有以下几个部分: 1.servlet部分   Export 2.工具类:TxtFileU ...

  8. 忙里偷闲写的小例子---读取android根目录下的文件或文件夹

    最近几天真的是各种意义上的忙,忙着考试,还要忙着课程设计,手上又有外包的项目,另一边学校的项目还要搞,自己的东西还在文档阶段,真的是让人想死啊!! 近半个月来,C#这方面的编码比较多,android和 ...

  9. proc文件系统探索 之 以数字命名的目录

    在proc根目录下,以数字命名的目录表示当前一个运行的进程,目录名即为进程的pid.其内的目录和文件给出了一些关于该进程的信息. niutao@niutao-desktop:/proc/6584$ l ...

随机推荐

  1. Lintcode375 Clone Binary Tree solution 题解

    [题目描述] For the given binary tree, return a deep copy of it. 深度复制一个二叉树,给定一个二叉树,返回一个他的克隆品. [题目链接] www. ...

  2. Visual Studio的一些快捷键

    1)CTRL + W选择当前单词 2) Ctrl+F10: 运行到光标处 1.CTRL + SHIFT + B生成解决方案   2.CTRL + F7 生成编译   3. CTRL + O 打开文件  ...

  3. UEditor Golang上传图片与附件

    UEditor图片与附件上传官方只支持ASP.ASP.NET.JSP.PHP四种语言版本,Golang就不在其中.因为自己开发系统的需要,我照着UEditor服务器端的接口自己实现了一个Golang版 ...

  4. Java 异常基础详解

    目录 1. Java 中的异常 1.1 什么是异常? 1.2 什么是异常处理? 1.2.1 异常处理的优势 1.3 Java 异常类的层次结构 1.4 异常类型 1.5 检查和未检查异常之间的区别 1 ...

  5. VueJs 源码解析 (四) initRender.Js

    vueJs 源码解析 (四) initRender.Js 在之前的文章中提到了 vuejs 源码中的 架构部分,以及 谈论到了 vue 源码三要素 vm.compiler.watcher 这三要素,那 ...

  6. ios开发-MapKit(地图框架)使用简介

    我们使用app的时候,很多软件都自带了地图功能.我们可以看到自己的位置,看到周围商场等信息.我们也可以导航,划线等. 其实苹果的MapKit使用起来还是很简单的.这里简单的介绍一下. 0.使用前准备 ...

  7. vue中自定义组件(插件)

    vue中自定义组件(插件) 原创 2017年01月04日 22:46:43 标签: 插件 在vue项目中,可以自定义组件像vue-resource一样使用Vue.use()方法来使用,具体实现方法: ...

  8. [AHOI 2009]chess 中国象棋

    Description 题库链接 给你一张 \(N\times M\) 的棋盘.要求每行每列最多放两个棋子,问总方案数. \(1\leq N,M\leq 100\) Solution 记 \(f_{i ...

  9. [SDOI 2013]森林

    Description 题库链接 给你 \(n\) 个节点,初始 \(m\) 条边, \(t\) 组操作.让你支持: 询问树上路径点权 \(K\) 小: 支持加边操作. 强制在线,所有状态保证是一个树 ...

  10. [CODEVS 1288]埃及分数

    Description 在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数. 如:2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为加数中有相同的. 对于一个分数a/ ...