buffer cache与page cache

目录:
1、buffer cache和page cache的区别
2、如何释放buffer cache和page cache

1、page cache用于优化文件系统的I/O,buffer cache用于优化磁盘的I/O。

一般来说 page cache缓存的是file,很重要。buffer cache缓存的是 inode,dentry等内容,相对不那么重要。
page cache常用于读操作的时候,将常常读取的file缓存起来;buffer cache则是将要写入磁盘的内容缓冲(零存整取)。

以下文章引自Quora:
the page cache caches pages of files to optimize file I/O.The buffer cache caches disk blocks to optimize block I/O.

page cache 缓存了file,以优化文件的I/O,buffer cache 缓冲了磁盘 block,以优化磁盘I/O。

Prior to Linux kernel version 2.4,the two caches were distinct : File were in the page cache, disk block were in the buffer cache. Given that most files are represented by a filesystem on a disk ,data was represented twice, once in each of the caches. Many Unix systems follow a similar pattern.

在Linux kernel 2.4之前,两个cache是有区别的:文件存在page cache内,磁盘块存在buffer cache内。大部分磁盘里的文件呈现到文件系统过程中,数据一般呈现两次,在每个cache呈现一次。很多unix系统都是类似模式。

This is simple to implement, but with an obvious inelegance and inefficiency. Starting with Linux kernel version 2.4, the contents of two caches were unified. The VM subsystem now drives I/O and it does so out of the page cache.If cached data has both a file and a block representation -- as most data does -- the buffer cache will simply point into the page cache; thus only one instance of the data is cached in memory. The page cache is what you picture when you think of a disk cache: It caches file data from a disk to make subsequent I/O faster.

这是很容易实现的,但是不够优雅和高效。Linux 2.4 开始,两个cache里的内容统一了。虚拟机子系统现在可以驱动I/O,也可以在页面缓存中进行。如果数据既以文件又以块的形式缓存 -- 这是大部分数据的缓存方式 -- 那么buffer cache将简单地指向page cache;因此仅是一个数据的实例存在内存里(另一个地方存指针)。page cache缓存了你看到的磁盘文件:它从磁盘里缓存file数据优化I/O的速度。

The buffer cache remains,however,as the kernel still needs to perform block I/O in terms of blocks, not pages. As most block represent file data, most of the buffer cache is represented by the page cache. But a small amount of block data isn't file backed-- metadata and raw block I/O for example -- and thus is solely represented by the buffer cache.

这里buffer cache还有一点,内核需要优化块I/O,而不是页存取。因为file数据是用块数据呈现的,大部分buffer cache 呈现为page cache。但是一小部分块数据不是文件备份 -- metadata 和 raw block I/O -- 这些数据用buffer cache存储(比如inode cache和dentry cache)。

2、在操作系统中释放cache的方式:
echo 1> /proc/sys/vm/drop_caches : 释放page cache
echo 2> /proc/sys/vm/drop_caches : 释放 inode cache和dentry cache
echo 3> /proc/sys/vm/drop_caches : 释放全部

你可能感兴趣的:(buffer cache与page cache)