Transfer: Linux file system IO performance optimization

For LINUX SA, server performance needs our special attention, including the optimization of CPU, IO, memory and other systems has become very important, here is a very good article on IO optimization for your reference and learning :

1. For information about page caching, you can use

cat /proc/meminfo 
to see. Cached refers to the memory size (diskcache-SwapCache) used for pagecache. The value of Dirty increases as cache pages are written.
Once the cache page starts to be written to disk, the value of Writeback will increase until the end of the write.   Linux uses the pdflush process to write data from the cache page to the hard disk to see how many pdflush processes there are
cat /proc/sys/vm/nr_pdflush_threads The behavior of pdflush is controlled by the parameters in
/proc/sys/vm/proc/sys/vm/ dirty_writeback_centisecs (default 500): 
1/100 seconds, how long to wake up pdflush to write cache page data to hard disk. Default 5 seconds to wake up 2 (more) threads.
If the wrteback time is longer than the dirty_writeback_centisecs time, there may be a problem.

 

The first thing pdflush does is read
/proc/sys/vm/dirty_expire_centiseconds (default 3000)
for 1/100th of a second. The expiration time (old data) of the data in the cache page will be written to the hard disk in the next cycle. The default 30 seconds is a long time.

The second thing is to determine whether the memory has reached the limit to be written to the hard disk, which is determined by the parameters:
/proc/sys/vm/dirty_background_ratio (default 10)
percentage value, and the maximum value of the expired page cache (dirty page cache) is retained. Based on the value of MmeFree+Cached-Mapped

pdflush writes to the hard disk to see two parameters:
1 Whether the data in the page cache exceeds 30 seconds, if so, mark it as a dirty page cache;
2 Whether the dirty page cache reaches 10% of the working memory;

The following parameters also affect the maximum percentage of pdflush
/proc/sys/vm/dirty_ratio (default 40)
total memory, the total amount of the largest dirty page cache the system can have. Above this value, enable pdflush to write to the hard disk. If the cache grows faster than pdflush, then the entire system encounters an I/O bottleneck at 40%, and all I/O must wait for the cache to be loaded into the hard disk by pdflush before restarting.

For systems with high write operations
dirty_background_ratio: The main tuning parameter. Lower this value if you need to keep the cache persistent rather than writing a large amount of writes to disk all at once.
dirty_ratio: The second adjustment parameter.

Swapping parameter
/proc/sys/vm/swappiness
By default, Linux tends to map from physical memory to hard disk cache, keeping the hard disk cache as large as possible. Unused page cache will be put into the swap area.
If the value is 0, swapping
100 will be avoided, and swapping will be used as much as possible. Using
less swapping will increase the response speed of the program; using more swapping will improve the usability of the system.

如果有大量的写操作,为避免I/O的长时间等待,可以设置:
$ echo 5 > /proc/sys/vm/dirty_background_ratio
$ echo 10 > /proc/sys/vm/dirty_ratio

文件系统数据缓冲需要频繁的内存分配。加大保留内存的值能提升系统速度和稳定。小于8G的内存,保留内存为64M,大于8G的设置为256M
$ echo 65536 > /proc/sys/vm/min_free_kbytes

I/O 调度器
cat /sys/block/[disk]/queue/scheduler

4中调度算法
noop anticipatory deadline [cfq] 
deadline :    deadline 算法保证对既定的IO请求以最小的延迟时间。
anticipatory:    有个IO发生后,如果又有进程请求IO,则产生一个默认6ms猜测时间,猜测下一个进程请求IO是干什么。这对于随机读取会造成较大的延时。
对数据库应用很糟糕,而对于Web Server等则会表现不错。
cfq:        对每个进程维护一个IO队列,各个进程发来的IO请求会被cfq以轮循方式处理,对每一个IO请求都是公平。适合离散读的应用。
noop:        对所有IO请求都用FIFO队列形式处理。默认IO不会存在性能问题。

改变调度器
$ echo deadline > /sys/block/sdX/queue/scheduler
对于数据库服务器,deadline算法是推荐的。

提高调度器请求队列的
$ echo 4096 > /sys/block/sdX/queue/nr_requests

有大量的读请求,默认的请求队列应付不过来,可以提高这个值。缺点是要牺牲一定的内存。
为了增加连续读取的吞吐量,可以增加预读数据量。预读的实际值是自适应的,所以使用一个较高的值,不会降低小型随机存取的性能。
$ echo 4096 > /sys/block/sdX/queue/read_ahead_kb
如果LINUX判断一个进程在顺序读取文件,那么它会提前读取进程所需文件的数据,放在缓存中。

服务器遇到磁盘写活动高峰,导致请求处理延迟非常大(超过3秒)。通过调整内核参数,将写活动的高峰分布成频繁的多次写,每次写入的数据比较少。这样可以把尖峰的写操作削平成多次写操作。以这种方式执行的效率比较低,因为内核不太有机会组合写操作。但对于繁忙的服务器,写操作将更一致地进行,并将极大地改进交互式性能。

<strong>/proc/sys/vm/dirty_ratio </strong>

控制文件系统的写缓冲区的大小,单位是百分比,表示占系统内存的百分比,表示当写缓冲使用到系统内存多少的时候,开始向磁盘写出数据。增大之会使用更多系统内存用于磁盘写缓冲,也可以极大提高系统的写性能。但是,当你需要持续、恒定的写入场合时,应该降低其数值。

<strong>/proc/sys/vm/dirty_background_ratio</strong>

控制文件系统的pdflush进程,在何时刷新磁盘。单位是百分比,表示系统内存的百分比,pdflush用于将内存中的内容和文件系统进行同步,比如说,当一个文件在内存中进行修改,pdflush负责将它写回硬盘.每当内存中的垃圾页(dirty page)超过10%的时候,pdflush就会将这些页面备份回硬盘.增大之会使用更多系统内存用于磁盘写缓冲,也可以极大提高系统的写性能。但是,当你需要持续、恒定的写入场合时,应该降低其数值:

<strong>/proc/sys/vm/dirty_writeback_centisecs</strong>

控制内核的脏数据刷新进程pdflush的运行间隔。单位是 1/100 秒。缺省数值是500,也就是 5 秒。如果你的系统是持续地写入动作,那么实际上还是降低这个数值比较好,这样可以把尖峰的写操作削平成多次写操作。
如果你的系统是短期地尖峰式的写操作,并且写入数据不大(几十M/次)且内存有比较多富裕,那么应该增大此数值。
该参数的设置应该小于dirty_expire_centisecs,但也不能太小,太小I/O太频繁,反而
使系统性能下降。具体可能需要在生产环境上测试。据说1:6 (dirty_expire_centisecs  : dirty_writeback_centisecs )的比例比较好。

<strong>/proc/sys/vm/dirty_expire_centisecs</strong>

声明Linux内核写缓冲区里面的数据多“旧”了之后,pdflush进程就开始考虑写到磁盘中去。单位是 1/100秒。缺省是 30000,也就是 30 秒的数据就算旧了,将会刷新磁盘。对于特别重载的写操作来说,这个值适当缩小也是好的,但也不能缩小太多,因为缩小太多也会导致IO提高太快。
当然,如果你的系统内存比较大,并且写入模式是间歇式的,并且每次写入的数据不大(比如几十M),那么这个值还是大些的好。

<strong>/proc/sys/vm/vfs_cache_pressure</strong>

表示内核回收用于directory和inode   cache内存的倾向;缺省值100表示内核将根据pagecache和swapcache,把directory和inode   cache保持在一个合理的百分比;降低该值低于100,将导致内核倾向于保留directory和inode   cache;增加该值超过100,将导致内核倾向于回收directory和inode   cache

<strong>/proc/sys/vm/min_free_kbytes</strong>

表示强制Linux   VM最低保留多少空闲内存(Kbytes)。
缺省设置:724(512M物理内存)

<strong>/proc/sys/vm/nr_pdflush_threads</strong>

表示当前正在运行的pdflush进程数量,在I/O负载高的情况下,内核会自动增加更多的pdflush进程。

<strong>/proc/sys/vm/overcommit_memory</strong>

指定了内核针对内存分配的策略,其值可以是0、1、2。

0,   表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。

1,   表示内核允许分配所有的物理内存,而不管当前的内存状态如何。

2,   表示内核允许分配超过所有物理内存和交换空间总和的内存(参照overcommit_ratio)。

缺省设置:0

<strong>/proc/sys/vm/overcommit_ratio</strong>

如果overcommit_memory=2,可以过载内存的百分比,通过以下公式来计算系统整体可用内存。系统可分配内存=交换空间+物理内存*overcommit_ratio/100
缺省设置:50(%)

<strong>/proc/sys/vm/page-cluster</strong>

表示在写一次到swap区的时候写入的页面数量,0表示1页,1表示2页,2表示4页。
缺省设置:3(2的3次方,8页)

<strong>/proc/sys/vm/swapiness</strong>

表示系统进行交换行为的程度,数值(0-100)越高,越可能发生磁盘交换。

更改:
/etc/sysctl.conf

<strong>vm.dirty_ratio = 40</strong>

sysctl -p

查看:

<strong>find /proc/sys/vm -name dirty* -print | while read name; do echo $name ;cat ${<strong>name</strong> </strong>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326607311&siteId=291194637