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
    ↓
内存压力 → 页面回收
    ├── 干净页 → 直接释放
    └── 脏页   → 写回后释放
相关推荐
mzhan0172 小时前
[晕事]今天做了件晕事98,把openssl-libs 强制删掉了
linux·网络·晕事·openssl-libs
知识分享小能手2 小时前
Ubuntu入门学习教程,从入门到精通, Ubuntu 22.04 的软件包管理 —— 全面详解(9)
linux·学习·ubuntu
爱学大树锯2 小时前
421 · 简化路径
linux·运维·服务器
wadesir2 小时前
Debian集群容量规划(从零开始掌握Linux服务器集群资源评估与优化)
linux·服务器·debian
GHL2842710902 小时前
redis编译调试(linux)
linux·数据库·redis
食咗未2 小时前
Linux USB HOST EXTERNAL USB TO ETH ADAPTER
linux·网络·驱动开发
黑猫学长呀2 小时前
【嵌入式系统科普】第2篇:什么是uboot/kernel/根文件系统/BootLoader/操作系统/bios/设备树
linux·arm开发·单片机·嵌入式硬件·arm
文弱书生6562 小时前
7-Robot类大体框架
linux·单片机·嵌入式硬件
jerryinwuhan2 小时前
linux_shell_1226_1
linux