如何从Linux上的系统缓存中逐出文件?

时间:2022-08-15 13:55:17

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?

运行性能测试时,文件系统缓存命中或未命中会显着影响测试结果。因此,通常在运行此类测试之前,使用的文件将从系统缓存中逐出。如何在Linux上做到这一点?

Clarification: If possible, the solution should not require root privileges.

澄清:如果可能,解决方案不应该要求root权限。

5 个解决方案

#1


11  

As a superuser you can do the following:

作为超级用户,您可以执行以下操作:

To free pagecache:

要释放pagecache:

  • echo 1 > /proc/sys/vm/drop_caches
  • echo 1> / proc / sys / vm / drop_caches

To free dentries and inodes:

要释放dentries和inode:

  • echo 2 > /proc/sys/vm/drop_caches
  • echo 2> / proc / sys / vm / drop_caches

To free pagecache, dentries and inodes:

要释放pagecache,dentries和inode:

  • echo 3 > /proc/sys/vm/drop_caches
  • echo 3> / proc / sys / vm / drop_caches

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.

正如我所说,只有超级用户(root)可以这样做。

#2


9  

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

这是来自http://insights.oetiker.ch/linux/fadvise.html

#3


5  

There is a command line utility by Eric Wong that makes it easy to invoke posix_fadvise:

Eric Wong有一个命令行实用程序,可以很容易地调用posix_fadvise:

http://git.bogomips.org/cgit/pcu.git/tree/README

It's then as simple as

然后就这么简单

$ pcu-fadvise -a dontneed filename-to-evict

#4


0  

Regarding use of O_DIRECT: that would perturb the results in another way. The kernel will attempt to DMA the filesystem data directly into your read() buffer, so it can be handed up to your application without any additional copy being done. Without O_DIRECT the kernel DMAs the file data into the page cache, and copies it from the page cache to your read() buffer.

关于O_DIRECT的使用:这会以另一种方式扰乱结果。内核将尝试将文件系统数据直接DMA到您的read()缓冲区中,因此可以将其传递给您的应用程序,而无需进行任何额外的复制。如果没有O_DIRECT,内核会将文件数据DMA到页面缓存中,并将其从页面缓存复制到read()缓冲区。

This is fine if your app is really going to use O_DIRECT in production. If you run performance tests with O_DIRECT and then remove O_DIRECT for production, your performance test will be unrealistic.

如果您的应用程序真的要在生产中使用O_DIRECT,这很好。如果使用O_DIRECT运行性能测试然后删除O_DIRECT进行生产,则性能测试将不切实际。

#5


-1  

If you can put the test data in a separate filesystem then mounting the filesystem afresh for the test will give you empty caches.

如果您可以将测试数据放在单独的文件系统中,那么重新安装文件系统以进行测试将为您提供空缓存。

If you list the test fileystem in /etc/fstab with the "user" option then you can mount it for the test without being superuser

如果您使用“user”选项在/ etc / fstab中列出测试文件系统,那么您可以安装它以进行测试而无需超级用户

#1


11  

As a superuser you can do the following:

作为超级用户,您可以执行以下操作:

To free pagecache:

要释放pagecache:

  • echo 1 > /proc/sys/vm/drop_caches
  • echo 1> / proc / sys / vm / drop_caches

To free dentries and inodes:

要释放dentries和inode:

  • echo 2 > /proc/sys/vm/drop_caches
  • echo 2> / proc / sys / vm / drop_caches

To free pagecache, dentries and inodes:

要释放pagecache,dentries和inode:

  • echo 3 > /proc/sys/vm/drop_caches
  • echo 3> / proc / sys / vm / drop_caches

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.

正如我所说,只有超级用户(root)可以这样做。

#2


9  

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

这是来自http://insights.oetiker.ch/linux/fadvise.html

#3


5  

There is a command line utility by Eric Wong that makes it easy to invoke posix_fadvise:

Eric Wong有一个命令行实用程序,可以很容易地调用posix_fadvise:

http://git.bogomips.org/cgit/pcu.git/tree/README

It's then as simple as

然后就这么简单

$ pcu-fadvise -a dontneed filename-to-evict

#4


0  

Regarding use of O_DIRECT: that would perturb the results in another way. The kernel will attempt to DMA the filesystem data directly into your read() buffer, so it can be handed up to your application without any additional copy being done. Without O_DIRECT the kernel DMAs the file data into the page cache, and copies it from the page cache to your read() buffer.

关于O_DIRECT的使用:这会以另一种方式扰乱结果。内核将尝试将文件系统数据直接DMA到您的read()缓冲区中,因此可以将其传递给您的应用程序,而无需进行任何额外的复制。如果没有O_DIRECT,内核会将文件数据DMA到页面缓存中,并将其从页面缓存复制到read()缓冲区。

This is fine if your app is really going to use O_DIRECT in production. If you run performance tests with O_DIRECT and then remove O_DIRECT for production, your performance test will be unrealistic.

如果您的应用程序真的要在生产中使用O_DIRECT,这很好。如果使用O_DIRECT运行性能测试然后删除O_DIRECT进行生产,则性能测试将不切实际。

#5


-1  

If you can put the test data in a separate filesystem then mounting the filesystem afresh for the test will give you empty caches.

如果您可以将测试数据放在单独的文件系统中,那么重新安装文件系统以进行测试将为您提供空缓存。

If you list the test fileystem in /etc/fstab with the "user" option then you can mount it for the test without being superuser

如果您使用“user”选项在/ etc / fstab中列出测试文件系统,那么您可以安装它以进行测试而无需超级用户