- 论坛徽章:
- 0
|
页高速缓存——一种主要的磁盘缓存,用于减少对磁盘的I/O操作
即:通过把磁盘中的数据缓存到物理内存中,把对磁盘的访问变为对物理内存的访问。
15.1 页高速缓存
页高速缓存缓存的是页面,包含最近被访问过的文件的全部页面。
一个物理页可能由多个不连续的物理磁盘块组成 => 使用address_space结构体描述页高速缓存中的页面
struct address_space {
struct inode *host; /* owning inode */
struct radix_tree_root page_tree; /* radix tree of all pages */
spinlock_t tree_lock; /* page_tree lock */
unsigned int i_mmap_writable; /* VM_SHARED ma count */
struct prio_tree_root i_mmap; /* list of all mappings */
struct list_head i_mmap_nonlinear; /* VM_NONLINEAR ma list */
spinlock_t i_mmap_lock; /* i_mmap lock */
atomic_t truncate_count; /* truncate re count */
unsigned long nrpages; /* total number of pages */
pgoff_t writeback_index; /* writeback start offset */
struct address_space_operations *a_ops; /* operations table 操作函数表*/
unsigned long flags; /* gfp_mask and error flags */
struct backing_dev_info *backing_dev_info; /* read-ahead information */
spinlock_t private_lock; /* private lock */
struct list_head private_list; /* private list */
struct address_space *assoc_mapping; /* associated buffers */
};
struct address_space_operations {
int (*writepage)(struct page *, struct writeback_control *);
int (*readpage) (struct file *, struct page *);
int (*sync_page) (struct page *);
int (*writepages) (struct address_space *, struct writeback_control *);
int (*set_page_dirty) (struct page *);
int (*readpages) (struct file *, struct address_space *,
struct list_head *, unsigned);
int (*prepare_write) (struct file *, struct page *, unsigned, unsigned);
int (*commit_write) (struct file *, struct page *, unsigned, unsigned);
sector_t (*bmap)(struct address_space *, sector_t);
int (*invalidatepage) (struct page *, unsigned long);
int (*releasepage) (struct page *, int);
int (*direct_IO) (int, struct kiocb *, const struct iovec *,
loff_t, unsigned long);
};
15.2 基树
页高速缓存通过address_space对象和一个偏移量进行搜索。
每个address_space都有唯一的基树(radix tree),保存在page_tree结构体中。
基树是个二叉树,只要指定文件偏移量,就能在基树迅速搜索到希望的数据。
15.3 缓冲区高速缓存
2.2及更早的内核中含有页高速缓存(缓存页)和缓冲区高速缓存(缓存缓冲);
2.4内核开始统一了这两种缓存,目前只有页高速缓存。
15.4 pdflush后台例程
脏数据——页高速缓存中的数据比后台存储的数据更新
在内存中累积起来的脏页最终必须写回磁盘:
(1) 空闲内存低于一个特定的阈值dirty_background_ratio,调用wakeup_bdflush()唤醒一个pdflush线程,pdflush线程调用background_writeout()将脏页写回磁盘,直到指定的最小数目的页被写到磁盘且内存空闲数超过阈值dirty_background_ratio——减轻内存不足的压力;
(2) 脏页在内存中驻留时间超过一个特定的阈值:系统启动时,内核初始化一个定时器,让它周期性唤醒pdflush线程,随后运行wb_kupdate()函数,把驻留时间超过百分之dirty_expire_ centisecs秒的脏页写回。
pdflush多线程:
避免拥堵。如果一个已存在的pdflush线程已持续工作1秒以上,内核就会创建一个新的pdflush线程,线程数量不能超过MAX_PDFLUSH_THREADS;相反,如果一个pdflush线程睡眠超过1秒,内核就会终止该线程,线程数量不得少于MIN_PDFLUSH_THREADS。线程数量取决于页回写数量和拥塞情况,动态调整。
pdflush设置:
Variable
Description
dirty_background_ratio
As a percentage of total memory, the number of pages at which the pdflush tHReads will begin writeback of dirty data.
dirty_expire_centisecs
In hundredths of a second, how old data must be to be written out next time a pdflush thread wakes to perform periodic writeback.
dirty_ratio
As a percentage of total memory, the number of pages a process generates before it begins writeback of dirty data.
dirty_writeback_centisecs
In hundredths of a second, how often the pdflush threads should wake up to write data back out to disk.
laptop_mode
A Boolean value controlling laptop mode. See the following section.
Laptop Mode膝上型电脑模式
——将硬盘转动的机械行为最小化,云村硬盘尽可能长时间停滞,从而延长电池供电时间。
/proc/sys/vm/laptop_mode文件进行配置,0/关闭,1/开启
dirty_background_ratio和dirty_expire_centisecs值很大。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/85048/showart_1904514.html |
|