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
    – Rolf
    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

3
  • 5
    You can also run the command from the shell, without system(): sudo hdparm -W 0 /dev/sda1 Commented Nov 26, 2013 at 11:19
  • 12
    Note: This only disables the write cache. How about the read caches? Commented Nov 26, 2013 at 11:20
  • 3
    @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. Commented Dec 17, 2016 at 20:36
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
  • 1
    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
    Commented Apr 3, 2014 at 17:47
  • @d33tah I'm pretty sure drop cache only drops clean cache and not dirty cache.
    – user267092
    Commented Dec 10, 2019 at 20:10
  • 1
    @user267092 As specified in the Linux kernel documentation (kernel.org/doc/Documentation/sysctl/vm.txt), the operation is not destructive. Commented Oct 29, 2020 at 9:31
-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
  • 12
    HD cache as mentioned in the OP.
    – canoe
    Commented Nov 26, 2013 at 13:23
  • 1
    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). Commented Nov 26, 2013 at 15:18
  • @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. Commented Jan 4, 2021 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.