Scripting‎ > ‎C examples‎ > ‎

load or unload a file from OS cache

Thank you for visiting this page, this page has been update in another link Load or unload a file from OS cache
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
https://sites.google.com/site/itmyshare/c-useful-examples-for-admin/fine-in-core-fincore-in-c

This perl code calls above code and tells you files buffering status, which opened by processes.
https://sites.google.com/site/itmyshare/system-admin-tips-and-tools/how-to-know-a-file-or-a-process-buffered-in-os-cache

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

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;
}


Here is 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


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>

RETURN VALUES
       Upon successful completion, posix_fadvise() returns zero. Otherwise, an error number is returned to indicate the error.

ERRORS
       The posix_fadvise() function will fail if:

       EBADF	 The fd argument is not a valid file descriptor.

       EINVAL	 The value of advice is invalid, or the value of len  is  less
		 than zero.

       ESPIPE	 The fd argument is associated with a pipe or FIFO.

USAGE
       The  posix_fadvise()  function  has a transitional interface for 64-bit
       file offsets. See lf64(5).

ATTRIBUTES
       See attributes(5) for descriptions of the following attributes:

       +-----------------------------+-----------------------------+
       |      ATTRIBUTE TYPE	     |	    ATTRIBUTE VALUE	   |
       +-----------------------------+-----------------------------+
       |Interface Stability	     |Committed 		   |
       +-----------------------------+-----------------------------+
       |MT-Level		     |MT-Safe			   |
       +-----------------------------+-----------------------------+
       |Standard		     |See standards(5). 	   |
       +-----------------------------+-----------------------------+



Comments