页面缓存(page cache)也叫File Cache,使用页面缓存是为了提高磁盘对文件的访问速度。顾名思义,“页面”是物理内存的概念,因此page cache是以物理页为单位缓存(不存在虚拟内存)。

linux一般会利用空闲的内存进File Cache,只有接受到内存申请时,才会清理页面缓存,因此Linux系统下很少空闲内存,这样做是为了提高文件访问效率。

页面缓存基于内存管理系统,同时又和文件系统打交道,是两者之间的一个重要纽带。应用层读取文件的方法有两个:mmap和read/write。

mmap第一次读取缓存时,因为还没有分配物理内存,因此会产生缺页错误,不过,在缺页错误里会通过handel_pte_fault()调用回调函数filemap_fault(),如下面代码(mm/filemap.c)。

int generic_file_mmap(struct file * file, struct vm_area_struct * vma)
{
        struct address_space *mapping = file->f_mapping;

        if (!mapping->a_ops->readpage)
                return -ENOEXEC;
        file_accessed(file);
        vma->vm_ops = &generic_file_vm_ops;
        vma->vm_flags |= VM_CAN_NONLINEAR;
        return 0;
}
const struct vm_operations_struct generic_file_vm_ops = {
        .fault          = filemap_fault,
};

linux缺页处理笔记如下

http://blog.csdn.net/xzongyuan/article/details/20365139


页面缓存管理

页面缓存使用结构struct address_space来描述。在include/linux/fs.h下定义。通常一个page cache对应一个inode文件节点,页面缓存的host域记录这个页面缓存所属的文件,对应的,inode结构体中有i_mapping 记录页面缓存。如果是交换页,host为null。

struct address_space {
	struct inode		*host;		/* owner: inode, block_device */
	struct radix_tree_root	page_tree;	/* radix tree of all pages */
	spinlock_t		tree_lock;	/* and lock protecting it */
	unsigned int		i_mmap_writable;/* count VM_SHARED mappings */
	struct prio_tree_root	i_mmap;		/* tree of private and shared mappings */
	struct list_head	i_mmap_nonlinear;/*list VM_NONLINEAR mappings */
	spinlock_t		i_mmap_lock;	/* protect tree, count, list */
	unsigned int		truncate_count;	/* Cover race condition with truncate */
	unsigned long		nrpages;	/* number of total pages */
	pgoff_t			writeback_index;/* writeback starts here */
	const struct address_space_operations *a_ops;	/* methods */
	unsigned long		flags;		/* error bits/gfp mask */
	struct backing_dev_info *backing_dev_info; /* device readahead, etc */
	spinlock_t		private_lock;	/* for use by the address_space */
	struct list_head	private_list;	/* ditto */
	struct address_space	*assoc_mapping;	/* ditto */
	struct mutex		unmap_mutex;    /* to protect unmapping */
} __attribute__((aligned(sizeof(long))));
	

linux中的文件访问都是通过页面缓存实现,因此需要一个高效的方法(radix tree)去访问缓存,内核提供对应的函数find_get_page(),它可以速查一个文件地址(inode)在文件缓存中的页。

struct page *find_get_page(struct address_space *mapping, pgoff_t offset)
{
	void **pagep;
	struct page *page;

	rcu_read_lock();
repeat:
	page = NULL;
	pagep = radix_tree_lookup_slot(&mapping->page_tree, offset);
	if (pagep) {
		page = radix_tree_deref_slot(pagep);
		if (unlikely(!page || page == RADIX_TREE_RETRY))
			goto repeat;

		if (!page_cache_get_speculative(page))
			goto repeat;

		/*
		 * Has the page moved?
		 * This is part of the lockless pagecache protocol. See
		 * include/linux/pagemap.h for details.
		 */
		if (unlikely(page != *pagep)) {
			page_cache_release(page);
			goto repeat;
		}
	}
	rcu_read_unlock();

	return page;
}



Logo

更多推荐