2009年2月 归档

/proc/meminfo

| 暂无评论 | 暂无引用通告

"Free," "buffer," "swap," "dirty." What does it all mean? If you said, "something to do with the Summer of '68", you may need a primer on 'meminfo'.

The entries in the /proc/meminfo can help explain what's going on with your memory usage, if you know how to read it.

Example of "cat /proc/meminfo":

root: total: used: free: shared: buffers: cached:Mem: 1055760384 1041887232 13873152 0 100417536 711233536Swap: 1077501952 8540160 1068961792

MemTotal: 1031016 kB MemFree: 13548 kBMemShared: 0 kBBuffers: 98064 kBCached: 692320 kBSwapCached: 2244 kBActive: 563112 kBInact_dirty: 309584 kBInact_clean: 79508 kBInact_target: 190440 kBHighTotal: 130992 kBHighFree: 1876 kBLowTotal: 900024 kBLowFree: 11672 kBSwapTotal: 1052248 kBSwapFree: 1043908 kBCommitted_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.

The VFS Inode Cache

| 暂无评论 | 暂无引用通告

当游历安装的文件系统的时候,它们的 VFS inode 不断地被读取,有时是写入。虚拟文件系统维护一个 inode cache ,用于加速对于所有安装的文件系统的访问。每一次从 inode cache 读出一个 VFS inode ,系统就可以省去对于物理设备的访问。

参见 fs/inode.c

     VFS inode cache 用散列表( hash table )的方式实现,条目是指针,指向既有同样 hash value 的 VFS inode 列表。一个 inode 的 hash value 从它的 inode 编号和包括这个文件系统的底层的物理设备的设备编号中计算出来。不论何时虚拟文件系统需要访问一个 inode ,它首先查看 VFS inode cache 。为了在 inode hash table 中查找一个 inode ,系统首先计算它的 hash value ,然后用它作为 inode hash table 的索引。这样得到了具有相同 hash value 的 inode 的列表的指针。然后它一次读取所有的 inode 直到它找到和它要找的 inode 具有相同的 inode 编号和相同的设备标识符的 inode 为止。

     如果可以在 cache 中找到这个 inode ,它的 count 就增加,表示它有了另一个用户,文件系统的访问继续进行。否则必须找到一个空闲的 VFS inode 让文件系统把 inode 读入到内存。如何得到一个空闲的 inode , VFS 有一系列选择。如果系统可以分配更多的 VFS inode ,它就这样做:它分配核心页并把它们分成新的、空闲的 inode ,放到 inode 列表中。系统中所有的 VFS inode 除了在 inode hash table 中之外也在一个由 first_inode 指向的列表。如果系统已经拥有了它允许有的所有的 inode ,它必须找到一个可以重用的 inode 。好的候选是哪些使用量( count )是 0 的 inode :这表示系统当前没有使用它们。真正重要的 VFS inode ,例如文件系统的 root inode ,已经有了一个大于 0 的使用量,所以永远不会选做重用。一旦定位到一个重用的候选,它就被清除。这个 VFS inode 可能是脏的,这时系统必须等待它被解锁然后才能继续。在重用之前这个 VFS inode 的候选必须被清除。

     虽然找到了一个新的 VFS inode ,还必须调用一个和文件系统相关的例程,用从底层的真正的文件系统中毒取得信息填充这个 inode 。当它填充的时候,这个新的 VFS inode 的使用量为 1 ,并被锁定,所以在它填入了有效的信息之前除了它没有其它进程可以访问。

     为了得到它实际需要的 VFS inode ,文件系统可能需要访问其它一些 inode 。这发生在你读取一个目录的时候:只有最终目录的 inode 是需要的,但是中间目录的 inode 也必须读取。当 VFS inode cache 使用过程并填满时,较少使用的 inode 会被废弃,较多使用的 inode 会保留在高速缓存中。

Linux分页管理机制

| 暂无评论 | 暂无引用通告

Linux使用分页管理机制来更加有效地利用物理内存.当创建一个进程时.仅仅把当前进程的一小部分真正装入内存.其余部分需要访问时.处理器产生一个页故障.由缺页中

断服务程序根据缺页虚拟地址和出错码调用写拷贝函数do--wp--page、此地址所属的vma的vm--ops指向的nopage、do--swap--page.swap--in等函数将需要的页换入物理内存。

随 着可执行映像的运行和页面的换入.系统中的内存有可能变得不足.这时Linux核心就必须调用kswapd守护进程释放部分物理内存。kswapd在系统 启动时由init进程建立。在系统的运行过程中。它被定期唤醒。检查系统中的空闲物理内存是否很少。如果是.则释放一部分内存.或者将一些页面换出到对换 空间。然后继续睡眠。

(1) 缺页中断和页面换入

页面换入主要由缺页中断服务入口函数do--page--fault来实现。当系统中产生页面故障时.如果虚拟内存地址有效.则产生错误的原因有如下两种:

· 虚拟内存地址对应的物理页不在内存中。那么它必然在磁盘或对换空间中.如果在磁盘上.那么我们调用do--nO--page函数.而do--no--page调用vma一> vm--ops一> nopage()

函数建立页面映射.从对换空间或磁盘中调入页面.或者通过do--swap--page()函数调用swap--in()来换入页面。

· 该虚拟地址对应的物理页在内存。但是被写保护.如果这种情况发生在一个共享页面上.则需要"写拷贝"函数do--wp--page来换入页面.do--wp--page函数首先调用一get--

free--page获得一新页面.然后调用copy--COW--page拷贝页面的内容.当然还要调用相应的刷新函数刷新TLB和缓存等。

(2) 页交换进程和页面换出

正 如我们上面所描述的.系统使用kswapd守护进程来定期地换出页面。使系统中有足够的空闲物理内存页。kswapd进程定期地检查系统中的空闲页面数. 如果少于一定值.则按照以下三中途径获得空闲页面:① 减少缓冲区和页面高速缓存的大小;② 把共享内存占用的页面置换到对换空间;③ 换出或丢弃物理内存页。

HTTP304

| 暂无评论 | 暂无引用通告
    Not Modified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提供If-Modified-Since头表示客户只想比指定日期更新的文档)。服务器告诉客户,原来缓冲的文档还可以继续使用。
如 果客户端在请求一个文件的时候,发现自己缓存的文件有 Last Modified ,那么在请求中会包含 If Modified Since ,这个时间就是缓存文件的 Last Modified 。因此,如果请求中包含 If Modified Since,就说明已经有缓存在客户端。只要判断这个时间和当前请求的文件的修改时间就可以确定是返回 304 还是 200 。对于静态文件,例如:CSS、图片,服务器会自动完成 Last Modified 和 If Modified Since 的比较,完成缓存或者更新。但是对于动态页面,就是动态产生的页面,往往没有包含 Last Modified 信息,这样浏览器、网关等都不会做缓存,也就是在每次请求的时候都完成一个 200 的请求。

将blog迁至dreamhost

| 暂无评论 | 暂无引用通告
      经过三个小时的折腾,顺利将blog迁至dreamhost,原来使用的是国内的"捌号主机"的"64位Perl服务器合租/30人季付",感觉贵了点,速度一般,有时晚上高峰期访问时偏慢,迁移过程序中,出现后台乱码问题,参考以下链接解决:
http://hiseven.net/distortion-code-in-mysql-of-wp-and-mt/

关于此归档

这里是2009年2月的所有日记,它们按照时间从新到老排序。

上一篇日记2009年1月

下一篇日记2009年3月

首页归档页可以看到最新的日记和所有日记。