ILD

using vmtouch clean page cache of a file
作者:Yuan Jianpeng 邮箱:yuanjp89@163.com
发布时间:2024-6-27 站点:Inside Linux Development

vmtouch是一个工具,统计一个文件的缓存情况,或者清楚一个文件的内核缓存。

代码仓库:https://github.com/hoytech/vmtouch


统计缓存情况

$ vmtouch Makefile
           Files: 1
     Directories: 0
  Resident Pages: 1/1  4K/4K  100%
         Elapsed: 3.7e-05 seconds


实现原理:通过mmap这个文件,然后通过mincore()报告对应的位置是不是在内存中。

mmap(NULL, 3166, PROT_READ, MAP_SHARED, 5, 0) = 0x7f0616641000

mincore(0x7f0616641000, 3166, [0])      = 0
munmap(0x7f0616641000, 3166)            = 0



清除缓存:

$ vmtouch -e Makefile
           Files: 1
     Directories: 0
   Evicted Pages: 1 (4K)
         Elapsed: 1.1e-05 seconds


原理是mmap之后,通过fadvise来报告为不需要:

mmap(NULL, 3166, PROT_READ, MAP_SHARED, 5, 0) = 0x7f04660a5000
fadvise64(5, 0, 3166, POSIX_FADV_DONTNEED) = 0
munmap(0x7f04660a5000, 3166)            = 0


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.