Page cache

定义

PageCache 是一个逻辑上的概念,指定是用于缓存disk数据到内存中的内存页。

数据结构

Page cache的核心结构体是**address_space**。

它是连接文件(inode)和内存页面(page)的桥梁 。每个打开的文件都有一个对应的 address_space,它管理该文件在内存中的所有缓存页。

在include/linux/fs.h文件中

cpp 复制代码
// PageCache 的核心数据结构
struct address_space {
    struct inode        *host;     // 所属文件
    struct xarray       i_pages;   // cache page们
    struct rw_semaphore invalidate_lock;
    gfp_t           gfp_mask;
    atomic_t        i_mmap_writable; /* 可写映射计数 */
#ifdef CONFIG_READ_ONLY_THP_FOR_FS
    /* number of thp, only for non-shmem files */
    atomic_t        nr_thps;
#endif
    struct rb_root_cached   i_mmap; /* 私有映射树 */
    struct rw_semaphore i_mmap_rwsem;
    unsigned long       nrpages;    /* 页面总数 */
    pgoff_t         writeback_index;
    const struct address_space_operations *a_ops; /* 操作函数 */
    unsigned long       flags;
    errseq_t        wb_err;
    spinlock_t      private_lock;
    struct list_head    private_list;
    void            *private_data;
} __attribute__((aligned(sizeof(long)))) __randomize_layout;

如何关联起来:

复制代码
一个文件 (inode)
    ↓
一个 address_space
    ↓
管理该文件的所有page cache (通过 i_pages)
cpp 复制代码
struct inode {
    // ...
    struct address_space *i_mapping;  // 文件的主要映射
    struct address_space i_data;      // 文件的地址空间
};
cpp 复制代码
// PageCache 页面在内存中的状态 
struct page { 
    struct address_space *mapping; // 属于哪个 PageCache 
    pgoff_t index; // 在文件中的偏移 
    struct list_head lru; // ← 链接到 LRU 链表 
    // ... 
};
cpp 复制代码
struct task_struct
    ↓ 通过mm_struct
struct mm_struct
    ↓ 通过VMA链表
struct vm_area_struct     // 虚拟内存区域
    ↓ vm_file指向
struct file
    ↓ f_mapping指向
struct address_space      // ← 我们关注的
    ↓ host指向  
struct inode             // 文件inode
    ↓ i_pages指向
struct address_space     // 其实是同一个
    ↓ page_tree包含
struct page              // 物理页面
    ↓ lru指向
LRU 链表管理
    ├── LRU_INACTIVE_FILE 链表
    └── LRU_ACTIVE_FILE 链表

PageCache 页面的生命周期

复制代码
首次访问文件页
    ↓
分配物理页struct page(Buddy System)
    ↓
读入磁盘数据
    ↓
加入 PageCache(address_space)
    ↓
加入 LRU_INACTIVE_FILE
    ↓
再次访问 → 移到 LRU_ACTIVE_FILE
    ↓
内存压力 → 页面回收
    ├── 干净页 → 直接释放
    └── 脏页   → 写回后释放
相关推荐
xlp666hub5 分钟前
【Linux驱动实战】:字符设备之ioctl与mutex全解析
linux·面试
守望时空3328 分钟前
使用NetworkManager替换当前网络管理器
linux·运维
爱网安的monkey brother1 小时前
Linux自用文档
linux
xlq223221 小时前
30.进程池IPC
linux·运维·服务器
nuomigege1 小时前
beagleboneblack刷入官方IOT镜像后无法运行nodered问题的处理
linux·运维·服务器
huaxiu52 小时前
ubuntu下应用打不开
linux·运维·ubuntu
m0_683124792 小时前
Ubuntu服务设置开机自启
linux·运维·ubuntu
BestOrNothing_20152 小时前
(1)双系统中Ubuntu22.04启动盘制作与启动盘恢复全过程
linux·ubuntu·双系统·启动盘制作·启动盘恢复
AI成长日志2 小时前
【实用工具教程】Linux常用命令速查与实战场景:文件操作、进程管理与网络调试高频命令解析
linux·php
落叶花开又一年2 小时前
检验检测机构资质认定远程评审工作程序
linux·运维·服务器