简体   繁体   English

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

[英]What is a good way to test the use of msync on recent Linux kernels?

I am using msync in my application on Linux 2.6 to ensure consistency in the event of a crash.我在 Linux 2.6 上的应用程序中使用 msync 以确保发生崩溃时的一致性。 I need to thoroughly test my usage of msync but the implementation seems to be flushing all the relevant pages for me.我需要彻底测试我对 msync 的使用,但实现似乎正在为我刷新所有相关页面。 Is there a way to prevent automatic flushing of mmap'd pages onto the disk to expose erroneous usage of msync on my part?有没有办法防止将mmap'd页面自动刷新到磁盘上以暴露我对msync的错误使用?

With apologies to @samold, "swappiness" has nothing to do with this.向@samold 道歉,“swappiness”与此无关。 Swappiness just affects how the kernel trades off swapping dirty anonymous pages versus evicting page cache pages when memory is low. Swappiness 只会影响 kernel 在 memory 较低时如何权衡交换脏匿名页面与逐出页面缓存页面。

You need to play with the Linux VM tunables controlling the pdflush task .您需要使用Linux VM 可调参数来控制 pdflush 任务 For starters, I would suggest:对于初学者,我建议:

sysctl -w vm.dirty_writeback_centisecs=360000

By default, vm.dirty_writeback_centisecs is 3000, which means the kernel will consider any dirty page older than 30 seconds to be "too old" and try to flush it to disk.默认情况下, vm.dirty_writeback_centisecs为 3000,这意味着 kernel 将认为任何超过 30 秒的脏页“太旧”并尝试将其刷新到磁盘。 By cranking it up to 1 hour, you should be able to avoid flushing dirty pages to disk at all, at least during a short test.通过将其延长至 1 小时,您应该能够完全避免将脏页刷新到磁盘,至少在短暂的测试期间是这样。 Except...除了...

sysctl -w vm.dirty_background_ratio=80

By default, vm.dirty_background_ratio is 10, as in 10 percent.默认情况下, vm.dirty_background_ratio为 10,即 10%。 That means when more than 10 percent of physical memory is occupied by dirty pages, the kernel will think it needs to get busy flushing something to disk, even if it is younger than dirty_writeback_centisecs .这意味着当超过 10% 的物理 memory 被脏页占用时,kernel 会认为它需要忙于将某些内容刷新到磁盘,即使它比dirty_writeback_centisecs更年轻。 Crank this one up to 80 or 90 and the kernel should be willing to tolerate most of RAM being occupied by dirty pages.将这个提升到 80 或 90,kernel 应该愿意容忍大部分 RAM 被脏页占用。 (I would not set this too high, though, since I bet nobody ever does that and it might trigger strange behavior.) Except... (不过,我不会把这个设置太高,因为我敢打赌从来没有人这样做过,它可能会引发奇怪的行为。)除了......

sysctl -w vm.dirty_ratio=90

By default, vm.dirty_ratio is 40, which means once 40% of RAM is dirty pages, processes attempting to create more dirty pages will block until something gets evicted.默认情况下, vm.dirty_ratio为 40,这意味着一旦 40% 的 RAM 是脏页,尝试创建更多脏页的进程将阻塞,直到某些东西被驱逐。 Always make this one bigger than dirty_background_ratio .始终使这个大于dirty_background_ratio Hm, come to think of it, set this one before that one, just to make sure this one is always larger.嗯,想一想,把这个放在那个之前,只是为了确保这个总是更大。

That's it for my initial suggestions.这就是我最初的建议。 It is possible that your kernel will start evicting pages anyway;无论如何,您的 kernel 可能会开始驱逐页面; the Linux VM is a mysterious beast and seems to get tweaked on every release. Linux VM 是一只神秘的野兽,似乎在每个版本中都会进行调整。 Hopefully this provides a starting point.希望这提供了一个起点。

See Documentation/sysctl/vm.txt in the kernel sources for a complete list of VM tunables.有关 VM 可调参数的完整列表,请参阅 kernel 源中的Documentation/sysctl/vm.txt (Preferably refer to the documentation for the kernel version you are actually using.) (最好参考您实际使用的 kernel 版本的文档。)

Finally, use the /proc/PID/pagemap interface to see which pages are actually dirty at any time.最后,使用/proc/PID/pagemap 接口随时查看哪些页面实际上是脏的。

A few guesses:几个猜测:

You can fiddle with the swappiness of the system via the /proc/sys/vm/swappiness tunable:您可以通过/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.

(Wow. proc(5) needs to be run through a spell-checker.) (哇proc(5)需要通过拼写检查器运行。)

If setting the swappiness to 0 doesn't do the trick, there are more tunable knobs;如果将swappiness设置为0不起作用,那么还有更多可调旋钮; the Documentation/laptops/laptop-mode.txt file contains a good description of the laptop_mode script's behaviors: 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.

You might wish to take these numbers to their extremes;您可能希望将这些数字发挥到极致; if you're really curious about your application's behavior, it sounds reasonable to set these values quite high and see how long a sync(1) command takes when it's all done.如果您真的对您的应用程序的行为感到好奇,那么将这些值设置得相当高并查看sync(1)命令完成后需要多长时间听起来是合理的。 But these are system-wide tunables -- other applications may not be so happy.但这些是系统范围的可调参数——其他应用程序可能不那么高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mmap、msync 和 linux 进程终止 - mmap, msync and linux process termination 新Linux内核中的内存隔离,还是什么? - Memory Isolation in new Linux Kernels, or what? 在Linux上测试网络代码的最准确方法是什么? - What is the most accurate way to test the networking code on Linux? 使用C编程语言,使多核RedHat Linux处理器在测试应用程序中仅使用一个内核的最佳方法是什么? - Using the C programming language, what is the best way to make a multicore RedHat Linux processor, use only one core in a test application? 这是测试缓冲区溢出的好方法吗? - Is this a good way to test for a buffer overflow? msync 中标志 MS_INVALIDATE 的确切含义是什么? - What's the exact meaning of the flag MS_INVALIDATE in msync? 关于Linux上的共享库有哪些好的做法? - What are good practices regarding shared libraries on Linux? 使用stat()函数测试DIRENT是目录还是文件的正确方法是什么? - What is the correct way to use the stat() function to test if a DIRENT is a directory or a file? 保持期限的好系统测试是什么? - What's a good system test for keeping a deadline? 有没有办法在运行Linux或Windows的台式计算机上“测试”C中“volatile”关键字的使用? - Is there a way to “test” the use of “volatile” keyword in C on a Desktop computer running Linux or Windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM