在最近的Linux内核上测试msync的使用有什么好方法?

时间:2011-07-09 02:42:01

标签: c linux kernel mmap

我在Linux 2.6上的应用程序中使用msync来确保崩溃时的一致性。我需要彻底测试我对msync的使用,但实现似乎正在为我刷新所有相关页面。有没有办法阻止mmap'd页面自动刷新到磁盘上以暴露我的错误使用msync?

2 个答案:

答案 0 :(得分:7)

向@samold道歉,“swappiness”与此无关。 Swappiness只影响内核交换掉脏脏匿名页面与内存不足时驱逐页面缓存页面的方式。

您需要使用Linux VM tunables controlling the pdflush task。对于初学者,我建议:

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,内核应该愿意容忍脏页占用的大部分RAM。 (但我不会将此太高设置为高,因为我打赌没有人会这样做,这可能会引发奇怪的行为。)除了......

sysctl -w vm.dirty_ratio=90

默认情况下,vm.dirty_ratio为40,这意味着一旦40%的RAM是脏页,尝试创建更多脏页的进程将阻止直到某些东西被驱逐。总是使这个比dirty_background_ratio大。嗯,来想一想,在那个之前设置这个,只是为了确保这一个总是更大。

这是我最初的建议。无论如何,你的内核可能会开始驱逐页面; Linux VM是一个神秘的野兽,似乎在每个版本都进行了调整。希望这提供了一个起点。

请参阅内核源代码中的Documentation/sysctl/vm.txt以获取VM可调参数的完整列表。 (最好参考您实际使用的内核版本的文档。)

最后,使用/proc/PID/pagemap interface随时查看哪些页面实际上是脏的。

答案 1 :(得分:0)

一些猜测:

您可以通过/proc/sys/vm/swappiness可调参数调整系统的 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)命令完成时需要多长时间是合理的。但这些是系统范围的可调参数 - 其他应用程序可能不那么高兴。