When running performance tests file system cache hit or miss can significantly influence test results. Therefore generally before running such tests used files are evicted from system cache. How to do that on Linux?
Clarification: If possible, the solution should not require root privileges.
As a superuser you can do the following:
To free pagecache:
To free dentries and inodes:
To free pagecache, dentries and inodes:
This operation will not "lose" any data (caches are written out to disk before their data is dropped), however, to really make sure all cache is cleaned, you should sync first. E.g. all caches should be cleared if you run
sync; echo 3 > /proc/sys/vm/drop_caches
As I said, only a superuser (root) may do so.
Ha, I have the answer:
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[]) {
int fd;
fd = open(argv[1], O_RDONLY);
fdatasync(fd);
posix_fadvise(fd, 0,0,POSIX_FADV_DONTNEED);
close(fd);
return 0;
}
This is from http://insights.oetiker.ch/linux/fadvise.html
There is a command line utility by Eric Wong that makes it easy to invoke posix_fadvise:
http://git.bogomips.org/cgit/pcu.git/tree/README
It's then as simple as
$ pcu-fadvise -a dontneed filename-to-evict
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With