11

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.

Currently I'm using Ubuntu 12.04, how can I do this?

Thanks.

1
  • If you're working on a separate partition you could mount that with -o sync Commented Apr 22, 2018 at 16:35

3 Answers 3

21

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:

#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.

To reenable caching again just use the -W 1 argument.

man hdparm, man system

Sign up to request clarification or add additional context in comments.

3 Comments

You can also run the command from the shell, without system(): sudo hdparm -W 0 /dev/sda1
Note: This only disables the write cache. How about the read caches?
@AaronDigulla, maybe that is what hdparm’s “-f” option is for: “Sync and flush the buffer cache for the device on exit.” There is also an “-F” option to “Flush the on-drive write cache buffer”; the emphasis on “write” for “-F” suggests that “-f” flushes read and write cache.
6

echo 100 > /proc/sys/vm/dirty_expire_centisecs

echo 100 > /proc/sys/vm/dirty_writeback_centisecs

this reduce to 1 second the flush from the RAM to disk

you can test with 0

or :

echo 1 > /proc/sys/vm/drop_caches

to flush all RAM to disk

3 Comments

Are you sure that the final command actually flushes the cache instead of just discarding it? I read in many places that this is a risky operation without using sync first.
@d33tah I'm pretty sure drop cache only drops clean cache and not dirty cache.
@user267092 As specified in the Linux kernel documentation (kernel.org/doc/Documentation/sysctl/vm.txt), the operation is not destructive.
-1

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:

  • 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

So the question is what you want to test and which caches you want to disable for this.

3 Comments

HD cache as mentioned in the OP.
The VFS cache usually plays a role in this as well. And since that's in memory, the CPU caches will be involved if the data isn't too big (well, L3 caches are up to 15 MB for i7, so "big" has become quite big).
@canoe The original question only says "disable the disk caching". It can mean any of internal drive cache, controller cache, VFS cache or libc buffering. For a school task I'd expect that the teacher really meant libc level buffering.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.