10

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.

Flaviops
  • 143
  • 1
  • 2
  • 7

3 Answers3

20

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

nio
  • 5,141
  • 2
  • 24
  • 35
  • 4
    You can also run the command from the shell, without `system()`: `sudo hdparm -W 0 /dev/sda1` – Aaron Digulla Nov 26 '13 at 11:19
  • 11
    Note: This only disables the write cache. How about the read caches? – Aaron Digulla Nov 26 '13 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. – PointedEars Dec 17 '16 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

  • 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 Apr 03 '14 at 17:47
  • @d33tah I'm pretty sure drop cache only drops clean cache and not dirty cache. – user267092 Dec 10 '19 at 20:10
  • 1
    @user267092 As specified in the Linux kernel documentation (https://www.kernel.org/doc/Documentation/sysctl/vm.txt), the operation is not destructive. – Keeley Hoek Oct 29 '20 at 09:31
0

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.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 12
    HD cache as mentioned in the OP. – canoe Nov 26 '13 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). – Aaron Digulla Nov 26 '13 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. – Mikko Rantalainen Jan 04 '21 at 08:18