我在Linux 2.6的应用程序中使用msync来确保发生崩溃时的一致性。 我需要彻底testing我的msync的使用,但实施似乎是冲洗所有相关页面为我。 有没有办法阻止自动刷新mmap'd页到磁盘上暴露我的部分msync的错误使用?
向@samold道歉,“swappiness”与此无关。 Swappiness只影响内核如何交换掉脏的匿名页面,而不是在内存不足时驱逐页面缓存页面。
您需要使用控制pdflush任务的Linux VM可调参数 。 对于初学者,我会建议:
sysctl -w vm.dirty_writeback_centisecs=360000
默认情况下, vm.dirty_writeback_centisecs
是3000,这意味着内核会认为超过30秒的脏页面“太旧”,并尝试将其刷新到磁盘。 通过启动最多1小时,至少在短暂的测试过程中,您应该能够避免将脏页面清理到磁盘。 除…
sysctl -w vm.dirty_background_ratio=80
默认情况下, vm.dirty_background_ratio
是10,如10%。 这意味着,当超过10%的物理内存被脏页占用时,内核会认为它需要忙于刷新某些内容到磁盘,即使它比dirty_writeback_centisecs
年轻。 将这个值调高到80或90,内核应该愿意容忍脏页面占用的大部分内存。 (我不想把它设得太高,因为我敢打赌,没有人会这样做,这可能会引发奇怪的行为。
sysctl -w vm.dirty_ratio=90
默认情况下, vm.dirty_ratio
是40,这意味着一旦有40%的RAM是脏页面,尝试创建更多脏页面的进程将被阻塞,直到被驱逐。 总是让这个比dirty_background_ratio
。 嗯,想一想,把它放在那个之前,只是为了确保这个一直是大的。
这是我最初的建议。 无论如何,你的内核可能会开始驱逐页面。 Linux虚拟机是一个神秘的野兽,似乎在每个版本上都做了调整。 希望这提供了一个起点。
有关VM可调参数的完整列表,请参阅内核源文件中的Documentation / sysctl / vm.txt 。 (最好参考您实际使用的内核版本的文档。)
最后,使用/ proc / PID / pagemap界面随时查看哪些页面实际上是脏的。
几个猜测:
您可以通过/proc/sys/vm/swappiness
可调参数来调整系统的/proc/sys/vm/swappiness
:
/proc/sys/vm/swappiness The value in this file controls how aggressively the kernel will swap memory pages. Higher values increase agressiveness, lower values descrease aggressiveness. The default value is 60.
(哇, proc(5)
需要通过拼写检查)。
如果将swappiness
设置为0
并不能解决问题,那么还有更多的可调节旋钮。 Documentation/laptops/laptop-mode.txt
文件包含了laptop_mode
脚本行为的一个很好的描述:
To increase the effectiveness of the laptop_mode strategy, the laptop_mode control script increases dirty_expire_centisecs and dirty_writeback_centisecs in /proc/sys/vm to about 10 minutes (by default), which means that pages that are dirtied are not forced to be written to disk as often. The control script also changes the dirty background ratio, so that background writeback of dirty pages is not done anymore. Combined with a higher commit value (also 10 minutes) for ext3 or ReiserFS filesystems (also done automatically by the control script), this results in concentration of disk activity in a small time interval which occurs only once every 10 minutes, or whenever the disk is forced to spin up by a cache miss. The disk can then be spun down in the periods of inactivity.
你可能希望把这些数字推到极限。 如果您对应用程序的行为非常好奇,将这些值设置得相当高并且看看sync(1)
命令完成后需要多长时间才是合理的。 但是这些是全系统可调谐的 – 其他应用可能不会那么高兴。