手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux缓存机制之页缓存

[日期:2012-02-27] 来源:Linux社区  作者:bullbat [字体:大 中 小]

内核采用一种通用的地址空间方案,来建立缓存数据与其来源之间的关联。

1)  内存中的页分配到每个地址空间。这些页的内容可以由用户进程或内核本身使用各式各样的方法操作。这些数据表示了缓存中的内容;

2)  后备存储器struct backing_dev_info指定了填充地址空间中页的数据的来源。地址空间关联到处理器的虚拟地址空间,是由处理器在虚拟内存中管理的一个区域到设备device上对应位置之间的一个映射。

如果访问了虚拟内存中的某个位置,该位置没有关联到物理内存页,内核可根据地址空间结构来找到读取数据的来源。

为支持数据传输,每个地址空间都提供了一组操作,以容许地址空间所涉及双方面的交互。

地址空间是内核中最关键的数据结构之一,对该数据结构的管理,已经演变为内核面对的最关键的问题之一。 页缓存的任务在于,获得一些物理内存页,以加速在块设备上按页为单位执行的操作。

内核使用了基数树来管理与一个地址空间相关的所有页,以便尽可能降低开销。对于基数树的理解在这里就不分析了,后面有空的时候再做分析。

地址空间操作

[cpp]
  1. struct address_space_operations {  
  2.     /*将地址空间的一页或多页写回到底层设备 
  3.     这是通过向块层发出一个相应的请求来完成的*/  
  4.     int (*writepage)(struct page *page, struct writeback_control *wbc);  
  5.     /*从后备存储器将一页或多个连续的页读入页帧*/  
  6.     int (*readpage)(struct file *, struct page *);  
  7.     /*对尚未回写到后备存储器的数据进行同步*/  
  8.     void (*sync_page)(struct page *);  
  9.   
  10.     /* Write back some dirty pages from this mapping. */  
  11.     int (*writepages)(struct address_space *, struct writeback_control *);  
  12.   
  13.     /* Set a page dirty.  Return true if this dirtied it */  
  14.     int (*set_page_dirty)(struct page *page);  
  15.   
  16.     int (*readpages)(struct file *filp, struct address_space *mapping,  
  17.             struct list_head *pages, unsigned nr_pages);  
  18.     /*执行由write系统调用触发的写操作*/  
  19.     int (*write_begin)(struct file *, struct address_space *mapping,  
  20.                 loff_t pos, unsigned len, unsigned flags,  
  21.                 struct page **pagep, void **fsdata);  
  22.     int (*write_end)(struct file *, struct address_space *mapping,  
  23.                 loff_t pos, unsigned len, unsigned copied,  
  24.                 struct page *page, void *fsdata);  
  25.   
  26.     /* Unfortunately this kludge is needed for FIBMAP. Don't use it */  
  27.     sector_t (*bmap)(struct address_space *, sector_t);  
  28.     void (*invalidatepage) (struct page *, unsigned long);  
  29.     int (*releasepage) (struct page *, gfp_t);  
  30.     ssize_t (*direct_IO)(int, struct kiocb *, const struct iovec *iov,  
  31.             loff_t offset, unsigned long nr_segs);  
  32.     int (*get_xip_mem)(struct address_space *, pgoff_t, int,  
  33.                         void **, unsigned long *);  
  34.     /* migrate the contents of a page to the specified target */  
  35.     int (*migratepage) (struct address_space *,  
  36.             struct page *, struct page *);  
  37.     int (*launder_page) (struct page *);  
  38.     int (*is_partially_uptodate) (struct page *, read_descriptor_t *,  
  39.                     unsigned long);  
  40.     int (*error_remove_page)(struct address_space *, struct page *);  
  41. };  

页面缓存的实现基于基数树,缓存属于内核中性能要求最苛刻的部分之一,而且广泛用于内核的所有子系统,实现也比较简单。举两个例子,其他的暂时不做分析了。

分配页面用于加入地址空间

[cpp]
  1. /*从伙伴系统中分配页面,页面的标志根据地址空间中的标志进行设置*/  
  2. static inline struct page *page_cache_alloc(struct address_space *x)  
  3. {  
  4.     return __page_cache_alloc(mapping_gfp_mask(x));  
  5. }  

分配完了添加到基数树中

[cpp]
  1. /* 
  2.  * Like add_to_page_cache_locked, but used to add newly allocated pages: 
  3.  * the page is new, so we can just run __set_page_locked() against it. 
  4.  */  
  5. static inline int add_to_page_cache(struct page *page,  
  6.         struct address_space *mapping, pgoff_t offset, gfp_t gfp_mask)  
  7. {  
  8.     int error;  
  9.   
  10.     __set_page_locked(page);  
  11.     /*实际的添加工作*/  
  12.     error = add_to_page_cache_locked(page, mapping, offset, gfp_mask);  
  13.     if (unlikely(error))  
  14.         __clear_page_locked(page);  
  15.     return error;  
  16. }  
[cpp]
  1. /** 
  2.  * add_to_page_cache_locked - add a locked page to the pagecache 
  3.  * @page:   page to add 
  4.  * @mapping:    the page's address_space 
  5.  * @offset: page index 
  6.  * @gfp_mask:   page allocation mode 
  7.  * 
  8.  * This function is used to add a page to the pagecache. It must be locked. 
  9.  * This function does not add the page to the LRU.  The caller must do that. 
  10.  */  
  11. int add_to_page_cache_locked(struct page *page, struct address_space *mapping,  
  12.         pgoff_t offset, gfp_t gfp_mask)  
  13. {  
  14.     int error;  
  15.   
  16.     VM_BUG_ON(!PageLocked(page));  
  17.   
  18.     error = mem_cgroup_cache_charge(page, current->mm,  
  19.                     gfp_mask & GFP_RECLAIM_MASK);  
  20.     if (error)  
  21.         goto out;  
  22.     /*树的相关结构申请*/  
  23.     error = radix_tree_preload(gfp_mask & ~__GFP_HIGHMEM);  
  24.     if (error == 0) {  
  25.         page_cache_get(page);/*使用计数加一*/  
  26.         page->mapping = mapping;  
  27.         page->index = offset;  
  28.       
  29.         spin_lock_irq(&mapping->tree_lock);  
  30.         /*实际的插入操作*/  
  31.         error = radix_tree_insert(&mapping->page_tree, offset, page);  
  32.         if (likely(!error)) {  
  33.             mapping->nrpages++;  
  34.             __inc_zone_page_state(page, NR_FILE_PAGES);  
  35.             if (PageSwapBacked(page))  
  36.                 __inc_zone_page_state(page, NR_SHMEM);  
  37.             spin_unlock_irq(&mapping->tree_lock);  
  38.         } else {  
  39.             page->mapping = NULL;  
  40.             spin_unlock_irq(&mapping->tree_lock);  
  41.             mem_cgroup_uncharge_cache_page(page);  
  42.             page_cache_release(page);  
  43.         }  
  44.         radix_tree_preload_end();  
  45.     } else  
  46.         mem_cgroup_uncharge_cache_page(page);  
  47. out:  
  48.     return error;  
  49. }  
linux
相关资讯       Linux教程 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* dd 发表于 2012/3/1 9:24:41
很不错