Want to load a file to cache, also want to unload it from OS cache when you don't want it? 
Using posix_fadvise you can hint the OS that it should drop certain file blocks from the cache, or load file into cache. Together with information from mincore that tells us which blocks are currently cached we can alter applications to work without disturbing the buffer cache.

See my another articles about how to measure file and files opened by a process cache status.
This c code tells you  a file in core status

This perl code calls above code and tells you files buffering status, which opened by processes.

This article shows how to use posix_fadvise to load and unload a file to/from os cache.

load_file_to_cache.c

This load_file_to_cache.c script to load a file to linux cache.

#define _XOPEN_SOURCE 600
#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_WILLNEED);
    close(fd);
    return 0;
}

Unload_file_from_cache.c

#define _XOPEN_SOURCE 600
#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;
}

Compile :

cc load_file_to_cache.c -o load_file_to_cache
cc unload_file_to_cache.c -o unload_file_from_cache

posix_fadvise

As you can see, both scripts used posix_fadvise function, to announce an intention to access file data in a specific pattern in the future, thus allowing the kernel to perform appropriate optimizations.


You can chose different options in the script for different purpose, here are options

POSIX_FADV_NORMAL        Specifies that the application has  no  advice to give  on  its behavior
with respect to the specified data. It is the default characteristic if
no advice is given for an open file.
POSIX_FADV_SEQUENTIAL    Specifies  that  the  application  expects  to  access the specified 
data  sequentially  from lower offsets to higher offsets.
POSIX_FADV_RANDOM        Specifies  that  the  application  expects  to  access the specified
data in a random order.
POSIX_FADV_WILLNEED      Specifies  that  the  application  expects  to  access the specified
data in the near future.
POSIX_FADV_DONTNEED      Specifies that the application expects that it  will not access the
specified data in the near future.
POSIX_FADV_NOREUSE       Specifies  that  the  application  expects  to  access the specified
data once  and  then  not reuse it thereafter.

These values are defined in <fcntl.h>



 

 

Comments powered by CComment