一. 刷新文件pagecache
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
int clear_file_cache(const char *filename)
{
struct stat st;
if(stat(filename , &st) < 0) {
fprintf(stderr , "stat localfile failed, path:%s\n",filename);
return -1;
}
int fd = open(filename, O_RDONLY);
if( fd < 0 ) {
fprintf(stderr , "open localfile failed, path:%s\n",filename);
return -1;
}
//clear cache by posix_fadvise
if( posix_fadvise(fd,0,st.st_size,POSIX_FADV_DONTNEED) != 0) {
printf("Cache FADV_DONTNEED failed, %s\n",strerror(errno));
}
else {
printf("Cache FADV_DONTNEED done\n");
}
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 2 )
{
printf("please input filename\n");
return -1;
}
clear_file_cache( argv[1] );
return 0;
}
交叉编译后生成fadvise,执行./fadvise 文件名
二. 查看文件是否刷新使用fincore
https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/
交叉编译 util-linux-2.32.tar.gz
1. 设置交叉环境变量
2. 因为使用libtinfo.so库,而我的交叉编译链里没有,但现在都用 libncurses.so.5.9,所以需要做一个软连接
ln -s libncurses.so.5.9 libtinfo.so
3. vim sys-utils/fallocate.c +354 增加分号
4. CC=arm-openwrt-linux-gcc LD=arm-openwrt-linux-ld ./configure --host=arm-linux --prefix=//home/happy/2disk/dirty_cow/util-linux-2.32/install
make
make install
会在install/bin目录下生成fincore文件
下载到板子上
执行./fincore /lib/ /lib/libcurl.so.4.5.0
RES PAGES SIZE FILE
32K 8 351K /lib/libcurl.so.4.5.0
可以使用一中的代码 刷新一下文件,再来查看,不过有的cache是刷新不掉的。