linux page cache和Disk Buffer原理

博客介绍了PageCache和Disk buffer的区别,Page Cache以Page为单位缓存文件内容,Disk Buffer以Block为单位缓存设备读写。还阐述了Disk Buffer的预读和回写策略,预读分同步和异步,回写有pdflush模式和bdi_writeback机制,后者可提高I/O性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、PageCache和Disk buffer的区别

Page Cache主要用来缓存文件的内容,以Page(4K)为单位组织;

Disk Buffer主要缓存设备的读写,以Block(默认1K)为单位组织管理,

二、DiskBuffer的策略

Disk Buffer有两大功能,预读和回写;

1 disk buffer的预读

预读其实就是利用了局部性原理,具体过程是:对于每个文件的第一个读请求,系统读入所请求的页面并读入紧随其后的少数几个页面(通常是三个页面),这时的预读称为同步预读。对于第二次读请求,如果所读页面不在Cache中,即不在前次预读的页中,则表明文件访问不是顺序访问,系统继续采用同步预读;如果所读页面在Cache中,则表明前次预读命中,操作系统把预读页的大小扩大一倍,此时预读过程是异步的,应用程序可以不等预读完成即可返回,只要后台慢慢读页面即可,这时的预读称为异步预读。任何接下来的读请求都会处于两种情况之一:第一种情况是所请求的页面处于预读的页面中,这时继续进行异步预读;第二种情况是所请求的页面处于预读页面之外,这时系统就要进行同步预读。

2. disk buffer的回写方式

i). pdflush模式(2.6.32内核以前)

下面两种情况下,脏页会被写回到磁盘:

  • 在空闲内存低于一个特定的阈值时,内核必须将脏页写回磁盘,以便释放内存。
  • 当脏页在内存中驻留超过一定的阈值时,内核必须将超时的脏页写会磁盘,以确保脏页不会无限期地驻留在内存中。

回写开始后,pdflush会持续写数据,直到满足以下两个条件:

  • 已经有指定的最小数目的页被写回到磁盘。
  • 空闲内存页已经回升,超过了阈值。

ii). bdi_writeback机制

bdi_writeback机制主要解决了原有fdflush机制存在的一个问题:在多磁盘的系统中,pdflush管理了所有磁盘的Cache,从而导致一定程度的I/O瓶颈。bdi_writeback机制为每个磁盘都创建了一个线程,专门负责这个磁盘的Page Cache的刷新工作,从而实现了每个磁盘的数据刷新在线程级的分离,提高了I/O性能。

### Linux Buffer Cache Explanation In the context of Linux systems, buffer and cache play crucial roles in optimizing performance by reducing disk I/O operations. The operating system uses a portion of RAM as a temporary storage area for data that has been recently read from or written to the disk. The **buffer** is used specifically for block devices (like hard drives), holding blocks of data temporarily while they are being transferred between memory and disk. Buffers store unwritten data until it can be efficiently flushed out to permanent storage[^2]. On the other hand, the **cache** holds copies of frequently accessed files so subsequent reads do not require accessing slower media again. When new information needs to be stored but there isn't enough space available within buffers or caches, older entries get evicted based on usage patterns such as Least Recently Used (LRU). To manage these resources effectively under certain conditions one might want to clear them manually using commands like `echo 3 > /proc/sys/vm/drop_caches` which instructs kernel documentation to clean up various types including pagecache, dentries, and inodes without affecting running applications' stability[^1]. However, this operation should only occur during maintenance windows since dropping all cached objects could lead to increased load times when those same items need reloading into main memory shortly afterward. ```bash # Example command to free pagecache, dentries, and inodes. sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches' ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值