Linux IO的buffer cache和page cache合并的原因

我先大概解释一下,这里“buffer cache”中的buffer指的是以前块设备层中用来缓存磁盘内容的结构,一个buffer大小就是磁盘中一个block的大小。这里“page cache”指的是文件系统层用于缓存读写内容的cache,因为这一层在设备层之上,因此和内核其他地方一样,以page为单位来管理。
我们看到free命令打印中,有的版本cached和buffers是分开的,有的版本是“cached/buffers”合并在一起的,和本文说的是一回事儿。

这里的“合并”指的是,将两层中的结构统一,都改为page cache,且page中包含一个一个的buffer结构,另外对缓存管理做了优化:在文件系统层缓存的page就无需在块设备层再缓存一份了,而是直接用(块设备中buffer)指针指向(文件系统page cache数据)的方式。当然如果只存在于块设备中的缓存(inode的元数据或直接读写块设备的缓存)还是只产生在块设备层。

原文中还有一些其他文章链接也挺有意思的。好了,下面是转载内容:

What is the major difference between the buffer cache and the page cache? Why were they separate entities in older kernels? Why were they merged later on?

原文地址:quora.com/Linux-Kernel

The page cache caches pages of files to optimize file I/O. The buffer cache caches disk blocks to optimize block I/O.

Prior to Linux kernel version 2.4, the two caches were distinct: Files were in the page cache, disk blocks 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.

This is simple to implement, but with an obvious inelegance and inefficiency. Starting with Linux kernel version 2.4, the contents of the 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.

The buffer cache remains, however, as the kernel still needs to perform block I/O in terms of blocks, not pages. As most blocks 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.

See also my answer to What is the difference between Buffers and Cached columns in /proc/meminfo output?

Linux内存buffer/cache很大是因为Linux系统采用了一种称为页缓存(Page Cache)的机制来提高文件系统的性能。具体来说,当文件被读取到内存中时,Linux会将读取到的文件数据缓存到内存的页缓存中,以便下次访问相同文件时可以直接从页缓存中获取,而不需要再次从磁盘中读取。这种机制可以大大提升系统IO的性能。 Linux内存buffer/cache的大小受到系统资源管理的影响,它会充分利用可用的内存空间来提供更高效的文件访问。当系统运行时,如果有大量的可用内存,并且没有其他进程需要使用,Linux会将这些多余的内存用于buffer/cache。这样一来,当需要从磁盘读取文件时,大部分情况下可以直接从内存中获取,而不必再次访问磁盘,从而大大提高系统的响应速度。 值得注意的是,虽然buffer/cache占用了大量内存,但这些内存并不是完全被占用,而是可以根据需要被释放的。当其他程序需要更多的内存时,Linux会自动将buffer/cache中的数据释放出来,以满足其他程序的需求。这个过程被称为自动缓存(Automatic caching),可以确保系统内存始终处于最佳状态。 总而言之,Linux内存buffer/cache很大是Linux系统为了提高文件系统的性能而采取的一种机制。通过将文件数据缓存到内存中,可以减少对磁盘的读取操作,从而大大提升了系统的响应速度。同时,这些内存可以根据需要进行释放,确保系统内存始终处于最佳状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值