解决Linux系统下磁盘IO紧张的一种方法

最近学习Neo4j图形数据库,遇到一个磁盘IO紧张的问题,经过网友的帮助,问题解决了,现在总结如下:

linux主要通过以下几个参数来控制上面的行为: 

1、vm.dirty_background_ratio这个参数指定了当文件系统缓存脏页数量达到系统内存百分之多少时(如5%)就会触发pdflush/flush/kdmflush等后台回写进程运行,将一定缓存的脏页异步地刷入外存;控制内存占用的阈值,这个是一个比例,但计算起来很折腾,是机器上free(包括cached)的内存的比例,而不是物理机,所以如果想更准确的控制的话,可以用下面的参数。 

2、vm.dirty_background_bytes控制内存占用的阈值,但是具体的数值。 

3、vm.dirty_ratio这个参数则指定了当文件系统缓存脏页数量达到系统内存百分之多少时(如10%),系统不得不开始处理缓存脏页(因为此时脏页数量已经比较多,为了避免数据丢失需要将一定脏页刷入外存);在此过程中很多应用进程可能会因为系统转而处理文件IO而阻塞。控制内存最多占用的比例,这个是物理内存,如果到达了这个比例,将会阻塞住所有的写动作,先把内存中所有的数据刷入磁盘,可以看到这个参数很关键,设置不好的话会导致应用的性能疯狂下降。 

4、vm.dirty_bytes: 控制内存最多占用的阈值,具体的字节数。 

5、vm.dirty_writeback_centisecs: 控制每隔多久把内存里的数据往磁盘里刷,默认是5s。 

6、vm.dirty_expire_centisecs控制在往磁盘刷的时候,刷放在内存超过多久的数据,默认是30s,按照上面的默认值每隔5s刷一次,所以其实就是每次刷全部的数据。

在这个机制的基础上,如物理内存是比较充足的,那么其实可以在搞活动等的时候临时调整下参数,给dirty这块区域更多的内存,同时将writeback的时间间隔拉长,如果能够做到空闲的物理内存支撑过搞活动的高峰时间那就完美了,那样的话在那个时候即使写文件动作很多,性能也是相当高的,这个方法需要值得注意的是,当dirty区域内存很大,而且很久才writeback一次,所以在writeback的那段时间会比较耗io,但通常是还好的,因为那个时候很多都会是顺序写。

neo4j的帮助手册中是这样说的:

As we have 4GB RAM on the machine and memory map a 1GB file that does not need its content
written to disk (until we tell it to do so because of logical log rotation or Neo4j kernel shutdown) it should
be possible to do endless random writes to that memory with high throughput. All we have to do is to
tell the Linux kernel to stop trying to be smart. Edit the /etc/sysctl.conf (need root access) and add the
following lines:
vm.dirty_background_ratio = 50
vm.dirty_ratio = 80

Then (as root) execute:
sysctl -p
The "vm.dirty_background_ratio" tells at what ratio should the linux kernel start the background task
of writing out dirty pages. We increased this from the default 10% to 50% and that should cover the
1GB memory mapped file. The "vm.dirty_ratio" tells at what ratio all IO writes become synchronous,
meaning that we can not do IO calls without waiting for the underlying device to complete them (which
is something you never want to happen).



你可能感兴趣的:(Linux,Neo4j,IO)