在 Linux 中禁用磁盘缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20215516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disabling disk cache in linux
提问by Flaviops
In a class project my teacher told us to make some code evaluations (C language) and to do so we need to disable the disk caching during the tests.
在一个课堂项目中,我的老师告诉我们进行一些代码评估(C 语言),为此我们需要在测试期间禁用磁盘缓存。
Currently I'm using Ubuntu 12.04, how can I do this?
目前我使用的是 Ubuntu 12.04,我该怎么做?
Thanks.
谢谢。
采纳答案by nio
You need root access to do this. You can run hdparm -W 0 /dev/sda
command to disable write caching, where you have to replace /dev/sda
with device for your drive:
您需要 root 访问权限才能执行此操作。您可以运行hdparm -W 0 /dev/sda
命令来禁用写入缓存,您必须在其中替换/dev/sda
驱动器的设备:
#include <stdlib.h>
...
system("hdparm -W 0 /dev/sda1");
You can also selectively disable write caching to individual partitions like this: hdparm -W 0 /dev/sda1
.
您也可以选择禁用写入缓存到各个分区是这样的:hdparm -W 0 /dev/sda1
。
To reenable caching again just use the -W 1
argument.
要再次重新启用缓存,只需使用该-W 1
参数。
回答by user3036159
echo 100 > /proc/sys/vm/dirty_expire_centisecs
echo 100 > /proc/sys/vm/dirty_expire_centisecs
echo 100 > /proc/sys/vm/dirty_writeback_centisecs
echo 100 > /proc/sys/vm/dirty_writeback_centisecs
this reduce to 1 second the flush from the RAM to disk
这将从 RAM 到磁盘的刷新减少到 1 秒
you can test with 0
你可以用 0 测试
or :
或者 :
echo 1 > /proc/sys/vm/drop_caches
echo 1 > /proc/sys/vm/drop_caches
to flush all RAM to disk
将所有 RAM 刷新到磁盘
回答by Aaron Digulla
I think you need to tell your teacher that it's no longer 1984. Modern computer systems have dozens of caches and there is no good way to disable them all:
我想你需要告诉你的老师,现在已经不是 1984 年了。现代计算机系统有几十个缓存,没有什么好方法可以将它们全部禁用:
- Cache on the hard disk itself
- Caches in the I/O hardware subsystem
- Caches in the virtual file system
- Several levels of caches in the CPU
- 缓存在硬盘本身上
- I/O 硬件子系统中的缓存
- 虚拟文件系统中的缓存
- CPU中的几级缓存
So the question is what you want to test and which caches you want to disable for this.
所以问题是你想测试什么以及你想为此禁用哪些缓存。