Disabling disk cache in linux

后端 未结 3 2047
日久生厌
日久生厌 2020-12-10 20:36

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 us

相关标签:
3条回答
  • 2020-12-10 20:53

    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

    0 讨论(0)
  • 2020-12-10 20:55

    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

    0 讨论(0)
  • 2020-12-10 21:00

    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.

    0 讨论(0)
提交回复
热议问题