Linux缓存机制之页缓存

发表于:2012-2-29 10:24

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:bullbat    来源:51Testing软件测试网采编

  Linux运用一个功能广泛的缓冲和缓存框架来提高系统的速度。缓冲和缓存利用一部分系统物理内存,确保最重要、最常使用的块设备数据在操作时可直接从主内存获取,而无需从低速设备读取。物理内存还用于存储从快设备读取的数据,使得随后对该数据的访问可直接在物理内存进行,而无需从外部设备再次取用。考虑系统中多种因素然后延迟写回在总体上改进了系统的性能。前面分析的部分,例如内存管理的slab缓存是一个内存到内存的缓存,其目地不是加速对低速设备的操作,而是对现有资源进行更简单、更高效的使用。文件系统的Dentry缓存也用于减少对低速块设备的访问,但他无法推广到通用场合,因为他是专门用于处理单一数据类型的。

  内核为块设备提供了两种通用的缓存方案:

  1)页缓存,针对以页为单位的所有操作,并考虑了特定体系结构上的页长度。一个主要的例子是内存映射技术。因为其他类型的文件访问也是基于内核中的这一技术实现的。所以页缓存实际上负责了块设备的大部分缓存工作

  2)块缓存,以块为操作单位。在进行I/O操作时,存取的单位是设备的各个块,而不是整个内存页。尽管页长度对所有文件系统都是相同的,但块长度取决于特定的文件系统或其设置。因而,块缓存必须能够处理不同长度的块。

  目前用于块传输的标准数据结构已经演变为structbio。用这种方式进行块传输更为高效,因为他可以合并同一请求中后续的块,加速处理的进行。在许多场合下,页缓存和块缓存是联合使用的。例如,一个缓存的页在写操作期间可以划分为不同的缓冲区,这样可以在更细的力度下,识别出页被修改的部分。好处在于,在将数据写回时,只需要回写被修改的部分,无需将这个页面传输回底层的块设备。

  页面缓存结构

  1. /*高速缓存的核心数据结构,对块设备的读写操作都放在该结构体里*/  
  2. struct address_space {  
  3.     /*与地址空间所管理的区域之间的关联数据结构之一 
  4.     inode结构指定了后备存储器*/  
  5.     struct inode        *host;      /* owner: inode, block_device */  
  6.     /*与地址空间所管理的区域之间的关联之二 
  7.     ,page_tree列出了地址空间中所有的物理内存页*/  
  8.     struct radix_tree_root  page_tree;  /* radix tree of all pages */  
  9.     spinlock_t      tree_lock;  /* and lock protecting it */  
  10.     /*所有用VM_SHARED属性创建的映射*/  
  11.     unsigned int        i_mmap_writable;/* count VM_SHARED mappings */  
  12.     /*基数根节点,该树包含了与该inode相关的所有 
  13.     普通内存映射。该树的任务在于,支持查找包含了 
  14.     给定区间中至少一页的所有内存区域*/  
  15.     struct prio_tree_root   i_mmap;     /* tree of private and shared mappings */  
  16.     /*包含所有在非线性映射中的页*/  
  17.     struct list_head    i_mmap_nonlinear;/*list VM_NONLINEAR mappings */  
  18.     spinlock_t      i_mmap_lock;    /* protect tree, count, list */  
  19.     unsigned int        truncate_count; /* Cover race condition with truncate */  
  20.     /*缓存页的总数*/  
  21.     unsigned long       nrpages;    /* number of total pages */  
  22.     pgoff_t         writeback_index;/* writeback starts here */  
  23.     const struct address_space_operations *a_ops;   /* methods */  
  24.     /*集主要用于保存映射页所来自的GFP内存区 
  25.     的有关信息*/  
  26.     unsigned long       flags;      /* error bits/gfp mask */  
  27.     /*指向后备存储器结构,该结构包含了与地址空间相关的 
  28.     后备存储器的有关信息,后备存储器是指与地址空间相关 
  29.     的外部设备,用做地址空间中信息的来源。他通常是块设备 
  30.     */  
  31.     struct backing_dev_info *backing_dev_info; /* device readahead, etc */  
  32.     spinlock_t      private_lock;   /* for use by the address_space */  
  33.     /*用于将包含文件系统元数据(通常是间接块)的buffer_head 
  34.     实例彼此连接起来*/  
  35.     struct list_head    private_list;   /* ditto */  
  36.     /*指向相关的地址空间的指针*/  
  37.     struct address_space    *assoc_mapping; /* ditto */  
  38. } __attribute__((aligned(sizeof(long))));

  后备存储信息

  1. struct backing_dev_info {  
  2.     struct list_head bdi_list;  
  3.     struct rcu_head rcu_head;  
  4.     /*最大预读数量,单位为PAGE_CACHE_SIZE*/  
  5.     unsigned long ra_pages; /* max readahead in PAGE_CACHE_SIZE units */  
  6.     /*对该成员,总是使用原子操作,指定了后备存储器的状态*/  
  7.     unsigned long state;    /* Always use atomic bitops on this */  
  8.     /*设备能力*/  
  9.     unsigned int capabilities; /* Device capabilities */  
  10.     congested_fn *congested_fn; /* Function pointer if device is md/dm */  
  11.     void *congested_data;   /* Pointer to aux data for congested func */  
  12.     void (*unplug_io_fn)(struct backing_dev_info *, struct page *);  
  13.     void *unplug_io_data;  
  14.   
  15.     char *name;  
  16.   
  17.     struct percpu_counter bdi_stat[NR_BDI_STAT_ITEMS];  
  18.   
  19.     struct prop_local_percpu completions;  
  20.     int dirty_exceeded;  
  21.   
  22.     unsigned int min_ratio;  
  23.     unsigned int max_ratio, max_prop_frac;  
  24.   
  25.     struct bdi_writeback wb;  /* default writeback info for this bdi */  
  26.     spinlock_t wb_lock;   /* protects update side of wb_list */  
  27.     struct list_head wb_list; /* the flusher threads hanging off this bdi */  
  28.     unsigned long wb_mask;    /* bitmask of registered tasks */  
  29.     unsigned int wb_cnt;      /* number of registered tasks */  
  30.   
  31.     struct list_head work_list;  
  32.   
  33.     struct device *dev;  
  34.   
  35. #ifdef CONFIG_DEBUG_FS  
  36.     struct dentry *debug_dir;  
  37.     struct dentry *debug_stats;  
  38. #endif  
  39. };

31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号