【问题标题】:Can I prevent a file page eviction without using mmap?我可以在不使用 mmap 的情况下防止文件页面驱逐吗?
【发布时间】:2020-10-08 08:22:35
【问题描述】:

我的理解是,我可以通过对文件执行 mmap 然后在映射内存上调用 mlock 来将文件保留在内存中。

有没有办法在不做 mmap 的情况下将文件数据保存在页面缓存中?具体来说,我想确保当我将数据附加到文件时,我正在写入的页面永远不会被驱逐。

我知道这种情况很少见,但在某些情况下我相信它可能会发生。例如,应用程序写入数据,等待比dirty_writeback_centisecs 更长的时间(之后页面变得干净并且可以被驱逐),然后写入更多数据。

【问题讨论】:

  • 把文件放到ramdisk上?

标签: linux linux-kernel filesystems page-caching


【解决方案1】:

我相信您对mlock 所做的事情的理解有些错误。它的预期用途是:

  1. 断言不会因为数据尚未从磁盘加载或换出而等待从内存中读取数据(出于性能原因,对实时应用程序至关重要)。
  2. 断言页面不会被换出(对于密码或明文私钥等私人数据至关重要)。

因此它断言页面已加载到 RAM 中并防止它们被换出。无法保证它会阻止从文件映射的脏页的写回(实际上并没有,请参见下面的实验)。

要提示内核您将很快从 fd 进行一些 读取,有 posix_fadvise(),所以

posix_fadvise(fd, offset, len,  POSIX_FADV_RANDOM);

可能会将文件的请求部分加载到页面缓存中。


我不能肯定地说,但我想实际上没有办法像现在那样禁止写回特定文件的脏页。可能有一些方法可以提示它,但我也没有看到。


mmap/mlock 实验

alexander@goblin ~/tmp $ cat mmap.c
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while (0)

int main(int argc, char *argv[]) {
    char *addr;
    int fd;
    struct stat sb;
    size_t length;

    if (argc != 2) {
        fprintf(stderr, "%s file\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    fd = open(argv[1], O_RDWR);
    if (fd == -1) {
        handle_error("open");
    }
    if (fstat(fd, &sb) == -1) {          /* To obtain file size */
        handle_error("fstat");
    }

    length = sb.st_size;

    addr = mmap(NULL, length , PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    if (addr == MAP_FAILED) {
        handle_error("mmap");
    }

    if(mlock(addr, length)<0) {
        handle_error("mlock");
    }

    strcpy(addr, "hello world!");

    sleep(100);

    munmap(addr, length);
    close(fd);

    exit(EXIT_SUCCESS);
}
alexander@goblin ~/tmp $ grep . /proc/sys/vm/dirty_{expire,writeback}_centisecs
/proc/sys/vm/dirty_expire_centisecs:1000
/proc/sys/vm/dirty_writeback_centisecs:500
alexander@goblin ~/tmp $ dd if=/dev/zero of=foo bs=4k count=1
1+0 records in
1+0 records out
4096 bytes (4.1 kB, 4.0 KiB) copied, 8.1296e-05 s, 50.4 MB/s
alexander@goblin ~/tmp $ fallocate -l 4096 foo
alexander@goblin ~/tmp $ sync foo
alexander@goblin ~/tmp $ sudo hdparm --fibmap foo

foo:
 filesystem blocksize 4096, begins at LBA 0; assuming 512 byte sectors.
 byte_offset  begin_LBA    end_LBA    sectors
           0  279061632  279061639          8
alexander@goblin ~/tmp $ sudo dd if=/dev/mapper/vg_main-gentoo_home count=8 skip=279061632 iflag=nocache 2>/dev/null | hexdump -C
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001000
alexander@goblin ~/tmp $ gcc mmap.c
alexander@goblin ~/tmp $ ./a.out foo &
[1] 26450
alexander@goblin ~/tmp $ sudo hdparm --fibmap foo

foo:
 filesystem blocksize 4096, begins at LBA 0; assuming 512 byte sectors.
 byte_offset  begin_LBA    end_LBA    sectors
           0  279061632  279061639          8
alexander@goblin ~/tmp $ sudo dd if=/dev/mapper/vg_main-gentoo_home count=8 skip=279061632 iflag=nocache 2>/dev/null | hexdump -C
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001000
alexander@goblin ~/tmp $ sleep 10
alexander@goblin ~/tmp $ sudo hdparm --fibmap foo

foo:
 filesystem blocksize 4096, begins at LBA 0; assuming 512 byte sectors.
 byte_offset  begin_LBA    end_LBA    sectors
           0  279061632  279061639          8
alexander@goblin ~/tmp $ sudo dd if=/dev/mapper/vg_main-gentoo_home count=8 skip=279061632 iflag=nocache 2>/dev/null | hexdump -C
00000000  68 65 6c 6c 6f 20 77 6f  72 6c 64 21 00 00 00 00  |hello world!....|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001000
alexander@goblin ~/tmp $ fg
./a.out foo
^C

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 2016-11-12
    • 2021-07-09
    • 2020-10-15
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多